1 6 7 package com.hp.hpl.jena.db.test; 8 9 24 25 import com.hp.hpl.jena.db.*; 26 import com.hp.hpl.jena.db.impl.IRDBDriver; 27 import com.hp.hpl.jena.rdf.model.*; 28 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 33 34 public class TestBasicOperations extends TestCase { 35 36 public TestBasicOperations(String name) { 37 super(name); 38 } 39 40 public static TestSuite suite() { 41 return new TestSuite(TestBasicOperations.class); 42 } 43 44 ModelRDB model = null; 45 IRDBDriver dbDriver = null; 46 IDBConnection conn = null; 47 48 protected void setUp() throws java.lang.Exception { 49 conn = TestConnection.makeAndCleanTestConnection(); 50 model = ModelRDB.createModel(conn); 51 dbDriver = conn.getDriver(); 52 } 53 54 protected void tearDown() throws java.lang.Exception { 55 model.close(); 56 model = null; 57 conn.cleanDB(); 58 conn.close(); 59 conn = null; 60 } 61 62 private void addRemove(Statement stmt) { 63 model.add(stmt); 64 assertTrue(model.contains(stmt)); 65 model.remove(stmt); 66 assertTrue(!model.contains(stmt)); 67 model.add(stmt); 68 assertTrue(model.contains(stmt)); 69 model.remove(stmt); 70 assertTrue(!model.contains(stmt)); 71 model.add(stmt); 72 model.add(stmt); 73 assertTrue(model.contains(stmt)); 74 model.remove(stmt); 75 assertTrue(!model.contains(stmt)); 76 model.add(stmt); 77 model.add(stmt); 78 model.add(stmt); 79 model.add(stmt); 80 model.add(stmt); 81 model.add(stmt); 82 assertTrue(model.contains(stmt)); 83 model.remove(stmt); 84 assertTrue(!model.contains(stmt)); 85 } 86 87 public void testAddRemoveURI() { 88 Resource s = model.createResource("test#subject"); 89 Property p = model.createProperty("test#predicate"); 90 Resource o = model.createResource("test#object"); 91 92 addRemove(model.createStatement(s, p, o)); 93 } 94 95 public void testAddRemoveLiteral() { 96 Resource s = model.createResource("test#subject"); 97 Property p = model.createProperty("test#predicate"); 98 Literal l = model.createLiteral("testLiteral"); 99 100 addRemove(model.createStatement(s, p, l)); 101 } 102 103 public void testSetLongObjectLenFailure() { 104 try { 105 int len = dbDriver.getLongObjectLength(); 106 dbDriver.setLongObjectLength(len / 2); 107 assertTrue(false); 108 } catch (Exception e) { 109 } 110 } 111 112 public void testLongObjectLen() { 113 long longLen = dbDriver.getLongObjectLength(); 114 assertTrue(longLen > 0 && longLen < 100000); 115 116 String base = "."; 117 StringBuffer buffer = new StringBuffer (1024 + (int) longLen); 118 119 120 121 long minLongLen = longLen - 32; 122 long maxLongLen = longLen + 32; 123 assertTrue(minLongLen > 0); 124 125 Resource s = model.createResource("test#subject"); 126 Property p = model.createProperty("test#predicate"); 127 Literal l; 128 Statement stmt; 129 while (buffer.length() < minLongLen) { 130 buffer.append(base); 131 } 132 133 long modelSizeBeg = model.size(); 134 while (buffer.length() < maxLongLen) { 135 l = model.createLiteral(buffer.toString()); 136 stmt = model.createStatement(s, p, l); 137 model.add(stmt); 138 assertTrue(model.contains(stmt)); 139 assertTrue(stmt.getObject().equals(l)); 140 buffer.append(base); 141 } 142 assertTrue(model.size() == (modelSizeBeg + maxLongLen - minLongLen)); 143 144 while (buffer.length() > minLongLen) { 145 buffer.deleteCharAt(0); 146 l = model.createLiteral(buffer.toString()); 147 stmt = model.createStatement(s, p, l); 148 assertTrue(model.contains(stmt)); 149 model.remove(stmt); 150 assertTrue(!model.contains(stmt)); 151 } 152 assertTrue(model.size() == modelSizeBeg); 153 } 154 155 public void testSetLongObjectLen() { 156 int len = dbDriver.getLongObjectLength(); 157 try { 158 tearDown(); 159 conn = TestConnection.makeTestConnection(); 160 dbDriver = conn.getDriver(); 161 len = dbDriver.getLongObjectLength(); 162 dbDriver.setLongObjectLength(len / 2); 163 model = ModelRDB.createModel(conn); 164 } catch (Exception e) { 165 assertTrue(false); 166 } 167 testLongObjectLen(); 168 169 model.close(); 171 try { 172 conn.close(); 173 conn = TestConnection.makeTestConnection(); 174 dbDriver = conn.getDriver(); 175 assertTrue(len == dbDriver.getLongObjectLength()); 176 model = ModelRDB.open(conn); 177 assertTrue(len / 2 == dbDriver.getLongObjectLength()); 178 } catch (Exception e) { 179 assertTrue(false); 180 } 181 } 182 183 public void testAddRemoveHugeLiteral() { 184 String base = "This is a huge string that repeats."; 185 StringBuffer buffer = new StringBuffer (4096); 186 while (buffer.length() < 4000) 187 buffer.append(base); 188 Resource s = model.createResource("test#subject"); 189 Property p = model.createProperty("test#predicate"); 190 Literal l = model.createLiteral(buffer.toString()); 191 192 addRemove(model.createStatement(s, p, l)); 193 } 194 195 public void testCompressHugeURI() throws java.lang.Exception { 196 IDBConnection conn = TestConnection.makeAndCleanTestConnection(); 199 IRDBDriver d = conn.getDriver(); 200 d.setDoCompressURI(true); 201 model = ModelRDB.createModel(conn); 202 String pfx = "a123456789"; 203 String longPfx = ""; 204 long longLen = dbDriver.getLongObjectLength(); 205 while ( longPfx.length() < longLen ) 207 longPfx += pfx; 208 String URI = longPfx + ":/www.foo/#bar"; 209 Resource s = model.createResource(URI); 210 Property p = model.createProperty("test#predicate"); 211 Resource o = model.createResource("test#object"); 212 addRemove(model.createStatement(s, p, o)); 213 } 214 215 public void testCompressHugeURI1() throws java.lang.Exception { 216 IDBConnection conn = TestConnection.makeAndCleanTestConnection(); 219 IRDBDriver d = conn.getDriver(); 220 d.setDoCompressURI(true); 221 model = ModelRDB.createModel(conn); 222 String pfx = "a123456789"; 223 String longPfx = ""; 224 long longLen = dbDriver.getLongObjectLength(); 225 while ( longPfx.length() < longLen ) 227 longPfx += pfx; 228 String URI = longPfx + ":/www.foo/#bar" + longPfx; 229 Resource s = model.createResource(URI); 230 Property p = model.createProperty("test#predicate"); 231 Resource o = model.createResource("test#object"); 232 addRemove(model.createStatement(s, p, o)); 233 } 234 235 public void testNoCompressHugeURI() throws java.lang.Exception { 236 IDBConnection conn = TestConnection.makeAndCleanTestConnection(); 239 IRDBDriver d = conn.getDriver(); 240 d.setDoCompressURI(false); 241 model = ModelRDB.createModel(conn); 242 String pfx = "a123456789"; 243 String longPfx = ""; 244 long longLen = dbDriver.getLongObjectLength(); 245 while ( longPfx.length() < longLen ) 247 longPfx += pfx; 248 String URI = longPfx + ":/www.foo/#bar"; 249 Resource s = model.createResource(URI); 250 Property p = model.createProperty("test#predicate"); 251 Resource o = model.createResource("test#object"); 252 addRemove(model.createStatement(s, p, o)); 253 } 254 255 public void testNoCompressHugeURI1() throws java.lang.Exception { 256 IDBConnection conn = TestConnection.makeAndCleanTestConnection(); 259 IRDBDriver d = conn.getDriver(); 260 d.setDoCompressURI(false); 261 model = ModelRDB.createModel(conn); 262 String pfx = "a123456789"; 263 String longPfx = ""; 264 long longLen = dbDriver.getLongObjectLength(); 265 while ( longPfx.length() < longLen ) 267 longPfx += pfx; 268 String URI = longPfx + ":/www.foo/#bar" + longPfx; 269 Resource s = model.createResource(URI); 270 Property p = model.createProperty("test#predicate"); 271 Resource o = model.createResource("test#object"); 272 addRemove(model.createStatement(s, p, o)); 273 } 274 275 public void testAddRemoveHugeURI() throws java.lang.Exception { 276 String pfx = "a123456789"; 277 String longPfx = ""; 278 long longLen = dbDriver.getLongObjectLength(); 279 while ( longPfx.length() < longLen ) 281 longPfx += pfx; 282 String URI = longPfx + ":/www.foo/#bar" + longPfx; 283 Resource s = model.createResource(URI); 284 Property p = model.createProperty("test#predicate"); 285 Resource o = model.createResource("test#object"); 286 addRemove(model.createStatement(s, p, o)); 287 } 288 289 public void testPrefixCache() throws java.lang.Exception { 290 IDBConnection conn = TestConnection.makeAndCleanTestConnection(); 293 IRDBDriver d = conn.getDriver(); 294 d.setDoCompressURI(true); 295 model = ModelRDB.createModel(conn); 296 int cacheSize = d.getCompressCacheSize(); 297 cacheSize = 10; 298 d.setCompressCacheSize(cacheSize); 299 300 String pfx = "a123456789"; 301 String longPfx = ""; 302 long longLen = dbDriver.getLongObjectLength(); 303 while ( longPfx.length() < longLen ) 305 longPfx += pfx; 306 int i; 307 for(i=0;i<cacheSize*2;i++) { 308 String URI = longPfx + i + ":/www.foo/#bar"; 309 Resource s = model.createResource(URI); 310 Property p = model.createProperty("test#predicate"); 311 Resource o = model.createResource("test#object"); 312 model.add(s, p, o); 313 } 314 for(i=0;i<cacheSize*2;i++) { 315 String URI = longPfx + i + ":/www.foo/#bar"; 316 Resource s = model.createResource(URI); 317 Property p = model.createProperty("test#predicate"); 318 Resource o = model.createResource("test#object"); 319 assertTrue(model.contains(s, p, o)); 320 } 321 322 } 323 324 public void testPrefixCachePersists() throws java.lang.Exception { 325 IDBConnection conn = TestConnection.makeAndCleanTestConnection(); 327 IRDBDriver d = conn.getDriver(); 328 d.setDoCompressURI(true); 329 model = ModelRDB.createModel(conn); 330 int cacheSize = d.getCompressCacheSize(); 331 d.setCompressCacheSize(cacheSize/2); 332 model.close(); 333 conn.close(); 334 335 conn = TestConnection.makeTestConnection(); 336 d = conn.getDriver(); 337 try { 338 d.setDoCompressURI(false); 339 assertFalse(true); } catch (Exception e) { 341 model = ModelRDB.createModel(conn,"NamedModel"); 342 assertTrue(d.getDoCompressURI() == true); 343 assertTrue(d.getCompressCacheSize() == cacheSize); 344 } 345 } 346 347 348 public void testAddRemoveDatatype() { 349 Resource s = model.createResource("test#subject"); 350 Property p = model.createProperty("test#predicate"); 351 Literal l = model.createTypedLiteral("stringType"); 352 353 addRemove(model.createStatement(s, p, l)); 354 } 355 356 public void testAddRemoveHugeDatatype() { 357 String base = "This is a huge string that repeats."; 358 StringBuffer buffer = new StringBuffer (4096); 359 while (buffer.length() < 4000) 360 buffer.append(base); 361 Resource s = model.createResource("test#subject"); 362 Property p = model.createProperty("test#predicate"); 363 Literal l2 = model.createTypedLiteral(buffer.toString()); 364 365 addRemove(model.createStatement(s, p, l2)); 366 } 367 368 public void testAddRemoveBNode() { 369 Resource s = model.createResource(); 370 Property p = model.createProperty("test#predicate"); 371 Resource o = model.createResource(); 372 373 addRemove(model.createStatement(s, p, o)); 374 375 } 376 377 public void testBNodeIdentityPreservation() { 378 Resource s = model.createResource(); 379 Property p = model.createProperty("test#predicate"); 380 Resource o = model.createResource(); 381 382 Statement spo = model.createStatement(s, p, o); 387 Statement ops = model.createStatement(o, p, s); 388 model.add(spo); 389 addRemove(ops); 390 assertTrue(model.contains(spo)); 391 model.remove(spo); 392 } 393 394 public void testDuplicateCheck() { 395 Resource s = model.createResource(); 396 Property p = model.createProperty("test#predicate"); 397 Resource o = model.createResource(); 398 Statement spo = model.createStatement(s, p, o); 399 model.add(spo); 400 try { 401 model.add(spo); assertTrue(model.size() == 1); 403 } catch (Exception e) { 404 assertTrue(false); } 406 model.setDoDuplicateCheck(false); 407 try { 408 model.add(spo); assertTrue(model.size() == 2); 410 } catch (Exception e) { 411 assertTrue(false); } 413 } 414 415 } 416 417 418 447 | Popular Tags |