1 6 7 package com.hp.hpl.jena.graph.test; 8 9 12 13 import com.hp.hpl.jena.graph.*; 14 import com.hp.hpl.jena.graph.impl.LiteralLabel; 15 import com.hp.hpl.jena.rdf.model.AnonId; 16 import com.hp.hpl.jena.shared.*; 17 18 import junit.framework.*; 19 20 public class TestTriple extends GraphTestBase 21 { 22 23 public TestTriple(String name) 24 { super( name ); } 25 26 public static TestSuite suite() 27 { return new TestSuite( TestTriple.class ); } 28 29 private static final String U = "http://some.domain.name/magic/spells.incant"; 30 private static final String N = "Alice"; 31 private static final LiteralLabel L = new LiteralLabel( "ashes are burning", "en", false ); 32 33 public void testTripleEquals() { 34 try { 35 Node.cache(false); 36 37 AnonId id = new AnonId(); 39 LiteralLabel L2 = new LiteralLabel(id.toString(), "", false); 40 String U2 = id.toString(); 41 String N2 = id.toString(); 42 43 Node[] nodes = new Node[] { 44 Node.ANY, 45 Node.createAnon(id), Node.createAnon(), 46 Node.createLiteral(L), Node.createLiteral(L2), 47 Node.createURI(U), Node.createURI(U2), 48 Node.createVariable(N), Node.createVariable(N2) 49 }; 50 51 Triple[] triples = 52 new Triple [nodes.length * nodes.length * nodes.length]; 53 for (int i=0; i<nodes.length; i++) { 54 for (int j=0; j<nodes.length; j++) { 55 for (int k=0; k<nodes.length; k++) { 56 triples[i*nodes.length*nodes.length + 57 j*nodes.length + 58 k] = new Triple(nodes[i], nodes[j], nodes[k]); 59 } 60 } 61 } 62 63 boolean[][] expected = new boolean[triples.length][triples.length]; 67 for (int i1=0; i1<nodes.length; i1++) { 68 for (int j1=0; j1<nodes.length; j1++) { 69 for (int k1=0; k1<nodes.length; k1++) { 70 for (int i2=0; i2<nodes.length; i2++) { 71 for (int j2=0; j2<nodes.length; j2++) { 72 for (int k2=0; k2<nodes.length; k2++) { 73 expected[i1*nodes.length*nodes.length + 74 j1*nodes.length + 75 k1] 76 [i2*nodes.length*nodes.length + 77 j2*nodes.length + 78 k2] = 79 nodes[i1].equals(nodes[i2]) && 80 nodes[j1].equals(nodes[j2]) && 81 nodes[k1].equals(nodes[k2]); 82 } 83 } 84 } 85 } 86 } 87 } 88 89 assertEquals("triple, null", triples[0].equals(null), false); 90 assertDiffer("triple, string", triples[0], "string"); 91 92 for (int i=0; i<triples.length; i++) { 94 for (int j=0; j<triples.length; j++) { 95 if (expected[i][j]) { 96 assertEquals("triples " + i + ", " + j , 97 triples[i], triples[j]); 98 } else { 99 assertDiffer("triples" + i + ", " + j, 100 triples[i], triples[j]); 101 } 102 } 103 } 104 105 } finally { 106 Node.cache(true); 107 } 108 } 109 110 public void testTripleCreate() 111 { 112 Node S = Node.create( "s" ), P = Node.create( "p" ), O = Node.create( "o" ); 113 assertEquals( new Triple( S, P, O ), Triple.create( S, P, O ) ); 114 } 115 116 public void testTripleCreateFromString() 117 { 118 Node S = Node.create( "a" ), P = Node.create( "_P" ), O = Node.create( "?c" ); 119 assertEquals( new Triple( S, P, O ), Triple.create( "a _P ?c") ); 120 } 121 122 126 public void testTriplePrefixes() 127 { 128 Node S = Node.create( "rdf:alpha" ), P = Node.create( "dc:creator" ); 129 Node O = Node.create( "spoo:notmapped" ); 130 Triple t = Triple.create( "rdf:alpha dc:creator spoo:notmapped" ); 131 assertEquals( new Triple( S, P, O ), t ); 132 } 133 134 public void testTripleCreationMapped() 135 { 136 PrefixMapping pm = PrefixMapping.Factory.create() 137 .setNsPrefix( "a", "ftp://foo/" ) 138 .setNsPrefix( "b", "http://spoo/" ) 139 ; 140 Triple wanted = Triple.create( "ftp://foo/x http://spoo/y c:z" ); 141 Triple got = Triple.create( pm, "a:x b:y c:z" ); 142 assertEquals( wanted, got ); 143 } 144 145 public void testPlainTripleMatches() 146 { 147 testMatches( "S P O" ); 148 testMatches( "_S _P _O" ); 149 testMatches( "1 2 3" ); 150 } 151 152 public void testAnyTripleMatches() 153 { 154 testMatches( "?? P O", "Z P O" ); 155 testMatches( "S ?? O", "S Q O" ); 156 testMatches( "S P ??", "S P oh" ); 157 testMatches( "?? ?? ??", "X Y Z" ); 158 testMatches( "?? ?? ??", "X Y 1" ); 159 testMatches( "?? ?? ??", "_X Y Z" ); 160 testMatches( "?? ?? ??", "X _Y Z" ); 161 } 162 163 private void testMatches( String triple ) 164 { testMatches( triple, triple ); } 165 166 private void testMatches( String pattern, String triple ) 167 { assertTrue( Triple.create( pattern ).matches( Triple.create( triple ) ) ); } 168 169 public void testPlainTripleDoesntMatch() 170 { 171 testMatchFails( "S P O", "Z P O" ); 172 testMatchFails( "S P O", "S Q O" ); 173 testMatchFails( "S P O", "S P oh" ); 174 } 175 176 public void testAnyTripleDoesntMatch() 177 { 178 testMatchFails( "?? P O", "S P oh" ); 179 testMatchFails( "S ?? O", "Z R O" ); 180 testMatchFails( "S P ??", "Z P oh" ); 181 } 182 183 public void testMatchFails( String pattern, String triple ) 184 { assertFalse( Triple.create( pattern ).matches( Triple.create( triple ) ) ); } 185 186 public void testMatchesNodes() 187 { 188 assertTrue( Triple.create( "S P O" ).matches( node("S" ), node( "P" ), node( "O" ) ) ); 189 assertTrue( Triple.create( "?? P O" ).matches( node("Z" ), node( "P" ), node( "O" ) ) ); 190 assertTrue( Triple.create( "S ?? O" ).matches( node("S" ), node( "Q" ), node( "O" ) ) ); 191 assertTrue( Triple.create( "S P ??" ).matches( node("S" ), node( "P" ), node( "I" ) ) ); 192 193 assertFalse( Triple.create( "S P O" ).matches( node("Z" ), node( "P" ), node( "O" ) ) ); 194 assertFalse( Triple.create( "S P O" ).matches( node("S" ), node( "Q" ), node( "O" ) ) ); 195 assertFalse( Triple.create( "S P O" ).matches( node("Z" ), node( "P" ), node( "I" ) ) ); 196 } 197 198 public void testElementMatches() 199 { 200 assertTrue( Triple.create( "S P O" ).subjectMatches( node( "S" ) ) ); 201 assertTrue( Triple.create( "S P O" ).predicateMatches( node( "P" ) ) ); 202 assertTrue( Triple.create( "S P O" ).objectMatches( node( "O" ) ) ); 203 204 assertFalse( Triple.create( "S P O" ).subjectMatches( node( "Z" ) ) ); 205 assertFalse( Triple.create( "S P O" ).predicateMatches( node( "Q" ) ) ); 206 assertFalse( Triple.create( "S P O" ).objectMatches( node( "I" ) ) ); 207 208 assertTrue( Triple.create( "?? P O" ).subjectMatches( node( "SUB" ) ) ); 209 assertTrue( Triple.create( "S ?? O" ).predicateMatches( node( "PRED" ) ) ); 210 assertTrue( Triple.create( "S P ??" ).objectMatches( node( "OBJ" ) ) ); 211 } 212 213 public void testConcrete() 214 { 215 assertTrue( Triple.create( "S P O" ).isConcrete() ); 216 assertTrue( Triple.create( "S P 11").isConcrete() ); 217 assertTrue( Triple.create( "S P _X").isConcrete() ); 218 assertTrue( Triple.create( "S _P 11").isConcrete() ); 219 assertTrue( Triple.create( "_S _P _O").isConcrete() ); 220 assertTrue( Triple.create( "10 11 12").isConcrete() ); 221 assertTrue( Triple.create( "S P 11").isConcrete() ); 222 assertFalse( Triple.create( "?? P 11").isConcrete() ); 223 assertFalse( Triple.create( "S ?? 11").isConcrete() ); 224 assertFalse( Triple.create( "S P ??").isConcrete() ); 225 assertFalse( Triple.create( "?S P 11").isConcrete() ); 226 assertFalse( Triple.create( "S ?P 11").isConcrete() ); 227 assertFalse( Triple.create( "S P ?O").isConcrete() ); 228 } 229 230 234 public void testTripleToStringOrdering() 235 { 236 Triple t1 = Triple.create( "subject predicate object" ); 237 assertTrue( "subject must be present", t1.toString().indexOf( "subject" ) >= 0 ); 238 assertTrue( "subject must preceed predicate", t1.toString().indexOf( "subject" ) < t1.toString().indexOf( "predicate" ) ); 239 assertTrue( "predicate must preceed object", t1.toString().indexOf( "predicate" ) < t1.toString().indexOf( "object" ) ); 240 } 241 242 public void testTripleToStringQuoting() 243 { 244 Triple t1 = Triple.create( "subject predicate 'object'" ); 245 assertTrue( t1.toString().indexOf( "\"object\"") > 0 ); 246 } 247 248 public void testTripleToStringWithPrefixing() 249 { 250 PrefixMapping pm = PrefixMapping.Factory.create(); 251 pm.setNsPrefix( "spoo", "eg://domain.dom/spoo#" ); 252 Triple t1 = Triple.create( "eg://domain.dom/spoo#a b c" ); 253 assertEquals( "spoo:a @eh:/b eh:/c", t1.toString( pm ) ); 254 } 255 256 } 257 286 | Popular Tags |