1   /*
2   wsmo4j - a WSMO API and Reference Implementation
3   
4   Copyright (c) 2004-2005 , OntoText Lab. / SIRMA
5                             University of Innsbruck, Austria  
6   
7   This library is free software; you can redistribute it and/or modify it under
8   the terms of the GNU Lesser General Public License as published by the Free
9   Software Foundation; either version 2.1 of the License, or (at your option)
10  any later version.
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  details.
15  You should have received a copy of the GNU Lesser General Public License along
16  with this library; if not, write to the Free Software Foundation, Inc.,
17  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  
20  package test.wsmo4j.factory;
21  
22  /**
23   * <p>Title: WSMO4J</p>
24   * <p>Description: WSMO API and a Reference Implementation</p>
25   * <p>Copyright:  Copyright (c) 2004</p>
26   * <p>Company: OntoText Lab. / SIRMA </p>
27   * @author not attributable
28   * @version 1.0
29   *
30   */
31  import java.net.URI;
32  
33  import org.omwg.ontology.*;
34  import org.wsmo.common.*;
35  import org.wsmo.common.exception.InvalidModelException;
36  import org.wsmo.mediator.*;
37  import org.wsmo.service.*;
38  
39  import test.wsmo4j.Wsmo4jTestCase;
40  
41  public class WSMOFactoryTest extends Wsmo4jTestCase {
42      
43      private String ns = "http://example.org/";
44      
45      public void testMetaModeling() {
46          IRI iri = factory.createIRI(ns+"duplicatedIRI");
47          WebService ws = factory.getWebService(iri);
48          Ontology ont = null;
49          
50          try {
51              ont = factory.getOntology(iri);
52              fail();
53          }
54          catch (Exception e) {
55          }
56          
57          IRI iri2 = factory.createIRI(ns+"duplicatedIRI2");
58          ws = factory.createWebService(iri2);
59          
60          try {
61              ont = factory.createOntology(iri2);
62              fail();
63          }
64          catch (Exception e) {
65          }
66      }
67  
68      public void testCreateAnonymousID() {
69          UnnumberedAnonymousID id = factory.createAnonymousID();
70          assertNotNull(id);
71          
72          // equality check
73          UnnumberedAnonymousID anotherID = factory.createAnonymousID();
74          assertFalse("Bad equals()", id.equals(anotherID));
75      }
76  
77      public void testCreateAxiom() throws InvalidModelException{
78          IRI axiomID = factory.createIRI(ns+"testAxiom");
79  
80          Axiom result = factory.createAxiom(axiomID);
81          assertNotNull("Unable to create Axiom!", result);
82          assertEquals("return value", result.getIdentifier(), axiomID);
83          
84          IRI anotherAxiomID = factory.createIRI(ns+"testAxiom(1)");
85          Axiom neqAxiom = factory.createAxiom(anotherAxiomID);
86          Axiom eqAxiom = factory.createAxiom(axiomID);
87          
88          assertSame("Bad axiom - should be identical", result, eqAxiom);
89          assertFalse("Bad equals()", result.equals(neqAxiom));
90          
91          boolean check = false;
92          try {
93              factory.createAxiom(null);
94          }
95          catch (IllegalArgumentException iae) {
96              check = true;
97          }
98          assertTrue("preconditions not checked!", check);
99      }
100 
101     public void testCreateCapability() {
102         IRI capID = factory.createIRI(ns+"testCapability");
103 
104         Capability result = factory.createCapability(capID);
105         assertNotNull("Unable to create Capability!", result);
106         assertEquals("return value", result.getIdentifier(), capID);
107 
108         IRI anotherCapID = factory.createIRI(ns+"testCapability(1)");
109         Capability neqCap = factory.createCapability(anotherCapID);
110         Capability eqCap = factory.createCapability(capID);
111         
112         assertSame("Bad capability - should be identical", result, eqCap);
113         assertFalse("Bad equals()", result.equals(neqCap));
114 
115         boolean check = false;
116         try {
117             factory.createCapability(null);
118         }
119         catch (IllegalArgumentException iae) {
120             check = true;
121         }
122         assertTrue("preconditions not checked!", check);
123 
124     }
125 
126     public void testCreateConcept() throws InvalidModelException {
127 
128         IRI ontURI = factory.createIRI(ns+"testCreateConcept#ontology01");
129         Ontology owner = factory.createOntology(ontURI);
130 
131         IRI conceptID = factory
132                 .createIRI(ns+"testCreateConcept#concept01");
133         Concept result = factory.createConcept(conceptID);
134         owner.addConcept(result);
135 
136         assertEquals("return value", result.getIdentifier(), conceptID);
137         assertEquals(result.getOntology(), owner);
138         assertEquals(owner.findConcept(conceptID), result);
139 
140         IRI anotherConceptID = factory.createIRI(ns+"testConcept(1)");
141         Concept neqConcept = factory.createConcept(anotherConceptID);
142         Concept eqConcept = factory.createConcept(conceptID);
143         
144         assertSame("Bad concept - should be identical", result, eqConcept);
145         assertFalse("Bad equals()", result.equals(neqConcept));
146 
147         //test with invalid args
148         boolean check1 = false;
149         try {
150             factory.createConcept(null);
151         }
152         catch (IllegalArgumentException ex) {
153             check1 = true;
154         }
155 
156         assertTrue("preconditions not checked!",  
157                 true == check1);
158     }
159 
160 
161     public void testCreateGGMediator() {
162         IRI medID = factory.createIRI(ns+"testGGMediator");
163 
164         GGMediator result = factory.createGGMediator(medID);
165         assertNotNull("Unable to create mediator!", result);
166         assertEquals("return value", result.getIdentifier(), medID);
167 
168         IRI anotherMedID = factory.createIRI(ns+"testGGMediator(2)");
169         GGMediator neqMed = factory.createGGMediator(anotherMedID);
170         GGMediator eqMed = factory.createGGMediator(medID);
171         
172         assertSame("Bad GGMediator - should be identical", result, eqMed);
173         assertFalse("Bad equals()", result.equals(neqMed));
174         
175         boolean check = false;
176         try {
177             factory.createGGMediator(null);
178         }
179         catch (IllegalArgumentException iae) {
180             check = true;
181         }
182         assertTrue("preconditions not checked!", check);
183     }
184 
185     public void testCreateGoal() {
186         IRI goalID = factory.createIRI(ns+"testGoal");
187 
188         Goal result = factory.createGoal(goalID);
189         assertNotNull("Unable to create goal!", result);
190         assertEquals("return value", result.getIdentifier(), goalID);
191 
192         IRI anotherGoalID = factory.createIRI(ns+"testGoal1");
193         assertTrue(factory.createGoal(goalID).equals(result));
194         assertFalse(factory.createGoal(anotherGoalID).equals(result));
195         
196         boolean check = false;
197         try {
198             factory.createGoal(null);
199         }
200         catch (IllegalArgumentException iae) {
201             check = true;
202         }
203         assertTrue("preconditions not checked!", check);
204     }
205 
206     public void testCreateInstance() throws InvalidModelException {
207 
208         // 1. insatnce with a concept
209         IRI ontURI = factory
210                 .createIRI(ns+"testCreateInstance#ontology01");
211         Ontology owner = factory.createOntology(ontURI);
212 
213         IRI conceptID = factory
214                 .createIRI(ns+"testCreateInstance#concept01");
215         Concept concept = factory.createConcept(conceptID);
216         owner.addConcept(concept);
217 
218         IRI instanceID = factory
219                 .createIRI(ns+"testCreateInstance#instance01");
220         Instance result = factory.createInstance(instanceID, concept);
221         owner.addInstance(result);
222 
223         assertEquals("return value", result.getIdentifier(), instanceID);
224         assertEquals(result.getOntology(), owner);
225         assertEquals(result.listConcepts().iterator().next(), concept);
226         assertEquals(owner.findInstance(instanceID), result);
227 
228         IRI anotherInstID = factory.createIRI(ns+"testCreateInstance#instance1.2");
229         Instance neqInst = factory.createInstance(anotherInstID);
230         Instance eqInst = factory.createInstance(instanceID);
231         
232         assertSame("Bad Instance - should be identical", result, eqInst);
233         assertFalse("Bad equals()", result.equals(neqInst));
234         
235 
236         IRI yetAnotherInstID = factory.createIRI(ns+"testCreateInstance#instance2.2");
237         Instance neqInst2 = factory.createInstance(yetAnotherInstID);
238         Instance eqInst2 = factory.createInstance(instanceID);
239         
240         assertSame("Bad Instance - should be identical", result, eqInst2);
241         assertFalse("Bad equals()", result.equals(neqInst2));
242         
243         assertFalse("Bad equals()", result.equals(neqInst));
244 
245         //test with invalid args
246         boolean check2 = false;
247 
248         try {
249             factory.createInstance(null);
250         }
251         catch (IllegalArgumentException ex) {
252             check2 = true;
253         }
254         assertTrue("precondition not checked",check2);
255 
256     }
257 
258     public void testCreateInterface() {
259         IRI interfaceID = factory.createIRI(ns+ns+"testInterface");
260         Interface result = factory.createInterface(interfaceID);
261         assertNotNull("Unable to create Interface!", result);
262         assertEquals("Unable to create Interface!", result.getIdentifier(),
263                 interfaceID);
264 
265         IRI anotherInterfaceID = factory.createIRI(ns+"testInterface(1)");
266         Interface neqInterface = factory.createInterface(anotherInterfaceID);
267         Interface eqInterface = factory.createInterface(interfaceID);
268         
269         assertSame("Bad Interface - should be identical", result, eqInterface);
270         assertFalse("Bad equals()", result.equals(neqInterface));
271         
272         boolean check = false;
273         try {
274             factory.createInterface(null);
275         }
276         catch (IllegalArgumentException iae) {
277             check = true;
278         }
279         assertTrue("preconditions not checked!", check);
280     }
281 
282     public void testCreateOOMediator() {
283         IRI medID = factory.createIRI(ns+"testOOMediator");
284 
285         OOMediator result = factory.createOOMediator(medID);
286         assertEquals("return value", result.getIdentifier(), medID);
287 
288         IRI anotherMedID = factory.createIRI(ns+"testOOMediator(1)");
289         OOMediator neqOOMediator = factory.createOOMediator(anotherMedID);
290         OOMediator eqOOMediator = factory.createOOMediator(medID);
291         
292         assertSame("Bad OOMediator - should be identical", result, eqOOMediator);
293         assertFalse("Bad equals()", result.equals(neqOOMediator));
294 
295         boolean check = false;
296         try {
297             factory.createOOMediator(null);
298         }
299         catch (IllegalArgumentException iae) {
300             check = true;
301         }
302         assertTrue("preconditions not checked!", check);
303     
304     }
305 
306     public void testCreateOntology() {
307         IRI ontoID = factory.createIRI(ns+"testOntology");
308 
309         Ontology result = factory.createOntology(ontoID);
310         assertNotNull("Failed to create ontology", result);
311         assertEquals("invalid ontology", result.getIdentifier(), ontoID);
312 
313         IRI anotherOntoID = factory.createIRI(ns+"testOntology(1)");
314         Ontology neqOntology = factory.createOntology(anotherOntoID);
315         Ontology eqOntology = factory.createOntology(ontoID);
316         
317         assertSame("Bad Ontology - should be identical", result, eqOntology);
318         assertFalse("Bad equals()", result.equals(neqOntology));
319         
320         boolean check = false;
321         try {
322             factory.createOntology(null);
323         }
324         catch (IllegalArgumentException iae) {
325             check = true;
326         }
327         assertTrue("preconditions not checked!", check);
328     }
329 
330 
331     public void testCreateRelation() throws InvalidModelException {
332         IRI relationID = factory
333                 .createIRI(ns+"testCreateConcept#relation01");
334         Relation result = factory.createRelation(relationID);
335 
336         assertNotNull("Failed to create Relation", result);
337         assertEquals("return value", result.getIdentifier(), relationID);
338 
339         IRI anotherRelID = factory.createIRI(ns+"testCreateConcept#relation02");
340         Relation neqRelation = factory.createRelation(anotherRelID);
341         Relation eqRelation = factory.createRelation(relationID);
342         
343         assertSame("Bad Relation - should be identical", result, eqRelation);
344         assertFalse("Bad equals()", result.equals(neqRelation));
345 
346     }
347 
348     public void testCreateRelationInstance() throws InvalidModelException {
349         IRI relationID = factory
350                 .createIRI(ns+"testCreateConcept#relation01");
351         Relation relation = factory.createRelation(relationID);
352 
353         IRI relInstID = factory
354                 .createIRI(ns+"testCreateConcept#rel_inst01");
355         RelationInstance result = factory.createRelationInstance(relInstID, relation);
356 
357         assertNotNull("Failed to create Relation Instance", result);
358         assertEquals("return value", result.getIdentifier(), relInstID);
359         assertEquals("Invalid instanceOf", result.getRelation(), relation);
360 
361         IRI anotherRelInstID = factory.createIRI(ns+"testCreateConcept#rel_inst02");
362         RelationInstance neqRelInst = factory.createRelationInstance(anotherRelInstID, relation);
363         RelationInstance eqRelInst = factory.createRelationInstance(relInstID, relation);
364         
365         assertSame("Bad RelationInstance - should be identical", result, eqRelInst);
366         assertFalse("Bad equals()", result.equals(neqRelInst));
367         
368         //test with invalid args
369         boolean check1 = false;
370 
371         try {
372             Identifier anotherID = factory.createAnonymousID();
373             factory.createRelationInstance(anotherID, null);
374         }
375         catch (InvalidModelException ex) {
376             check1 = true;
377         }
378         assertTrue("no relation instance without relation",check1);
379     }
380 
381     public void testCreateIRI() throws Exception {
382 
383         URI uri = new URI("http://wsmo4j.sourceforge.net/");
384 
385         IRI result = factory.createIRI(uri.toString());
386         assertNotNull("Failed to create IRI", result);
387         assertEquals("return value", uri.toString(), result.toString());
388 
389         boolean check = false;
390         try {
391             factory.createIRI(null);
392         }
393         catch (IllegalArgumentException iae) {
394             check = true;
395         }
396         assertTrue("preconditions not checked!", check);
397     }
398 
399 
400     public void testCreateWGMediator() {
401         IRI medID = factory.createIRI(ns+"testWGMediator");
402 
403         WGMediator result = factory.createWGMediator(medID);
404         assertNotNull("failed to create mediator", result);
405         assertEquals("return value", result.getIdentifier(), medID);
406 
407         boolean check = false;
408         try {
409             factory.createWGMediator(null);
410         }
411         catch (IllegalArgumentException iae) {
412             check = true;
413         }
414         assertTrue("preconditions not checked!", check);
415     }
416 
417     public void testCreateWWMediator() {
418         IRI medID = factory.createIRI(ns+"testWWMediator");
419 
420         WWMediator result = factory.createWWMediator(medID);
421         assertNotNull("failed to create mediator", result);
422         assertEquals("return value", result.getIdentifier(), medID);
423 
424         boolean check = false;
425         try {
426             factory.createWWMediator(null);
427         }
428         catch (IllegalArgumentException iae) {
429             check = true;
430         }
431         assertTrue("preconditions not checked!", check);
432     }
433 
434     public void testCreateWebService() {
435         IRI wsID = factory.createIRI(ns+"testWebService");
436 
437         WebService result = factory.createWebService(wsID);
438         assertNotNull("Failed to create WS!", result);
439         assertEquals("return value", result.getIdentifier(), wsID);
440 
441         boolean check = false;
442         try {
443             factory.createWebService(null);
444         }
445         catch (IllegalArgumentException iae) {
446             check = true;
447         }
448         assertTrue("preconditions not checked!", check);
449     }
450 }
451 
452 /*
453  * $Log$
454  * Revision 1.3  2006/02/13 13:10:25  vassil_momtchev
455  * metamodeling of TopEntity disallowed; unittests fixed;
456  *
457  * Revision 1.2  2005/11/04 06:14:26  holgerlausen
458  * fixed unit tests relying on relative iris
459  *
460  * Revision 1.1  2005/09/12 14:26:14  vassil_momtchev
461  * extends Wsmo4jTestCase
462  *
463  * Revision 1.9  2005/09/06 18:40:46  holgerlausen
464  * removed obsolate tests for numbered anonymous ids
465  *
466  * Revision 1.8  2005/09/02 14:10:04  holgerlausen
467  * unit tests are all greenisch now. Moved the ones that are pending to different package. Will check in more detail next week
468  *
469  * Revision 1.7  2005/09/02 13:32:45  ohamano
470  * move logicalexpression packages from ext to core
471  * move tests from logicalexpression.test to test module
472  *
473  * Revision 1.6  2005/08/04 05:43:24  holgerlausen
474  * validity check does not belong here yet...
475  * findbugs fixes
476  *
477  * Revision 1.5  2005/07/29 10:44:37  holgerlausen
478  * wsmo4j test cases run again, however due to open issues some of them fail
479  *
480  * Revision 1.4  2005/01/25 13:58:10  alex_simov
481  * Refactoring:
482  * 1. Instance: getMemberOf()/setMemberOf() replaced by getConcept()/setConcept()
483  * 2. RelationInstance: getInstanceOf()/setInstanceOf() replaced by getRelation()/setRelation()
484  *
485  * Revision 1.3  2005/01/12 16:16:29  alex_simov
486  * checkstyle formatting
487  *
488  * Revision 1.2  2005/01/06 11:15:54  alex_simov
489  * a small test correction
490  *
491  * Revision 1.1  2005/01/04 14:13:33  alex_simov
492  * File TestWSMOFactoryImpl.java is renamed to WSMOFactoryTest.java
493  *
494  * Revision 1.7  2004/11/26 12:22:40  marin_dimitrov
495  * Factory::getFactory renamed to getWSMOFactory
496  *
497  * Revision 1.6  2004/11/15 17:00:40  marin_dimitrov
498  * methods that are not implemented fail()
499  *
500  * Revision 1.5  2004/11/12 10:59:18  marin_dimitrov
501  * more tests
502  *
503  * Revision 1.4  2004/11/12 10:34:29  marin_dimitrov
504  * org.wsmo.common.Factory refactored (no init() call)
505  *
506  * Revision 1.3  2004/11/10 16:23:58  marin_dimitrov
507  * 1. createConcept test extended
508  * 2. createInstance tested
509  *
510  * Revision 1.2  2004/11/10 15:08:03  marin_dimitrov
511  * added header and footer
512  *
513  */