Skip to content

Commit aa10af9

Browse files
committed
Implementing requested changes.
1 parent 9b6f63e commit aa10af9

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

src/main/java/edu/ie3/datamodel/io/IoUtil.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,11 @@ public static String harmonizeFileSeparator(String in) {
3333
* Ensure to have harmonized file separator across the whole path. Will replace all occurrences *
3434
* of "\" and "/" by the systems file separator.
3535
*
36-
* @param path an option for a path to harmonize
37-
* @return the option for a harmonized path
36+
* @param path the path to harmonize
37+
* @return the harmonized path
3838
*/
3939
public static Path harmonizeFileSeparator(Path path) {
40-
String in = path.toString();
41-
in =
42-
IoUtil.FILE_SEPARATOR_REPLACEMENT.equals("\\\\")
43-
? in.replaceFirst("^" + IoUtil.FILE_SEPARATOR_REGEX, "")
44-
: in;
45-
return Path.of(IoUtil.harmonizeFileSeparator(in));
40+
return Path.of(IoUtil.harmonizeFileSeparator(path.toString()));
4641
}
4742

4843
/**

src/main/java/edu/ie3/datamodel/io/connectors/CsvFileConnector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public BufferedReader initReader(Class<? extends UniqueEntity> clz)
184184
"Cannot get reader for entity '{}' as no file naming strategy for this file exists. Exception: {}",
185185
clz.getSimpleName(),
186186
e);
187-
throw new ConnectorException("Cannot init reader due to the following exception: ", e);
187+
throw e;
188188
}
189189
}
190190

src/main/java/edu/ie3/datamodel/utils/FileUtils.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
/** Some utility functionalities. */
1818
public class FileUtils {
19+
public static Pattern FILE_NAME_PATTERN =
20+
Pattern.compile(
21+
"^(?<fileName>[^\\\\/\\s.]{0,255})(?:\\.(?<extension>[a-zA-Z0-9]{0,10}(?:\\.[a-zA-Z0-9]{0,10})?))?$");
22+
public static String CSV_FILE_EXTENSION = "csv";
23+
24+
private FileUtils() {
25+
throw new IllegalStateException("Utility classes cannot be instantiated");
26+
}
27+
1928
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
2029

2130
/**
@@ -56,11 +65,6 @@ public static Optional<Path> of(Optional<String> fileName, Optional<Path> direct
5665
* @return a definition of the file
5766
*/
5867
public static Path ofCsv(String fileName, Path directoryPath) {
59-
Pattern FILE_NAME_PATTERN =
60-
Pattern.compile(
61-
"^(?<fileName>[^\\\\/\\s.]{0,255})(?:\\.(?<extension>[a-zA-Z0-9]{0,10}(?:\\.[a-zA-Z0-9]{0,10})?))?$");
62-
String FILE_EXTENSION = "csv";
63-
6468
/* Remove all file separators at the beginning and end of a directory path and ensure harmonized file separator */
6569
Path dirPath =
6670
Objects.nonNull(directoryPath) ? IoUtil.harmonizeFileSeparator(directoryPath) : Path.of("");
@@ -70,12 +74,12 @@ public static Path ofCsv(String fileName, Path directoryPath) {
7074

7175
if (matcher.matches()) {
7276
String extension = matcher.group("extension");
73-
if (Objects.nonNull(extension) && !extension.equalsIgnoreCase(FILE_EXTENSION))
77+
if (Objects.nonNull(extension) && !extension.equalsIgnoreCase(CSV_FILE_EXTENSION))
7478
logger.warn(
7579
"You provided a file name with extension '{}'. It will be overridden to '{}'.",
7680
extension,
77-
FILE_EXTENSION);
78-
return dirPath.resolve(matcher.group("fileName") + "." + FILE_EXTENSION);
81+
CSV_FILE_EXTENSION);
82+
return dirPath.resolve(matcher.group("fileName") + "." + CSV_FILE_EXTENSION);
7983
} else {
8084
throw new IllegalArgumentException(
8185
"The file name '"

src/test/groovy/edu/ie3/datamodel/io/naming/FileNamingStrategyTest.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,4 +982,15 @@ class FileNamingStrategyTest extends Specification {
982982
idFilePath.present
983983
idFilePath.get() == Path.of("prefix_coordinates_suffix")
984984
}
985+
986+
def "The FileNamingStrategy with DefaultHierarchy returns the Id Coordinate file path correctly"() {
987+
def fns = new FileNamingStrategy(new EntityPersistenceNamingStrategy("prefix", "suffix"), defaultHierarchy)
988+
989+
when:
990+
def idFilePath = fns.getIdCoordinateFilePath()
991+
992+
then:
993+
idFilePath.present
994+
idFilePath.get() == defaultHierarchy.baseDirectory.get().resolve("prefix_coordinates_suffix")
995+
}
985996
}

src/test/groovy/edu/ie3/datamodel/io/source/csv/CsvRawGridSourceTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class CsvRawGridSourceTest extends Specification implements CsvTestDataMeta {
2929
RawGridSource source
3030

3131
def setupSpec() {
32-
TypeSource typeSource = new TypeSource(new CsvDataSource(csvSep, typeFolderPath as Path, fileNamingStrategy))
33-
source = new RawGridSource(typeSource, new CsvDataSource(csvSep, gridDefaultFolderPath as Path, fileNamingStrategy))
32+
TypeSource typeSource = new TypeSource(new CsvDataSource(csvSep, typeFolderPath, fileNamingStrategy))
33+
source = new RawGridSource(typeSource, new CsvDataSource(csvSep, gridDefaultFolderPath, fileNamingStrategy))
3434
}
3535

3636
def "The CsvRawGridSource is able to convert single valid AssetInputEntityData to ConnectorInputEntityData"() {

0 commit comments

Comments
 (0)