1   /*
2   wsmo4j - a WSMO API and Reference Implementation
3   
4   Copyright (c) 2005, University of Innsbruck, Austria
5   
6   This library is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the Free
8   Software Foundation; either version 2.1 of the License, or (at your option)
9   any later version.
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  details.
14  You should have received a copy of the GNU Lesser General Public License along
15  with this library; if not, write to the Free Software Foundation, Inc.,
16  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18  package test.wsmo4j.validator;
19  
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Vector;
24  
25  import junit.framework.TestCase;
26  
27  import org.omwg.logicalexpression.LogicalExpression;
28  import org.omwg.ontology.Axiom;
29  import org.omwg.ontology.Ontology;
30  import org.wsmo.factory.DataFactory;
31  import org.wsmo.factory.Factory;
32  import org.wsmo.factory.LogicalExpressionFactory;
33  import org.wsmo.factory.WsmoFactory;
34  import org.wsmo.validator.ValidationError;
35  import org.wsmo.validator.ValidationWarning;
36  import org.wsmo.wsml.Parser;
37  
38  /**
39   * TestCase for the validator tests.
40   * 
41   * @author nathalie.steinmetz@deri.org
42   */
43  abstract public class ValidatorTestCase extends TestCase {
44  
45      protected Parser parser;
46      
47      protected WsmoFactory factory;
48      
49      protected LogicalExpressionFactory leFactory;
50      
51      protected DataFactory dataFactory;
52      
53      protected List <LogicalExpression> leSet;
54      
55      public Ontology ontology;
56      
57      public Vector <ValidationError> errors;
58      
59      public Vector <ValidationWarning> warnings;
60      
61      public String ns = "http://ex.org#";
62      
63      public Axiom axiom;
64      
65      public LogicalExpression le;
66      
67      protected String getOntHeader(){
68          return "namespace {_\""+ns+"\"} \n" +
69                  "ontology ont"+System.currentTimeMillis()+"\n";
70      }
71  
72      protected String getOntLocalName() {
73          return "ontology ont" + System.currentTimeMillis() + "\n";
74      }
75      
76      /**
77       * @see TestCase#setUp()
78       */
79      protected void setUp() throws Exception {
80          super.setUp();
81          // create parameters for the parser
82          HashMap <String, Object> createParams = new HashMap <String, Object> ();
83          createParams.put(Factory.PROVIDER_CLASS,"org.deri.wsmo4j.factory.LogicalExpressionFactoryImpl");
84          leFactory = Factory.createLogicalExpressionFactory(createParams);
85          factory = Factory.createWsmoFactory(null);
86          dataFactory = Factory.createDataFactory(null);
87          createParams = new HashMap <String, Object> ();
88          createParams.put(Factory.WSMO_FACTORY, factory);
89          createParams.put(Factory.LE_FACTORY, leFactory);
90          createParams.put(Parser.CACHE_LOGICALEXPRESSION_STRING, "true");
91  
92          // create parser 
93          parser = Factory.createParser(createParams);
94          errors = new Vector <ValidationError>();
95          warnings = new Vector <ValidationWarning>();
96          
97          ontology = factory.createOntology(factory.createIRI(ns + "ont" + System.currentTimeMillis()));
98          ontology.setDefaultNamespace(factory.createIRI(ns));
99          axiom = factory.createAxiom(factory.createIRI(ns + "axiom" + System.currentTimeMillis()));
100         ontology.addAxiom(axiom);
101         
102         leSet = new Vector <LogicalExpression>();
103     }
104 
105     /**
106      * Add a given logical expression as definition to the test axiom.
107      * 
108      * @param axiom String representing the logical expression to be checked 
109      */
110     protected void createAxiomDef(String s) throws Exception {
111         le = leFactory.createLogicalExpression(s, ontology); 
112         axiom.addDefinition(le);
113     }
114     
115     /**
116      * Remove all definitions from the test axiom.
117      */
118     protected void removeAxiomDef() throws Exception {
119         Iterator <LogicalExpression> it = axiom.listDefinitions().iterator();
120         while (it.hasNext()) {
121             leSet.add(it.next());
122         }
123         for (int i=0; i<leSet.size(); i++) {
124             LogicalExpression le = leSet.get(i);
125             axiom.removeDefinition(le);   
126         }
127     }
128     
129     /**
130      * Give out the error messages.
131      * 
132      * @param l list containing the error messages
133      */
134     protected void printError(java.util.List l){
135         for (int i=0; i<l.size(); i++) {
136             System.out.println("\nError " + (i+1) + ":\n" + l.get(i) + "\n\n --------------------------------");
137         }
138     }
139     
140     protected void printWarning(List l) {
141         for (int i=0; i<l.size(); i++) {
142             System.out.println("\nWarning " + (i+1) + ":\n" + l.get(i) + "\n\n -------------------------------");
143         }
144     }
145     
146     /**
147      * @see TestCase#tearDown()
148      */
149     protected void tearDown(){
150         leFactory=null;
151         factory=null;
152         parser=null;
153         ontology=null;
154         axiom=null;
155         errors=null;
156         leSet.clear();
157         le=null;
158         System.gc();
159     }
160     
161 }