Commit 2e12ddb8 authored by Eric Duminil's avatar Eric Duminil
Browse files

It's hard to test private static final LOGGER. :-/

parent 533e5299
......@@ -6,11 +6,12 @@
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.logging.Logger;
import org.osgeo.proj4j.CoordinateReferenceSystem;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;
// Example usage:
// --input /home/ricou/Desktop/CGSC_Repository/Würzburg.proj/LoD2_566_5516_2_BY.gml,/home/ricou/Desktop/CGSC_Repository/Würzburg.proj/LoD2_568_5516_2_BY.gml
......@@ -21,8 +22,10 @@
@Command(name = "region_chooser", mixinStandardHelpOptions = true, version = "regionChooser x.x", description = "Extracts a region from one or more citygmls.", sortOptions = false)
class RegionChooserCommandLineInterface implements Callable<Integer>
{
@Spec
CommandSpec spec;
private static final Logger LOGGER = Logger.getLogger(RegionChooserCommandLineInterface.class.getName());
//TODO: Add --gui?
@Option(names = { "-i",
"--input" }, required = true, split = ",", description = "Citygml files to extract from", paramLabel = "input.gml")
......@@ -53,7 +56,7 @@ public Integer call() throws Exception {
} else {
localCRS = RegionChooserUtils.crsFromSrsName("EPSG:" + espgId);
}
LOGGER.info("Coordinate system: " + localCRS);
logInfo("Coordinate system: " + localCRS);
String wktPolygon;
......@@ -75,7 +78,7 @@ public Integer call() throws Exception {
wktPolygon = RegionChooserUtils.wktPolygonToLocalCRS(wktPolygon, localCRS);
}
LOGGER.info("WKT Polygon expressed in local coordinates: " + wktPolygon);
logInfo("WKT Polygon expressed in local coordinates: " + wktPolygon);
StringBuilder sb = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, localCRS.toString(), citygmls);
......@@ -84,6 +87,10 @@ public Integer call() throws Exception {
return 0;
}
private void logInfo(String message) {
spec.commandLine().getErr().println(message);
}
private static String getInput() {
try (Scanner myObj = new Scanner(System.in)) {
return myObj.nextLine();
......
package eu.simstadt.regionchooser;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
......@@ -43,7 +45,6 @@ public void restoreStreams() {
@Test
void testNoInput() {
new CommandLine(new RegionChooserCommandLineInterface()).execute("");
// originalOut.println(err.toString());
String expectedErr = "Missing required options: '--input=input.gml', '--output=output.gml', '--wkt=polygon.wkt'";
assertTrue(err.toString().contains(expectedErr), err.toString() + " should contain " + expectedErr);
}
......@@ -58,11 +59,12 @@ void testExtractRegionFromTwoCitygmls() throws IOException {
try (BufferedWriter wkt = Files.newBufferedWriter(inWKT)) {
wkt.write(wktPolygon);
}
assertFalse(Files.exists(outGML));
new CommandLine(new RegionChooserCommandLineInterface()).execute("--input=" + citygml1 + "," + citygml2,
"--output=" + outGML, "--wkt=" + inWKT, "--epsg=31463", "--local");
String expectedLog = "Buildings found in selected region 20";
assertTrue(err.toString().contains(expectedLog), err.toString() + " should contain " + expectedLog);
assertTrue(Files.exists(outGML));
assertTrue(Files.size(outGML) > 600_000);
assertEquals(20, countBuildings(outGML));
}
......@@ -76,13 +78,18 @@ void testExtractRegionFromTwoCitygmlsInWGS84() throws IOException {
try (BufferedWriter wkt = Files.newBufferedWriter(inWKT)) {
wkt.write(wktPolygon);
}
assertFalse(Files.exists(outGML));
new CommandLine(new RegionChooserCommandLineInterface()).execute("--input=" + citygml1 + "," + citygml2,
"--output=" + outGML, "--wkt=" + inWKT);
String expectedLog = "EPSG:31463";
assertTrue(err.toString().contains(expectedLog), err.toString() + " should contain " + expectedLog);
expectedLog = "Buildings found in selected region 22";
assertTrue(err.toString().contains(expectedLog), err.toString() + " should contain " + expectedLog);
assertTrue(Files.exists(outGML));
assertTrue(Files.size(outGML) > 300_000);
assertEquals(22, countBuildings(outGML));
}
private long countBuildings(Path outGML) throws IOException {
return Files.readAllLines(outGML).stream().filter(line -> line.contains("bldg:Building gml:id=")).count();
}
@Test
......@@ -92,13 +99,13 @@ void testExtractRegionWithStandardInput() throws IOException {
Path outGML = Files.createTempFile("output", ".gml");
InputStream stdin = new ByteArrayInputStream(wktPolygon.getBytes(StandardCharsets.UTF_8));
System.setIn(stdin);
assertFalse(Files.exists(outGML));
new CommandLine(new RegionChooserCommandLineInterface()).execute("--input=" + citygml, "--output=" + outGML,
"--wkt=-");
String expectedLog = "EPSG:32118";
originalOut.println(err.toString());
assertTrue(err.toString().contains(expectedLog), err.toString() + " should contain " + expectedLog);
expectedLog = "Buildings found in selected region 2";
assertTrue(err.toString().contains(expectedLog), err.toString() + " should contain " + expectedLog);
assertTrue(Files.exists(outGML));
assertEquals(2, countBuildings(outGML));
}
@Test
......@@ -116,6 +123,7 @@ void testExtractRegionWithMissingInput() throws IOException {
assertTrue(err.toString().contains(expectedLog), err.toString() + " should contain " + expectedLog);
expectedLog = "Please provide \"POLYGON((x1 y1, x2 y2, ...))\" to standard input.";
assertTrue(err.toString().contains(expectedLog), err.toString() + " should contain " + expectedLog);
assertFalse(Files.exists(outGML));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment