001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: TestNodeToTriplesMap.java,v 1.19 2008/01/02 12:05:30 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.graph.test;
007:
008: import java.util.*;
009:
010: import com.hp.hpl.jena.graph.*;
011: import com.hp.hpl.jena.graph.Triple.*;
012: import com.hp.hpl.jena.mem.NodeToTriplesMap;
013:
014: import junit.framework.TestSuite;
015:
016: /**
017: TestNodeToTriplesMap: added, post-hoc, by kers once NTM got
018: rather complicated. So these tests may be (are, at the moment)
019: incomplete.
020:
021: @author kers
022: */
023: public class TestNodeToTriplesMap extends GraphTestBase {
024: public TestNodeToTriplesMap(String name) {
025: super (name);
026: }
027:
028: public static TestSuite suite() {
029: return new TestSuite(TestNodeToTriplesMap.class);
030: }
031:
032: protected NodeToTriplesMap ntS = new NodeToTriplesMap(
033: Field.getSubject, Field.getPredicate, Field.getObject);
034:
035: protected NodeToTriplesMap ntP = new NodeToTriplesMap(
036: Field.getPredicate, Field.getObject, Field.getSubject);
037:
038: protected NodeToTriplesMap ntO = new NodeToTriplesMap(
039: Field.getObject, Field.getPredicate, Field.getSubject);
040:
041: protected static final Node x = node("x");
042:
043: protected static final Node y = node("y");
044:
045: public void testZeroSize() {
046: testZeroSize("fresh NTM", ntS);
047: }
048:
049: protected void testZeroSize(String title, NodeToTriplesMap nt) {
050: assertEquals(title + " should have size 0", 0, nt.size());
051: assertEquals(title + " should be isEmpty()", true, nt.isEmpty());
052: assertEquals(title + " should have empty domain", false, nt
053: .domain().hasNext());
054: }
055:
056: public void testAddOne() {
057: ntS.add(triple("x P y"));
058: testJustOne(x, ntS);
059: }
060:
061: public void testAddOneTwice() {
062: addTriples(ntS, "x P y; x P y");
063: testJustOne(x, ntS);
064: }
065:
066: protected void testJustOne(Node x, NodeToTriplesMap nt) {
067: assertEquals(1, nt.size());
068: assertEquals(false, nt.isEmpty());
069: assertEquals(just(x), iteratorToSet(nt.domain()));
070: }
071:
072: public void testAddTwoUnshared() {
073: addTriples(ntS, "x P a; y Q b");
074: assertEquals(2, ntS.size());
075: assertEquals(false, ntS.isEmpty());
076: assertEquals(both(x, y), iteratorToSet(ntS.domain()));
077: }
078:
079: public void testAddTwoShared() {
080: addTriples(ntS, "x P a; x Q b");
081: assertEquals(2, ntS.size());
082: assertEquals(false, ntS.isEmpty());
083: assertEquals(just(x), iteratorToSet(ntS.domain()));
084: }
085:
086: public void testClear() {
087: addTriples(ntS, "x P a; x Q b; y R z");
088: ntS.clear();
089: testZeroSize("cleared NTM", ntS);
090: }
091:
092: public void testAllIterator() {
093: String triples = "x P b; y P d; y P f";
094: addTriples(ntS, triples);
095: assertEquals(tripleSet(triples),
096: iteratorToSet(ntS.iterateAll()));
097: }
098:
099: public void testOneIterator() {
100: addTriples(ntS, "x P b; y P d; y P f");
101: assertEquals(tripleSet("x P b"), iteratorToSet(ntS.iterator(x,
102: null)));
103: assertEquals(tripleSet("y P d; y P f"), iteratorToSet(ntS
104: .iterator(y, null)));
105: }
106:
107: public void testRemove() {
108: addTriples(ntS, "x P b; y P d; y R f");
109: ntS.remove(triple("y P d"));
110: assertEquals(2, ntS.size());
111: assertEquals(tripleSet("x P b; y R f"), iteratorToSet(ntS
112: .iterateAll()));
113: }
114:
115: public void testRemoveByIterator() {
116: addTriples(ntS, "x nice a; a nasty b; x nice c");
117: addTriples(ntS, "y nice d; y nasty e; y nice f");
118: Iterator it = ntS.iterateAll();
119: while (it.hasNext()) {
120: Triple t = (Triple) it.next();
121: if (t.getPredicate().equals(node("nasty")))
122: it.remove();
123: }
124: assertEquals(
125: tripleSet("x nice a; x nice c; y nice d; y nice f"),
126: iteratorToSet(ntS.iterateAll()));
127: }
128:
129: public void testIteratorWIthPatternOnEmpty() {
130: assertEquals(tripleSet(""), iteratorToSet(ntS
131: .iterateAll(triple("a P b"))));
132: }
133:
134: public void testIteratorWIthPatternOnSomething() {
135: addTriples(ntS, "x P a; y P b; y R c");
136: assertEquals(tripleSet("x P a"), iteratorToSet(ntS
137: .iterateAll(triple("x P ??"))));
138: assertEquals(tripleSet("y P b; y R c"), iteratorToSet(ntS
139: .iterateAll(triple("y ?? ??"))));
140: assertEquals(tripleSet("x P a; y P b"), iteratorToSet(ntS
141: .iterateAll(triple("?? P ??"))));
142: assertEquals(tripleSet("y R c"), iteratorToSet(ntS
143: .iterateAll(triple("?? ?? c"))));
144: }
145:
146: public void testUnspecificRemoveS() {
147: addTriples(ntS, "x P a; y Q b; z R c");
148: ntS.remove(triple("x P a"));
149: assertEquals(tripleSet("y Q b; z R c"), iteratorToSet(ntS
150: .iterateAll()));
151: }
152:
153: public void testUnspecificRemoveP() {
154: addTriples(ntP, "x P a; y Q b; z R c");
155: ntP.remove(triple("y Q b"));
156: assertEquals(tripleSet("x P a; z R c"), iteratorToSet(ntP
157: .iterateAll()));
158: }
159:
160: public void testUnspecificRemoveO() {
161: addTriples(ntO, "x P a; y Q b; z R c");
162: ntO.remove(triple("z R c"));
163: assertEquals(tripleSet("x P a; y Q b"), iteratorToSet(ntO
164: .iterateAll()));
165: }
166:
167: public void testAddBooleanResult() {
168: assertEquals(true, ntS.add(triple("x P y")));
169: assertEquals(false, ntS.add(triple("x P y")));
170: /* */
171: assertEquals(true, ntS.add(triple("y Q z")));
172: assertEquals(false, ntS.add(triple("y Q z")));
173: /* */
174: assertEquals(true, ntS.add(triple("y R s")));
175: assertEquals(false, ntS.add(triple("y R s")));
176: }
177:
178: public void testRemoveBooleanResult() {
179: assertEquals(false, ntS.remove(triple("x P y")));
180: ntS.add(triple("x P y"));
181: assertEquals(false, ntS.remove(triple("x Q y")));
182: assertEquals(true, ntS.remove(triple("x P y")));
183: assertEquals(false, ntS.remove(triple("x P y")));
184: }
185:
186: public void testContains() {
187: addTriples(ntS, "x P y; a P b");
188: assertTrue(ntS.contains(triple("x P y")));
189: assertTrue(ntS.contains(triple("a P b")));
190: assertFalse(ntS.contains(triple("x P z")));
191: assertFalse(ntS.contains(triple("y P y")));
192: assertFalse(ntS.contains(triple("x R y")));
193: assertFalse(ntS.contains(triple("e T f")));
194: assertFalse(ntS.contains(triple("_x F 17")));
195: }
196:
197: // TODO more here
198:
199: protected void addTriples(NodeToTriplesMap nt, String facts) {
200: Triple[] t = tripleArray(facts);
201: for (int i = 0; i < t.length; i += 1)
202: nt.add(t[i]);
203: }
204:
205: protected static Set just(Object x) {
206: Set result = new HashSet();
207: result.add(x);
208: return result;
209: }
210:
211: protected static Set both(Object x, Object y) {
212: Set result = just(x);
213: result.add(y);
214: return result;
215: }
216:
217: }
218:
219: /*
220: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
221: All rights reserved.
222:
223: Redistribution and use in source and binary forms, with or without
224: modification, are permitted provided that the following conditions
225: are met:
226:
227: 1. Redistributions of source code must retain the above copyright
228: notice, this list of conditions and the following disclaimer.
229:
230: 2. Redistributions in binary form must reproduce the above copyright
231: notice, this list of conditions and the following disclaimer in the
232: documentation and/or other materials provided with the distribution.
233:
234: 3. The name of the author may not be used to endorse or promote products
235: derived from this software without specific prior written permission.
236:
237: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
238: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
239: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
240: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
241: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
242: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
243: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
245: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
246: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
247: */
|