Save/Load

* enchanted build.gradle
* added test for save/load (still incomplete)
* implemented save/load (still incomplete)
This commit is contained in:
2019-06-13 17:53:43 +02:00
parent 34302b6e92
commit e4de9981be
4 changed files with 129 additions and 18 deletions

View File

@@ -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'*/
}

View File

@@ -547,7 +547,6 @@ public interface Graph<V, W extends Number> extends Iterable<V> {
*/
Map<V, List<Edge<V, W>>> distance(V source) throws NullPointerException, IllegalArgumentException;
/*
static void save(Graph<?, ?> graph, String file) throws IOException {
Map<String, Object> map = new HashMap<>();
map.put("vertices", graph.vertices());
@@ -560,21 +559,27 @@ public interface Graph<V, W extends Number> extends Iterable<V> {
}
@SuppressWarnings("unchecked")
static void load(Graph<Object, Number> graph, String file) throws IOException {
static <V, W extends Number> void load(Graph<V, W> 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<String, Object> map = gson.fromJson(fileContent.toString(), Map.class);
graph.addAllVertices((Collection<Object>)map.get("vertices"));
graph.addAllEdges((Collection<Edge<Object, Number>>)map.get("edges"));
Collection<V> vertices = (Collection<V>)map.get("vertices");
graph.addAllVertices(vertices);
Collection<Map<String, Object>>collection = (Collection<Map<String, Object>>)map.get("edges");
for (Map<String, Object> 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*

View File

@@ -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<String, Integer> 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());

1
test/resources/test.json Normal file
View File

@@ -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}]}