001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestTripleBunch.java,v 1.8 2008/01/02 12:09:01 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.mem.test;
007:
008: import java.util.ArrayList;
009: import java.util.HashSet;
010: import java.util.List;
011: import java.util.Set;
012:
013: import com.hp.hpl.jena.graph.Triple;
014: import com.hp.hpl.jena.graph.test.GraphTestBase;
015: import com.hp.hpl.jena.mem.*;
016: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
017:
018: /**
019: Test triple bunch implementations - NOT YET FINISHED
020: @author kers
021: */
022: public abstract class TestTripleBunch extends GraphTestBase {
023: protected static final Triple tripleSPO = triple("s P o");
024: protected static final Triple tripleXQY = triple("x Q y");
025:
026: public TestTripleBunch(String name) {
027: super (name);
028: }
029:
030: protected static final TripleBunch emptyBunch = new ArrayBunch();
031:
032: protected abstract TripleBunch getBunch();
033:
034: public void testEmptyBunch() {
035: TripleBunch b = getBunch();
036: assertEquals(0, b.size());
037: assertFalse(b.contains(tripleSPO));
038: assertFalse(b.contains(tripleXQY));
039: assertFalse(b.iterator().hasNext());
040: }
041:
042: public void testAddElement() {
043: TripleBunch b = getBunch();
044: b.add(tripleSPO);
045: assertEquals(1, b.size());
046: assertTrue(b.contains(tripleSPO));
047: assertEquals(listOf(tripleSPO), iteratorToList(b.iterator()));
048: }
049:
050: public void testAddElements() {
051: TripleBunch b = getBunch();
052: b.add(tripleSPO);
053: b.add(tripleXQY);
054: assertEquals(2, b.size());
055: assertTrue(b.contains(tripleSPO));
056: assertTrue(b.contains(tripleXQY));
057: assertEquals(setOf(tripleSPO, tripleXQY), iteratorToSet(b
058: .iterator()));
059: }
060:
061: public void testRemoveOnlyElement() {
062: TripleBunch b = getBunch();
063: b.add(tripleSPO);
064: b.remove(tripleSPO);
065: assertEquals(0, b.size());
066: assertFalse(b.contains(tripleSPO));
067: assertFalse(b.iterator().hasNext());
068: }
069:
070: public void testRemoveFirstOfTwo() {
071: TripleBunch b = getBunch();
072: b.add(tripleSPO);
073: b.add(tripleXQY);
074: b.remove(tripleSPO);
075: assertEquals(1, b.size());
076: assertFalse(b.contains(tripleSPO));
077: assertTrue(b.contains(tripleXQY));
078: assertEquals(listOf(tripleXQY), iteratorToList(b.iterator()));
079: }
080:
081: public void testTableGrows() {
082: TripleBunch b = getBunch();
083: b.add(tripleSPO);
084: b.add(tripleXQY);
085: b.add(triple("a I b"));
086: b.add(triple("c J d"));
087: }
088:
089: public void testIterator() {
090: TripleBunch b = getBunch();
091: b.add(triple("a P b"));
092: b.add(triple("c Q d"));
093: b.add(triple("e R f"));
094: assertEquals(tripleSet("a P b; c Q d; e R f"), b.iterator()
095: .toSet());
096: }
097:
098: public void testIteratorRemoveOneItem() {
099: TripleBunch b = getBunch();
100: b.add(triple("a P b"));
101: b.add(triple("c Q d"));
102: b.add(triple("e R f"));
103: ExtendedIterator it = b.iterator();
104: while (it.hasNext())
105: if (it.next().equals(triple("c Q d")))
106: it.remove();
107: assertEquals(tripleSet("a P b; e R f"), b.iterator().toSet());
108: }
109:
110: public void testIteratorRemoveAlltems() {
111: TripleBunch b = getBunch();
112: b.add(triple("a P b"));
113: b.add(triple("c Q d"));
114: b.add(triple("e R f"));
115: ExtendedIterator it = b.iterator();
116: while (it.hasNext())
117: it.removeNext();
118: assertEquals(tripleSet(""), b.iterator().toSet());
119: }
120:
121: protected List listOf(Triple x) {
122: List result = new ArrayList();
123: result.add(x);
124: return result;
125: }
126:
127: protected Set setOf(Triple x, Triple y) {
128: Set result = setOf(x);
129: result.add(y);
130: return result;
131: }
132:
133: protected Set setOf(Triple x) {
134: Set result = new HashSet();
135: result.add(x);
136: return result;
137: }
138: }
139:
140: /*
141: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
142: * All rights reserved.
143: *
144: * Redistribution and use in source and binary forms, with or without
145: * modification, are permitted provided that the following conditions
146: * are met:
147: * 1. Redistributions of source code must retain the above copyright
148: * notice, this list of conditions and the following disclaimer.
149: * 2. Redistributions in binary form must reproduce the above copyright
150: * notice, this list of conditions and the following disclaimer in the
151: * documentation and/or other materials provided with the distribution.
152: * 3. The name of the author may not be used to endorse or promote products
153: * derived from this software without specific prior written permission.
154: *
155: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
156: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
158: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
159: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
160: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
161: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
162: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
163: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
164: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
165: */
|