KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > ontology > impl > test > TestResource


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 23-May-2003
9  * Filename $RCSfile: TestResource.java,v $
10  * Revision $Revision: 1.12 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/04/11 16:39:55 $
14  * by $Author: ian_dickinson $
15  *
16  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * (see footer for full conditions)
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.ontology.impl.test;
23
24
25 // Imports
26
///////////////
27
import junit.framework.TestSuite;
28
29 import com.hp.hpl.jena.ontology.*;
30 import com.hp.hpl.jena.rdf.model.RDFNode;
31 import com.hp.hpl.jena.rdf.model.Resource;
32 import com.hp.hpl.jena.vocabulary.RDF;
33
34
35
36 /**
37  * <p>
38  * Unit test cases for ontology resources
39  * </p>
40  *
41  * @author Ian Dickinson, HP Labs
42  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
43  * @version CVS $Id: TestResource.java,v 1.12 2005/04/11 16:39:55 ian_dickinson Exp $
44  */

45 public class TestResource
46     extends OntTestBase
47 {
48     // Constants
49
//////////////////////////////////
50

51     // Static variables
52
//////////////////////////////////
53

54
55     // Instance variables
56
//////////////////////////////////
57

58     // Constructors
59
//////////////////////////////////
60

61     static public TestSuite suite() {
62         return new TestResource( "TestResource" );
63     }
64
65     public TestResource( String JavaDoc name ) {
66         super( name );
67     }
68
69
70     // External signature methods
71
//////////////////////////////////
72

73     // Internal implementation methods
74
//////////////////////////////////
75

76     public OntTestCase[] getTests() {
77         return new OntTestCase[] {
78             new OntTestCase( "OntResource.sameAs", true, false, true, false ) {
79                 public void ontTest( OntModel m ) throws Exception JavaDoc {
80                     Profile prof = m.getProfile();
81                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
82                     OntResource b = (OntResource) m.getResource( NS + "b" ).as( OntResource.class );
83                     OntResource c = (OntResource) m.getResource( NS + "c" ).as( OntResource.class );
84
85                     a.addSameAs( b );
86                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.SAME_AS() ) );
87                     assertEquals( "a should be sameAs b", b, a.getSameAs() );
88
89                     a.addSameAs( c );
90                     assertEquals( "Cardinality should be 2", 2, a.getCardinality( prof.SAME_AS() ) );
91                     iteratorTest( a.listSameAs(), new Object JavaDoc[] {b, c} );
92
93                     assertTrue( "a should be the same as b", a.isSameAs( b ) );
94                     assertTrue( "a should be the same as c", a.isSameAs( c ) );
95
96                     a.setSameAs( b );
97                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.SAME_AS() ) );
98                     assertEquals( "a should be sameAs b", b, a.getSameAs() );
99
100                     a.removeSameAs( c );
101                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.SAME_AS() ) );
102                     a.removeSameAs( b );
103                     assertEquals( "Cardinality should be 0", 0, a.getCardinality( prof.SAME_AS() ) );
104                 }
105             },
106             new OntTestCase( "OntResource.differentFrom", true, true, true, false ) {
107                 public void ontTest( OntModel m ) throws Exception JavaDoc {
108                     Profile prof = m.getProfile();
109                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
110                     OntResource b = (OntResource) m.getResource( NS + "b" ).as( OntResource.class );
111                     OntResource c = (OntResource) m.getResource( NS + "c" ).as( OntResource.class );
112
113                     a.addDifferentFrom( b );
114                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.DIFFERENT_FROM() ) );
115                     assertEquals( "a should be differentFrom b", b, a.getDifferentFrom() );
116
117                     a.addDifferentFrom( c );
118                     assertEquals( "Cardinality should be 2", 2, a.getCardinality( prof.DIFFERENT_FROM() ) );
119                     iteratorTest( a.listDifferentFrom(), new Object JavaDoc[] {b, c} );
120
121                     assertTrue( "a should be diff from b", a.isDifferentFrom( b ) );
122                     assertTrue( "a should be diff from c", a.isDifferentFrom( c ) );
123
124                     a.setDifferentFrom( b );
125                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.DIFFERENT_FROM() ) );
126                     assertEquals( "a should be differentFrom b", b, a.getDifferentFrom() );
127
128                     a.removeDifferentFrom( c );
129                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.DIFFERENT_FROM() ) );
130                     a.removeDifferentFrom( b );
131                     assertEquals( "Cardinality should be 0", 0, a.getCardinality( prof.DIFFERENT_FROM() ) );
132                 }
133             },
134             new OntTestCase( "OntResource.seeAlso", true, true, true, true ) {
135                 public void ontTest( OntModel m ) throws Exception JavaDoc {
136                     Profile prof = m.getProfile();
137                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
138                     OntResource b = (OntResource) m.getResource( NS + "b" ).as( OntResource.class );
139                     OntResource c = (OntResource) m.getResource( NS + "c" ).as( OntResource.class );
140
141                     a.addSeeAlso( b );
142                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.SEE_ALSO() ) );
143                     assertEquals( "a should be seeAlso b", b, a.getSeeAlso() );
144
145                     a.addSeeAlso( c );
146                     assertEquals( "Cardinality should be 2", 2, a.getCardinality( prof.SEE_ALSO() ) );
147                     iteratorTest( a.listSeeAlso(), new Object JavaDoc[] {b, c} );
148
149                     assertTrue( "a should have seeAlso b", a.hasSeeAlso( b ) );
150                     assertTrue( "a should have seeAlso c", a.hasSeeAlso( c ) );
151
152                     a.setSeeAlso( b );
153                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.SEE_ALSO() ) );
154                     assertEquals( "a should be seeAlso b", b, a.getSeeAlso() );
155
156                     a.removeSeeAlso( c );
157                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.SEE_ALSO() ) );
158                     a.removeSeeAlso( b );
159                     assertEquals( "Cardinality should be 0", 0, a.getCardinality( prof.SEE_ALSO() ) );
160                 }
161             },
162             new OntTestCase( "OntResource.isDefinedBy", true, true, true, true ) {
163                 public void ontTest( OntModel m ) throws Exception JavaDoc {
164                     Profile prof = m.getProfile();
165                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
166                     OntResource b = (OntResource) m.getResource( NS + "b" ).as( OntResource.class );
167                     OntResource c = (OntResource) m.getResource( NS + "c" ).as( OntResource.class );
168
169                     a.addIsDefinedBy( b );
170                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.IS_DEFINED_BY() ) );
171                     assertEquals( "a should be isDefinedBy b", b, a.getIsDefinedBy() );
172
173                     a.addIsDefinedBy( c );
174                     assertEquals( "Cardinality should be 2", 2, a.getCardinality( prof.IS_DEFINED_BY() ) );
175                     iteratorTest( a.listIsDefinedBy(), new Object JavaDoc[] {b, c} );
176
177                     assertTrue( "a should be defined by b", a.isDefinedBy( b ) );
178                     assertTrue( "a should be defined by c", a.isDefinedBy( c ) );
179
180                     a.setIsDefinedBy( b );
181                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.IS_DEFINED_BY() ) );
182                     assertEquals( "a should be isDefinedBy b", b, a.getIsDefinedBy() );
183
184                     a.removeDefinedBy( c );
185                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.IS_DEFINED_BY() ) );
186                     a.removeDefinedBy( b );
187                     assertEquals( "Cardinality should be 0", 0, a.getCardinality( prof.IS_DEFINED_BY() ) );
188             }
189             },
190             new OntTestCase( "OntResource.versionInfo", true, true, true, false ) {
191                 public void ontTest( OntModel m ) throws Exception JavaDoc {
192                     Profile prof = m.getProfile();
193                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
194
195                     a.addVersionInfo( "some info" );
196                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.VERSION_INFO() ) );
197                     assertEquals( "a has wrong version info", "some info", a.getVersionInfo() );
198
199                     a.addVersionInfo( "more info" );
200                     assertEquals( "Cardinality should be 2", 2, a.getCardinality( prof.VERSION_INFO() ) );
201                     iteratorTest( a.listVersionInfo(), new Object JavaDoc[] {"some info", "more info"} );
202
203                     assertTrue( "a should have some info", a.hasVersionInfo( "some info" ) );
204                     assertTrue( "a should have more info", a.hasVersionInfo( "more info" ) );
205
206                     a.setVersionInfo( "new info" );
207                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.VERSION_INFO() ) );
208                     assertEquals( "a has wrong version info", "new info", a.getVersionInfo() );
209
210                     a.removeVersionInfo( "old info" );
211                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.VERSION_INFO() ) );
212                     a.removeVersionInfo( "new info" );
213                     assertEquals( "Cardinality should be 0", 0, a.getCardinality( prof.VERSION_INFO() ) );
214                 }
215             },
216             new OntTestCase( "OntResource.label.nolang", true, true, true, true ) {
217                 public void ontTest( OntModel m ) throws Exception JavaDoc {
218                     Profile prof = m.getProfile();
219                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
220
221                     a.addLabel( "some info", null );
222                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.LABEL() ) );
223                     assertEquals( "a has wrong label", "some info", a.getLabel( null ) );
224
225                     a.addLabel( "more info", null );
226                     assertEquals( "Cardinality should be 2", 2, a.getCardinality( prof.LABEL() ) );
227                     iteratorTest( a.listLabels( null ), new Object JavaDoc[] {m.createLiteral( "some info" ), m.createLiteral( "more info" )} );
228
229                     assertTrue( "a should have label some info", a.hasLabel( "some info", null ) );
230                     assertTrue( "a should have label more info", a.hasLabel( "more info", null ) );
231
232                     a.setLabel( "new info", null );
233                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.LABEL() ) );
234                     assertEquals( "a has wrong label", "new info", a.getLabel( null ) );
235
236                     a.removeLabel( "foo", null );
237                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.LABEL() ) );
238                     a.removeLabel( "new info", null );
239                     assertEquals( "Cardinality should be 0", 0, a.getCardinality( prof.LABEL() ) );
240                 }
241             },
242             new OntTestCase( "OntResource.label.lang", true, true, true, true ) {
243                 public void ontTest( OntModel m ) throws Exception JavaDoc {
244                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
245
246                     a.addLabel( "good", "EN" );
247                     assertEquals( "wrong label", "good", a.getLabel( null ) );
248
249                     a.addLabel( "bon", "FR" );
250
251                     assertEquals( "wrong label", "good", a.getLabel( "EN" ) );
252                     assertEquals( "wrong label", null, a.getLabel( "EN-GB" ) ); // no literal with a specific enough language
253
assertEquals( "wrong label", "bon", a.getLabel( "FR" ) );
254
255                     assertTrue( "a should have label good", a.hasLabel( "good", "EN" ) );
256                     assertTrue( "a should have label bon", a.hasLabel( "bon", "FR" ) );
257                     assertTrue( "a should note have label good (DE)", !a.hasLabel( "good", "DE" ) );
258
259                     a.addLabel( "spiffing", "EN-GB" );
260                     a.addLabel( "duude", "EN-US" );
261
262                     assertEquals( "wrong label", "spiffing", a.getLabel( "EN-GB" ) );
263                     assertEquals( "wrong label", "duude", a.getLabel( "EN-US" ) );
264                     assertEquals( "wrong label", null, a.getLabel( "DE" ) );
265
266                     a.addLabel( "abcdef", "AB-CD" );
267                     assertEquals( "wrong label", "abcdef", a.getLabel( "AB" ) );
268                     assertEquals( "wrong label", null, a.getLabel( "AB-XY" ) );
269
270                     a.removeLabel( "abcde", "AB-CD" );
271                     assertEquals( "Cardinality should be 5", 5, a.getCardinality( a.getProfile().LABEL() ) );
272                     a.removeLabel( "abcdef", "AB-CD" );
273                     assertEquals( "Cardinality should be 4", 4, a.getCardinality( a.getProfile().LABEL() ) );
274                 }
275             },
276             new OntTestCase( "OntResource.comment.nolang", true, true, true, true ) {
277                 public void ontTest( OntModel m ) throws Exception JavaDoc {
278                     Profile prof = m.getProfile();
279                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
280
281                     a.addComment( "some info", null );
282                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.COMMENT() ) );
283                     assertEquals( "a has wrong comment", "some info", a.getComment( null ) );
284
285                     a.addComment( "more info", null );
286                     assertEquals( "Cardinality should be 2", 2, a.getCardinality( prof.COMMENT() ) );
287                     iteratorTest( a.listComments( null ), new Object JavaDoc[] {m.createLiteral( "some info" ), m.createLiteral( "more info" )} );
288
289                     assertTrue( "a should have comment some info", a.hasComment( "some info", null ) );
290                     assertTrue( "a should have comment more info", a.hasComment( "more info", null ) );
291
292                     a.setComment( "new info", null );
293                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.COMMENT() ) );
294                     assertEquals( "a has wrong comment", "new info", a.getComment( null ) );
295
296                     a.removeComment( "foo", null );
297                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( prof.COMMENT() ) );
298                     a.removeComment( "new info", null );
299                     assertEquals( "Cardinality should be 0", 0, a.getCardinality( prof.COMMENT() ) );
300                 }
301             },
302             new OntTestCase( "OntResource.comment.lang", true, true, true, true ) {
303                 public void ontTest( OntModel m ) throws Exception JavaDoc {
304                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
305
306                     a.addComment( "good", "EN" );
307                     assertEquals( "wrong comment", "good", a.getComment( null ) );
308
309                     a.addComment( "bon", "FR" );
310
311                     assertEquals( "wrong comment", "good", a.getComment( "EN" ) );
312                     assertEquals( "wrong comment", null, a.getComment( "EN-GB" ) ); // no literal with a specific enough language
313
assertEquals( "wrong comment", "bon", a.getComment( "FR" ) );
314
315                     assertTrue( "a should have label good", a.hasComment( "good", "EN" ) );
316                     assertTrue( "a should have label bon", a.hasComment( "bon", "FR" ) );
317                     assertTrue( "a should note have label good (DE)", !a.hasComment( "good", "DE" ) );
318
319                     a.addComment( "spiffing", "EN-GB" );
320                     a.addComment( "duude", "EN-US" );
321
322                     assertEquals( "wrong comment", "spiffing", a.getComment( "EN-GB" ) );
323                     assertEquals( "wrong comment", "duude", a.getComment( "EN-US" ) );
324                     assertEquals( "wrong comment", null, a.getComment( "DE" ) );
325
326                     a.addComment( "abcdef", "AB-CD" );
327                     assertEquals( "wrong comment", "abcdef", a.getComment( "AB" ) );
328                     assertEquals( "wrong comment", null, a.getComment( "AB-XY" ) );
329
330                     a.removeComment( "abcde", "AB-CD" );
331                     assertEquals( "Cardinality should be 5", 5, a.getCardinality( a.getProfile().COMMENT() ) );
332                     a.removeComment( "abcdef", "AB-CD" );
333                     assertEquals( "Cardinality should be 4", 4, a.getCardinality( a.getProfile().COMMENT() ) );
334                 }
335             },
336             new OntTestCase( "OntResource.type (no inference)", true, true, true, true ) {
337                 public void ontTest( OntModel m ) throws Exception JavaDoc {
338                     OntClass A = m.createClass( NS + "A" );
339                     OntClass B = m.createClass( NS + "B" );
340                     A.addSubClass( B );
341
342                     OntResource a = (OntResource) m.getResource( NS + "a" ).as( OntResource.class );
343                     assertEquals( "Cardinality of rdf:type is wrong", 0, a.getCardinality( RDF.type ) );
344
345                     a.addRDFType( B );
346                     assertEquals( "rdf:type of a is wrong", B, a.getRDFType() );
347                     assertEquals( "rdf:type of a is wrong", B, a.getRDFType( false ) );
348
349                     iteratorTest( a.listRDFTypes( false ), new Object JavaDoc[] {B} ); // only B since we're not using an inference model
350
iteratorTest( a.listRDFTypes( true ), new Object JavaDoc[] {B} );
351
352                     a.addRDFType( A );
353                     iteratorTest( a.listRDFTypes( false ), new Object JavaDoc[] {A,B} );
354                     iteratorTest( a.listRDFTypes( true ), new Object JavaDoc[] {B} );
355
356                     assertTrue( "a should not be of class A direct", !a.hasRDFType( A, true ));
357                     assertTrue( "a should not be of class B direct", a.hasRDFType( B, true ));
358
359                     OntClass C = m.createClass( NS + "C" );
360                     a.setRDFType( C );
361                     assertTrue( "a should be of class C", a.hasRDFType( C, false ));
362                     assertTrue( "a should not be of class A", !a.hasRDFType( A, false ));
363                     assertTrue( "a should not be of class B", !a.hasRDFType( B, false ));
364
365                     a.removeRDFType( B );
366                     assertEquals( "Cardinality should be 1", 1, a.getCardinality( RDF.type ) );
367                     a.removeRDFType( C );
368                     assertEquals( "Cardinality should be 0", 0, a.getCardinality( RDF.type ) );
369                 }
370             },
371             new OntTestCase( "OntResource.remove", true, true, true, true ) {
372                 public void ontTest( OntModel m ) throws Exception JavaDoc {
373                     OntClass A = m.createClass( NS + "A" );
374                     OntClass B = m.createClass( NS + "B" );
375                     OntClass C = m.createClass( NS + "C" );
376                     OntClass D = m.createClass( NS + "D" );
377                     OntClass E = m.createClass( NS + "E" );
378                     A.addSubClass( B );
379                     A.addSubClass( C );
380                     C.addSubClass( D );
381                     C.addSubClass( E );
382
383                     assertTrue( "super-class of E", E.hasSuperClass( C, false ) );
384                     iteratorTest( A.listSubClasses(), new Object JavaDoc[] {B,C} );
385
386                     C.remove();
387
388                     assertTrue( "super-class of D", !D.hasSuperClass( C, false ) );
389                     assertTrue( "super-class of E", !E.hasSuperClass( C, false ) );
390                     iteratorTest( A.listSubClasses(), new Object JavaDoc[] {B} );
391                 }
392             },
393             new OntTestCase( "OntResource.asClass", true, true, true, true ) {
394                 public void ontTest( OntModel m ) throws Exception JavaDoc {
395                     Resource r = m.createResource();
396                     r.addProperty( RDF.type, m.getProfile().CLASS() );
397                     OntResource or = (OntResource) r.as( OntResource.class );
398                     boolean f = owlFull();
399                     assertFalse( "should not be annotation prop", or.isAnnotationProperty() );
400                     assertFalse( "should not be all different", or.isAllDifferent() );
401                     assertTrue( "should be class", or.isClass() );
402                     assertFalse( "should not be property", or.isProperty() );
403                     assertFalse( "should not be object property", or.isObjectProperty() );
404                     assertFalse( "should not be datatype property", or.isDatatypeProperty() );
405                     assertTrue( "should not be individual", owlFull() || !or.isIndividual() );
406                     assertFalse( "should not be data range", or.isDataRange() );
407                     assertFalse( "should not be ontology", or.isOntology() );
408
409                     RDFNode n = or.asClass();
410                     assertTrue( "Should be OntClass", n instanceof OntClass );
411                 }
412             },
413             new OntTestCase( "OntResource.asAnnotationProperty", true, true, false, false) {
414                 public void ontTest( OntModel m ) throws Exception JavaDoc {
415                     if (m.getProfile().ANNOTATION_PROPERTY() == null) {
416                         throw new ProfileException(null,null);
417                     }
418                     Resource r = m.createResource();
419                     r.addProperty( RDF.type, m.getProfile().ANNOTATION_PROPERTY() );
420                     OntResource or = (OntResource) r.as( OntResource.class );
421
422                     assertTrue( "should be annotation prop", or.isAnnotationProperty() );
423                     assertFalse( "should not be all different", or.isAllDifferent() );
424                     assertFalse( "should not be class", or.isClass() );
425                     assertTrue( "should be property", or.isProperty() );
426                     assertFalse( "should not be object property", or.isObjectProperty() );
427                     assertFalse( "should not be datatype property", or.isDatatypeProperty() );
428                     assertFalse( "should not be individual", or.isIndividual() );
429                     assertFalse( "should not be data range", or.isDataRange() );
430                     assertFalse( "should not be ontology", or.isOntology() );
431
432                     RDFNode n = or.asAnnotationProperty();
433                     assertTrue( "Should be AnnotationProperty", n instanceof AnnotationProperty);
434                 }
435             },
436             new OntTestCase( "OntResource.asObjectProperty", true, true, true, false) {
437                 public void ontTest( OntModel m ) throws Exception JavaDoc {
438                     if (m.getProfile().OBJECT_PROPERTY() == null) {
439                         throw new ProfileException(null,null);
440                     }
441                     Resource r = m.createResource();
442                     r.addProperty( RDF.type, m.getProfile().OBJECT_PROPERTY() );
443                     OntResource or = (OntResource) r.as( OntResource.class );
444
445                     assertFalse( "should not be annotation prop", or.isAnnotationProperty() );
446                     assertFalse( "should not be all different", or.isAllDifferent() );
447                     assertFalse( "should not be class", or.isClass() );
448                     assertTrue( "should be property", or.isProperty() );
449                     assertTrue( "should be object property", or.isObjectProperty() );
450                     assertFalse( "should not be datatype property", or.isDatatypeProperty() );
451                     assertFalse( "should not be individual", or.isIndividual() );
452                     assertFalse( "should not be data range", or.isDataRange() );
453                     assertFalse( "should not be ontology", or.isOntology() );
454
455                     RDFNode n = or.asObjectProperty();
456                     assertTrue( "Should be ObjectProperty", n instanceof ObjectProperty);
457                 }
458             },
459             new OntTestCase( "OntResource.asDatatypeProperty", true, true, true, false) {
460                 public void ontTest( OntModel m ) throws Exception JavaDoc {
461                     if (m.getProfile().DATATYPE_PROPERTY() == null) {
462                         throw new ProfileException(null,null);
463                     }
464                     Resource r = m.createResource();
465                     r.addProperty( RDF.type, m.getProfile().DATATYPE_PROPERTY() );
466                     OntResource or = (OntResource) r.as( OntResource.class );
467
468                     assertFalse( "should not be annotation prop", or.isAnnotationProperty() );
469                     assertFalse( "should not be all different", or.isAllDifferent() );
470                     assertFalse( "should not be class", or.isClass() );
471                     assertTrue( "should be property", or.isProperty() );
472                     assertFalse( "should not be object property", or.isObjectProperty() );
473                     assertTrue( "should be datatype property", or.isDatatypeProperty() );
474                     assertFalse( "should not be individual", or.isIndividual() );
475                     assertFalse( "should not be data range", or.isDataRange() );
476                     assertFalse( "should not be ontology", or.isOntology() );
477
478                     RDFNode n = or.asDatatypeProperty();
479                     assertTrue( "Should be DatatypeProperty", n instanceof DatatypeProperty);
480                 }
481             },
482             new OntTestCase( "OntResource.asAllDifferent", true, true, false, false) {
483                 public void ontTest( OntModel m ) throws Exception JavaDoc {
484                     if (m.getProfile().ALL_DIFFERENT() == null) {
485                         throw new ProfileException(null,null);
486                     }
487                     Resource r = m.createResource();
488                     r.addProperty( RDF.type, m.getProfile().ALL_DIFFERENT() );
489                     OntResource or = (OntResource) r.as( OntResource.class );
490
491                     assertFalse( "should not be annotation prop", or.isAnnotationProperty() );
492                     assertTrue( "should be all different", or.isAllDifferent() );
493                     assertFalse( "should not be class", or.isClass() );
494                     assertFalse( "should not be property", or.isProperty() );
495                     assertFalse( "should not be object property", or.isObjectProperty() );
496                     assertFalse( "should not be datatype property", or.isDatatypeProperty() );
497                     assertFalse( "should not be individual", or.isIndividual() );
498                     assertFalse( "should not be data range", or.isDataRange() );
499                     assertFalse( "should not be ontology", or.isOntology() );
500
501                     RDFNode n = or.asAllDifferent();
502                     assertTrue( "Should be AnnotationProperty", n instanceof AllDifferent);
503                 }
504             },
505             new OntTestCase( "OntResource.asProperty", true, true, true, true ) {
506                 public void ontTest( OntModel m ) throws Exception JavaDoc {
507                     Resource r = m.createResource();
508                     r.addProperty( RDF.type, m.getProfile().PROPERTY() );
509                     OntResource or = (OntResource) r.as( OntResource.class );
510
511                     assertFalse( "should not be annotation prop", or.isAnnotationProperty() );
512                     assertFalse( "should not be all different", or.isAllDifferent() );
513                     assertFalse( "should not be class", or.isClass() );
514                     assertTrue( "should be property", or.isProperty() );
515                     assertFalse( "should not be object property", or.isObjectProperty() );
516                     assertFalse( "should not be datatype property", or.isDatatypeProperty() );
517                     assertFalse( "should not be individual", or.isIndividual() );
518                     assertFalse( "should not be data range", or.isDataRange() );
519                     assertFalse( "should not be ontology", or.isOntology() );
520
521                     RDFNode n = or.asProperty();
522                     assertTrue( "Should be OntProperty", n instanceof OntProperty);
523                 }
524             },
525             new OntTestCase( "OntResource.asIndividual", true, true, true, true ) {
526                 public void ontTest( OntModel m ) throws Exception JavaDoc {
527                     Resource r = m.createResource();
528                     Resource s = m.createResource();
529                     s.addProperty( RDF.type, m.getProfile().CLASS() );
530                     r.addProperty( RDF.type, s );
531                     OntResource or = (OntResource) r.as( OntResource.class );
532
533                     assertFalse( "should not be annotation prop", or.isAnnotationProperty() );
534                     assertFalse( "should not be all different", or.isAllDifferent() );
535                     assertFalse( "should not be class", or.isClass() );
536                     assertFalse( "should not be property", or.isProperty() );
537                     assertFalse( "should not be object property", or.isObjectProperty() );
538                     assertFalse( "should not be datatype property", or.isDatatypeProperty() );
539                     assertTrue( "should be individual", or.isIndividual() );
540                     assertFalse( "should not be data range", or.isDataRange() );
541                     assertFalse( "should not be ontology", or.isOntology() );
542
543                     RDFNode n = or.asIndividual();
544                     assertTrue( "Should be individual", n instanceof Individual);
545                 }
546             },
547             new OntTestCase( "OntResource.asDataRange", true, false, false, false ) {
548                 public void ontTest( OntModel m ) throws Exception JavaDoc {
549                     if (m.getProfile().DATARANGE() == null) {
550                         throw new ProfileException(null,null);
551                     }
552                     Resource r = m.createResource();
553                     r.addProperty( RDF.type, m.getProfile().DATARANGE() );
554                     OntResource or = (OntResource) r.as( OntResource.class );
555
556                     assertFalse( "should not be annotation prop", or.isAnnotationProperty() );
557                     assertFalse( "should not be all different", or.isAllDifferent() );
558                     assertFalse( "should not be class", or.isClass() );
559                     assertFalse( "should not be property", or.isProperty() );
560                     assertFalse( "should not be object property", or.isObjectProperty() );
561                     assertFalse( "should not be datatype property", or.isDatatypeProperty() );
562                     assertFalse( "should not be individual", or.isIndividual() );
563                     assertTrue( "should be data range", or.isDataRange() );
564                     assertFalse( "should not be ontology", or.isOntology() );
565
566                     RDFNode n = or.asDataRange();
567                     assertTrue( "Should be DataRange", n instanceof DataRange );
568                 }
569             },
570             new OntTestCase( "OntResource.asOntology", true, true, true, false ) {
571                 public void ontTest( OntModel m ) throws Exception JavaDoc {
572                     if (m.getProfile().ONTOLOGY() == null) {
573                         throw new ProfileException(null,null);
574                     }
575                     Resource r = m.createResource();
576                     r.addProperty( RDF.type, m.getProfile().ONTOLOGY() );
577                     OntResource or = (OntResource) r.as( OntResource.class );
578
579                     assertFalse( "should not be annotation prop", or.isAnnotationProperty() );
580                     assertFalse( "should not be all different", or.isAllDifferent() );
581                     assertFalse( "should not be class", or.isClass() );
582                     assertFalse( "should not be property", or.isProperty() );
583                     assertFalse( "should not be object property", or.isObjectProperty() );
584                     assertFalse( "should not be datatype property", or.isDatatypeProperty() );
585                     assertFalse( "should not be individual", or.isIndividual() );
586                     assertFalse( "should not be data range", or.isDataRange() );
587                     assertTrue( "should be ontology", or.isOntology() );
588
589                     RDFNode n = or.asOntology();
590                     assertTrue( "Should be Ontology", n instanceof Ontology);
591                 }
592             },
593             new OntTestCase( "OntResource.isLanguageTerm", true, true, true, true ) {
594                 public void ontTest( OntModel m ) throws Exception JavaDoc {
595                     // class is defined (differently) in every profile
596
OntResource or = (OntResource) m.getProfile().CLASS().inModel(m).as( OntResource.class );
597                     assertTrue( "should be a lang term", or.isOntLanguageTerm() );
598
599                     or = m.createOntResource( "http://foo/bar" );
600                     assertFalse( "should not be a lang term", or.isOntLanguageTerm() );
601                 }
602             },
603         };
604     }
605
606     //==============================================================================
607
// Inner class definitions
608
//==============================================================================
609

610 }
611
612
613 /*
614     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
615     All rights reserved.
616
617     Redistribution and use in source and binary forms, with or without
618     modification, are permitted provided that the following conditions
619     are met:
620
621     1. Redistributions of source code must retain the above copyright
622        notice, this list of conditions and the following disclaimer.
623
624     2. Redistributions in binary form must reproduce the above copyright
625        notice, this list of conditions and the following disclaimer in the
626        documentation and/or other materials provided with the distribution.
627
628     3. The name of the author may not be used to endorse or promote products
629        derived from this software without specific prior written permission.
630
631     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
632     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
633     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
634     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
635     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
636     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
637     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
638     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
639     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
640     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
641 */

642
643
644
Popular Tags