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 4 Mar 2003
009: * Filename $RCSfile: TestMultiUnion.java,v $
010: * Revision $Revision: 1.12 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:07:21 $
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.graph.compose.test;
019:
020: // Imports
021: ///////////////
022:
023: import com.hp.hpl.jena.graph.*;
024: import com.hp.hpl.jena.graph.compose.*;
025: import com.hp.hpl.jena.graph.compose.MultiUnion.MultiUnionStatisticsHandler;
026: import com.hp.hpl.jena.graph.test.*;
027: import com.hp.hpl.jena.rdf.model.*;
028:
029: import java.util.*;
030:
031: import junit.framework.*;
032:
033: /**
034: * <p>
035: * Unit tests for multi-union graph.
036: * </p>
037: *
038: * @author Ian Dickinson, HP Labs
039: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
040: * @version CVS $Id: TestMultiUnion.java,v 1.12 2008/01/02 12:07:21 andy_seaborne Exp $
041: */
042: public class TestMultiUnion extends AbstractTestGraph {
043: // Constants
044: //////////////////////////////////
045:
046: // Static variables
047: //////////////////////////////////
048:
049: // Instance variables
050: //////////////////////////////////
051:
052: // Constructors
053: //////////////////////////////////
054:
055: public TestMultiUnion(String s) {
056: super (s);
057: }
058:
059: // External signature methods
060: //////////////////////////////////
061:
062: public static TestSuite suite() {
063: return new TestSuite(TestMultiUnion.class);
064: }
065:
066: public Graph getGraph() {
067: Graph gBase = graphWith(""), g1 = graphWith("");
068: return new MultiUnion(new Graph[] { gBase, g1 });
069: };
070:
071: public void testEmptyGraph() {
072: Graph m = new MultiUnion();
073: Graph g0 = graphWith("x p y");
074:
075: assertEquals("Empty model should have size zero", 0, m.size());
076: assertFalse("Empty model should not contain another graph", m
077: .dependsOn(g0));
078: }
079:
080: /**
081: A MultiUnion graph should have a MultiUnionStatisticsHandler, and that
082: handler should point right back to that graph.
083: */
084: public void testMultiUnionHasMultiUnionStatisticsHandler() {
085: MultiUnion mu = new MultiUnion();
086: GraphStatisticsHandler sh = mu.getStatisticsHandler();
087: assertInstanceOf(MultiUnionStatisticsHandler.class, sh);
088: assertSame(mu, ((MultiUnionStatisticsHandler) sh).getUnion());
089: }
090:
091: // public void testDeferredReifier()
092: // {
093: // Graph g1 = graphWith( "" ), g2 = graphWith( "" );
094: // MultiUnion m = new MultiUnion( new Graph[] {g1, g2} );
095: // m.setBaseGraph( g1 );
096: // assertSame( m.getReifier(), g1.getReifier() );
097: // }
098:
099: public void testGraphSize1() {
100: Graph g0 = graphWith("x p y");
101: Graph g1 = graphWith("x p z; z p zz"); // disjoint with g0
102: Graph g2 = graphWith("x p y; z p a"); // intersects with g1
103:
104: Graph m01 = new MultiUnion(new Graph[] { g0, g1 });
105: Graph m10 = new MultiUnion(new Graph[] { g1, g0 });
106: Graph m12 = new MultiUnion(new Graph[] { g1, g2 });
107: Graph m21 = new MultiUnion(new Graph[] { g2, g1 });
108: Graph m02 = new MultiUnion(new Graph[] { g0, g2 });
109: Graph m20 = new MultiUnion(new Graph[] { g2, g0 });
110:
111: Graph m00 = new MultiUnion(new Graph[] { g0, g0 });
112:
113: int s0 = g0.size();
114: int s1 = g1.size();
115: int s2 = g2.size();
116:
117: assertEquals("Size of union of g0 and g1 not correct", s0 + s1,
118: m01.size());
119: assertEquals("Size of union of g1 and g0 not correct", s0 + s1,
120: m10.size());
121:
122: assertEquals("Size of union of g1 and g2 not correct", s1 + s2,
123: m12.size());
124: assertEquals("Size of union of g2 and g1 not correct", s1 + s2,
125: m21.size());
126:
127: assertEquals("Size of union of g0 and g2 not correct", s0 + s2
128: - 1, m02.size());
129: assertEquals("Size of union of g2 and g0 not correct", s0 + s2
130: - 1, m20.size());
131:
132: assertEquals("Size of union of g0 with itself not correct", s0,
133: m00.size());
134: }
135:
136: public void testGraphSize2() {
137: Graph g0 = graphWith("x p y");
138: Graph g1 = graphWith("x p z; z p zz"); // disjoint with g0
139: Graph g2 = graphWith("x p y; z p a"); // intersects with g1
140:
141: Graph m01 = new MultiUnion(iterateOver(g0, g1));
142: Graph m10 = new MultiUnion(iterateOver(g1, g0));
143: Graph m12 = new MultiUnion(iterateOver(g1, g2));
144: Graph m21 = new MultiUnion(iterateOver(g2, g1));
145: Graph m02 = new MultiUnion(iterateOver(g0, g2));
146: Graph m20 = new MultiUnion(iterateOver(g2, g0));
147:
148: Graph m00 = new MultiUnion(iterateOver(g0, g0));
149:
150: int s0 = g0.size();
151: int s1 = g1.size();
152: int s2 = g2.size();
153:
154: assertEquals("Size of union of g0 and g1 not correct", s0 + s1,
155: m01.size());
156: assertEquals("Size of union of g1 and g0 not correct", s0 + s1,
157: m10.size());
158:
159: assertEquals("Size of union of g1 and g2 not correct", s1 + s2,
160: m12.size());
161: assertEquals("Size of union of g2 and g1 not correct", s1 + s2,
162: m21.size());
163:
164: assertEquals("Size of union of g0 and g2 not correct", s0 + s2
165: - 1, m02.size());
166: assertEquals("Size of union of g2 and g0 not correct", s0 + s2
167: - 1, m20.size());
168:
169: assertEquals("Size of union of g0 with itself not correct", s0,
170: m00.size());
171: }
172:
173: public void testGraphAddSize() {
174: Graph g0 = graphWith("x p y");
175: Graph g1 = graphWith("x p z; z p zz"); // disjoint with g0
176: Graph g2 = graphWith("x p y; z p a"); // intersects with g1
177:
178: int s0 = g0.size();
179: int s1 = g1.size();
180: int s2 = g2.size();
181:
182: MultiUnion m0 = new MultiUnion(new Graph[] { g0 });
183:
184: assertEquals("Size of union of g0 not correct", s0, m0.size());
185: m0.addGraph(g1);
186: assertEquals("Size of union of g1 and g0 not correct", s0 + s1,
187: m0.size());
188:
189: m0.addGraph(g2);
190: assertEquals("Size of union of g0, g1 and g2 not correct", s0
191: + s1 + s2 - 1, m0.size());
192:
193: m0.removeGraph(g1);
194: assertEquals("Size of union of g0 and g2 not correct", s0 + s2
195: - 1, m0.size());
196:
197: m0.removeGraph(g0);
198: assertEquals("Size of union of g2 not correct", s2, m0.size());
199:
200: // remove again
201: m0.removeGraph(g0);
202: assertEquals("Size of union of g2 not correct", s2, m0.size());
203:
204: m0.removeGraph(g2);
205: assertEquals("Size of empty union not correct", 0, m0.size());
206:
207: }
208:
209: public void testAdd() {
210: Graph g0 = graphWith("x p y");
211: Graph g1 = graphWith("x p z; z p zz"); // disjoint with g0
212: Graph g2 = graphWith("x p y; z p a"); // intersects with g1
213:
214: MultiUnion m = new MultiUnion(new Graph[] { g0, g1 });
215:
216: int s0 = g0.size();
217: int s1 = g1.size();
218: int s2 = g2.size();
219: int m0 = m.size();
220:
221: // add a triple to the union
222: m.add(triple("a q b"));
223:
224: assertEquals("m.size should have increased by one", m0 + 1, m
225: .size());
226: assertEquals("g0.size should have increased by one", s0 + 1, g0
227: .size());
228: assertEquals("g1 size should be constant", s1, g1.size());
229:
230: // change the designated receiver and try again
231: m.setBaseGraph(g1);
232:
233: s0 = g0.size();
234: s1 = g1.size();
235: s2 = g2.size();
236: m0 = m.size();
237:
238: m.add(triple("a1 q b1"));
239:
240: assertEquals("m.size should have increased by one", m0 + 1, m
241: .size());
242: assertEquals("g0 size should be constant", s0, g0.size());
243: assertEquals("g1.size should have increased by one", s1 + 1, g1
244: .size());
245:
246: // check that we can't make g2 the designated updater
247: boolean expected = false;
248: try {
249: m.setBaseGraph(g2);
250: } catch (IllegalArgumentException e) {
251: expected = true;
252: }
253: assertTrue("Should not have been able to make g2 the updater",
254: expected);
255: }
256:
257: public void testDelete() {
258: Graph g0 = graphWith("x p y");
259: Graph g1 = graphWith("x p z; z p zz"); // disjoint with g0
260:
261: MultiUnion m = new MultiUnion(new Graph[] { g0, g1 });
262:
263: checkDeleteSizes(1, 2, 3, g0, g1, m);
264:
265: m.delete(triple("x p y"));
266: checkDeleteSizes(0, 2, 2, g0, g1, m);
267:
268: m.delete(triple("x p y"));
269: checkDeleteSizes(0, 2, 2, g0, g1, m);
270:
271: m.setBaseGraph(g1);
272:
273: m.delete(triple("x p z"));
274: checkDeleteSizes(0, 1, 1, g0, g1, m);
275:
276: m.delete(triple("z p zz"));
277: checkDeleteSizes(0, 0, 0, g0, g1, m);
278: }
279:
280: public void testContains() {
281: Graph g0 = graphWith("x p y");
282: Graph g1 = graphWith("x p z; z p zz"); // disjoint with g0
283:
284: MultiUnion m = new MultiUnion(new Graph[] { g0, g1 });
285:
286: assertTrue("m should contain triple", m
287: .contains(triple("x p y ")));
288: assertTrue("m should contain triple", m
289: .contains(triple("x p z ")));
290: assertTrue("m should contain triple", m
291: .contains(triple("z p zz ")));
292:
293: assertFalse("m should not contain triple", m
294: .contains(triple("zz p z ")));
295: }
296:
297: /* Test using a model to wrap a multi union */
298: public void testModel() {
299: Graph g0 = graphWith("x p y");
300: MultiUnion u = new MultiUnion(new Graph[] { g0 });
301:
302: Model m = ModelFactory.createModelForGraph(u);
303:
304: assertEquals("Model size not correct", 1, m.size());
305:
306: Graph g1 = graphWith("x p z; z p zz"); // disjoint with g0
307: u.addGraph(g1);
308:
309: assertEquals("Model size not correct", 3, m.size());
310:
311: // adds one more statement to the model
312: m.read("file:testing/ontology/list0.rdf");
313: assertEquals("Model size not correct", 4, m.size());
314:
315: // debug m.write( System.out );
316: }
317:
318: // Internal implementation methods
319: //////////////////////////////////
320:
321: protected void checkDeleteSizes(int s0, int s1, int m0, Graph g0,
322: Graph g1, Graph m) {
323: assertEquals("Delete check: g0 size", s0, g0.size());
324: assertEquals("Delete check: g1 size", s1, g1.size());
325: assertEquals("Delete check: m size", m0, m.size());
326: }
327:
328: protected Iterator iterateOver(Object x0) {
329: List l = new ArrayList();
330: l.add(x0);
331: return l.iterator();
332: }
333:
334: protected Iterator iterateOver(Object x0, Object x1) {
335: List l = new ArrayList();
336: l.add(x0);
337: l.add(x1);
338: return l.iterator();
339: }
340:
341: protected Iterator iterateOver(Object x0, Object x1, Object x2) {
342: List l = new ArrayList();
343: l.add(x0);
344: l.add(x1);
345: l.add(x2);
346: return l.iterator();
347: }
348:
349: //==============================================================================
350: // Inner class definitions
351: //==============================================================================
352:
353: }
354:
355: /*
356: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
357: All rights reserved.
358:
359: Redistribution and use in source and binary forms, with or without
360: modification, are permitted provided that the following conditions
361: are met:
362:
363: 1. Redistributions of source code must retain the above copyright
364: notice, this list of conditions and the following disclaimer.
365:
366: 2. Redistributions in binary form must reproduce the above copyright
367: notice, this list of conditions and the following disclaimer in the
368: documentation and/or other materials provided with the distribution.
369:
370: 3. The name of the author may not be used to endorse or promote products
371: derived from this software without specific prior written permission.
372:
373: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
374: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
375: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
376: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
377: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
378: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
379: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
380: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
381: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
382: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
383: */
|