// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package tests.xaal.parser.modules; import java.io.StringReader; import java.util.Enumeration; import java.util.Iterator; import junit.framework.TestCase; import xaal.objects.Initial; import xaal.objects.Xaal; import xaal.objects.structures.Array; import xaal.objects.structures.DataStructure; import xaal.objects.structures.Edge; import xaal.objects.structures.Index; import xaal.objects.structures.Key; import xaal.objects.structures.Marker; import xaal.objects.structures.Node; import xaal.objects.structures.Structure; import xaal.parser.ParserModule; import xaal.parser.ParserModuleException; import xaal.parser.XmlParser; import xaal.parser.modules.XaalDSParserModule; import xaal.parser.modules.XaalMainParserModule; /** * @author vkaravir * */ public class XaalDSParserModuleTest extends TestCase { private XmlParser xp; /** * @param name */ public XaalDSParserModuleTest(String name) { super(name); } /* (non-Javadoc) * @see junit.framework.TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); } /* (non-Javadoc) * @see junit.framework.TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); } private void initializeXP() throws ParserModuleException { xp = new XmlParser(); xp.registerParserModule(XaalDSParserModule.class.getName()); xp.registerParserModule(XaalMainParserModule.class.getName()); } /** * Test method for {@link xaal.parser.modules.XaalDSParserModule#startElementArray(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)}. */ public void testElementArray() throws Exception { initializeXP(); xp.parse(new StringReader(""+ ""+ "C")); Initial in = commonTests(); Iterator i = in.getStructures(); assertEquals("Should have created a structure", true, i.hasNext()); Object o = i.next(); assertEquals("The structure should be an instance of " + Array.class.getName(), true, (o instanceof Array)); Array a = (Array) o; assertEquals(false, a.isIndexed()); assertEquals("Size should be 7 while it is " + a.getSize(), 7, a.getSize()); assertEquals("Array orientation not set correctly", "vertical", a.getOrientation()); assertEquals("Id not initialized properly", "array", a.getId()); Index i0 = a.getIndex(0); assertEquals("Index 0 not intialized properly", true, i0 != null); Index i2 = a.getIndex(2); assertEquals("Index 2 should not be intialized", false, i2 != null); o = i0.getContent(); assertEquals("Contents of i0 not an instance of " + Key.class.getName(), true, o instanceof Key); Key k = (Key) o; assertEquals("Contents of i0 not initialized properly", true, k != null); assertEquals("Label of i0 not set correctly", "test", i0.getLabel()); assertEquals("Id of i0 not set correctly", "i0", i0.getId()); assertEquals("Index of i0 not set correctly", 0, i0.getIndex()); assertEquals("Value of key of index 0 not set correctly", "A", k.getValue()); assertEquals("Id of key of index 0 not set correctly", "keyA", k.getId()); Index i4 = a.getIndex(4); k = (Key)i4.getContent(); assertEquals("Value of key of index 4 not set correctly", "C", k.getValue()); assertEquals("Should have created only one structure", false, i.hasNext()); } /** * Test method for {@link xaal.parser.modules.XaalDSParserModule#startElementGraph(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)}. * @throws Exception */ public void testElementGraph() throws Exception { initializeXP(); xp.parse(new StringReader("" + "" + "" + "" + "" + "" + "" + "" + "" + "")); Initial in = commonTests(); Xaal x = (Xaal) XaalMainParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); Iterator i = in.getStructures(); assertEquals("Should have created a structure", true, i.hasNext()); Object o = i.next(); assertEquals("The structure should be an instance of " + DataStructure.class.getName(), true, (o instanceof DataStructure)); DataStructure ds = (DataStructure) o; assertEquals("The structure should be of type Graph", DataStructure.GRAPH_TYPE, ds.getType()); assertEquals("Id not initialized properly", "test", ds.getId()); assertEquals("Should have created only one structure", false, i.hasNext()); i = ds.getNodes(); Node n1 = (Node) i.next(); assertEquals("Node not intialized properly", true, n1 != null); o = n1.getContent(); assertEquals("Contents of n1 not an instance of " + Key.class.getName(), true, o instanceof Key); Key k = (Key) o; assertEquals("Contents of n1 not initialized properly", true, k != null); assertEquals("Value of key of node not set correctly", "D", k.getValue()); assertEquals("Label of n1 not set correctly", "test", n1.getLabel()); assertEquals("Id of n1 not set correctly", "n1", n1.getId()); assertEquals("Id of key of index 0 not set correctly", "keyD", k.getId()); assertEquals("Should have created more nodes", true, i.hasNext()); i.next(); assertEquals("Should have created more nodes", true, i.hasNext()); i.next(); assertEquals("Should have created more nodes", true, i.hasNext()); i.next(); assertEquals("Should not have created more nodes", false, i.hasNext()); i = ds.getReferences(); Edge e1 = (Edge) i.next(); assertEquals("Edge not intialized properly", true, e1 != null); assertEquals(n1.getId(), e1.getFrom()); assertEquals(n1, x.getStructure(e1.getFrom())); assertEquals(true, e1.isDirected()); assertEquals("Label of e1 not set correctly", "test", e1.getLabel()); assertEquals("Id of e1 not set correctly", "e1", e1.getId()); } /** * Test method for {@link xaal.parser.modules.XaalDSParserModule#startElementList(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)}. * @throws Exception */ public void testElementList() throws Exception { initializeXP(); xp.parse(new StringReader("" + "" + "" + "" + "" + "" + "" + "" + "")); Initial in = commonTests(); Xaal x = (Xaal) XaalMainParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); Iterator i = in.getStructures(); assertEquals("Should have created a structure", true, i.hasNext()); Object o = i.next(); assertEquals("The structure should be an instance of " + DataStructure.class.getName(), true, (o instanceof DataStructure)); DataStructure ds = (DataStructure) o; assertEquals("The structure should be of type List", DataStructure.LIST_TYPE, ds.getType()); assertEquals("Id not initialized properly", "test", ds.getId()); assertEquals("Should have created only one structure", false, i.hasNext()); i = ds.getNodes(); Node n1 = (Node) i.next(); assertEquals("Node not intialized properly", true, n1 != null); o = n1.getContent(); assertEquals("Contents of n1 not an instance of " + Key.class.getName(), true, o instanceof Key); Key k = (Key) o; assertEquals("Contents of n1 not initialized properly", true, k != null); assertEquals("Value of key of node not set correctly", "D", k.getValue()); assertEquals("Label of n1 not set correctly", "test", n1.getLabel()); assertEquals("Id of n1 not set correctly", "n1", n1.getId()); assertEquals("Id of key of index 0 not set correctly", "keyD", k.getId()); assertEquals("Should have created more nodes", true, i.hasNext()); i.next(); assertEquals("Should have created more nodes", true, i.hasNext()); i.next(); assertEquals("Should have created more nodes", true, i.hasNext()); i.next(); assertEquals("Should not have created more nodes", false, i.hasNext()); i = ds.getReferences(); Edge e1 = (Edge) i.next(); assertEquals("Edge not intialized properly", true, e1 != null); assertEquals(n1.getId(), e1.getFrom()); assertEquals(n1, x.getStructure(e1.getFrom())); assertEquals(true, e1.isDirected()); assertEquals("Label of e1 not set correctly", "test", e1.getLabel()); assertEquals("Id of e1 not set correctly", "e1", e1.getId()); } /** * Test method for {@link xaal.parser.modules.XaalDSParserModule#startElementTree(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)}. * @throws * @throws Exception */ public void testElementTree() throws Exception { initializeXP(); xp.parse(new StringReader("" + "" + "" + "" + "" + "" + "" + "" + "")); Initial in = commonTests(); Xaal x = (Xaal) XaalMainParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); Iterator i = in.getStructures(); assertEquals("Should have created a structure", true, i.hasNext()); Object o = i.next(); assertEquals("The structure should be an instance of " + DataStructure.class.getName(), true, (o instanceof DataStructure)); DataStructure ds = (DataStructure) o; assertEquals("The structure should be of type Tree", DataStructure.TREE_TYPE, ds.getType()); assertEquals("Id not initialized properly", "test", ds.getId()); assertEquals("Should have created only one structure", false, i.hasNext()); i = ds.getNodes(); Node n1 = (Node) i.next(); assertEquals("Node not intialized properly", true, n1 != null); o = n1.getContent(); assertEquals("Contents of n1 not an instance of " + Key.class.getName(), true, o instanceof Key); Key k = (Key) o; assertEquals("Contents of n1 not initialized properly", true, k != null); assertEquals("Value of key of node not set correctly", "D", k.getValue()); assertEquals("Label of n1 not set correctly", "test", n1.getLabel()); assertEquals("Id of n1 not set correctly", "n1", n1.getId()); assertEquals("Id of key of index 0 not set correctly", "keyD", k.getId()); assertEquals("Should have created more nodes", true, i.hasNext()); i.next(); assertEquals("Should have created more nodes", true, i.hasNext()); i.next(); assertEquals("Should have created more nodes", true, i.hasNext()); i.next(); assertEquals("Should not have created more nodes", false, i.hasNext()); i = ds.getReferences(); Edge e1 = (Edge) i.next(); assertEquals("Edge not intialized properly", true, e1 != null); assertEquals(n1.getId(), e1.getFrom()); assertEquals(n1, x.getStructure(e1.getFrom())); assertEquals(true, e1.isDirected()); assertEquals("Label of e1 not set correctly", "test", e1.getLabel()); assertEquals("Id of e1 not set correctly", "e1", e1.getId()); } public void testElementMarker() throws Exception { initializeXP(); xp.parse(new StringReader("" + "" + "")); Initial in = commonTests(); Xaal x = (Xaal) XaalMainParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); Iterator i = in.getStructures(); Object o = i.next(); assertEquals("Should have created another structure", true, i.hasNext()); Marker m = (Marker) i.next(); i = ((DataStructure) o).getNodes(); Node n1 = (Node) i.next(); assertEquals(n1, m.getTarget()); assertEquals("curr", m.getLabel()); } /** * Test method for {@link xaal.parser.modules.XaalDSParserModule#startElementStructureProperty(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)}. * @throws Exception */ public void testElementStructureProperty() throws Exception { initializeXP(); xp.parse(new StringReader("" + "" + "" + "")); Initial in = commonTests(); Iterator i = in.getStructures(); DataStructure ds = (DataStructure) i.next(); Enumeration e = ds.getProperties(); assertEquals("No structure-properties set", true, e.hasMoreElements()); String s = (String) e.nextElement(); assertEquals("Structure-property test not handled properly", "test", s); assertEquals("Too many structure-properties set", false, e.hasMoreElements()); assertEquals("Structure-property class not handled properly", "testvalue", ds.getStrClass()); assertEquals("Structure-property test not handled properly", "testvalue", ds.getProperty("test")); } /** * Test method for {@link xaal.parser.modules.XaalDSParserModule#startElementEdge(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)}. * @throws Exception */ public void testNestedElements() throws Exception { initializeXP(); xp.parse(new StringReader("" + "" + "" + "" + "")); Initial in = commonTests(); Xaal x = (Xaal) XaalMainParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); Iterator i = in.getStructures(); assertEquals("Should have created a structure", true, i.hasNext()); Object o = i.next(); assertEquals("The structure should be an instance of " + DataStructure.class.getName(), true, (o instanceof DataStructure)); DataStructure ds = (DataStructure) o; assertEquals("The structure should be of type Tree", DataStructure.TREE_TYPE, ds.getType()); assertEquals("Id not initialized properly", "test", ds.getId()); Iterator i2 = ds.getNodes(); Node n1 = (Node) i2.next(); Structure s = n1.getContent(); assertEquals(true, s instanceof Array); assertEquals(true, s != null); Array a = (Array) s; assertEquals("innertest", a.getId()); assertEquals("1", ((Key) a.getIndex(0).getContent()).getValue()); assertEquals("2", ((Key) a.getIndex(1).getContent()).getValue()); assertEquals("Should have created more structures", true, i.hasNext()); a = (Array) i.next(); assertEquals("outertest", a.getId()); assertEquals("1", ((Key) a.getIndex(0).getContent()).getValue()); assertEquals("2", ((Key) a.getIndex(1).getContent()).getValue()); } private Initial commonTests() { Object o = ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); assertEquals("XAAL_ROOT_OBJECT is null", true, o != null); assertEquals("XAAL_ROOT_OBJECT not instance of " + Xaal.class.getName(), true, o instanceof Xaal); Xaal x = (Xaal) o; assertEquals("Xaal.getInitial() returned null", true, x.getInitial() != null); return x.getInitial(); } }