From decd6a50a82481616c6ac309b512905ad4614d0a Mon Sep 17 00:00:00 2001
From: Hallvard Traetteberg <hal@ntnu.no>
Date: Wed, 31 Jan 2018 14:48:21 +0100
Subject: [PATCH] Litt flere tester.

---
 .../gr1800/app/core/DocumentStorageImpl.java  | 10 ++++--
 .../java/tdt4140/gr1800/app/core/AppTest.java | 35 ++++++++++++++++---
 .../app/core/GeoLocationsPersistenceTest.java | 12 +++++--
 tdt4140-gr18nn/app.ui/pom.xml                 | 25 +++++++++++++
 .../java/tdt4140/gr18nn/app/ui/FxAppTest.java | 16 +++++++++
 5 files changed, 88 insertions(+), 10 deletions(-)

diff --git a/tdt4140-gr1800/app.core/src/main/java/tdt4140/gr1800/app/core/DocumentStorageImpl.java b/tdt4140-gr1800/app.core/src/main/java/tdt4140/gr1800/app/core/DocumentStorageImpl.java
index 4e91dfa..559279c 100644
--- a/tdt4140-gr1800/app.core/src/main/java/tdt4140/gr1800/app/core/DocumentStorageImpl.java
+++ b/tdt4140-gr1800/app.core/src/main/java/tdt4140/gr1800/app/core/DocumentStorageImpl.java
@@ -16,6 +16,11 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L> {
 		this.documentLocation = documentLocation;
 	}
 
+	protected void setDocumentAndLocation(D document, L documentLocation) {
+		setDocument(document);
+		setDocumentLocation(documentLocation);
+	}
+	
 	protected abstract D getDocument();
 	protected abstract void setDocument(D document);
 
