KickJava   Java API By Example, From Geeks To Geeks.

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


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 13-Jun-2003
9  * Filename $RCSfile: TestDAMLClass.java,v $
10  * Revision $Revision: 1.14 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/04/08 17:37:11 $
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.daml.impl.test;
23
24
25
26 // Imports
27
///////////////
28
import junit.framework.*;
29
30 import com.hp.hpl.jena.ontology.daml.*;
31 import com.hp.hpl.jena.rdf.model.*;
32 import com.hp.hpl.jena.vocabulary.DAML_OIL;
33 import com.hp.hpl.jena.vocabulary.RDFS;
34
35
36
37 /**
38  * <p>
39  * unit tests for DAML Class
40  * </p>
41  *
42  * @author Ian Dickinson, HP Labs
43  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
44  * @version CVS $Id: TestDAMLClass.java,v 1.14 2005/04/08 17:37:11 ian_dickinson Exp $
45  */

46 public class TestDAMLClass
47     extends DAMLTestBase
48 {
49     // Constants
50
//////////////////////////////////
51

52     // Static variables
53
//////////////////////////////////
54

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

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

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

73     public OntTestCase[] getTests() {
74         return new OntTestCase[] {
75             new OntTestCase( "DAMLClass.prop_subClassOf" ) {
76                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
77                     DAMLClass A = m.createDAMLClass( NS + "A" );
78                     DAMLClass B = m.createDAMLClass( NS + "B" );
79                     DAMLClass C = m.createDAMLClass( NS + "C" );
80
81                     assertEquals( "prop_subClassOf property", RDFS.subClassOf, A.prop_subClassOf().getProperty() );
82
83                     assertEquals( "sub-class cardinality", 2, A.prop_subClassOf().count() );
84                     A.prop_subClassOf().add( B );
85                     iteratorTest( A.prop_subClassOf().getAll(), new Object JavaDoc[] {A,B,DAML_OIL.Thing} );
86                     assertEquals( "sub-class cardinality", 3, A.prop_subClassOf().count() );
87                     A.prop_subClassOf().add( C );
88                     assertEquals( "sub-class cardinality", 4, A.prop_subClassOf().count() );
89
90                     iteratorTest( A.prop_subClassOf().getAll(), new Object JavaDoc[] {A,B,C,DAML_OIL.Thing} );
91
92                     A.prop_subClassOf().remove( C );
93                     assertEquals( "sub-class cardinality", 3, A.prop_subClassOf().count() );
94
95                     iteratorTest( A.prop_subClassOf().getAll(), new Object JavaDoc[] {A,B,DAML_OIL.Thing} );
96
97                     assertTrue( "hasValue", A.prop_subClassOf().hasValue( B ) );
98                     assertTrue( "hasValue", !A.prop_subClassOf().hasValue( C ) );
99                 }
100             },
101             new OntTestCase( "DAMLClass.prop_disjointWith" ) {
102                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
103                     DAMLClass A = m.createDAMLClass( NS + "A" );
104                     DAMLClass B = m.createDAMLClass( NS + "B" );
105                     DAMLClass C = m.createDAMLClass( NS + "C" );
106
107                     assertEquals( "prop_disjointwith property", DAML_OIL.disjointWith, A.prop_disjointWith().getProperty() );
108
109                     assertEquals( "disjointWith cardinality", 0, A.prop_disjointWith().count() );
110                     A.prop_disjointWith().add( B );
111                     assertEquals( "disjointwith cardinality", 1, A.prop_disjointWith().count() );
112                     A.prop_disjointWith().add( C );
113                     assertEquals( "disjointWith cardinality", 2, A.prop_disjointWith().count() );
114
115                     iteratorTest( A.prop_disjointWith().getAll(), new Object JavaDoc[] {B,C} );
116
117                     A.prop_disjointWith().remove( C );
118                     assertEquals( "disjointwith cardinality", 1, A.prop_disjointWith().count() );
119
120                     iteratorTest( A.prop_disjointWith().getAll(), new Object JavaDoc[] {B} );
121
122                     assertTrue( "hasValue", A.prop_disjointWith().hasValue( B ) );
123                     assertTrue( "hasValue", !A.prop_disjointWith().hasValue( C ) );
124                 }
125             },
126             new OntTestCase( "DAMLClass.prop_sameClassAs" ) {
127                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
128                     DAMLClass A = m.createDAMLClass( NS + "A" );
129                     DAMLClass B = m.createDAMLClass( NS + "B" );
130                     DAMLClass C = m.createDAMLClass( NS + "C" );
131
132                     assertEquals( "prop_sameClassAs property", DAML_OIL.sameClassAs, A.prop_sameClassAs().getProperty() );
133
134                     assertEquals( "sameClassAs cardinality", 0, A.prop_sameClassAs().count() );
135                     A.prop_sameClassAs().add( B );
136                     assertEquals( "sameClassAs cardinality", 1, A.prop_sameClassAs().count() );
137                     A.prop_sameClassAs().add( C );
138                     assertEquals( "sameClassAs cardinality", 2, A.prop_sameClassAs().count() );
139
140                     iteratorTest( A.prop_sameClassAs().getAll(), new Object JavaDoc[] {B,C} );
141
142                     A.prop_sameClassAs().remove( C );
143                     assertEquals( "sameClassAs cardinality", 1, A.prop_sameClassAs().count() );
144
145                     iteratorTest( A.prop_sameClassAs().getAll(), new Object JavaDoc[] {B} );
146
147                     assertTrue( "hasValue", A.prop_sameClassAs().hasValue( B ) );
148                     assertTrue( "hasValue", !A.prop_sameClassAs().hasValue( C ) );
149                 }
150             },
151             new OntTestCase( "DAMLClass.prop_complementOf" ) {
152                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
153                     DAMLClass A = m.createDAMLClass( NS + "A" );
154                     DAMLClass B = m.createDAMLClass( NS + "B" );
155                     DAMLClass C = m.createDAMLClass( NS + "C" );
156
157                     assertEquals( "prop_complementOf property", DAML_OIL.complementOf, A.prop_complementOf().getProperty() );
158
159                     assertEquals( "complementOf cardinality", 0, A.prop_complementOf().count() );
160                     A.prop_complementOf().add( B );
161                     assertEquals( "complementOf cardinality", 1, A.prop_complementOf().count() );
162                     A.prop_complementOf().add( C );
163                     assertEquals( "complementOf cardinality", 2, A.prop_complementOf().count() );
164
165                     iteratorTest( A.prop_complementOf().getAll(), new Object JavaDoc[] {B,C} );
166
167                     A.prop_complementOf().remove( C );
168                     assertEquals( "complementOf cardinality", 1, A.prop_complementOf().count() );
169
170                     iteratorTest( A.prop_complementOf().getAll(), new Object JavaDoc[] {B} );
171
172                     assertTrue( "hasValue", A.prop_complementOf().hasValue( B ) );
173                     assertTrue( "hasValue", !A.prop_complementOf().hasValue( C ) );
174                 }
175             },
176             new OntTestCase( "DAMLClass.getSubClasses" ) {
177                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
178                     DAMLClass A = m.createDAMLClass( NS + "A" );
179                     DAMLClass B = m.createDAMLClass( NS + "B" );
180                     DAMLClass C = m.createDAMLClass( NS + "C" );
181
182                     C.prop_subClassOf().add( B );
183                     B.prop_subClassOf().add( A );
184
185                     assertEquals( "subClassOf A", B, A.getSubClass() );
186
187                     iteratorTest( A.getSubClasses(), new Object JavaDoc[] {B,C} );
188                     iteratorTest( A.getSubClasses( false ), new Object JavaDoc[] {B} );
189                     iteratorTest( A.getSubClasses( true ), new Object JavaDoc[] {B,C} );
190                 }
191             },
192             new OntTestCase( "DAMLClass.getSuperClasses" ) {
193                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
194                     DAMLClass A = m.createDAMLClass( NS + "A" );
195                     DAMLClass B = m.createDAMLClass( NS + "B" );
196                     DAMLClass C = m.createDAMLClass( NS + "C" );
197
198                     A.prop_subClassOf().add( B );
199                     B.prop_subClassOf().add( C );
200
201                     iteratorTest( A.getSuperClasses(), new Object JavaDoc[] {B,C,DAML_OIL.Thing} );
202                     iteratorTest( A.getSuperClasses( false ), new Object JavaDoc[] {B} );
203                     iteratorTest( A.getSuperClasses( true ), new Object JavaDoc[] {B,C,DAML_OIL.Thing} );
204                 }
205             },
206             new OntTestCase( "DAMLClass.getSameClasses" ) {
207                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
208                     DAMLClass A = m.createDAMLClass( NS + "A" );
209                     DAMLClass B = m.createDAMLClass( NS + "B" );
210                     DAMLClass C = m.createDAMLClass( NS + "C" );
211
212                     A.prop_sameClassAs().add( B );
213                     B.prop_sameClassAs().add( C );
214
215                     // no inference
216
iteratorTest( A.getSameClasses(), new Object JavaDoc[] {B} );
217
218                     A.prop_sameClassAs().add( C ); // could be inferred
219

220                     iteratorTest( A.getSameClasses(), new Object JavaDoc[] {B,C} );
221                 }
222             },
223             new OntTestCase( "DAMLClass.getInstances" ) {
224                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
225                     DAMLClass A = m.createDAMLClass( NS + "A" );
226                     DAMLInstance a = m.createDAMLInstance( A, NS + "a" );
227                     DAMLInstance b = m.createDAMLInstance( A, NS + "b" );
228                     DAMLInstance c = m.createDAMLInstance( A, NS + "c" );
229
230                     iteratorTest( A.getInstances(), new Object JavaDoc[] {a,b,c} );
231                 }
232             },
233             new OntTestCase( "DAMLClass.disjointUnionOf" ) {
234                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
235                     DAMLClass A = m.createDAMLClass( NS + "A" );
236                     DAMLClass B = m.createDAMLClass( NS + "B" );
237                     DAMLClass C = m.createDAMLClass( NS + "C" );
238                     DAMLClass D = m.createDAMLClass( NS + "D" );
239
240                     assertEquals( "prop_disjointUnionOf property", DAML_OIL.disjointUnionOf, A.prop_disjointUnionOf().getProperty() );
241
242                     assertEquals( "disjointUnionOf cardinality", 0, A.prop_disjointUnionOf().count() );
243                     A.prop_disjointUnionOf().add( m.createDAMLList( new RDFNode[] {B,C} ) );
244
245                     assertEquals( "disjointUnionOf cardinality", 1, A.prop_disjointUnionOf().count() );
246
247                     iteratorTest( A.prop_disjointUnionOf().getList().getAll(), new Object JavaDoc[] {B,C} );
248                     A.prop_disjointUnionOf().getList().add( D );
249                     iteratorTest( A.prop_disjointUnionOf().getList().getAll(), new Object JavaDoc[] {B,C,D} );
250
251                     assertTrue( "contains", A.prop_disjointUnionOf().getList().contains( D ) );
252                     assertTrue( "contains", A.prop_disjointUnionOf().getList().contains( B ) );
253                     assertTrue( "contains", A.prop_disjointUnionOf().getList().contains( C ) );
254                 }
255             },
256             new OntTestCase( "DAMLClass.unionOf" ) {
257                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
258                     DAMLClass A = m.createDAMLClass( NS + "A" );
259                     DAMLClass B = m.createDAMLClass( NS + "B" );
260                     DAMLClass C = m.createDAMLClass( NS + "C" );
261                     DAMLClass D = m.createDAMLClass( NS + "D" );
262
263                     assertEquals( "prop_unionOf property", DAML_OIL.unionOf, A.prop_unionOf().getProperty() );
264
265                     assertEquals( "unionOf cardinality", 0, A.prop_unionOf().count() );
266                     A.prop_unionOf().add( m.createDAMLList( new RDFNode[] {B,C} ) );
267
268                     assertEquals( "unionOf cardinality", 1, A.prop_unionOf().count() );
269
270                     iteratorTest( A.prop_unionOf().getList().getAll(), new Object JavaDoc[] {B,C} );
271                     A.prop_unionOf().getList().add( D );
272                     iteratorTest( A.prop_unionOf().getList().getAll(), new Object JavaDoc[] {B,C,D} );
273
274                     assertTrue( "contains", A.prop_unionOf().getList().contains( D ) );
275                     assertTrue( "contains", A.prop_unionOf().getList().contains( B ) );
276                     assertTrue( "contains", A.prop_unionOf().getList().contains( C ) );
277                 }
278             },
279             new OntTestCase( "DAMLClass.intersectionOf" ) {
280                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
281                     DAMLClass A = m.createDAMLClass( NS + "A" );
282                     DAMLClass B = m.createDAMLClass( NS + "B" );
283                     DAMLClass C = m.createDAMLClass( NS + "C" );
284                     DAMLClass D = m.createDAMLClass( NS + "D" );
285
286                     assertEquals( "prop_intersectionOf property", DAML_OIL.intersectionOf, A.prop_intersectionOf().getProperty() );
287
288                     assertEquals( "intersectionOf cardinality", 0, A.prop_intersectionOf().count() );
289                     A.prop_intersectionOf().add( m.createDAMLList( new RDFNode[] {B,C} ) );
290
291                     assertEquals( "intersectionOf cardinality", 1, A.prop_intersectionOf().count() );
292
293                     iteratorTest( A.prop_intersectionOf().getList().getAll(), new Object JavaDoc[] {B,C} );
294                     A.prop_intersectionOf().getList().add( D );
295                     iteratorTest( A.prop_intersectionOf().getList().getAll(), new Object JavaDoc[] {B,C,D} );
296
297                     assertTrue( "contains", A.prop_intersectionOf().getList().contains( D ) );
298                     assertTrue( "contains", A.prop_intersectionOf().getList().contains( B ) );
299                     assertTrue( "contains", A.prop_intersectionOf().getList().contains( C ) );
300                 }
301             },
302             new OntTestCase( "DAMLClass.oneOf" ) {
303                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
304                     DAMLClass A = m.createDAMLClass( NS + "A" );
305                     DAMLClass B = m.createDAMLClass( NS + "B" );
306                     DAMLClass C = m.createDAMLClass( NS + "C" );
307                     DAMLClass D = m.createDAMLClass( NS + "D" );
308
309                     assertEquals( "prop_oneOf property", DAML_OIL.oneOf, A.prop_oneOf().getProperty() );
310
311                     assertEquals( "oneOf cardinality", 0, A.prop_oneOf().count() );
312                     A.prop_oneOf().add( m.createDAMLList( new RDFNode[] {B,C} ) );
313
314                     assertEquals( "oneOf cardinality", 1, A.prop_oneOf().count() );
315
316                     iteratorTest( A.prop_oneOf().getList().getAll(), new Object JavaDoc[] {B,C} );
317                     A.prop_oneOf().getList().add( D );
318                     iteratorTest( A.prop_oneOf().getList().getAll(), new Object JavaDoc[] {B,C,D} );
319
320                     assertTrue( "contains", A.prop_oneOf().getList().contains( D ) );
321                     assertTrue( "contains", A.prop_oneOf().getList().contains( B ) );
322                     assertTrue( "contains", A.prop_oneOf().getList().contains( C ) );
323                 }
324             },
325             new OntTestCase( "DAMLClass.getDefinedProperties" ) {
326                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
327                     DAMLClass A = m.createDAMLClass( NS + "A" );
328                     DAMLClass B = m.createDAMLClass( NS + "B" );
329
330                     B.prop_subClassOf().add( A );
331
332                     DAMLObjectProperty p = m.createDAMLObjectProperty( NS + "p" );
333                     DAMLObjectProperty q = m.createDAMLObjectProperty( NS + "q" );
334                     DAMLObjectProperty r = m.createDAMLObjectProperty( NS + "r" );
335
336                     p.prop_domain().add( B );
337                     q.prop_domain().add( B );
338                     r.prop_domain().add( A );
339
340                     iteratorTest( A.getDefinedProperties(), new Object JavaDoc[] {r} );
341                     iteratorTest( B.getDefinedProperties(), new Object JavaDoc[] {p,q,r} ); // note no inference
342
}
343             },
344             new OntTestCase( "DAMLRestriction.prop_onProperty" ) {
345                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
346                     DAMLRestriction A = m.createDAMLRestriction( NS + "A" );
347                     DAMLObjectProperty p = m.createDAMLObjectProperty( NS + "p" );
348
349                     assertEquals( "prop_onProperty property", DAML_OIL.onProperty, A.prop_onProperty().getProperty() );
350
351                     assertEquals( "onProperty cardinality", 0, A.prop_onProperty().count() );
352                     A.prop_onProperty().add( p );
353
354                     assertEquals( "onProperty cardinality", 1, A.prop_onProperty().count() );
355
356                     iteratorTest( A.prop_onProperty().getAll(), new Object JavaDoc[] {p} );
357                 }
358             },
359             new OntTestCase( "DAMLRestriction.prop_toClass" ) {
360                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
361                     DAMLRestriction A = m.createDAMLRestriction( NS + "A" );
362                     DAMLClass B = m.createDAMLClass( NS + "B" );
363
364                     assertEquals( "prop_toClass property", DAML_OIL.toClass, A.prop_toClass().getProperty() );
365
366                     assertEquals( "toClass cardinality", 0, A.prop_toClass().count() );
367                     A.prop_toClass().add( B );
368
369                     assertEquals( "toClass cardinality", 1, A.prop_toClass().count() );
370
371                     iteratorTest( A.prop_toClass().getAll(), new Object JavaDoc[] {B} );
372                 }
373             },
374             new OntTestCase( "DAMLRestriction.prop_hasClass" ) {
375                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
376                     DAMLRestriction A = m.createDAMLRestriction( NS + "A" );
377                     DAMLClass B = m.createDAMLClass( NS + "B" );
378
379                     assertEquals( "prop_hasClass property", DAML_OIL.hasClass, A.prop_hasClass().getProperty() );
380
381                     assertEquals( "hasClass cardinality", 0, A.prop_hasClass().count() );
382                     A.prop_hasClass().add( B );
383
384                     assertEquals( "hasClass cardinality", 1, A.prop_hasClass().count() );
385
386                     iteratorTest( A.prop_hasClass().getAll(), new Object JavaDoc[] {B} );
387                 }
388             },
389             new OntTestCase( "DAMLRestriction.prop_cardinality" ) {
390                 public void doTest( DAMLModel m ) throws Exception JavaDoc {
391                     DAMLRestriction A = m.createDAMLRestriction( NS + "A" );
392
393                     assertEquals( "prop_cardinality property", DAML_OIL.cardinality, A.prop_cardinality().getProperty() );
394
395                     assertEquals( "cardinality cardinality", 0, A.prop_cardinality().count() );
396                     A.prop_cardinality().addInt( 1 );
397
398                     assertEquals( "cardinality cardinality", 1, A.prop_cardinality().count() );
399
400                     assertEquals( "cardinality", 1, A.prop_cardinality().getInt() );
401                 }
402             },
403         };
404     }
405
406     // Internal implementation methods
407
//////////////////////////////////
408

409     //==============================================================================
410
// Inner class definitions
411
//==============================================================================
412

413 }
414
415
416 /*
417     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
418     All rights reserved.
419
420     Redistribution and use in source and binary forms, with or without
421     modification, are permitted provided that the following conditions
422     are met:
423
424     1. Redistributions of source code must retain the above copyright
425        notice, this list of conditions and the following disclaimer.
426
427     2. Redistributions in binary form must reproduce the above copyright
428        notice, this list of conditions and the following disclaimer in the
429        documentation and/or other materials provided with the distribution.
430
431     3. The name of the author may not be used to endorse or promote products
432        derived from this software without specific prior written permission.
433
434     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
435     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
436     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
437     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
438     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
439     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
440     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
441     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
442     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
443     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
444 */

445
Popular Tags