001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.model.util;
007:
008: import java.util.Collection;
009: import java.util.HashSet;
010:
011: import junit.framework.TestCase;
012:
013: import org.openrdf.model.BNode;
014: import org.openrdf.model.Statement;
015: import org.openrdf.model.URI;
016: import org.openrdf.model.ValueFactory;
017: import org.openrdf.model.impl.ValueFactoryImpl;
018: import org.openrdf.model.vocabulary.RDF;
019: import org.openrdf.model.vocabulary.RDFS;
020:
021: /**
022: * @author jeen
023: */
024: public class ModelUtilTest extends TestCase {
025:
026: private Collection<Statement> model1;
027:
028: private Collection<Statement> model2;
029:
030: private static ValueFactory VF = new ValueFactoryImpl();
031:
032: private URI foo;
033:
034: private URI bar;
035:
036: @Override
037: protected void setUp() {
038: model1 = new HashSet<Statement>();
039: model2 = new HashSet<Statement>();
040:
041: foo = VF.createURI("http://example.org/foo");
042: bar = VF.createURI("http://example.org/bar");
043: }
044:
045: public void testModelsEqual() {
046:
047: // two identical statements, no bnodes
048: model1.add(VF.createStatement(foo, RDF.TYPE, bar));
049:
050: assertFalse(ModelUtil.equals(model1, model2));
051:
052: model2.add(VF.createStatement(foo, RDF.TYPE, bar));
053:
054: assertTrue(ModelUtil.equals(model1, model2));
055:
056: // add same statement again
057: model2.add(VF.createStatement(foo, RDF.TYPE, bar));
058:
059: assertTrue("Duplicate statement should not be considered",
060: ModelUtil.equals(model1, model2));
061:
062: // two identical statements with bnodes added.
063: model1.add(VF.createStatement(foo, RDF.TYPE, VF.createBNode()));
064: model2.add(VF.createStatement(foo, RDF.TYPE, VF.createBNode()));
065:
066: assertTrue(ModelUtil.equals(model1, model2));
067:
068: // chained bnodes
069: BNode chainedNode1 = VF.createBNode();
070:
071: model1.add(VF.createStatement(bar, RDFS.SUBCLASSOF,
072: chainedNode1));
073: model1.add(VF.createStatement(chainedNode1, RDFS.SUBCLASSOF,
074: foo));
075:
076: BNode chainedNode2 = VF.createBNode();
077:
078: model2.add(VF.createStatement(bar, RDFS.SUBCLASSOF,
079: chainedNode2));
080: model2.add(VF.createStatement(chainedNode2, RDFS.SUBCLASSOF,
081: foo));
082:
083: assertTrue(ModelUtil.equals(model1, model2));
084:
085: // two bnode statements with non-identical predicates
086:
087: model1.add(VF.createStatement(foo, foo, VF.createBNode()));
088: model2.add(VF.createStatement(foo, bar, VF.createBNode()));
089:
090: assertFalse(ModelUtil.equals(model1, model2));
091:
092: }
093:
094: public void testIsSubset() {
095:
096: // two empty sets
097: assertTrue(ModelUtil.isSubset(model1, model2));
098: assertTrue(ModelUtil.isSubset(model2, model1));
099:
100: // two identical statements, no bnodes
101: model1.add(VF.createStatement(foo, RDF.TYPE, bar));
102:
103: assertFalse(ModelUtil.isSubset(model1, model2));
104: assertTrue(ModelUtil.isSubset(model2, model1));
105:
106: model2.add(VF.createStatement(foo, RDF.TYPE, bar));
107:
108: assertTrue(ModelUtil.isSubset(model1, model2));
109: assertTrue(ModelUtil.isSubset(model2, model1));
110:
111: // two identical statements with bnodes added.
112: model1.add(VF.createStatement(foo, RDF.TYPE, VF.createBNode()));
113:
114: assertFalse(ModelUtil.isSubset(model1, model2));
115: assertTrue(ModelUtil.isSubset(model2, model1));
116:
117: model2.add(VF.createStatement(foo, RDF.TYPE, VF.createBNode()));
118:
119: assertTrue(ModelUtil.isSubset(model1, model2));
120: assertTrue(ModelUtil.isSubset(model2, model1));
121:
122: // chained bnodes
123: BNode chainedNode1 = VF.createBNode();
124:
125: model1.add(VF.createStatement(bar, RDFS.SUBCLASSOF,
126: chainedNode1));
127: model1.add(VF.createStatement(chainedNode1, RDFS.SUBCLASSOF,
128: foo));
129:
130: assertFalse(ModelUtil.isSubset(model1, model2));
131: assertTrue(ModelUtil.isSubset(model2, model1));
132:
133: BNode chainedNode2 = VF.createBNode();
134:
135: model2.add(VF.createStatement(bar, RDFS.SUBCLASSOF,
136: chainedNode2));
137: model2.add(VF.createStatement(chainedNode2, RDFS.SUBCLASSOF,
138: foo));
139:
140: assertTrue(ModelUtil.isSubset(model1, model2));
141: assertTrue(ModelUtil.isSubset(model2, model1));
142:
143: // two bnode statements with non-identical predicates
144:
145: model1.add(VF.createStatement(foo, foo, VF.createBNode()));
146: model2.add(VF.createStatement(foo, bar, VF.createBNode()));
147:
148: assertFalse(ModelUtil.isSubset(model1, model2));
149: assertFalse(ModelUtil.isSubset(model2, model1));
150: }
151: }
|