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 dc4206406ee5027dabfc27e5a246d4107a255c6e..7c09f41946ba050362435e03f4f7f353f9342b9f 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 0000000000000000000000000000000000000000..f43ba340d6afe61fdc5ad907a409aba8eba91913 --- /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 08f61251a6d88c8e0582a93f9c9e3e5aa82dc5ed..4eb7dc5817e158f0f753367c7da9daad9945c19a 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()); }