From 386b7a16dbe9d8c474a854b9c2efa5ddc7db2213 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Mon, 10 Oct 2022 11:34:47 +0200
Subject: [PATCH] Full CLI tests. :D

---
 ...RegionChooserCommandLineInterfaceTest.java | 64 +++++++++++++++----
 1 file changed, 51 insertions(+), 13 deletions(-)

diff --git a/src/test/java/eu/simstadt/regionchooser/RegionChooserCommandLineInterfaceTest.java b/src/test/java/eu/simstadt/regionchooser/RegionChooserCommandLineInterfaceTest.java
index 7f9886e..dd61dcf 100644
--- a/src/test/java/eu/simstadt/regionchooser/RegionChooserCommandLineInterfaceTest.java
+++ b/src/test/java/eu/simstadt/regionchooser/RegionChooserCommandLineInterfaceTest.java
@@ -1,28 +1,66 @@
 package eu.simstadt.regionchooser;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import java.io.PrintWriter;
-import java.io.StringWriter;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.junit.jupiter.api.AfterEach; // JUnit 5
+import org.junit.jupiter.api.BeforeEach; // JUnit 5
 import org.junit.jupiter.api.Test;
 import picocli.CommandLine;
 
 
 class RegionChooserCommandLineInterfaceTest
 {
+	final PrintStream originalOut = System.out;
+	final PrintStream originalErr = System.err;
+	final ByteArrayOutputStream out = new ByteArrayOutputStream();
+	final ByteArrayOutputStream err = new ByteArrayOutputStream();
 
-	@Test
-	void testCLIWithoutArgument() {
-		RegionChooserCommandLineInterface app = new RegionChooserCommandLineInterface();
-		CommandLine cmd = new CommandLine(app);
+	private static final Path TEST_REPOSITORY = Paths.get("src/test/resources/testdata/");
 
-		StringWriter sw = new StringWriter();
-		cmd.setOut(new PrintWriter(sw));
+	@BeforeEach
+	public void setUpStreams() {
+		out.reset();
+		err.reset();
+		System.setOut(new PrintStream(out));
+		System.setErr(new PrintStream(err));
+	}
 
-		// black box testing
-		int exitCode = cmd.execute("--input=input.gml", "--output=output.gml", "--wkt=input.wkt");
-		assertEquals(0, exitCode);
-		assertEquals("Your output is abc...", sw.toString());
+	@AfterEach
+	public void restoreStreams() {
+		System.setOut(originalOut);
+		System.setErr(originalErr);
 	}
 
+	@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);
+	}
+
+	@Test
+	void testExtractRegionFromTwoCitygmls() throws IOException {
+		String wktPolygon = "POLYGON((3512984.7003764412 5405148.310572891,3513038.6360455155 5405010.072163861,3513142.7277745553 5405004.02571992,3514204.1661769524 5405563.192081669,3514399.2818417274 5405720.905457244,3514291.6158155007 5405896.706492759,3512984.7003764412 5405148.310572891))";
+		Path citygml1 = TEST_REPOSITORY.resolve("Stuttgart.proj/Stuttgart_LOD0_LOD1_small.gml");
+		Path citygml2 = TEST_REPOSITORY.resolve("Stuttgart.proj/Stöckach_überarbeitete GML-NoBuildingPart.gml");
+		Path outGML = Files.createTempFile("output", ".gml");
+		Path inWKT = Files.createTempFile("polygon", ".wkt");
+		try (BufferedWriter wkt = Files.newBufferedWriter(inWKT)) {
+			wkt.write(wktPolygon);
+		}
+		new CommandLine(new RegionChooserCommandLineInterface()).execute("--input=" + citygml1 + "," + citygml2,
+				"--output=" + outGML, "--wkt=" + inWKT, "--epsg=31463", "--local");
+		String expectedErr = "Buildings found in selected region 20";
+		assertTrue(err.toString().contains(expectedErr), err.toString() + " should contain " + expectedErr);
+		originalOut.println(Files.size(outGML));
+		assertTrue(Files.size(outGML) > 600_000);
+	}
 }
 
-- 
GitLab