From c917ab209f146eb3726ca9bf6fa4c9eb02afbc22 Mon Sep 17 00:00:00 2001 From: Hallvard Traetteberg <hal@ntnu.no> Date: Tue, 30 Jan 2018 09:30:29 +0100 Subject: [PATCH] Flere tester --- .../java/tdt4140/gr1800/app/core/App.java | 19 ++++++++++- .../java/tdt4140/gr1800/app/core/AppTest.java | 34 +++++++++++++++++++ .../app/core/GeoLocationsPersistenceTest.java | 22 ++++++------ 3 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/AppTest.java diff --git a/tdt4140-gr1800/app.core/src/main/java/tdt4140/gr1800/app/core/App.java b/tdt4140-gr1800/app.core/src/main/java/tdt4140/gr1800/app/core/App.java index dc42064..7c09f41 100644 --- a/tdt4140-gr1800/app.core/src/main/java/tdt4140/gr1800/app/core/App.java +++ b/tdt4140-gr1800/app.core/src/main/java/tdt4140/gr1800/app/core/App.java @@ -51,7 +51,24 @@ public class App { } return null; } - + + public Collection<GeoLocations> getGeoLocations(String... names) { + Collection<GeoLocations> result = new ArrayList<GeoLocations>(); + if (geoLocations != null) { + if (names != null) { + for (String name : names) { + GeoLocations gl = getGeoLocations(name); + if (gl != null) { + result.add(gl); + } + } + } else { + result.addAll(geoLocations); + } + } + return result; + } + // private DocumentStorageImpl<Collection<GeoLocations>, File> documentStorage = new DocumentStorageImpl<Collection<GeoLocations>, File>() { 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 new file mode 100644 index 0000000..f43ba34 --- /dev/null +++ b/tdt4140-gr1800/app.core/src/test/java/tdt4140/gr1800/app/core/AppTest.java @@ -0,0 +1,34 @@ +package tdt4140.gr1800.app.core; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class AppTest { + + private App app; + + @Before + public void setUp() { + app = new App(); + } + + @Test + public void testLoadDocument() { + 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) { + Assert.fail("Couldn't open " + file); + } + Assert.assertEquals(file, documentStorage.getDocumentLocation()); + GeoLocationsPersistenceTest.testGeoLocationsDotJson(app.getGeoLocations((String[]) null)); + } +} 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 08f6125..4eb7dc5 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 @@ -2,7 +2,6 @@ package tdt4140.gr1800.app.core; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -24,20 +23,20 @@ public class GeoLocationsPersistenceTest { @Test public void testLoadLocations() { - testLoadLocations(getClass().getResourceAsStream("geoLocations.json")); - } - - private void testLoadLocations(InputStream inputStream) { try { - Collection<GeoLocations> geoLocations = persistence.loadLocations(inputStream); - Assert.assertEquals(2, geoLocations.size()); - Iterator<GeoLocations> it = geoLocations.iterator(); - GeoLocationsTest.assertGeoLocations(it.next(), new LatLong(63, 10), new LatLong(63.1, 10.1)); - GeoLocationsTest.assertGeoLocations(it.next(), new LatLong(64, 11), new LatLong(64.1, 11.1)); + Collection<GeoLocations> geoLocations = persistence.loadLocations(getClass().getResourceAsStream("geoLocations.json")); + testGeoLocationsDotJson(geoLocations); } catch (Exception e) { Assert.fail(e.getMessage()); } } + + public static void testGeoLocationsDotJson(Collection<GeoLocations> geoLocations) { + Assert.assertEquals(2, geoLocations.size()); + Iterator<GeoLocations> it = geoLocations.iterator(); + GeoLocationsTest.assertGeoLocations(it.next(), new LatLong(63, 10), new LatLong(63.1, 10.1)); + GeoLocationsTest.assertGeoLocations(it.next(), new LatLong(64, 11), new LatLong(64.1, 11.1)); + } @Test public void testSaveLocations() { @@ -48,7 +47,8 @@ public class GeoLocationsPersistenceTest { try { persistence.saveLocations(geoLocations, outputStream); outputStream.close(); - testLoadLocations(new ByteArrayInputStream(outputStream.toByteArray())); + Collection<GeoLocations> geoLocations2 = persistence.loadLocations(new ByteArrayInputStream(outputStream.toByteArray())); + testGeoLocationsDotJson(geoLocations2); } catch (Exception e) { Assert.fail(e.getMessage()); } -- GitLab