@@ -25,13 +30,12 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L> {
 	
 	@Override
 	public void newDocument() {
-		setDocument(createDocument());
+		setDocumentAndLocation(createDocument(), null);
 	}
 
 	@Override
 	public void openDocument(L storage) throws IOException {
-		setDocument(loadDocument(storage));
-		setDocumentLocation(storage);
+		setDocumentAndLocation(loadDocument(storage), storage);
 	}
 
 	@Override
diff --git a/tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/AppTest.java b/tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/AppTest.java
index f43ba34..8add303 100644
--- a/tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/AppTest.java
+++ b/tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/AppTest.java
@@ -18,11 +18,15 @@ public class AppTest {
 	}
 
 	@Test
-	public void testLoadDocument() {
+	public void testNewDocument() {
+		IDocumentStorage<File> documentStorage = app.getDocumentStorage();
+		documentStorage.newDocument();
+		Assert.assertFalse(app.getGeoLocationNames().iterator().hasNext());
+		Assert.assertNull(documentStorage.getDocumentLocation());
+	}
+
+	protected void testLoadDocument(File file) {
 		IDocumentStorage<File> documentStorage = app.getDocumentStorage();
-		URL url = getClass().getResource("geoLocations.json");
-		Assert.assertEquals("Not file URL", "file", url.getProtocol()); 
-		File file = new File(url.getPath());
 		try {
 			documentStorage.openDocument(file);
 		} catch (IOException e) {
@@ -31,4 +35,27 @@ public class AppTest {
 		Assert.assertEquals(file, documentStorage.getDocumentLocation());
 		GeoLocationsPersistenceTest.testGeoLocationsDotJson(app.getGeoLocations((String[]) null));
 	}
+	
+	@Test
+	public void testLoadDocument() {
+		URL url = getClass().getResource("geoLocations.json");
+		Assert.assertEquals("Not file URL", "file", url.getProtocol()); 
+		File file = new File(url.getPath());
+		testLoadDocument(file);
+	}
+
+	@Test
+	public void testSaveDocument() {
+		testLoadDocument();
+		try {
+			IDocumentStorage<File> documentStorage = app.getDocumentStorage();
+			File tempFile = File.createTempFile("geoLocations", ".json");
+			tempFile.deleteOnExit();
+			documentStorage.setDocumentLocation(tempFile);
+			documentStorage.saveDocument();
+			testLoadDocument(tempFile);
+		} catch (IOException e) {
+			Assert.fail(e.getMessage());
+		}
+	}
 }
diff --git a/tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/GeoLocationsPersistenceTest.java b/tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/GeoLocationsPersistenceTest.java
index 4eb7dc5..73a426f 100644
--- a/tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/GeoLocationsPersistenceTest.java
+++ b/tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/GeoLocationsPersistenceTest.java
@@ -3,6 +3,7 @@ package tdt4140.gr1800.app.core;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
 
@@ -31,6 +32,13 @@ public class GeoLocationsPersistenceTest {
 		}
 	}
 	
+	public static Collection<GeoLocations> createGeoLocationsDotJson() {
+		return Arrays.asList(
+			new GeoLocations("1", new LatLong(63, 10), new LatLong(63.1, 10.1)),
+			new GeoLocations("2", new LatLong(64, 11), new LatLong(64.1, 11.1))
+		);
+	}
+	
 	public static void testGeoLocationsDotJson(Collection<GeoLocations> geoLocations) {
 		Assert.assertEquals(2, geoLocations.size());
 		Iterator<GeoLocations> it = geoLocations.iterator();
@@ -40,9 +48,7 @@ public class GeoLocationsPersistenceTest {
 
 	@Test
 	public void testSaveLocations() {
-		Collection<GeoLocations> geoLocations = new ArrayList<GeoLocations>();
-		geoLocations.add(new GeoLocations("1", new LatLong(63, 10), new LatLong(63.1, 10.1)));
-		geoLocations.add(new GeoLocations("2", new LatLong(64, 11), new LatLong(64.1, 11.1)));
+		Collection<GeoLocations> geoLocations = createGeoLocationsDotJson();
 		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 		try {
 			persistence.saveLocations(geoLocations, outputStream);
diff --git a/tdt4140-gr18nn/app.ui/pom.xml b/tdt4140-gr18nn/app.ui/pom.xml
index f81bdf8..a96b8e3 100644
--- a/tdt4140-gr18nn/app.ui/pom.xml
+++ b/tdt4140-gr18nn/app.ui/pom.xml
@@ -8,6 +8,31 @@
 		<artifactId>tdt4140-gr18nn</artifactId>
 		<version>0.0.1-SNAPSHOT</version>
 	</parent>
+	
+	<profiles>
+		<profile>
+			<id>gitlab-ci</id>
+			<activation>
+				<property>
+					<name>gitlab-ci</name>
+					<value>true</value>
+				</property>
+			</activation>
+			<!-- 
+			<properties>
+				<skip-ui-tests>true</skip-ui-tests>
+			</properties>
+			 -->
+			<dependencies>
+				<dependency>
+		            <groupId>org.testfx</groupId>
+		            <artifactId>openjfx-monocle</artifactId>
+		            <version>8u76-b04</version> <!-- jdk-9+181 for Java 9 -->
+		            <scope>test</scope>
+		        </dependency>
+			</dependencies>
+		</profile>
+	</profiles>
 
 	<dependencies>
 		<dependency>
diff --git a/tdt4140-gr18nn/app.ui/src/test/java/tdt4140/gr18nn/app/ui/FxAppTest.java b/tdt4140-gr18nn/app.ui/src/test/java/tdt4140/gr18nn/app/ui/FxAppTest.java
index e654ea3..5c78774 100644
--- a/tdt4140-gr18nn/app.ui/src/test/java/tdt4140/gr18nn/app/ui/FxAppTest.java
+++ b/tdt4140-gr18nn/app.ui/src/test/java/tdt4140/gr18nn/app/ui/FxAppTest.java
@@ -1,5 +1,6 @@
 package tdt4140.gr18nn.app.ui;
 
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.testfx.framework.junit.ApplicationTest;
 
@@ -9,6 +10,21 @@ import javafx.scene.Scene;
 import javafx.stage.Stage;
 
 public class FxAppTest extends ApplicationTest {
+	
+	@BeforeClass
+	public static void headless() {
+		if (Boolean.valueOf(System.getProperty("gitlab-ci", "false"))) {
+			System.setProperty("prism.verbose", "true"); // optional
+			System.setProperty("java.awt.headless", "true");
+			System.setProperty("testfx.robot", "glass");
+			System.setProperty("testfx.headless", "true");
+			System.setProperty("glass.platform", "Monocle");
+			System.setProperty("monocle.platform", "Headless");
+			System.setProperty("prism.order", "sw");
+			System.setProperty("prism.text", "t2k");
+			System.setProperty("testfx.setup.timeout", "2500");
+		}
+	}
 
 	@Override
     public void start(Stage stage) throws Exception {
-- 
GitLab