Skip to content

Commit

Permalink
improve utility methods and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Jan 17, 2024
1 parent 205ef4d commit 1f9b769
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,25 @@ public class ApplicationUtils {
private ApplicationUtils() {
}


/**
* Merge given arguments with custom ones.
*
* @param args given args, usually input from command line / main method
* @param defaultArgs default arguments that will be added to existing ones.
*/
public static String[] mergeArgs(String[] args, String... defaultArgs) {
String[] mergedArgs = new String[args.length + defaultArgs.length];
System.arraycopy(args, 0, mergedArgs, 0, args.length);
System.arraycopy(defaultArgs, 0, mergedArgs, args.length, defaultArgs.length);
return mergedArgs;
}

/**
* Extends a context (usually config location) with an relative filename.
* If the results is a local file, the path will be returned. Otherwise, it will be an url.
* The results can be used as input for command line parameter or {@link IOUtils#resolveFileOrResource(String)}.
*
* @return string with path or URL
*/
public static String resolve(URL context, String filename) {
Expand All @@ -61,9 +76,9 @@ public static Path globFile(Path path, String pattern) {

try {
return Files.list(path)
.filter(p -> m.matches(p.getFileName()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("No " + pattern + " file found."));
.filter(p -> m.matches(p.getFileName()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("No " + pattern + " file found."));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -154,11 +169,19 @@ public static void checkCommand(Class<? extends MATSimAppCommand> command) {
boolean input = false;
boolean output = false;
for (Field field : fields) {
if (field.getType().equals(InputOptions.class))
if (field.getType().equals(InputOptions.class)) {
input = true;
CommandLine.Mixin mixin = field.getAnnotation(CommandLine.Mixin.class);
if (mixin == null)
throw new IllegalArgumentException(String.format("The command %s has no @Mixin annotation for InputOptions %s.", command, field.getName()));
}

if (field.getType().equals(OutputOptions.class))
if (field.getType().equals(OutputOptions.class)) {
output = true;
CommandLine.Mixin mixin = field.getAnnotation(CommandLine.Mixin.class);
if (mixin == null)
throw new IllegalArgumentException(String.format("The command %s has no @Mixin annotation for OutputOptions %s.", command, field.getName()));
}
}

if (!input) {
Expand Down Expand Up @@ -213,7 +236,7 @@ public static Path matchInput(String name, Path dir) {
return path.get();

// Match more general pattern at last
path = matchPattern( ".+\\.[a-zA-Z0-9]*_" + name + "\\..+", dir);
path = matchPattern(".+\\.[a-zA-Z0-9]*_" + name + "\\..+", dir);
if (path.isPresent())
return path.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.matsim.application.analysis.TestAnalysis;
import org.matsim.application.analysis.TestDependentAnalysis;
import org.matsim.application.options.ShpOptions;
import org.matsim.application.prepare.population.CleanPopulation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

public class ApplicationUtilsTest {
Expand All @@ -17,4 +19,26 @@ void shp() {
assertFalse(ApplicationUtils.acceptsOptions(TestDependentAnalysis.class, ShpOptions.class));

}

@Test
void checkCommand() {

ApplicationUtils.checkCommand(TestAnalysis.class);

// Just picked a random class that does not use the command spec, if this command is converted, another one has to be picked.
assertThrows(IllegalArgumentException.class, () -> ApplicationUtils.checkCommand(CleanPopulation.class));

}

@Test
void mergeArgs() {


String[] result = ApplicationUtils.mergeArgs(new String[]{"--a", "1", "--b", "2"},
"--a", "3", "--c", "4");

assertThat(result)
.containsExactly("--a", "1", "--b", "2", "--a", "3", "--c", "4");

}
}

0 comments on commit 1f9b769

Please sign in to comment.