001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email Ian.Dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 23-May-2003
009: * Filename $RCSfile: TestResource.java,v $
010: * Revision $Revision: 1.19 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:08:39 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * (see footer for full conditions)
018: *****************************************************************************/package com.hp.hpl.jena.ontology.impl.test;
019:
020: // Imports
021: ///////////////
022: import junit.framework.TestSuite;
023:
024: import com.hp.hpl.jena.ontology.*;
025: import com.hp.hpl.jena.rdf.model.Literal;
026: import com.hp.hpl.jena.rdf.model.NodeIterator;
027: import com.hp.hpl.jena.rdf.model.RDFNode;
028: import com.hp.hpl.jena.rdf.model.Resource;
029: import com.hp.hpl.jena.vocabulary.RDF;
030:
031: /**
032: * <p>
033: * Unit test cases for ontology resources
034: * </p>
035: *
036: * @author Ian Dickinson, HP Labs
037: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
038: * @version CVS $Id: TestResource.java,v 1.19 2008/01/02 12:08:39 andy_seaborne Exp $
039: */
040: public class TestResource extends OntTestBase {
041: // Constants
042: //////////////////////////////////
043:
044: // Static variables
045: //////////////////////////////////
046:
047: // Instance variables
048: //////////////////////////////////
049:
050: // Constructors
051: //////////////////////////////////
052:
053: static public TestSuite suite() {
054: return new TestResource("TestResource");
055: }
056:
057: public TestResource(String name) {
058: super (name);
059: }
060:
061: // External signature methods
062: //////////////////////////////////
063:
064: // Internal implementation methods
065: //////////////////////////////////
066:
067: public OntTestCase[] getTests() {
068: return new OntTestCase[] {
069: new OntTestCase("OntResource.sameAs", true, false,
070: true, false) {
071: public void ontTest(OntModel m) throws Exception {
072: Profile prof = m.getProfile();
073: OntResource a = (OntResource) m.getResource(
074: NS + "a").as(OntResource.class);
075: OntResource b = (OntResource) m.getResource(
076: NS + "b").as(OntResource.class);
077: OntResource c = (OntResource) m.getResource(
078: NS + "c").as(OntResource.class);
079:
080: a.addSameAs(b);
081: assertEquals("Cardinality should be 1", 1, a
082: .getCardinality(prof.SAME_AS()));
083: assertEquals("a should be sameAs b", b, a
084: .getSameAs());
085:
086: a.addSameAs(c);
087: assertEquals("Cardinality should be 2", 2, a
088: .getCardinality(prof.SAME_AS()));
089: iteratorTest(a.listSameAs(), new Object[] { b,
090: c });
091:
092: assertTrue("a should be the same as b", a
093: .isSameAs(b));
094: assertTrue("a should be the same as c", a
095: .isSameAs(c));
096:
097: a.setSameAs(b);
098: assertEquals("Cardinality should be 1", 1, a
099: .getCardinality(prof.SAME_AS()));
100: assertEquals("a should be sameAs b", b, a
101: .getSameAs());
102:
103: a.removeSameAs(c);
104: assertEquals("Cardinality should be 1", 1, a
105: .getCardinality(prof.SAME_AS()));
106: a.removeSameAs(b);
107: assertEquals("Cardinality should be 0", 0, a
108: .getCardinality(prof.SAME_AS()));
109: }
110: },
111: new OntTestCase("OntResource.differentFrom", true,
112: true, true, false) {
113: public void ontTest(OntModel m) throws Exception {
114: Profile prof = m.getProfile();
115: OntResource a = (OntResource) m.getResource(
116: NS + "a").as(OntResource.class);
117: OntResource b = (OntResource) m.getResource(
118: NS + "b").as(OntResource.class);
119: OntResource c = (OntResource) m.getResource(
120: NS + "c").as(OntResource.class);
121:
122: a.addDifferentFrom(b);
123: assertEquals("Cardinality should be 1", 1, a
124: .getCardinality(prof.DIFFERENT_FROM()));
125: assertEquals("a should be differentFrom b", b,
126: a.getDifferentFrom());
127:
128: a.addDifferentFrom(c);
129: assertEquals("Cardinality should be 2", 2, a
130: .getCardinality(prof.DIFFERENT_FROM()));
131: iteratorTest(a.listDifferentFrom(),
132: new Object[] { b, c });
133:
134: assertTrue("a should be diff from b", a
135: .isDifferentFrom(b));
136: assertTrue("a should be diff from c", a
137: .isDifferentFrom(c));
138:
139: a.setDifferentFrom(b);
140: assertEquals("Cardinality should be 1", 1, a
141: .getCardinality(prof.DIFFERENT_FROM()));
142: assertEquals("a should be differentFrom b", b,
143: a.getDifferentFrom());
144:
145: a.removeDifferentFrom(c);
146: assertEquals("Cardinality should be 1", 1, a
147: .getCardinality(prof.DIFFERENT_FROM()));
148: a.removeDifferentFrom(b);
149: assertEquals("Cardinality should be 0", 0, a
150: .getCardinality(prof.DIFFERENT_FROM()));
151: }
152: },
153: new OntTestCase("OntResource.seeAlso", true, true,
154: true, true) {
155: public void ontTest(OntModel m) throws Exception {
156: Profile prof = m.getProfile();
157: OntResource a = (OntResource) m.getResource(
158: NS + "a").as(OntResource.class);
159: OntResource b = (OntResource) m.getResource(
160: NS + "b").as(OntResource.class);
161: OntResource c = (OntResource) m.getResource(
162: NS + "c").as(OntResource.class);
163:
164: a.addSeeAlso(b);
165: assertEquals("Cardinality should be 1", 1, a
166: .getCardinality(prof.SEE_ALSO()));
167: assertEquals("a should be seeAlso b", b, a
168: .getSeeAlso());
169:
170: a.addSeeAlso(c);
171: assertEquals("Cardinality should be 2", 2, a
172: .getCardinality(prof.SEE_ALSO()));
173: iteratorTest(a.listSeeAlso(), new Object[] { b,
174: c });
175:
176: assertTrue("a should have seeAlso b", a
177: .hasSeeAlso(b));
178: assertTrue("a should have seeAlso c", a
179: .hasSeeAlso(c));
180:
181: a.setSeeAlso(b);
182: assertEquals("Cardinality should be 1", 1, a
183: .getCardinality(prof.SEE_ALSO()));
184: assertEquals("a should be seeAlso b", b, a
185: .getSeeAlso());
186:
187: a.removeSeeAlso(c);
188: assertEquals("Cardinality should be 1", 1, a
189: .getCardinality(prof.SEE_ALSO()));
190: a.removeSeeAlso(b);
191: assertEquals("Cardinality should be 0", 0, a
192: .getCardinality(prof.SEE_ALSO()));
193: }
194: },
195: new OntTestCase("OntResource.isDefinedBy", true, true,
196: true, true) {
197: public void ontTest(OntModel m) throws Exception {
198: Profile prof = m.getProfile();
199: OntResource a = (OntResource) m.getResource(
200: NS + "a").as(OntResource.class);
201: OntResource b = (OntResource) m.getResource(
202: NS + "b").as(OntResource.class);
203: OntResource c = (OntResource) m.getResource(
204: NS + "c").as(OntResource.class);
205:
206: a.addIsDefinedBy(b);
207: assertEquals("Cardinality should be 1", 1, a
208: .getCardinality(prof.IS_DEFINED_BY()));
209: assertEquals("a should be isDefinedBy b", b, a
210: .getIsDefinedBy());
211:
212: a.addIsDefinedBy(c);
213: assertEquals("Cardinality should be 2", 2, a
214: .getCardinality(prof.IS_DEFINED_BY()));
215: iteratorTest(a.listIsDefinedBy(), new Object[] {
216: b, c });
217:
218: assertTrue("a should be defined by b", a
219: .isDefinedBy(b));
220: assertTrue("a should be defined by c", a
221: .isDefinedBy(c));
222:
223: a.setIsDefinedBy(b);
224: assertEquals("Cardinality should be 1", 1, a
225: .getCardinality(prof.IS_DEFINED_BY()));
226: assertEquals("a should be isDefinedBy b", b, a
227: .getIsDefinedBy());
228:
229: a.removeDefinedBy(c);
230: assertEquals("Cardinality should be 1", 1, a
231: .getCardinality(prof.IS_DEFINED_BY()));
232: a.removeDefinedBy(b);
233: assertEquals("Cardinality should be 0", 0, a
234: .getCardinality(prof.IS_DEFINED_BY()));
235: }
236: },
237: new OntTestCase("OntResource.versionInfo", true, true,
238: true, false) {
239: public void ontTest(OntModel m) throws Exception {
240: Profile prof = m.getProfile();
241: OntResource a = (OntResource) m.getResource(
242: NS + "a").as(OntResource.class);
243:
244: a.addVersionInfo("some info");
245: assertEquals("Cardinality should be 1", 1, a
246: .getCardinality(prof.VERSION_INFO()));
247: assertEquals("a has wrong version info",
248: "some info", a.getVersionInfo());
249:
250: a.addVersionInfo("more info");
251: assertEquals("Cardinality should be 2", 2, a
252: .getCardinality(prof.VERSION_INFO()));
253: iteratorTest(a.listVersionInfo(), new Object[] {
254: "some info", "more info" });
255:
256: assertTrue("a should have some info", a
257: .hasVersionInfo("some info"));
258: assertTrue("a should have more info", a
259: .hasVersionInfo("more info"));
260:
261: a.setVersionInfo("new info");
262: assertEquals("Cardinality should be 1", 1, a
263: .getCardinality(prof.VERSION_INFO()));
264: assertEquals("a has wrong version info",
265: "new info", a.getVersionInfo());
266:
267: a.removeVersionInfo("old info");
268: assertEquals("Cardinality should be 1", 1, a
269: .getCardinality(prof.VERSION_INFO()));
270: a.removeVersionInfo("new info");
271: assertEquals("Cardinality should be 0", 0, a
272: .getCardinality(prof.VERSION_INFO()));
273: }
274: },
275: new OntTestCase("OntResource.label.nolang", true, true,
276: true, true) {
277: public void ontTest(OntModel m) throws Exception {
278: Profile prof = m.getProfile();
279: OntResource a = (OntResource) m.getResource(
280: NS + "a").as(OntResource.class);
281:
282: a.addLabel("some info", null);
283: assertEquals("Cardinality should be 1", 1, a
284: .getCardinality(prof.LABEL()));
285: assertEquals("a has wrong label", "some info",
286: a.getLabel(null));
287:
288: a.addLabel("more info", null);
289: assertEquals("Cardinality should be 2", 2, a
290: .getCardinality(prof.LABEL()));
291: iteratorTest(a.listLabels(null), new Object[] {
292: m.createLiteral("some info"),
293: m.createLiteral("more info") });
294:
295: assertTrue("a should have label some info", a
296: .hasLabel("some info", null));
297: assertTrue("a should have label more info", a
298: .hasLabel("more info", null));
299:
300: a.setLabel("new info", null);
301: assertEquals("Cardinality should be 1", 1, a
302: .getCardinality(prof.LABEL()));
303: assertEquals("a has wrong label", "new info", a
304: .getLabel(null));
305:
306: a.removeLabel("foo", null);
307: assertEquals("Cardinality should be 1", 1, a
308: .getCardinality(prof.LABEL()));
309: a.removeLabel("new info", null);
310: assertEquals("Cardinality should be 0", 0, a
311: .getCardinality(prof.LABEL()));
312: }
313: },
314: new OntTestCase("OntResource.label.lang", true, true,
315: true, true) {
316: public void ontTest(OntModel m) throws Exception {
317: OntResource a = (OntResource) m.getResource(
318: NS + "a").as(OntResource.class);
319:
320: a.addLabel("good", "EN");
321: assertEquals("wrong label", "good", a
322: .getLabel(null));
323:
324: a.addLabel("bon", "FR");
325:
326: assertEquals("wrong label", "good", a
327: .getLabel("EN"));
328: assertEquals("wrong label", null, a
329: .getLabel("EN-GB")); // no literal with a specific enough language
330: assertEquals("wrong label", "bon", a
331: .getLabel("FR"));
332:
333: assertTrue("a should have label good", a
334: .hasLabel("good", "EN"));
335: assertTrue("a should have label bon", a
336: .hasLabel("bon", "FR"));
337: assertTrue(
338: "a should note have label good (DE)",
339: !a.hasLabel("good", "DE"));
340:
341: a.addLabel("spiffing", "EN-GB");
342: a.addLabel("duude", "EN-US");
343:
344: assertEquals("wrong label", "spiffing", a
345: .getLabel("EN-GB"));
346: assertEquals("wrong label", "duude", a
347: .getLabel("EN-US"));
348: assertEquals("wrong label", null, a
349: .getLabel("DE"));
350:
351: a.addLabel("abcdef", "AB-CD");
352: assertEquals("wrong label", "abcdef", a
353: .getLabel("AB"));
354: assertEquals("wrong label", null, a
355: .getLabel("AB-XY"));
356:
357: a.removeLabel("abcde", "AB-CD");
358: assertEquals("Cardinality should be 5", 5, a
359: .getCardinality(a.getProfile().LABEL()));
360: a.removeLabel("abcdef", "AB-CD");
361: assertEquals("Cardinality should be 4", 4, a
362: .getCardinality(a.getProfile().LABEL()));
363: }
364: },
365: new OntTestCase("OntResource.comment.nolang", true,
366: true, true, true) {
367: public void ontTest(OntModel m) throws Exception {
368: Profile prof = m.getProfile();
369: OntResource a = (OntResource) m.getResource(
370: NS + "a").as(OntResource.class);
371:
372: a.addComment("some info", null);
373: assertEquals("Cardinality should be 1", 1, a
374: .getCardinality(prof.COMMENT()));
375: assertEquals("a has wrong comment",
376: "some info", a.getComment(null));
377:
378: a.addComment("more info", null);
379: assertEquals("Cardinality should be 2", 2, a
380: .getCardinality(prof.COMMENT()));
381: iteratorTest(a.listComments(null),
382: new Object[] {
383: m.createLiteral("some info"),
384: m.createLiteral("more info") });
385:
386: assertTrue("a should have comment some info", a
387: .hasComment("some info", null));
388: assertTrue("a should have comment more info", a
389: .hasComment("more info", null));
390:
391: a.setComment("new info", null);
392: assertEquals("Cardinality should be 1", 1, a
393: .getCardinality(prof.COMMENT()));
394: assertEquals("a has wrong comment", "new info",
395: a.getComment(null));
396:
397: a.removeComment("foo", null);
398: assertEquals("Cardinality should be 1", 1, a
399: .getCardinality(prof.COMMENT()));
400: a.removeComment("new info", null);
401: assertEquals("Cardinality should be 0", 0, a
402: .getCardinality(prof.COMMENT()));
403: }
404: },
405: new OntTestCase("OntResource.comment.lang", true, true,
406: true, true) {
407: public void ontTest(OntModel m) throws Exception {
408: OntResource a = (OntResource) m.getResource(
409: NS + "a").as(OntResource.class);
410:
411: a.addComment("good", "EN");
412: assertEquals("wrong comment", "good", a
413: .getComment(null));
414:
415: a.addComment("bon", "FR");
416:
417: assertEquals("wrong comment", "good", a
418: .getComment("EN"));
419: assertEquals("wrong comment", null, a
420: .getComment("EN-GB")); // no literal with a specific enough language
421: assertEquals("wrong comment", "bon", a
422: .getComment("FR"));
423:
424: assertTrue("a should have label good", a
425: .hasComment("good", "EN"));
426: assertTrue("a should have label bon", a
427: .hasComment("bon", "FR"));
428: assertTrue(
429: "a should note have label good (DE)",
430: !a.hasComment("good", "DE"));
431:
432: a.addComment("spiffing", "EN-GB");
433: a.addComment("duude", "EN-US");
434:
435: assertEquals("wrong comment", "spiffing", a
436: .getComment("EN-GB"));
437: assertEquals("wrong comment", "duude", a
438: .getComment("EN-US"));
439: assertEquals("wrong comment", null, a
440: .getComment("DE"));
441:
442: a.addComment("abcdef", "AB-CD");
443: assertEquals("wrong comment", "abcdef", a
444: .getComment("AB"));
445: assertEquals("wrong comment", null, a
446: .getComment("AB-XY"));
447:
448: a.removeComment("abcde", "AB-CD");
449: assertEquals("Cardinality should be 5", 5, a
450: .getCardinality(a.getProfile()
451: .COMMENT()));
452: a.removeComment("abcdef", "AB-CD");
453: assertEquals("Cardinality should be 4", 4, a
454: .getCardinality(a.getProfile()
455: .COMMENT()));
456: }
457: },
458: new OntTestCase("OntResource.type (no inference)",
459: true, true, true, true) {
460: public void ontTest(OntModel m) throws Exception {
461: OntClass A = m.createClass(NS + "A");
462: OntClass B = m.createClass(NS + "B");
463: A.addSubClass(B);
464:
465: OntResource a = (OntResource) m.getResource(
466: NS + "a").as(OntResource.class);
467: assertEquals(
468: "Cardinality of rdf:type is wrong", 0,
469: a.getCardinality(RDF.type));
470:
471: a.addRDFType(B);
472: assertEquals("rdf:type of a is wrong", B, a
473: .getRDFType());
474: assertEquals("rdf:type of a is wrong", B, a
475: .getRDFType(false));
476:
477: iteratorTest(a.listRDFTypes(false),
478: new Object[] { B }); // only B since we're not using an inference model
479: iteratorTest(a.listRDFTypes(true),
480: new Object[] { B });
481:
482: a.addRDFType(A);
483: iteratorTest(a.listRDFTypes(false),
484: new Object[] { A, B });
485: iteratorTest(a.listRDFTypes(true),
486: new Object[] { B });
487:
488: assertTrue("a should not be of class A direct",
489: !a.hasRDFType(A, true));
490: assertTrue("a should not be of class B direct",
491: a.hasRDFType(B, true));
492:
493: OntClass C = m.createClass(NS + "C");
494: a.setRDFType(C);
495: assertTrue("a should be of class C", a
496: .hasRDFType(C, false));
497: assertTrue("a should not be of class A", !a
498: .hasRDFType(A, false));
499: assertTrue("a should not be of class B", !a
500: .hasRDFType(B, false));
501:
502: a.removeRDFType(B);
503: assertEquals("Cardinality should be 1", 1, a
504: .getCardinality(RDF.type));
505: a.removeRDFType(C);
506: assertEquals("Cardinality should be 0", 0, a
507: .getCardinality(RDF.type));
508: }
509: },
510: new OntTestCase("OntResource.remove", true, true, true,
511: true) {
512: public void ontTest(OntModel m) throws Exception {
513: OntClass A = m.createClass(NS + "A");
514: OntClass B = m.createClass(NS + "B");
515: OntClass C = m.createClass(NS + "C");
516: OntClass D = m.createClass(NS + "D");
517: OntClass E = m.createClass(NS + "E");
518: A.addSubClass(B);
519: A.addSubClass(C);
520: C.addSubClass(D);
521: C.addSubClass(E);
522:
523: assertTrue("super-class of E", E.hasSuperClass(
524: C, false));
525: iteratorTest(A.listSubClasses(), new Object[] {
526: B, C });
527:
528: C.remove();
529:
530: assertTrue("super-class of D", !D
531: .hasSuperClass(C, false));
532: assertTrue("super-class of E", !E
533: .hasSuperClass(C, false));
534: iteratorTest(A.listSubClasses(),
535: new Object[] { B });
536: }
537: },
538: new OntTestCase("OntResource.asClass", true, true,
539: true, true) {
540: public void ontTest(OntModel m) throws Exception {
541: Resource r = m.createResource();
542: r.addProperty(RDF.type, m.getProfile().CLASS());
543: OntResource or = (OntResource) r
544: .as(OntResource.class);
545: assertFalse("should not be annotation prop", or
546: .isAnnotationProperty());
547: assertFalse("should not be all different", or
548: .isAllDifferent());
549: assertTrue("should be class", or.isClass());
550: assertFalse("should not be property", or
551: .isProperty());
552: assertFalse("should not be object property", or
553: .isObjectProperty());
554: assertFalse("should not be datatype property",
555: or.isDatatypeProperty());
556: assertTrue("should not be individual",
557: owlFull() || !or.isIndividual());
558: assertFalse("should not be data range", or
559: .isDataRange());
560: assertFalse("should not be ontology", or
561: .isOntology());
562:
563: RDFNode n = or.asClass();
564: assertTrue("Should be OntClass",
565: n instanceof OntClass);
566: }
567: },
568: new OntTestCase("OntResource.asAnnotationProperty",
569: true, true, false, false) {
570: public void ontTest(OntModel m) throws Exception {
571: if (m.getProfile().ANNOTATION_PROPERTY() == null) {
572: throw new ProfileException(null, null);
573: }
574: Resource r = m.createResource();
575: r.addProperty(RDF.type, m.getProfile()
576: .ANNOTATION_PROPERTY());
577: OntResource or = (OntResource) r
578: .as(OntResource.class);
579:
580: assertTrue("should be annotation prop", or
581: .isAnnotationProperty());
582: assertFalse("should not be all different", or
583: .isAllDifferent());
584: assertFalse("should not be class", or.isClass());
585: assertTrue("should be property", or
586: .isProperty());
587: assertFalse("should not be object property", or
588: .isObjectProperty());
589: assertFalse("should not be datatype property",
590: or.isDatatypeProperty());
591: assertFalse("should not be individual", or
592: .isIndividual());
593: assertFalse("should not be data range", or
594: .isDataRange());
595: assertFalse("should not be ontology", or
596: .isOntology());
597:
598: RDFNode n = or.asAnnotationProperty();
599: assertTrue("Should be AnnotationProperty",
600: n instanceof AnnotationProperty);
601: }
602: },
603: new OntTestCase("OntResource.asObjectProperty", true,
604: true, true, false) {
605: public void ontTest(OntModel m) throws Exception {
606: if (m.getProfile().OBJECT_PROPERTY() == null) {
607: throw new ProfileException(null, null);
608: }
609: Resource r = m.createResource();
610: r.addProperty(RDF.type, m.getProfile()
611: .OBJECT_PROPERTY());
612: OntResource or = (OntResource) r
613: .as(OntResource.class);
614:
615: assertFalse("should not be annotation prop", or
616: .isAnnotationProperty());
617: assertFalse("should not be all different", or
618: .isAllDifferent());
619: assertFalse("should not be class", or.isClass());
620: assertTrue("should be property", or
621: .isProperty());
622: assertTrue("should be object property", or
623: .isObjectProperty());
624: assertFalse("should not be datatype property",
625: or.isDatatypeProperty());
626: assertFalse("should not be individual", or
627: .isIndividual());
628: assertFalse("should not be data range", or
629: .isDataRange());
630: assertFalse("should not be ontology", or
631: .isOntology());
632:
633: RDFNode n = or.asObjectProperty();
634: assertTrue("Should be ObjectProperty",
635: n instanceof ObjectProperty);
636: }
637: },
638: new OntTestCase("OntResource.asDatatypeProperty", true,
639: true, true, false) {
640: public void ontTest(OntModel m) throws Exception {
641: if (m.getProfile().DATATYPE_PROPERTY() == null) {
642: throw new ProfileException(null, null);
643: }
644: Resource r = m.createResource();
645: r.addProperty(RDF.type, m.getProfile()
646: .DATATYPE_PROPERTY());
647: OntResource or = (OntResource) r
648: .as(OntResource.class);
649:
650: assertFalse("should not be annotation prop", or
651: .isAnnotationProperty());
652: assertFalse("should not be all different", or
653: .isAllDifferent());
654: assertFalse("should not be class", or.isClass());
655: assertTrue("should be property", or
656: .isProperty());
657: assertFalse("should not be object property", or
658: .isObjectProperty());
659: assertTrue("should be datatype property", or
660: .isDatatypeProperty());
661: assertFalse("should not be individual", or
662: .isIndividual());
663: assertFalse("should not be data range", or
664: .isDataRange());
665: assertFalse("should not be ontology", or
666: .isOntology());
667:
668: RDFNode n = or.asDatatypeProperty();
669: assertTrue("Should be DatatypeProperty",
670: n instanceof DatatypeProperty);
671: }
672: },
673: new OntTestCase("OntResource.asAllDifferent", true,
674: true, false, false) {
675: public void ontTest(OntModel m) throws Exception {
676: if (m.getProfile().ALL_DIFFERENT() == null) {
677: throw new ProfileException(null, null);
678: }
679: Resource r = m.createResource();
680: r.addProperty(RDF.type, m.getProfile()
681: .ALL_DIFFERENT());
682: OntResource or = (OntResource) r
683: .as(OntResource.class);
684:
685: assertFalse("should not be annotation prop", or
686: .isAnnotationProperty());
687: assertTrue("should be all different", or
688: .isAllDifferent());
689: assertFalse("should not be class", or.isClass());
690: assertFalse("should not be property", or
691: .isProperty());
692: assertFalse("should not be object property", or
693: .isObjectProperty());
694: assertFalse("should not be datatype property",
695: or.isDatatypeProperty());
696: assertFalse("should not be individual", or
697: .isIndividual());
698: assertFalse("should not be data range", or
699: .isDataRange());
700: assertFalse("should not be ontology", or
701: .isOntology());
702:
703: RDFNode n = or.asAllDifferent();
704: assertTrue("Should be AnnotationProperty",
705: n instanceof AllDifferent);
706: }
707: },
708: new OntTestCase("OntResource.asProperty", true, true,
709: true, true) {
710: public void ontTest(OntModel m) throws Exception {
711: Resource r = m.createResource();
712: r.addProperty(RDF.type, m.getProfile()
713: .PROPERTY());
714: OntResource or = (OntResource) r
715: .as(OntResource.class);
716:
717: assertFalse("should not be annotation prop", or
718: .isAnnotationProperty());
719: assertFalse("should not be all different", or
720: .isAllDifferent());
721: assertFalse("should not be class", or.isClass());
722: assertTrue("should be property", or
723: .isProperty());
724: assertFalse("should not be object property", or
725: .isObjectProperty());
726: assertFalse("should not be datatype property",
727: or.isDatatypeProperty());
728: assertFalse("should not be individual", or
729: .isIndividual());
730: assertFalse("should not be data range", or
731: .isDataRange());
732: assertFalse("should not be ontology", or
733: .isOntology());
734:
735: RDFNode n = or.asProperty();
736: assertTrue("Should be OntProperty",
737: n instanceof OntProperty);
738: }
739: },
740: new OntTestCase("OntResource.asIndividual", true, true,
741: true, true) {
742: public void ontTest(OntModel m) throws Exception {
743: Resource r = m.createResource();
744: Resource s = m.createResource();
745: s.addProperty(RDF.type, m.getProfile().CLASS());
746: r.addProperty(RDF.type, s);
747: OntResource or = (OntResource) r
748: .as(OntResource.class);
749:
750: assertFalse("should not be annotation prop", or
751: .isAnnotationProperty());
752: assertFalse("should not be all different", or
753: .isAllDifferent());
754: assertFalse("should not be class", or.isClass());
755: assertFalse("should not be property", or
756: .isProperty());
757: assertFalse("should not be object property", or
758: .isObjectProperty());
759: assertFalse("should not be datatype property",
760: or.isDatatypeProperty());
761: assertTrue("should be individual", or
762: .isIndividual());
763: assertFalse("should not be data range", or
764: .isDataRange());
765: assertFalse("should not be ontology", or
766: .isOntology());
767:
768: RDFNode n = or.asIndividual();
769: assertTrue("Should be individual",
770: n instanceof Individual);
771: }
772: },
773: new OntTestCase("OntResource.asDataRange", true, false,
774: false, false) {
775: public void ontTest(OntModel m) throws Exception {
776: if (m.getProfile().DATARANGE() == null) {
777: throw new ProfileException(null, null);
778: }
779: Resource r = m.createResource();
780: r.addProperty(RDF.type, m.getProfile()
781: .DATARANGE());
782: OntResource or = (OntResource) r
783: .as(OntResource.class);
784:
785: assertFalse("should not be annotation prop", or
786: .isAnnotationProperty());
787: assertFalse("should not be all different", or
788: .isAllDifferent());
789: assertFalse("should not be class", or.isClass());
790: assertFalse("should not be property", or
791: .isProperty());
792: assertFalse("should not be object property", or
793: .isObjectProperty());
794: assertFalse("should not be datatype property",
795: or.isDatatypeProperty());
796: assertFalse("should not be individual", or
797: .isIndividual());
798: assertTrue("should be data range", or
799: .isDataRange());
800: assertFalse("should not be ontology", or
801: .isOntology());
802:
803: RDFNode n = or.asDataRange();
804: assertTrue("Should be DataRange",
805: n instanceof DataRange);
806: }
807: },
808: new OntTestCase("OntResource.asOntology", true, true,
809: true, false) {
810: public void ontTest(OntModel m) throws Exception {
811: if (m.getProfile().ONTOLOGY() == null) {
812: throw new ProfileException(null, null);
813: }
814: Resource r = m.createResource();
815: r.addProperty(RDF.type, m.getProfile()
816: .ONTOLOGY());
817: OntResource or = (OntResource) r
818: .as(OntResource.class);
819:
820: assertFalse("should not be annotation prop", or
821: .isAnnotationProperty());
822: assertFalse("should not be all different", or
823: .isAllDifferent());
824: assertFalse("should not be class", or.isClass());
825: assertFalse("should not be property", or
826: .isProperty());
827: assertFalse("should not be object property", or
828: .isObjectProperty());
829: assertFalse("should not be datatype property",
830: or.isDatatypeProperty());
831: assertFalse("should not be individual", or
832: .isIndividual());
833: assertFalse("should not be data range", or
834: .isDataRange());
835: assertTrue("should be ontology", or
836: .isOntology());
837:
838: RDFNode n = or.asOntology();
839: assertTrue("Should be Ontology",
840: n instanceof Ontology);
841: }
842: },
843: new OntTestCase("OntResource.isLanguageTerm", true,
844: true, true, true) {
845: public void ontTest(OntModel m) throws Exception {
846: // class is defined (differently) in every profile
847: OntResource or = (OntResource) m.getProfile()
848: .CLASS().inModel(m).as(
849: OntResource.class);
850: assertTrue("should be a lang term", or
851: .isOntLanguageTerm());
852:
853: or = m.createOntResource("http://foo/bar");
854: assertFalse("should not be a lang term", or
855: .isOntLanguageTerm());
856: }
857: },
858: new OntTestCase("OntResource.getOntModel", true, true,
859: true, true) {
860: public void ontTest(OntModel m) throws Exception {
861: OntResource or = m
862: .createOntResource("http://foo/bar");
863: OntModel m0 = or.getOntModel();
864: assertEquals(m, m0);
865: }
866: },
867: new OntTestCase(
868: "OntResource.getPropertyValue - object prop",
869: true, true, true, true) {
870: public void ontTest(OntModel m) throws Exception {
871: OntResource a = m
872: .createOntResource("http://foo/bar#a");
873: Resource b = m
874: .createResource("http://foo/bar#b");
875: OntProperty p = m
876: .createOntProperty("http://foo/bar#p");
877: m.add(a, p, b);
878: Object bb = a.getPropertyValue(p);
879: assertEquals(b, bb);
880: assertTrue(
881: "Return value should be an OntResource",
882: bb instanceof OntResource);
883: }
884: },
885: new OntTestCase(
886: "OntResource.getPropertyValue - missing prop",
887: true, true, true, true) {
888: public void ontTest(OntModel m) throws Exception {
889: OntResource a = m
890: .createOntResource("http://foo/bar#a");
891: Resource b = m
892: .createResource("http://foo/bar#b");
893: OntProperty p = m
894: .createOntProperty("http://foo/bar#p");
895: OntProperty q = m
896: .createOntProperty("http://foo/bar#q");
897: m.add(a, p, b);
898: Object bb = a.getPropertyValue(q);
899: assertNull(bb);
900: }
901: },
902: new OntTestCase(
903: "OntResource.listPropertyValues - object prop",
904: true, true, true, true) {
905: public void ontTest(OntModel m) throws Exception {
906: OntResource a = m
907: .createOntResource("http://foo/bar#a");
908: Resource b = m
909: .createResource("http://foo/bar#b");
910: OntProperty p = m
911: .createOntProperty("http://foo/bar#p");
912: Literal l = m.createTypedLiteral(false);
913: m.add(a, p, b);
914: m.add(a, p, l);
915: NodeIterator ni = a.listPropertyValues(p);
916:
917: while (ni.hasNext()) {
918: RDFNode n = ni.nextNode();
919: if (n.isResource()) {
920: assertEquals(b, n);
921: assertTrue(
922: "Return value should be an OntResource",
923: n instanceof OntResource);
924: }
925: }
926: }
927: },
928:
929: };
930: }
931:
932: //==============================================================================
933: // Inner class definitions
934: //==============================================================================
935:
936: }
937:
938: /*
939: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
940: All rights reserved.
941:
942: Redistribution and use in source and binary forms, with or without
943: modification, are permitted provided that the following conditions
944: are met:
945:
946: 1. Redistributions of source code must retain the above copyright
947: notice, this list of conditions and the following disclaimer.
948:
949: 2. Redistributions in binary form must reproduce the above copyright
950: notice, this list of conditions and the following disclaimer in the
951: documentation and/or other materials provided with the distribution.
952:
953: 3. The name of the author may not be used to endorse or promote products
954: derived from this software without specific prior written permission.
955:
956: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
957: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
958: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
959: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
960: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
961: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
962: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
963: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
964: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
965: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
966: */
|