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.rdf.model.impl.ModelCom; 11 import com.hp.hpl.jena.shared.*; 12 import com.hp.hpl.jena.graph.*; 13 14 17 public abstract class AbstractTestModel extends ModelTestBase 18 { 19 public AbstractTestModel( String name ) 20 { super(name); } 21 22 public abstract Model getModel(); 23 24 private Model model; 25 26 public void setUp() 27 { model = getModel(); } 28 29 public void tearDown() 30 { model.close(); } 31 32 public void testTransactions() 33 { 34 Command cmd = new Command() 35 { public Object execute() { return null; } }; 36 if (model.supportsTransactions()) model.executeInTransaction( cmd ); 37 } 38 39 public void testCreateResourceFromNode() 40 { 41 RDFNode S = model.getRDFNode( Node.create( "spoo:S" ) ); 42 assertTrue( S instanceof Resource ); 43 assertEquals( "spoo:S", ((Resource) S).getURI() ); 44 } 45 46 public void testCreateLiteralFromNode() 47 { 48 RDFNode S = model.getRDFNode( Node.create( "42" ) ); 49 assertTrue( S instanceof Literal ); 50 assertEquals( "42", ((Literal) S).getLexicalForm() ); 51 } 52 53 public void testCreateBlankFromNode() 54 { 55 RDFNode S = model.getRDFNode( Node.create( "_Blank" ) ); 56 assertTrue( S instanceof Resource ); 57 assertEquals( new AnonId( "_Blank" ), ((Resource) S).getId() ); 58 } 59 60 public void testIsEmpty() 61 { 62 Statement S1 = statement( model, "model rdf:type nonEmpty" ); 63 Statement S2 = statement( model, "pinky rdf:type Pig" ); 64 assertTrue( model.isEmpty() ); 65 model.add( S1 ); 66 assertFalse( model.isEmpty() ); 67 model.add( S2 ); 68 assertFalse( model.isEmpty() ); 69 model.remove( S1 ); 70 assertFalse( model.isEmpty() ); 71 model.remove( S2 ); 72 assertTrue( model.isEmpty() ); 73 } 74 75 public void testContainsResource() 76 { 77 modelAdd( model, "x R y; _a P _b" ); 78 assertTrue( model.containsResource( resource( model, "x" ) ) ); 79 assertTrue( model.containsResource( resource( model, "R" ) ) ); 80 assertTrue( model.containsResource( resource( model, "y" ) ) ); 81 assertTrue( model.containsResource( resource( model, "_a" ) ) ); 82 assertTrue( model.containsResource( resource( model, "P" ) ) ); 83 assertTrue( model.containsResource( resource( model, "_b" ) ) ); 84 assertFalse( model.containsResource( resource( model, "i" ) ) ); 85 assertFalse( model.containsResource( resource( model, "_j" ) ) ); 86 } 87 88 92 public void testGetProperty() 93 { 94 modelAdd( model, "x P a; x P b; x R c" ); 95 Resource x = resource( model, "x" ); 96 assertEquals( resource( model, "c" ), x.getProperty( property( model, "R" ) ).getObject() ); 97 RDFNode ob = x.getProperty( property( model, "P" ) ).getObject(); 98 assertTrue( ob.equals( resource( model, "a" ) ) || ob.equals( resource( model, "b" ) ) ); 99 assertNull( x.getProperty( property( model, "noSuchPropertyHere" ) ) ); 100 } 101 102 public void testToStatement() 103 { 104 Triple t = triple( "a P b" ); 105 Statement s = model.asStatement( t ); 106 assertEquals( node( "a" ), s.getSubject().asNode() ); 107 assertEquals( node( "P" ), s.getPredicate().asNode() ); 108 assertEquals( node( "b" ), s.getObject().asNode() ); 109 } 110 111 public void testAsRDF() 112 { 113 RDFNode r = model.asRDFNode( node( "a" ) ); 114 } 115 116 public void testRemoveAll() 117 { 118 testRemoveAll( "" ); 119 testRemoveAll( "a RR b" ); 120 testRemoveAll( "x P y; a Q b; c R 17; _d S 'e'" ); 121 testRemoveAll( "subject Predicate 'object'; http://nowhere/x scheme:cunning not:plan" ); 122 } 123 124 protected void testRemoveAll( String statements ) 125 { 126 modelAdd( model, statements ); 127 assertSame( model, model.removeAll() ); 128 assertEquals( "model should have size 0 following removeAll(): ", 0, model.size() ); 129 } 130 131 139 protected String [][] cases = 140 { 141 { "x R y", "x R y", "" }, 142 { "x R y; a P b", "x R y", "a P b" }, 143 { "x R y; a P b", "?? R y", "a P b" }, 144 { "x R y; a P b", "x R ??", "a P b" }, 145 { "x R y; a P b", "x ?? y", "a P b" }, 146 { "x R y; a P b", "?? ?? ??", "" }, 147 { "x R y; a P b; c P d", "?? P ??", "x R y" }, 148 { "x R y; a P b; x S y", "x ?? ??", "a P b" }, 149 }; 150 151 156 public void testRemoveSPO() 157 { 158 ModelCom mc = (ModelCom) ModelFactory.createDefaultModel(); 159 for (int i = 0; i < cases.length; i += 1) 160 for (int j = 0; j < 3; j += 1) 161 { 162 Model content = getModel(); 163 Model baseContent = copy( content ); 164 modelAdd( content, cases[i][0] ); 165 Triple remove = triple( cases[i][1] ); 166 Node s = remove.getSubject(), p = remove.getPredicate(), o = remove.getObject(); 167 Resource S = (Resource) (s.equals( Node.ANY ) ? null : mc.getRDFNode( s )); 168 Property P = (Property)((p.equals( Node.ANY ) ? null : mc.getRDFNode( p ).as( Property.class ))); 169 RDFNode O = o.equals( Node.ANY ) ? null : mc.getRDFNode( o ); 170 Model expected = modelWithStatements( cases[i][2] ); 171 content.removeAll( S, P, O ); 172 Model finalContent = copy( content ).remove( baseContent ); 173 assertIsoModels( cases[i][1], expected, finalContent ); 174 } 175 } 176 177 protected Model copy( Model m ) 178 { 179 return ModelFactory.createDefaultModel().add( m ); 180 } 181 } 182 183 184 | Popular Tags |