package it.univr.di.cstnu.graph; import it.univr.di.cstnu.graph.Edge.ConstraintType; import it.univr.di.labeledvalue.ALabelAlphabet.ALetter; import org.junit.Before; import org.junit.Test; import java.io.*; import java.nio.charset.StandardCharsets; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; /** * @author posenato */ @SuppressWarnings({"FieldMayBeStatic", "FieldCanBeLocal"}) public class STNUGraphMLWriterTest { private final String fileName = "src/test/resources/testGraphML.stnu"; private TNGraph g; private final String fileOk = """ Number of contingents in the graph 0 Network Type CSTNU Number of edges in the graph 0 Number of vertices in the graph 0 Graph Name The x coordinate for the visualization. A positive value. 0 The y coordinate for the visualization. A positive value. 0 Type: Possible values: contingent|requirement|derived|internal. requirement Value for STN edge. Format: 'integer' Case Value. Format: 'LC(NodeName):integer' or 'UC(NodeName):integer' 1 STNU 2 4 testGraphML 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 contingent UC(Y):-5 contingent LC(Y):2 """; /** * @throws Exception nope */ @Before public void setUp() throws Exception { g = new TNGraph<>("testGraphML", STNUEdgeInt.class); final LabeledNode Z = LabeledNodeSupplier.get("Z"); final LabeledNode Ω = LabeledNodeSupplier.get("Ω"); final LabeledNode X = LabeledNodeSupplier.get("X"); final LabeledNode Y = LabeledNodeSupplier.get("Y"); final STNUEdge xy = new STNUEdgeInt("XY"); xy.setLabeledValue(new ALetter("Y"), 2, false); xy.setConstraintType(ConstraintType.contingent); final STNUEdge yx = new STNUEdgeInt("YX"); yx.setLabeledValue(new ALetter("Y"), -5, true); yx.setConstraintType(ConstraintType.contingent); g.addVertex(Z); g.addVertex(Ω); g.addVertex(X); g.addVertex(Y); g.addEdge(xy, X, Y); g.addEdge(yx, Y, X); } /** * nope */ @Test public void testGraphMLStringWriter() { final TNGraphMLWriter graphWriter = new TNGraphMLWriter(null); final String graphXML = graphWriter.save(g).trim(); assertEquals(fileOk, graphXML); } /** * @throws IOException nope */ @Test public void testGraphMLWriterAbstractLayoutOfLabeledNodeLabeledIntEdge() throws IOException { final TNGraphMLWriter graphWriter = new TNGraphMLWriter(null); graphWriter.save(g, new File(fileName)); try (final BufferedReader input = new BufferedReader( new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8))) { // don't use new // FileReader(this.fileName) because for Java 8 it does not accept "UTF-8" @SuppressWarnings("CheckForOutOfMemoryOnLargeArrayAllocation") final char[] fileAsChar = new char[4200]; if (input.read(fileAsChar) == -1) { fail("Problem reading " + fileName); } final String fileAsString = new String(fileAsChar); input.close(); assertEquals(fileOk, fileAsString.trim()); } } }