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 30 import junit.framework.*; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 public class TestCompareToMem extends TestCase 35 { 36 37 public TestCompareToMem( String name ) 38 { super( name ); } 39 40 public static TestSuite suite() 41 { return new TestSuite( TestCompareToMem.class ); } 42 43 static Log logger = LogFactory.getLog( TestCompareToMem.class ); 44 45 Model modelrdf = null; 46 Model modelmem = null; 47 48 IDBConnection conn = null; 49 50 protected void setUp() throws java.lang.Exception { 51 conn = TestConnection.makeAndCleanTestConnection(); 52 modelrdf = ModelRDB.createModel(conn); 53 modelmem = ModelFactory.createDefaultModel(); 54 } 55 56 protected void tearDown() throws java.lang.Exception { 57 modelrdf.close(); 58 modelrdf = null; 59 conn.cleanDB(); 60 conn.close(); 61 conn = null; 62 } 63 64 private void addRemove(Statement stmt) { 65 modelrdf.add(stmt); 66 modelmem.add(stmt); 67 68 assertTrue( modelmem.size() == 1); 69 assertTrue( modelrdf.size() == 1); 70 71 compareModels(); 72 73 modelrdf.remove(stmt); 74 modelmem.remove(stmt); 75 76 assertTrue( modelmem.size() == 0); 77 assertTrue( modelrdf.size() == 0); 78 79 compareModels(); 80 } 81 82 private void compareModels() { 83 84 Iterator it = modelmem.listStatements(); 85 while( it.hasNext()) { 86 Statement s = (Statement)it.next(); 87 if( ! modelrdf.contains(s)) { 88 logger.error("Statment:"+s+" is in mem but not rdf"); 89 logModel(modelmem, "Mem"); 90 logModel(modelrdf, "RDF"); 91 } 92 assertTrue( modelrdf.contains(s)); 93 } 94 it = modelrdf.listStatements(); 95 while( it.hasNext()) { 96 Statement s = (Statement)it.next(); 97 if( ! modelmem.contains(s)) { 98 logger.error("Statment:"+s+" is in rdf but not memory"); 99 logModel(modelmem, "Mem"); 100 logModel(modelrdf, "RDF"); 101 } 102 assertTrue( modelmem.contains(s)); 103 } 104 } 105 106 private void logModel(Model m, String name) { 107 logger.debug("Model"); 108 Iterator it = m.listStatements(); 109 while( it.hasNext()) { 110 Statement s = (Statement)it.next(); 111 RDFNode object = s.getObject(); 112 if( object instanceof Literal ) 113 logger.debug(name+": "+s.getSubject()+s.getPredicate()+((Literal)object).getValue()+" "+((Literal)object).getDatatype()+" "+((Literal)object).getLanguage()); 114 else 115 logger.debug(name+": "+it.next()); 116 } 117 } 118 119 public void testAddRemoveURI() { 120 Resource s = modelrdf.createResource("test#subject"); 121 Property p = modelrdf.createProperty("test#predicate"); 122 Resource o = modelrdf.createResource("test#object"); 123 124 addRemove( modelrdf.createStatement(s,p,o)); 125 } 126 127 public void testAddRemoveLiteral() { 128 Resource s = modelrdf.createResource("test#subject"); 129 Property p = modelrdf.createProperty("test#predicate"); 130 Literal l = modelrdf.createLiteral("testLiteral"); 131 132 addRemove( modelrdf.createStatement(s,p,l)); 133 } 134 135 public void testAddRemoveHugeLiteral() { 136 String base = "This is a huge string that repeats."; 137 StringBuffer buffer = new StringBuffer (4096); 138 while(buffer.length() < 4000 ) 139 buffer.append(base); 140 Resource s = modelrdf.createResource("test#subject"); 141 Property p = modelrdf.createProperty("test#predicate"); 142 Literal l = modelrdf.createLiteral(buffer.toString()); 143 144 addRemove( modelrdf.createStatement(s,p,l)); 145 } 146 147 public void testAddRemoveDatatype() { 148 Resource s = modelrdf.createResource("test#subject"); 149 Property p = modelrdf.createProperty("test#predicate"); 150 Literal l = modelrdf.createTypedLiteral("stringType"); 151 152 addRemove( modelrdf.createStatement(s,p,l)); 153 } 154 155 public void testAddRemoveHugeDatatype() { 156 String base = "This is a huge string that repeats."; 157 StringBuffer buffer = new StringBuffer (4096); 158 while(buffer.length() < 4000 ) 159 buffer.append(base); 160 Resource s = modelrdf.createResource("test#subject"); 161 Property p = modelrdf.createProperty("test#predicate"); 162 Literal l2 = modelrdf.createTypedLiteral(buffer.toString()); 163 164 addRemove( modelrdf.createStatement(s,p,l2)); 165 } 166 167 public void testAddRemoveHugeLiteral2() { 168 String base = "This is a huge string that repeats."; 169 StringBuffer buffer = new StringBuffer (4096); 170 while(buffer.length() < 4000 ) 171 buffer.append(base); 172 Resource s = modelmem.createResource("test#subject"); 173 Property p = modelmem.createProperty("test#predicate"); 174 Literal l2 = modelmem.createLiteral(buffer.toString()); 175 Literal l3 = modelmem.createLiteral(buffer.toString()+"."); 176 177 Statement st1 = modelmem.createStatement(s,p,l2); 178 Statement st2 = modelmem.createStatement(s,p,l3); 179 modelrdf.add(st1); 180 modelmem.add(st1); 181 182 compareModels(); 183 184 modelrdf.add(st2); 185 modelmem.add(st2); 186 187 compareModels(); 188 189 modelrdf.remove(st2); 190 modelmem.remove(st2); 191 192 compareModels(); 193 194 modelrdf.remove(st1); 195 modelmem.remove(st1); 196 197 } 198 199 } 200 201 202 231 | Popular Tags |