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.assertTrue; import static org.junit.Assert.fail; /** * @author posenato */ @SuppressWarnings({"FieldMayBeStatic", "FieldCanBeLocal"}) public class STNUGraphMLWriterTest { /** * Where this test writes. It used to be {@code src/test/resources/testGraphML.stnu}, a **versioned** * file that {@code STNUGraphMLReaderTest} reads as its input: running the suite overwrote it, so the * reader test depended on whether this one had run first, and every {@code mvn test} left the * working copy dirty. A file in {@code target} belongs to nobody and is wiped by {@code clean}. */ private final String fileName = "target/testGraphMLWriter.stnu"; private DenseTCGraph g; /** */ @Before public void setUp() { g = new DenseTCGraph<>("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 TCGraphMLWriter graphWriter = new TCGraphMLWriter(); final String graphXML = graphWriter.save(g).trim(); assertTrue(graphXML.contains("UC(Y):-5")); assertTrue(graphXML.contains("LC(Y):2")); } /** * @throws IOException nope */ @Test public void testGraphMLWriterAbstractLayoutOfLabeledNodeLabeledIntEdge() throws IOException { final TCGraphMLWriter graphWriter = new TCGraphMLWriter(); 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(); assertTrue(fileAsString.contains("")); } } }