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 import com.hp.hpl.jena.vocabulary.DB; 29 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 33 34 35 public class TestMultiModel extends TestCase 36 { 37 String DefModel = GraphRDB.DEFAULT; 38 39 40 public TestMultiModel( String name ) 41 { super( name ); } 42 43 public static TestSuite suite() 44 { return new TestSuite( TestMultiModel.class ); } 45 46 Model model = null; 47 ModelRDB dmod1 = null; 48 ModelRDB dmod2 = null; 49 ModelRDB nmod1 = null; 50 ModelRDB nmod2 = null; 51 IDBConnection conn = null; 52 IRDBDriver dbDriver; 53 54 protected void setUp() throws java.lang.Exception { 55 conn = TestConnection.makeAndCleanTestConnection(); 56 dbDriver = conn.getDriver(); 57 model = ModelRDB.createModel(conn); 58 conn.getDriver().setStoreWithModel(DefModel); 59 dmod1 = ModelRDB.createModel(conn,"Def_Model_1"); 60 conn.getDriver().setStoreWithModel("Def_Model_1"); 61 dmod2 = ModelRDB.createModel(conn, "Def_Model_2"); 62 conn.getDriver().setStoreWithModel(null); 63 nmod1 = ModelRDB.createModel(conn,"Named_Model_1"); 64 conn.getDriver().setStoreWithModel("Named_Model_1"); 65 nmod2 = ModelRDB.createModel(conn,"Named_Model_2"); 66 } 67 68 protected void tearDown() throws java.lang.Exception { 69 dmod1.close(); dmod2.close(); 70 nmod1.close(); nmod2.close(); 71 conn.cleanDB(); 72 conn.close(); 73 conn = null; 74 } 75 76 77 public void addToDBGraphProp ( Model model, Property prop, String val ) { 78 StmtIterator iter = model.listStatements( 80 new SimpleSelector(null, DB.graphName, (RDFNode) null)); 81 assertTrue(iter.hasNext()); 82 83 Statement stmt = iter.nextStatement(); 84 assertTrue(iter.hasNext() == false); 85 Resource graphURI = stmt.getSubject(); 86 Literal l = model.createLiteral(val); 87 Statement s = model.createStatement(graphURI,prop,l); 88 model.add(s); 89 assertTrue(model.contains(s)); 90 } 91 92 private void addOnModel(Model model, Statement stmt) { 93 model.add(stmt); 94 assertTrue( model.contains(stmt) ); 95 model.add(stmt); 96 assertTrue( model.contains(stmt) ); 97 } 98 99 private void rmvOnModel(Model model, Statement stmt) { 100 assertTrue( model.contains(stmt) ); 101 model.remove(stmt); 102 assertTrue( !model.contains(stmt) ); 103 model.add(stmt); 104 assertTrue( model.contains(stmt) ); 105 model.remove(stmt); 106 assertTrue( !model.contains(stmt) ); 107 } 108 109 110 private void addRemove(Statement stmt) { 111 addOnModel(model,stmt); 112 addOnModel(dmod1,stmt); 113 addOnModel(dmod2,stmt); 114 addOnModel(nmod1,stmt); 115 addOnModel(nmod2,stmt); 116 117 rmvOnModel(nmod2,stmt); 118 rmvOnModel(nmod1,stmt); 119 rmvOnModel(dmod2,stmt); 120 rmvOnModel(dmod1,stmt); 121 rmvOnModel(model,stmt); 122 } 123 124 public void testAddRemoveURI() { 125 Resource s = model.createResource("test#subject"); 126 Property p = model.createProperty("test#predicate"); 127 Resource o = model.createResource("test#object"); 128 129 addRemove( model.createStatement(s,p,o)); 130 } 131 132 public void testAddRemoveLiteral() { 133 Resource s = model.createResource("test#subject"); 134 Property p = model.createProperty("test#predicate"); 135 Literal l = model.createLiteral("testLiteral"); 136 137 addRemove( model.createStatement(s,p,l)); 138 } 139 140 public void testSetLongObjectLenFailure() { 141 try { 142 int len = dbDriver.getLongObjectLength(); 143 dbDriver.setLongObjectLength(len / 2); 144 assertTrue(false); 145 } catch (Exception e) { 146 } 147 } 148 149 public void testLongObjectLen() { 150 long longLen = dbDriver.getLongObjectLength(); 151 assertTrue(longLen > 0 && longLen < 100000); 152 153 String base = "."; 154 StringBuffer buffer = new StringBuffer (1024 + (int) longLen); 155 156 157 158 long minLongLen = longLen - 32; 159 long maxLongLen = longLen + 32; 160 assertTrue(minLongLen > 0); 161 162 Resource s = model.createResource("test#subject"); 163 Property p = model.createProperty("test#predicate"); 164 Literal l; 165 Statement stmt; 166 while (buffer.length() < minLongLen) { 167 buffer.append(base); 168 } 169 170 long modelSizeBeg = model.size(); 171 while (buffer.length() < maxLongLen) { 172 l = model.createLiteral(buffer.toString()); 173 stmt = model.createStatement(s, p, l); 174 model.add(stmt); 175 assertTrue(model.contains(stmt)); 176 assertTrue(stmt.getObject().equals(l)); 177 buffer.append(base); 178 } 179 assertTrue(model.size() == (modelSizeBeg + maxLongLen - minLongLen)); 180 181 while (buffer.length() > minLongLen) { 182 buffer.deleteCharAt(0); 183 l = model.createLiteral(buffer.toString()); 184 stmt = model.createStatement(s, p, l); 185 assertTrue(model.contains(stmt)); 186 model.remove(stmt); 187 assertTrue(!model.contains(stmt)); 188 } 189 assertTrue(model.size() == modelSizeBeg); 190 } 191 192 public void testSetLongObjectLen() { 193 int len = dbDriver.getLongObjectLength(); 194 try { 195 tearDown(); 196 conn = TestConnection.makeTestConnection(); 197 dbDriver = conn.getDriver(); 198 len = dbDriver.getLongObjectLength(); 199 dbDriver.setLongObjectLength(len / 2); 200 model = ModelRDB.createModel(conn); 201 } catch (Exception e) { 202 assertTrue(false); 203 } 204 testLongObjectLen(); 205 206 model.close(); 208 try { 209 conn.close(); 210 conn = TestConnection.makeTestConnection(); 211 dbDriver = conn.getDriver(); 212 assertTrue(len == dbDriver.getLongObjectLength()); 213 model = ModelRDB.open(conn); 214 assertTrue(len / 2 == dbDriver.getLongObjectLength()); 215 } catch (Exception e) { 216 assertTrue(false); 217 } 218 } 219 220 public void testAddRemoveHugeLiteral() { 221 String base = "This is a huge string that repeats."; 222 StringBuffer buffer = new StringBuffer (4096); 223 while(buffer.length() < 4000 ) 224 buffer.append(base); 225 Resource s = model.createResource("test#subject"); 226 Property p = model.createProperty("test#predicate"); 227 Literal l = model.createLiteral(buffer.toString()); 228 229 addRemove( model.createStatement(s,p,l)); 230 } 231 232 public void testAddRemoveDatatype() { 233 Resource s = model.createResource("test#subject"); 234 Property p = model.createProperty("test#predicate"); 235 Literal l = model.createTypedLiteral("stringType"); 236 237 addRemove( model.createStatement(s,p,l)); 238 } 239 240 public void testAddRemoveHugeDatatype() { 241 String base = "This is a huge string that repeats."; 242 StringBuffer buffer = new StringBuffer (4096); 243 while(buffer.length() < 4000 ) 244 buffer.append(base); 245 Resource s = model.createResource("test#subject"); 246 Property p = model.createProperty("test#predicate"); 247 Literal l2 = model.createTypedLiteral(buffer.toString()); 248 249 addRemove( model.createStatement(s,p,l2)); 250 } 251 252 public void testAddRemoveBNode() { 253 Resource s = model.createResource(); 254 Property p = model.createProperty("test#predicate"); 255 Resource o = model.createResource(); 256 257 addRemove( model.createStatement(s,p,o)); 258 259 } 260 261 public void testBNodeIdentityPreservation() { 262 Resource s = model.createResource(); 263 Property p = model.createProperty("test#predicate"); 264 Resource o = model.createResource(); 265 266 Statement spo = model.createStatement(s,p,o); 271 Statement ops = model.createStatement(o,p,s); 272 model.add(spo); 273 addRemove(ops); 274 assertTrue( model.contains(spo)); 275 model.remove(spo); 276 } 277 278 279 } 280 281 282 311 | Popular Tags |