1   package test.wsmo4j.parser.xml;
2   
3   import java.io.*;
4   import java.util.*;
5   
6   import org.omwg.ontology.*;
7   import org.wsmo.common.*;
8   import org.wsmo.factory.*;
9   import org.wsmo.wsml.*;
10  
11  import test.wsmo4j.*;
12  import static test.wsmo4j.Utils.*;
13  
14  /**
15   * All test methods for xml parsing/serialization
16   * 
17   * @author not attributable
18   */
19  public class XmlParserTest extends Wsmo4jTestCase {
20  
21      protected Parser parser;
22  
23      protected Serializer serializer;
24  
25      protected final String TEST_FILE = "test/xml-ontology-elements-test.xml";
26      
27      protected final String TEST_FILE_WSML = "test/d16-example.wsml";
28  
29      protected void setUp() throws Exception {
30          super.setUp();
31  
32          Map <String, Object> properties = new TreeMap <String, Object> ();
33          properties.put(Factory.PROVIDER_CLASS, "com.ontotext.wsmo4j.parser.xml.WsmlXmlParser");
34          properties.put(Factory.WSMO_FACTORY, factory);
35          properties.put(Factory.LE_FACTORY, leFactory);
36          parser = Factory.createParser(properties);
37          properties = new TreeMap <String, Object> ();
38          properties.put(Factory.PROVIDER_CLASS,
39                  "com.ontotext.wsmo4j.serializer.xml.WsmlXmlSerializer");
40          serializer = Factory.createSerializer(properties);
41      }
42  
43      protected void tearDown() throws Exception {
44          super.tearDown();
45          factory = null;
46          leFactory = null;
47          parser = null;
48          serializer = null;
49          System.gc(); // tries to reinitialize all singletons in WSMO!
50      }
51      
52      public void testD16Example() throws Exception {
53          StringBuffer buffer = new StringBuffer();
54          InputStreamReader reader = new InputStreamReader(getStream(TEST_FILE_WSML));
55          Parser wsmlParser = Factory.createParser(null);
56          TopEntity[] te = wsmlParser.parse(reader);
57          
58          serializer.serialize(te, buffer);
59          parser.parse(buffer);
60      }
61  
62      /**
63       * Try to parse/serialized multiple times a file
64       * @throws Exception
65       */
66      public void testFileIdentityAfterSerializationParse() throws Exception {
67          StringBuffer buffer = new StringBuffer();
68          InputStreamReader reader = new InputStreamReader(getStream(TEST_FILE));
69          int c = -1;
70          while ((c = reader.read()) != -1) {
71              buffer.append((char) c);
72          }
73  
74          TopEntity[] entities = parser.parse(buffer);
75  
76          // check the content of the xml
77  
78          assertEquals(entities.length, 1);
79          assertTrue(entities[0] instanceof Ontology);
80          Ontology o = (Ontology) entities[0];
81          assertEquals(o.listNFPValues().size(), 2);
82          assertTrue(o.listNFPValues(factory.createIRI("http://purl.org/dc/elements/1.1#title"))
83                  .iterator().next() instanceof SimpleDataValue);
84          assertTrue(o.listNFPValues(factory.createIRI("http://purl.org/dc/elements/1.1#date"))
85                  .iterator().next() instanceof ComplexDataValue);
86          assertEquals(o.listInstances().size(), 1);
87          assertEquals(o.listRelations().size(), 1);
88  
89          // end check content
90  
91          int lastHashCode = calcHashCodeBuffer(buffer);
92          buffer = new StringBuffer();
93          serializer.serialize(entities, buffer);
94          if (lastHashCode != calcHashCodeBuffer(buffer)) {
95              fail("Some information is lost during parse serialization!");
96          }
97      }
98  
99      private int calcHashCodeBuffer(CharSequence c) {
100         StringBuffer buffer = new StringBuffer(c.length());
101         for (int i = 0; i < c.length(); i++) {
102             if (c.charAt(i) > 32) {
103                 buffer.append(c.charAt(i));
104             }
105         }
106         
107         // Order of the wsml elements is different
108         // return buffer.toString().hashCode();
109         return buffer.toString().length();
110     }
111 }
112 
113 /*
114  * $Log$
115  * Revision 1.10  2007/04/02 12:13:24  morcen
116  * Generics support added to wsmo-api, wsmo4j and wsmo-test
117  *
118  * Revision 1.9  2006/03/29 11:20:52  vassil_momtchev
119  * tests updated
120  *
121  * Revision 1.8  2006/01/13 10:03:05  vassil_momtchev
122  * test improved
123  *
124  * Revision 1.7  2006/01/11 13:28:42  vassil_momtchev
125  * Parser's constants moved to Factory
126  *
127  * Revision 1.6  2005/11/28 15:35:53  vassil_momtchev
128  * full class names of xml parser and serializer updated
129  *
130  * Revision 1.5  2005/10/05 12:56:28  vassil_momtchev
131  * test updated
132  *
133  * Revision 1.4  2005/09/09 06:35:48  holgerlausen
134  * all open unit tests are moved to clarification:* wokring unit tests under test.*, Wsmo4jTestSuite contains now all tests that are expect to work
135  *
136  * Revision 1.3  2005/09/08 20:20:06  holgerlausen
137  * fixed XML serialization LE integration
138  *
139  * Revision 1.2  2005/09/07 07:01:47  holgerlausen
140  * updated path
141  *
142  * Revision 1.1  2005/08/08 09:24:02  vassil_momtchev
143  * xml parser unittest
144  *
145  */