KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > compose > test > TestMultiUnion


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 4 Mar 2003
9  * Filename $RCSfile: TestMultiUnion.java,v $
10  * Revision $Revision: 1.8 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/02/21 11:52:07 $
14  * by $Author: andy_seaborne $
15  *
16  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * (see footer for full conditions)
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.graph.compose.test;
23
24
25 // Imports
26
///////////////
27

28 import com.hp.hpl.jena.graph.*;
29 import com.hp.hpl.jena.graph.compose.*;
30 import com.hp.hpl.jena.graph.test.*;
31 import com.hp.hpl.jena.rdf.model.*;
32
33 import java.util.*;
34
35 import junit.framework.*;
36
37
38 /**
39  * <p>
40  * Unit tests for multi-union graph.
41  * </p>
42  *
43  * @author Ian Dickinson, HP Labs
44  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
45  * @version CVS $Id: TestMultiUnion.java,v 1.8 2005/02/21 11:52:07 andy_seaborne Exp $
46  */

47 public class TestMultiUnion
48     extends AbstractTestGraph
49 {
50     // Constants
51
//////////////////////////////////
52

53
54     // Static variables
55
//////////////////////////////////
56

57
58     // Instance variables
59
//////////////////////////////////
60

61
62     // Constructors
63
//////////////////////////////////
64

65     public TestMultiUnion( String JavaDoc s ) {
66         super( s );
67     }
68     
69
70     // External signature methods
71
//////////////////////////////////
72

73     public static TestSuite suite()
74         { return new TestSuite( TestMultiUnion.class ); }
75     
76     public Graph getGraph()
77         {
78         Graph gBase = graphWith( "" ), g1 = graphWith( "" );
79         return new MultiUnion( new Graph[] {gBase, g1} );
80         };
81
82     public void testEmptyGraph() {
83         Graph m = new MultiUnion();
84         Graph g0 = graphWith( "x p y");
85         
86         assertEquals( "Empty model should have size zero", 0, m.size() );
87         assertFalse( "Empty model should not contain another graph", m.dependsOn( g0 ) );
88     }
89     
90    
91 // public void testDeferredReifier()
92
// {
93
// Graph g1 = graphWith( "" ), g2 = graphWith( "" );
94
// MultiUnion m = new MultiUnion( new Graph[] {g1, g2} );
95
// m.setBaseGraph( g1 );
96
// assertSame( m.getReifier(), g1.getReifier() );
97
// }
98

99
100     public void testGraphSize1() {
101         Graph g0 = graphWith( "x p y" );
102         Graph g1 = graphWith( "x p z; z p zz" ); // disjoint with g0
103
Graph g2 = graphWith( "x p y; z p a" ); // intersects with g1
104

105         Graph m01 = new MultiUnion( new Graph[] {g0, g1} );
106         Graph m10 = new MultiUnion( new Graph[] {g1, g0} );
107         Graph m12 = new MultiUnion( new Graph[] {g1, g2} );
108         Graph m21 = new MultiUnion( new Graph[] {g2, g1} );
109         Graph m02 = new MultiUnion( new Graph[] {g0, g2} );
110         Graph m20 = new MultiUnion( new Graph[] {g2, g0} );
111         
112         Graph m00 = new MultiUnion( new Graph[] {g0, g0} );
113         
114         int s0 = g0.size();
115         int s1 = g1.size();
116         int s2 = g2.size();
117         
118         assertEquals( "Size of union of g0 and g1 not correct", s0+s1, m01.size() );
119         assertEquals( "Size of union of g1 and g0 not correct", s0+s1, m10.size() );
120         
121         assertEquals( "Size of union of g1 and g2 not correct", s1+s2, m12.size() );
122         assertEquals( "Size of union of g2 and g1 not correct", s1+s2, m21.size() );
123
124         assertEquals( "Size of union of g0 and g2 not correct", s0+s2 - 1, m02.size() );
125         assertEquals( "Size of union of g2 and g0 not correct", s0+s2 - 1, m20.size() );
126         
127         assertEquals( "Size of union of g0 with itself not correct", s0, m00.size() );
128     }
129     
130     
131     public void testGraphSize2() {
132         Graph g0 = graphWith( "x p y" );
133         Graph g1 = graphWith( "x p z; z p zz" ); // disjoint with g0
134
Graph g2 = graphWith( "x p y; z p a" ); // intersects with g1
135

136         Graph m01 = new MultiUnion( iterateOver( g0, g1 ) );
137         Graph m10 = new MultiUnion( iterateOver( g1, g0 ) );
138         Graph m12 = new MultiUnion( iterateOver( g1, g2 ) );
139         Graph m21 = new MultiUnion( iterateOver( g2, g1 ) );
140         Graph m02 = new MultiUnion( iterateOver( g0, g2 ) );
141         Graph m20 = new MultiUnion( iterateOver( g2, g0 ) );
142         
143         Graph m00 = new MultiUnion( iterateOver( g0, g0 ) );
144         
145         int s0 = g0.size();
146         int s1 = g1.size();
147         int s2 = g2.size();
148         
149         assertEquals( "Size of union of g0 and g1 not correct", s0+s1, m01.size() );
150         assertEquals( "Size of union of g1 and g0 not correct", s0+s1, m10.size() );
151         
152         assertEquals( "Size of union of g1 and g2 not correct", s1+s2, m12.size() );
153         assertEquals( "Size of union of g2 and g1 not correct", s1+s2, m21.size() );
154
155         assertEquals( "Size of union of g0 and g2 not correct", s0+s2 - 1, m02.size() );
156         assertEquals( "Size of union of g2 and g0 not correct", s0+s2 - 1, m20.size() );
157         
158         assertEquals( "Size of union of g0 with itself not correct", s0, m00.size() );
159     }
160     
161     
162     public void testGraphAddSize() {
163         Graph g0 = graphWith( "x p y" );
164         Graph g1 = graphWith( "x p z; z p zz" ); // disjoint with g0
165
Graph g2 = graphWith( "x p y; z p a" ); // intersects with g1
166

167         int s0 = g0.size();
168         int s1 = g1.size();
169         int s2 = g2.size();
170         
171         MultiUnion m0 = new MultiUnion( new Graph[] {g0} );
172         
173         assertEquals( "Size of union of g0 not correct", s0, m0.size() );
174         m0.addGraph( g1 );
175         assertEquals( "Size of union of g1 and g0 not correct", s0+s1, m0.size() );
176         
177         m0.addGraph( g2 );
178         assertEquals( "Size of union of g0, g1 and g2 not correct", s0+s1+s2 -1, m0.size() );
179         
180         m0.removeGraph( g1 );
181         assertEquals( "Size of union of g0 and g2 not correct", s0+s2 -1, m0.size() );
182         
183         m0.removeGraph( g0 );
184         assertEquals( "Size of union of g2 not correct", s2, m0.size() );
185         
186         // remove again
187
m0.removeGraph( g0 );
188         assertEquals( "Size of union of g2 not correct", s2, m0.size() );
189         
190         m0.removeGraph( g2 );
191         assertEquals( "Size of empty union not correct", 0, m0.size() );
192         
193     }
194     
195     
196     public void testAdd() {
197         Graph g0 = graphWith( "x p y" );
198         Graph g1 = graphWith( "x p z; z p zz" ); // disjoint with g0
199
Graph g2 = graphWith( "x p y; z p a" ); // intersects with g1
200

201         MultiUnion m = new MultiUnion( new Graph[] {g0, g1} );
202         
203         int s0 = g0.size();
204         int s1 = g1.size();
205         int s2 = g2.size();
206         int m0 = m.size();
207
208         // add a triple to the union
209
m.add( triple( "a q b" ) );
210         
211         assertEquals( "m.size should have increased by one", m0 + 1, m.size() );
212         assertEquals( "g0.size should have increased by one", s0 + 1, g0.size() );
213         assertEquals( "g1 size should be constant", s1, g1.size() );
214         
215         // change the designated receiver and try again
216
m.setBaseGraph( g1 );
217         
218         s0 = g0.size();
219         s1 = g1.size();
220         s2 = g2.size();
221         m0 = m.size();
222         
223         m.add( triple( "a1 q b1" ));
224
225         assertEquals( "m.size should have increased by one", m0 + 1, m.size() );
226         assertEquals( "g0 size should be constant", s0, g0.size() );
227         assertEquals( "g1.size should have increased by one", s1 + 1, g1.size() );
228         
229         // check that we can't make g2 the designated updater
230
boolean expected = false;
231         try {
232             m.setBaseGraph( g2 );
233         }
234         catch (IllegalArgumentException JavaDoc e) {
235             expected = true;
236         }
237         assertTrue( "Should not have been able to make g2 the updater", expected );
238     }
239     
240     
241     public void testDelete() {
242         Graph g0 = graphWith( "x p y" );
243         Graph g1 = graphWith( "x p z; z p zz" ); // disjoint with g0
244

245         MultiUnion m = new MultiUnion( new Graph[] {g0, g1} );
246         
247         checkDeleteSizes( 1, 2, 3, g0, g1, m );
248         
249         m.delete( triple( "x p y") );
250         checkDeleteSizes( 0, 2, 2, g0, g1, m );
251
252         m.delete( triple( "x p y") );
253         checkDeleteSizes( 0, 2, 2, g0, g1, m );
254
255         m.setBaseGraph( g1 );
256
257         m.delete( triple( "x p z") );
258         checkDeleteSizes( 0, 1, 1, g0, g1, m );
259
260         m.delete( triple( "z p zz") );
261         checkDeleteSizes( 0, 0, 0, g0, g1, m );
262     }
263     
264     
265     public void testContains() {
266         Graph g0 = graphWith( "x p y" );
267         Graph g1 = graphWith( "x p z; z p zz" ); // disjoint with g0
268

269         MultiUnion m = new MultiUnion( new Graph[] {g0, g1} );
270  
271         assertTrue( "m should contain triple", m.contains( triple( "x p y ")));
272         assertTrue( "m should contain triple", m.contains( triple( "x p z ")));
273         assertTrue( "m should contain triple", m.contains( triple( "z p zz ")));
274         
275         assertFalse( "m should not contain triple", m.contains( triple( "zz p z ")));
276     }
277     
278     
279     /* Test using a model to wrap a multi union */
280     public void testModel() {
281         Graph g0 = graphWith( "x p y" );
282         MultiUnion u = new MultiUnion( new Graph[] {g0} );
283         
284         Model m = ModelFactory.createModelForGraph( u );
285         
286         assertEquals( "Model size not correct", 1, m.size() );
287         
288         Graph g1 = graphWith( "x p z; z p zz" ); // disjoint with g0
289
u.addGraph( g1 );
290         
291         assertEquals( "Model size not correct", 3, m.size() );
292         
293         // adds one more statement to the model
294
m.read( "file:testing/ontology/list0.rdf" );
295         assertEquals( "Model size not correct", 4, m.size() );
296         
297         // debug m.write( System.out );
298
}
299     
300     
301     // Internal implementation methods
302
//////////////////////////////////
303

304     protected void checkDeleteSizes( int s0, int s1, int m0, Graph g0, Graph g1, Graph m ) {
305         assertEquals( "Delete check: g0 size", s0, g0.size() );
306         assertEquals( "Delete check: g1 size", s1, g1.size() );
307         assertEquals( "Delete check: m size", m0, m.size() );
308     }
309     
310     protected Iterator iterateOver( Object JavaDoc x0 ) {
311         List l = new ArrayList();
312         l.add( x0 );
313         return l.iterator();
314     }
315     protected Iterator iterateOver( Object JavaDoc x0, Object JavaDoc x1 ) {
316         List l = new ArrayList();
317         l.add( x0 );
318         l.add( x1 );
319         return l.iterator();
320     }
321     protected Iterator iterateOver( Object JavaDoc x0, Object JavaDoc x1, Object JavaDoc x2 ) {
322         List l = new ArrayList();
323         l.add( x0 );
324         l.add( x1 );
325         l.add( x2 );
326         return l.iterator();
327     }
328     
329     
330     
331     //==============================================================================
332
// Inner class definitions
333
//==============================================================================
334

335
336 }
337
338
339 /*
340     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
341     All rights reserved.
342
343     Redistribution and use in source and binary forms, with or without
344     modification, are permitted provided that the following conditions
345     are met:
346
347     1. Redistributions of source code must retain the above copyright
348        notice, this list of conditions and the following disclaimer.
349
350     2. Redistributions in binary form must reproduce the above copyright
351        notice, this list of conditions and the following disclaimer in the
352        documentation and/or other materials provided with the distribution.
353
354     3. The name of the author may not be used to endorse or promote products
355        derived from this software without specific prior written permission.
356
357     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
358     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
359     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
360     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
361     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
362     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
363     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
364     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
365     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
366     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
367 */

368
Popular Tags