1 6 7 package com.hp.hpl.jena.rdf.model.test; 8 9 import com.hp.hpl.jena.rdf.model.*; 10 import com.hp.hpl.jena.util.CollectionFactory; 11 import com.hp.hpl.jena.vocabulary.RDF; 12 import com.hp.hpl.jena.graph.test.*; 13 14 import java.util.*; 15 16 19 public abstract class AbstractTestReifiedStatements extends ModelTestBase 20 { 21 public AbstractTestReifiedStatements( String name ) 22 { 23 super( name ); 24 } 25 26 public abstract Model getModel(); 27 28 private Model model; 29 private Resource S; 30 private Property P; 31 private RDFNode O; 32 private Statement SPO; 33 private Statement SPO2; 34 35 private static final String aURI = "jena:test/reifying#someURI"; 36 private static final String anotherURI = "jena:test/reifying#anotherURI"; 37 private static final String anchor = "jena:test/Reifying#"; 38 39 public void setUp() 40 { 41 model = getModel(); 42 Resource S2 = model.createResource( anchor + "subject2" ); 43 S = model.createResource( anchor + "subject" ); 44 P = model.createProperty( anchor + "predicate" ); 45 O = model.createLiteral( anchor + "object" ); 46 SPO = model.createStatement( S, P, O ); 47 SPO2 = model.createStatement( S2, P, O ); 48 } 49 50 54 public void testBasicReification() 55 { 56 if (model.getReificationStyle() != ModelFactory.Minimal) 57 { Resource R = model.createResource( aURI ); 58 model.add( R, RDF.type, RDF.Statement ); 59 model.add( R, RDF.subject, S ); 60 model.add( R, RDF.predicate, P ); 61 model.add( R, RDF.object, O ); 62 RDFNode rs = R.as( ReifiedStatement.class ); 63 assertEquals( "can recover statement", SPO, ((ReifiedStatement) rs).getStatement() ); } 64 } 65 66 71 public void testReificationCombinations() 72 { 73 Resource RR = model.createResource( aURI ), SS = model.createResource( anotherURI ); 74 Property PP = (Property) RR.as( Property.class ); 75 Object [][] statements = 76 { 77 { model.createStatement( RR, RDF.type, RDF.Statement ), new Integer (1) }, 78 { model.createStatement( RR, RDF.subject, SS ), new Integer (2) }, 79 { model.createStatement( RR, RDF.predicate, PP ), new Integer (4) }, 80 { model.createStatement( RR, RDF.object, O ), new Integer (8) }, 81 { model.createStatement( SS, PP, O ), new Integer (16) }, 82 { model.createStatement( RR, PP, O ), new Integer (32) }, 83 { model.createStatement( SS, RDF.subject, SS ), new Integer (64) }, 84 { model.createStatement( SS, RDF.predicate, PP ), new Integer (128) }, 85 { model.createStatement( SS, RDF.object, O ), new Integer (256) }, 86 { model.createStatement( SS, RDF.type, RDF.Statement ), new Integer (512) } 87 }; 88 if (model.getReificationStyle() != ModelFactory.Minimal) 89 testCombinations( model, RR, 0, statements, statements.length ); 90 } 91 92 99 private void testCombinations( Model m, Resource R, int mask, Object [][] statements, int n ) 100 { 101 if (n == 0) 102 { 103 try 104 { 105 ReifiedStatement rs = (ReifiedStatement) R.as( ReifiedStatement.class ); 107 assertTrue( "should not reify: not all components present [" + mask + "]: " + rs, (mask & 15) == 15 ); 109 } 111 catch (DoesNotReifyException e) 112 { assertFalse( "should reify: all components present", mask == 15 ); } 114 } 115 else 116 { 117 int i = n - 1; 118 Statement s = (Statement) statements[i][0]; 119 int bits = ((Integer ) statements[i][1]).intValue(); 120 testCombinations( m, R, mask, statements, i ); 121 m.add( s ); 122 testCombinations( m, R, mask + bits, statements, i ); 123 m.remove( s ); 124 } 125 } 126 127 public void testThisWillBreak() 128 { 129 Resource R = model.createResource( aURI ); 130 SPO.createReifiedStatement( aURI ); 131 model.add( R, RDF.subject, R ); 132 } 133 134 137 public void testDirtyReification() 138 { 139 Resource R = model.createResource( aURI ); 140 model.add( R, RDF.type, RDF.Statement ); 141 model.add( R, RDF.subject, S ); 142 model.add( R, RDF.subject, P ); 143 testDoesNotReify( "boo", R ); 144 } 145 146 public void testDoesNotReify( String title, Resource r ) 147 { 148 try { r.as( ReifiedStatement.class ); fail( title + " (" + r + ")" ); } 149 catch (DoesNotReifyException e) { } 150 } 151 152 public void testConversion() 153 { 154 final String uri = "spoo:handle"; 155 model.createReifiedStatement( uri, SPO ); 156 ReifiedStatement rs2 = (ReifiedStatement) model.createResource( uri ).as( ReifiedStatement.class ); 157 assertEquals( "recover statement", SPO, rs2.getStatement() ); 158 } 159 160 public void testDoesNotReifyUnknown() 161 { 162 testDoesNotReify( "model should not reify rubbish", model.createResource( "spoo:rubbish" ) ); 163 } 164 165 public void testConstructionByURI() 166 { 167 ReifiedStatement rs = model.createReifiedStatement( "spoo:handle", SPO ); 168 ReifiedStatement rs2 = SPO.createReifiedStatement( "spoo:gripper"); 169 assertEquals( "recover statement (URI)", SPO, rs.getStatement() ); 170 assertEquals( "recover URI", "spoo:handle", rs.getURI() ); 171 assertEquals( "recover URI", "spoo:gripper", rs2.getURI() ); 172 } 173 174 public void testStatementAndModel( String title, ReifiedStatement rs, Model m, Statement st ) 175 { 176 assertEquals( title + ": recover statement", st, rs.getStatement() ); 177 assertEquals( title + ": recover model", m, rs.getModel() ); 178 } 179 180 public void testConstructionFromStatements() 181 { 182 testStatementAndModel( "fromStatement", SPO.createReifiedStatement(), model, SPO ); 183 } 184 185 public void testConstructionFromModels() 186 { 187 testStatementAndModel( "fromModel", model.createReifiedStatement( SPO ) , model, SPO ); 188 } 189 190 194 public Set getSetRS( Model m ) 195 { return GraphTestBase.iteratorToSet( m.listReifiedStatements() ); } 196 197 protected static Set empty = CollectionFactory.createHashedSet(); 198 199 205 public void testListReifiedStatements() 206 { 207 assertEquals( "initially: no reified statements", empty, getSetRS( model ) ); 208 ReifiedStatement rs = model.createReifiedStatement( aURI, SPO ); 209 211 model.add( rs, P, O ); 212 Set justRS = arrayToSet( new Object [] {rs} ); 213 assertEquals( "post-add: one reified statement", justRS, getSetRS( model ) ); 214 model.add( S, P, rs ); 215 assertEquals( "post-add: still one reified statement", justRS, getSetRS( model ) ); 216 217 ReifiedStatement rs2 = model.createReifiedStatement( anotherURI, SPO2 ); 218 Set bothRS = arrayToSet( new Object [] {rs, rs2} ); 219 model.add( rs2, P, O ); 220 assertEquals( "post-add: still one reified statement", bothRS, getSetRS( model ) ); 221 } 222 223 228 public void testListDoesntCrash() 229 { 230 model.createReifiedStatement( SPO ); 231 model.createReifiedStatement( SPO2 ); 232 assertTrue( "should be non-empty", model.listReifiedStatements().hasNext() ); 233 } 234 235 public Set getSetRS( Model m, Statement st ) 236 { return GraphTestBase.iteratorToSet( m.listReifiedStatements( st ) ); } 237 238 public void testListReifiedSpecificStatements() 239 { 240 assertEquals( "no statements should match st", empty, getSetRS( model, SPO ) ); 241 242 ReifiedStatement rs = model.createReifiedStatement( aURI, SPO ); 243 ReifiedStatement rs2 = model.createReifiedStatement( anotherURI, SPO2 ); 244 model.add( rs, P, O ); 245 247 Set justRS2 = arrayToSet( new Object [] {rs2} ); 248 model.add( rs2, P, O ); 249 assertEquals( "now one matching statement", justRS2, getSetRS( model, SPO2 ) ); 250 } 251 252 public void testStatementListReifiedStatements() 253 { 254 Statement st = SPO; 255 Model m = model; 256 assertEquals( "it's not there yet", empty, GraphTestBase.iteratorToSet( st.listReifiedStatements() ) ); 257 ReifiedStatement rs = m.createReifiedStatement( aURI, st ); 258 Set justRS = arrayToSet( new Object [] {rs} ); 259 m.add( rs, P, O ); 260 assertEquals( "it's here now", justRS, GraphTestBase.iteratorToSet( st.listReifiedStatements() ) ); 261 } 262 263 public void testIsReified() 264 { 265 ReifiedStatement rs = model.createReifiedStatement( aURI, SPO ); 266 Resource BS = model.createResource( anchor + "BS" ); 267 Property BP = model.createProperty( anchor + "BP" ); 268 RDFNode BO = model.createProperty( anchor + "BO" ); 269 model.add( rs, P, O ); 270 assertTrue( "st should be reified now", SPO.isReified() ); 271 assertTrue( "m should have st reified now", model.isReified( SPO ) ); 272 assertFalse( "this new statement should not be reified", model.createStatement( BS, BP, BO ).isReified() ); 273 } 274 275 public void testGetAny() 276 { 277 Resource r = model.getAnyReifiedStatement( SPO ); 278 assertTrue( "should get reified statement back", r instanceof ReifiedStatement ); 279 assertEquals( "should get me the statement", SPO, ((ReifiedStatement) r).getStatement() ); 280 } 281 282 public void testRemoveReificationWorks() 283 { 284 Statement st = SPO; 285 Model m = model; 286 m.createReifiedStatement( aURI, st ); 287 assertTrue( "st is now reified", st.isReified() ); 288 m.removeAllReifications( st ); 289 assertFalse( "st is no longer reified", st.isReified() ); 290 } 291 292 296 public void testLeosBug() 297 { 298 Model A = getModel(); 299 Statement st = statement( A, "pigs fly south" ); 300 ReifiedStatement rst = st.createReifiedStatement( "eh:pointer" ); 301 A.removeReification( rst ); 302 assertIsoModels( ModelFactory.createDefaultModel(), A ); 303 } 304 305 public void testRR() 306 { 307 Statement st = SPO; 308 Model m = model; 309 ReifiedStatement rs1 = m.createReifiedStatement( aURI, st ); 310 ReifiedStatement rs2 = m.createReifiedStatement( anotherURI, st ); 311 m.removeReification( rs1 ); 312 testNotReifying( m, aURI ); 313 assertTrue( "st is still reified", st.isReified() ); 314 m.removeReification( rs2 ); 315 assertFalse( "st should no longer be reified", st.isReified() ); 316 } 317 318 private void testNotReifying( Model m, String uri ) 319 { 320 try 321 { 322 m.createResource( uri ).as( ReifiedStatement.class ); 323 fail( "there should be no reifiedStatement for " + uri ); 324 } 325 catch (DoesNotReifyException e) 326 { } 327 } 328 329 public void testDoesNotReifyElsewhere() 330 { 331 final String uri = "spoo:rubbish"; 332 Model m2 = getModel(); 333 model.createReifiedStatement( uri, SPO ); 334 testDoesNotReify( "blue model should not reify rubbish", m2.createResource( uri ) ); 335 } 336 } 348 349 350 | Popular Tags |