1 6 7 package com.hp.hpl.jena.db.test; 8 9 24 25 import java.util.Iterator ; 26 27 import com.hp.hpl.jena.db.*; 28 import com.hp.hpl.jena.rdf.model.*; 29 import com.hp.hpl.jena.vocabulary.RDF; 30 31 import junit.framework.*; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 public class TestReifierCompareToMem extends TestCase 36 { 37 38 public TestReifierCompareToMem( String name ) 39 { super( name ); } 40 41 public static TestSuite suite() 42 { return new TestSuite( TestReifierCompareToMem.class ); } 43 44 protected static Log logger = LogFactory.getLog( TestReifierCompareToMem.class ); 45 46 Model modelrdb = null; 47 Model modelmem = null; 48 49 IDBConnection conn = null; 50 51 protected void setUp() throws java.lang.Exception { 52 conn = TestConnection.makeAndCleanTestConnection(); 53 modelrdb = ModelRDB.createModel(conn); 54 modelmem = ModelFactory.createDefaultModel(); 55 } 56 57 protected void tearDown() throws java.lang.Exception { 58 modelrdb.close(); 59 modelrdb = null; 60 conn.cleanDB(); 61 conn.close(); 62 conn = null; 63 } 64 65 private void addRemove(Statement stmt) { 66 modelrdb.add(stmt); 67 modelmem.add(stmt); 68 69 compareModels(); 70 71 modelrdb.remove(stmt); 72 modelmem.remove(stmt); 73 74 compareModels(); 75 } 76 77 private void compareModels() { 78 79 Iterator it = modelmem.listStatements(); 80 while( it.hasNext()) { 81 Statement s = (Statement)it.next(); 82 if( ! modelrdb.contains(s)) { 83 logger.error( "Statment:"+s+" is in mem but not rdf"); 84 logModel(modelmem, "Mem"); 85 logModel(modelrdb, "RDF"); 86 } 87 assertTrue( modelrdb.contains(s)); 88 } 89 it = modelrdb.listStatements(); 90 while( it.hasNext()) { 91 Statement s = (Statement)it.next(); 92 if( ! modelmem.contains(s)) { 93 logger.error("Statment:"+s+" is in rdf but not memory"); 94 logModel(modelmem, "Mem"); 95 logModel(modelrdb, "RDF"); 96 } 97 assertTrue( modelmem.contains(s)); 98 } 99 100 assertTrue( modelmem.size() == modelrdb.size() ); 101 } 102 103 private void logModel(Model m, String name) { 104 logger.debug("Model"); 105 Iterator it = m.listStatements(); 106 while( it.hasNext()) { 107 logger.debug( name + ": " + it.next() ); 108 } 115 } 116 117 public void testAddPredicate() { 118 119 Resource s = modelrdb.createResource("SSS"), o = modelrdb.createResource("OOO"); 120 121 Statement stmt = modelrdb.createStatement(s,RDF.object,o); 122 modelrdb.add(stmt); 123 modelmem.add(stmt); 124 125 compareModels(); 126 127 modelrdb.remove(stmt); 128 modelmem.remove(stmt); 129 130 compareModels(); 131 132 } 133 134 public void testAddRemoveFullReification() { 135 136 Resource s = modelrdb.createResource("SSS"), p = modelrdb.createResource("PPP"), o = modelrdb.createResource("OOO"); 137 138 Statement stmtT = modelrdb.createStatement(s,RDF.type,RDF.Statement); 139 Statement stmtS = modelrdb.createStatement(s,RDF.subject,s); 140 Statement stmtP = modelrdb.createStatement(s,RDF.predicate,p); 141 Statement stmtO = modelrdb.createStatement(s,RDF.object,o); 142 143 modelrdb.add(stmtT); 144 modelmem.add(stmtT); 145 146 compareModels(); 147 148 modelrdb.add(stmtS); 149 modelmem.add(stmtS); 150 151 compareModels(); 152 153 modelrdb.add(stmtP); 154 modelmem.add(stmtP); 155 156 compareModels(); 157 158 modelrdb.add(stmtO); 159 modelmem.add(stmtO); 160 161 compareModels(); 162 163 modelrdb.remove(stmtO); 164 modelmem.remove(stmtO); 165 166 compareModels(); 167 168 modelrdb.remove(stmtP); 169 modelmem.remove(stmtP); 170 171 compareModels(); 172 173 modelrdb.remove(stmtS); 174 modelmem.remove(stmtS); 175 176 compareModels(); 177 178 modelrdb.remove(stmtT); 179 modelmem.remove(stmtT); 180 181 compareModels(); 182 } 183 184 public void testAddRemoveLiteralObject() { 185 Resource s = modelrdb.createResource("test#subject"); 186 Literal l = modelrdb.createLiteral("testLiteral"); 187 188 addRemove( modelrdb.createStatement(s,RDF.object,l)); 189 } 190 191 public void testAddRemoveHugeLiteralObject() { 192 String base = "This is a huge string that repeats."; 193 StringBuffer buffer = new StringBuffer (4096); 194 while(buffer.length() < 4000 ) 195 buffer.append(base); 196 Resource s = modelrdb.createResource("test#subject"); 197 Literal l = modelrdb.createLiteral(buffer.toString()); 198 199 addRemove( modelrdb.createStatement(s,RDF.object,l)); 200 } 201 202 public void testAddRemoveDatatypeObject() { 203 Resource s = modelrdb.createResource("test#subject"); 204 Literal l = modelrdb.createTypedLiteral("stringType"); 205 206 addRemove( modelrdb.createStatement(s,RDF.object,l)); 207 } 208 209 public void testAddRemoveHugeDatatypeObject() { 210 String base = "This is a huge string that repeats."; 211 StringBuffer buffer = new StringBuffer (4096); 212 while(buffer.length() < 4000 ) 213 buffer.append(base); 214 Resource s = modelrdb.createResource("test#subject"); 215 Literal l2 = modelrdb.createTypedLiteral(buffer.toString()); 216 217 addRemove( modelrdb.createStatement(s,RDF.object,l2)); 218 } 219 220 public void testAddRemoveHugeLiteral2Object() { 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 = modelmem.createResource("test#subject"); 226 Literal l2 = modelmem.createLiteral(buffer.toString()); 227 Literal l3 = modelmem.createLiteral(buffer.toString()+"."); 228 229 Statement st1 = modelmem.createStatement(s,RDF.object,l2); 230 Statement st2 = modelmem.createStatement(s,RDF.object,l3); 231 modelrdb.add(st1); 232 modelmem.add(st1); 233 234 compareModels(); 235 236 modelrdb.add(st2); 237 modelmem.add(st2); 238 239 compareModels(); 240 241 modelrdb.remove(st2); 242 modelmem.remove(st2); 243 244 compareModels(); 245 246 modelrdb.remove(st1); 247 modelmem.remove(st1); 248 249 } 250 251 } 252 253 254 283 | Popular Tags |