/** * */ package it.univr.di.cstnu.graph; import it.univr.di.labeledvalue.Constants; import org.junit.Assert; import org.junit.Test; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; import java.util.Objects; /** * Allows to read a DenseTCGraph from a file written in GraphML format.
GraphML format allows the definition of different attributes for the tNGraph, vertices * and edges.
All attributes are defined in the first part of a GraphML file. Examples of GraphML file that can read by this class are given in the * Instances directory under CstnuTool one. * * @author posenato * @version $Id: $Id */ public class STNUGraphMLReaderTest { /** * */ static final File fileSTNU = new File("src/test/resources/testGraphML.stnu"); /** * */ TCGraphMLReader readerSTNU; /** * */ DenseTCGraph stnu; /** * @throws Exception if the file cannot read. */ @Test public void testGraphAttribute() throws Exception { this.readerSTNU = new TCGraphMLReader<>(); this.stnu = this.readerSTNU.readGraph(fileSTNU, STNUEdgeInt.class); Assert.assertEquals("testGraphML", this.stnu.getName()); } /** * @throws IOException none * @throws ParserConfigurationException none * @throws SAXException none */ @Test public void testSTNU() throws Exception { this.readerSTNU = new TCGraphMLReader<>(); this.stnu = this.readerSTNU.readGraph(fileSTNU, STNUEdgeInt.class); Assert.assertEquals(-5, Objects.requireNonNull(this.stnu.getEdge("YX")).getLabeledValue()); Assert.assertEquals(2, Objects.requireNonNull(this.stnu.getEdge("XY")).getLabeledValue()); final TCGraphMLWriter writer = new TCGraphMLWriter(); final String graphXML = writer.save(this.stnu); this.readerSTNU = new TCGraphMLReader<>(); this.stnu = this.readerSTNU.readGraph(graphXML, STNUEdgeInt.class); Assert.assertEquals(-5, Objects.requireNonNull(this.stnu.getEdge("YX")).getLabeledValue()); Assert.assertEquals(2, Objects.requireNonNull(this.stnu.getEdge("XY")).getLabeledValue()); } /** * @throws IOException none * @throws ParserConfigurationException none * @throws SAXException none */ @Test public void testSTNU1() throws Exception { this.readerSTNU = new TCGraphMLReader<>(); this.stnu = this.readerSTNU.readGraph(fileSTNU, STNUEdgeInt.class); Assert.assertEquals(2, this.stnu.getEdgeCount()); } /** * @throws IOException none * @throws ParserConfigurationException none * @throws SAXException none */ @Test public void testSTNU33() throws Exception { this.readerSTNU = new TCGraphMLReader<>(); this.stnu = this.readerSTNU.readGraph(fileSTNU, STNUEdgeInt.class); Assert.assertEquals(Constants.INT_NULL, Objects.requireNonNull(this.stnu.getEdge("YX")).getValue()); Assert.assertEquals(Constants.INT_NULL, Objects.requireNonNull(this.stnu.getEdge("XY")).getValue()); } /** The GraphML reader must populate the requested backend directly, without a dense conversion. * @throws Exception*/ @Test public void testSTNUSparseBackend() throws Exception { this.readerSTNU = new TCGraphMLReader<>(); final TemporalConstraintGraph sparse = this.readerSTNU.readGraph( fileSTNU, STNUEdgeInt.class, new SparseTCGraph<>("prototype", STNUEdgeInt.class)); Assert.assertTrue(sparse instanceof SparseTCGraph); Assert.assertEquals(2, sparse.getEdgeCount()); Assert.assertEquals(-5, Objects.requireNonNull(sparse.getEdge("YX")).getLabeledValue()); Assert.assertEquals(2, Objects.requireNonNull(sparse.getEdge("XY")).getLabeledValue()); Assert.assertEquals(fileSTNU, sparse.getInputFile()); } /** Sparse graphs can be serialized and read again without using the dense/JUNG backend. * @throws Exception*/ @Test public void testSparseRoundTrip() throws Exception { this.readerSTNU = new TCGraphMLReader<>(); final TemporalConstraintGraph sparse = this.readerSTNU.readGraph( fileSTNU, STNUEdgeInt.class, new SparseTCGraph<>("prototype", STNUEdgeInt.class)); final String serialized = new TCGraphMLWriter().save(sparse); final TemporalConstraintGraph reloaded = this.readerSTNU.readGraph( serialized, STNUEdgeInt.class, sparse); Assert.assertTrue(reloaded instanceof SparseTCGraph); Assert.assertEquals(2, reloaded.getEdgeCount()); Assert.assertEquals(-5, Objects.requireNonNull(reloaded.getEdge("YX")).getLabeledValue()); Assert.assertEquals(2, Objects.requireNonNull(reloaded.getEdge("XY")).getLabeledValue()); } }