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.locator;
19  
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.omwg.ontology.Ontology;
28  import org.wsmo.common.exception.SynchronisationException;
29  import org.wsmo.factory.Factory;
30  import org.wsmo.locator.Locator;
31  import org.wsmo.locator.LocatorManager;
32  import org.wsmo.wsml.Parser;
33  
34  import test.wsmo4j.Wsmo4jTestCase;
35  import static test.wsmo4j.Utils.*
36  ;/**
37   * Checks if local and remote files are correctly imported. 
38   * 
39   * <pre>
40   * Created on Aug 22, 2006
41   * Committed by $Author: lcekov $
42   * $Source$,
43   * </pre>
44   * 
45   * @author nathalie.steinmetz@deri.org
46   * @version $Revision: 2221 $ $Date: 2007-10-11 14:46:28 +0300 (Thu, 11 Oct 2007) $
47   */
48  public class LocatorImplTest extends Wsmo4jTestCase {
49  	
50  	Parser wsmlParser = null;
51  	
52  	protected void setUp() throws Exception {
53  		wsmlParser = Factory.createParser(null);
54  	}
55  	
56  	/**
57       * This test parses a wsml file and tries to import 4 others. One of those 
58       * is imported from a web server, one locally from the hard disc, the 
59       * third one is not resolvable / parsable and the fourth one is the wsml 
60       * file itself.
61       */
62  	public void testImportOntologies()throws Exception {
63          // read test file and parse it 
64          InputStream is = this.getClass().getClassLoader().getResourceAsStream(
65                  "test/wsmo4j/locator/importsOntology.wsml");
66          assertNotNull(is);
67          // assuming first topentity in file is an ontology  
68          Ontology ontology = (Ontology)wsmlParser.parse(new InputStreamReader(is))[0];  
69          
70          
71          // This map contains a mapping from locigal URIs to physical locations. 
72          // The physical address in this map needs to be changed with respect to 
73          // the real physical location on a test computer
74          HashMap <String, String> mapping = new HashMap <String, String>();
75          mapping.put("http://www.example.org/ontologies/example7", 
76  				getResource("test/wsmo4j/validator/valid_wsml_flight_test.wsml").toString());
77          
78  		HashMap <String, Object> prefs = new HashMap <String, Object> ();
79  		prefs.put(Locator.URI_MAPPING, mapping);
80  		prefs.put(Factory.PROVIDER_CLASS, "org.deri.wsmo4j.locator.LocatorImpl");
81  		
82  		Locator locator = LocatorManager.createLocator(prefs);
83  		Factory.getLocatorManager().addLocator(locator);
84  		
85  		Set <Ontology> ontologies = new HashSet <Ontology> ();
86  		
87  		if (ontology.listOntologies().size() > 0) {
88  			for (Iterator it = ontology.listOntologies().iterator(); it.hasNext();){
89  				Ontology ont = (Ontology) it.next();
90  				try {
91  					ont.getWsmlVariant();
92  					if (ont != null) {
93  						ontologies.add(ont);
94  					}
95  				} catch (SynchronisationException e) {}		
96  			} 
97  		}
98  		
99  		for (Iterator it = ontologies.iterator(); it.hasNext();) {
100 			Ontology ont = (Ontology) it.next();
101 			if (ont != null) {
102 				System.out.println("Successfully imported ontology: "
103 						+ ont.getIdentifier().toString());
104 				System.out.println("The ontology contains " +
105 						ont.listConcepts().size() + " concepts, " +
106 						ont.listInstances().size() + " instances, " + 
107 						ont.listRelations().size() + " relations and " + 
108 						ont.listAxioms().size() + " axioms.");
109 			}
110 		}
111 		Factory.getLocatorManager().removeLocator(locator);
112 	}
113 	
114 }
115 /*
116  * $Log$
117  * Revision 1.4  2007/04/02 12:13:22  morcen
118  * Generics support added to wsmo-api, wsmo4j and wsmo-test
119  *
120  * Revision 1.3  2006/11/27 11:37:14  nathaliest
121  * *** empty log message ***
122  *
123  * Revision 1.2  2006/08/29 15:19:48  nathaliest
124  * changed the validator to call the memory based locator manager and fixed a problem causing loops at the location of ontologies in the validator
125  *
126  * Revision 1.1  2006/08/22 16:26:14  nathaliest
127  * added tests for the new locator implementation
128  *
129  * 
130  */