1 19 20 package com.hp.hpl.jena.graph.compose.test; 23 24 25 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 47 public class TestMultiUnion 48 extends AbstractTestGraph 49 { 50 53 54 57 58 61 62 65 public TestMultiUnion( String s ) { 66 super( s ); 67 } 68 69 70 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 99 100 public void testGraphSize1() { 101 Graph g0 = graphWith( "x p y" ); 102 Graph g1 = graphWith( "x p z; z p zz" ); Graph g2 = graphWith( "x p y; z p a" ); 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" ); Graph g2 = graphWith( "x p y; z p a" ); 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" ); Graph g2 = graphWith( "x p y; z p a" ); 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 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" ); Graph g2 = graphWith( "x p y; z p a" ); 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 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 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 boolean expected = false; 231 try { 232 m.setBaseGraph( g2 ); 233 } 234 catch (IllegalArgumentException 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" ); 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" ); 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 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" ); u.addGraph( g1 ); 290 291 assertEquals( "Model size not correct", 3, m.size() ); 292 293 m.read( "file:testing/ontology/list0.rdf" ); 295 assertEquals( "Model size not correct", 4, m.size() ); 296 297 } 299 300 301 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 x0 ) { 311 List l = new ArrayList(); 312 l.add( x0 ); 313 return l.iterator(); 314 } 315 protected Iterator iterateOver( Object x0, Object x1 ) { 316 List l = new ArrayList(); 317 l.add( x0 ); 318 l.add( x1 ); 319 return l.iterator(); 320 } 321 protected Iterator iterateOver( Object x0, Object x1, Object 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 335 336 } 337 338 339 368 | Popular Tags |