From e4de9981becac7cff7a6462d86d809475c452f14 Mon Sep 17 00:00:00 2001 From: Giacomo Date: Thu, 13 Jun 2019 17:53:43 +0200 Subject: [PATCH] Save/Load * enchanted build.gradle * added test for save/load (still incomplete) * implemented save/load (still incomplete) --- build.gradle | 17 +++- src/berack96/sim/util/graph/Graph.java | 19 +++-- test/berack96/test/sim/TestGraph.java | 110 +++++++++++++++++++++++-- test/resources/test.json | 1 + 4 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 test/resources/test.json diff --git a/build.gradle b/build.gradle index b01d19f..f2ad5f6 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,19 @@ apply plugin: 'eclipse' version='1.0-SNAPSHOT' +test { + useJUnitPlatform() +} + +sourceSets { + main { + java { + srcDirs "src" + srcDirs "test" + } + } +} + repositories { mavenCentral() } @@ -10,6 +23,6 @@ repositories { dependencies { compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5' - compile group: 'commons-collections', name: 'commons-collections', version: '3.2' - testCompile group: 'junit', name: 'junit', version: '4.+' + /*compile group: 'commons-collections', name: 'commons-collections', version: '3.2'*/ + /*testCompile 'junit:junit:4.4'*/ } \ No newline at end of file diff --git a/src/berack96/sim/util/graph/Graph.java b/src/berack96/sim/util/graph/Graph.java index 276ca86..bc0a228 100644 --- a/src/berack96/sim/util/graph/Graph.java +++ b/src/berack96/sim/util/graph/Graph.java @@ -547,7 +547,6 @@ public interface Graph extends Iterable { */ Map>> distance(V source) throws NullPointerException, IllegalArgumentException; - /* static void save(Graph graph, String file) throws IOException { Map map = new HashMap<>(); map.put("vertices", graph.vertices()); @@ -560,21 +559,27 @@ public interface Graph extends Iterable { } @SuppressWarnings("unchecked") - static void load(Graph graph, String file) throws IOException { + static void load(Graph graph, String file) throws IOException { + graph.removeAllVertex(); + Gson gson = new Gson(); FileReader reader = new FileReader(file); StringBuilder fileContent = new StringBuilder(); int c; while((c = reader.read()) != -1) - fileContent.append(c); + fileContent.append((char)c); reader.close(); - Map map = gson.fromJson(fileContent.toString(), Map.class); - graph.addAllVertices((Collection)map.get("vertices")); - graph.addAllEdges((Collection>)map.get("edges")); + + Collection vertices = (Collection)map.get("vertices"); + graph.addAllVertices(vertices); + + Collection>collection = (Collection>)map.get("edges"); + for (Map edge : collection) { + graph.addEdge((V) edge.get("source"), (V) edge.get("destination"), (W) edge.get("weight")); + } } - */ // TODO maybe -> STATIC saveOnFile(orString) INSTANCE loadFromFile(orString), but need JSON parser // TODO maybe, but i don't think so... STATIC DISTANCE V* -> V* diff --git a/test/berack96/test/sim/TestGraph.java b/test/berack96/test/sim/TestGraph.java index b47c2c1..48e31d6 100644 --- a/test/berack96/test/sim/TestGraph.java +++ b/test/berack96/test/sim/TestGraph.java @@ -1,5 +1,29 @@ package berack96.test.sim; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + import berack96.sim.util.graph.Edge; import berack96.sim.util.graph.Graph; import berack96.sim.util.graph.MapGraph; @@ -7,26 +31,44 @@ import berack96.sim.util.graph.Vertex; import berack96.sim.util.graph.visit.BFS; import berack96.sim.util.graph.visit.DFS; import berack96.sim.util.graph.visit.VisitInfo; -import org.junit.Before; -import org.junit.Test; - -import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; - -import static org.junit.Assert.*; +@SuppressWarnings("deprecation") public class TestGraph { private Graph graph; - + + private final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + + private final String encoding = "UTF-8"; private final Exception nullException = new NullPointerException(Graph.PARAM_NULL); private final Exception notException = new IllegalArgumentException(Graph.VERTEX_NOT_CONTAINED); private final Exception unsuppException = new UnsupportedOperationException(Vertex.REMOVED); + @Before public void before() { // Change here the instance for changing all the test for that particular class graph = new MapGraph<>(); + + PrintStream p = null; + try { + p = new PrintStream(bytes, true, encoding); + System.setErr(p); + System.setOut(p); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + @After + public void after() { + try { + String printed = bytes.toString(encoding); + if (!printed.isEmpty()) + fail("Remove the printed string in the methods: " + printed); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } } @Test @@ -1270,7 +1312,57 @@ public class TestGraph { assertEquals(4, graph.numberOfVertices()); } - // TODO test saveFile + @Test + public void saveLoad() { + /* + * This graph should be like this + * + * 1 -> 2 <- 6 7 + * ^ ^ + * | | | | + * v v + * 3 <- 5 -> 4 8 + */ + + String fileName = "test/resources/test.json"; + + graph.addVertexIfAbsent("1"); + graph.addVertexIfAbsent("2"); + graph.addVertexIfAbsent("3"); + graph.addVertexIfAbsent("4"); + graph.addVertexIfAbsent("5"); + graph.addVertexIfAbsent("6"); + graph.addVertexIfAbsent("7"); + graph.addVertexIfAbsent("8"); + + graph.addEdge("1", "2", 1); + graph.addEdge("1", "3", 1); + graph.addEdge("2", "5", 4); + graph.addEdge("4", "6", 6); + graph.addEdge("5", "3", 2); + graph.addEdge("5", "4", 5); + graph.addEdge("6", "2", 2); + graph.addEdge("8", "7", 9); + + try { + Graph.save(graph, fileName); + Graph.load(graph, fileName); + graph.removeAllVertex(); + Graph.load(graph, fileName); + } catch (Exception e) { + fail(e.getMessage()); + } + + graph = null; + shouldThrow(new NullPointerException(), () -> { try { + Graph.load(graph, fileName); + } catch (IOException e) { + fail(); + e.printStackTrace(); + } }); + } + + private void shouldContain(Collection actual, Object... expected) { assertEquals("They have not the same number of elements\nActual: " + actual, expected.length, actual.size()); diff --git a/test/resources/test.json b/test/resources/test.json new file mode 100644 index 0000000..eb6138f --- /dev/null +++ b/test/resources/test.json @@ -0,0 +1 @@ +{"vertices":["1","2","3","4","5","6","7","8"],"edges":[{"source":"1","destination":"2","weight":1},{"source":"1","destination":"3","weight":1},{"source":"5","destination":"4","weight":5},{"source":"6","destination":"2","weight":2},{"source":"5","destination":"3","weight":2},{"source":"8","destination":"7","weight":9},{"source":"4","destination":"6","weight":6},{"source":"2","destination":"5","weight":4}]} \ No newline at end of file