1 6 7 package com.hp.hpl.jena.shared.test; 8 9 import com.hp.hpl.jena.shared.*; 10 import com.hp.hpl.jena.shared.impl.PrefixMappingImpl; 11 import com.hp.hpl.jena.graph.test.*; 12 13 import java.util.*; 14 15 21 22 public abstract class AbstractTestPrefixMapping extends GraphTestBase 23 { 24 public AbstractTestPrefixMapping( String name ) 25 { super( name ); } 26 27 abstract protected PrefixMapping getMapping(); 28 29 static final String crispURI = "http://crisp.nosuch.net/"; 30 static final String ropeURI = "scheme:rope/string#"; 31 static final String butterURI = "ftp://ftp.nowhere.at.all/cream#"; 32 33 36 public void testEmptyPrefix() 37 { 38 PrefixMapping pm = getMapping(); 39 pm.setNsPrefix( "", crispURI ); 40 assertEquals( crispURI, pm.getNsPrefixURI( "" ) ); 41 } 42 43 static final String [] badNames = 44 { 45 "<hello>", 46 "foo:bar", 47 "with a space", 48 "-argument" 49 }; 50 51 54 public void testCheckNames() 55 { 56 PrefixMapping ns = getMapping(); 57 for (int i = 0; i < badNames.length; i += 1) 58 { 59 String bad = badNames[i]; 60 try 61 { 62 ns.setNsPrefix( bad, crispURI ); 63 fail( "'" + bad + "' is an illegal prefix and should be trapped" ); 64 } 65 catch (PrefixMapping.IllegalPrefixException e) { pass(); } 66 } 67 } 68 69 73 public void testPrefixMappingMapping() 74 { 75 String toast = "ftp://ftp.nowhere.not/"; 76 assertDiffer( "crisp and toast must differ", crispURI, toast ); 77 78 PrefixMapping ns = getMapping(); 79 assertEquals( "crisp should be unset", null, ns.getNsPrefixURI( "crisp" ) ); 80 assertEquals( "toast should be unset", null, ns.getNsPrefixURI( "toast" ) ); 81 assertEquals( "butter should be unset", null, ns.getNsPrefixURI( "butter" ) ); 82 83 ns.setNsPrefix( "crisp", crispURI ); 84 assertEquals( "crisp should be set", crispURI, ns.getNsPrefixURI( "crisp" ) ); 85 assertEquals( "toast should still be unset", null, ns.getNsPrefixURI( "toast" ) ); 86 assertEquals( "butter should still be unset", null, ns.getNsPrefixURI( "butter" ) ); 87 88 ns.setNsPrefix( "toast", toast ); 89 assertEquals( "crisp should be set", crispURI, ns.getNsPrefixURI( "crisp" ) ); 90 assertEquals( "toast should be set", toast, ns.getNsPrefixURI( "toast" ) ); 91 assertEquals( "butter should still be unset", null, ns.getNsPrefixURI( "butter" ) ); 92 } 93 94 98 public void testReversePrefixMapping() 99 { 100 PrefixMapping ns = getMapping(); 101 String uriA = "http://jena.hpl.hp.com/A#", uriB = "http://jena.hpl.hp.com/"; 102 String uriC = "http://jena.hpl.hp.com/Csharp/"; 103 String prefixA = "aa", prefixB = "bb"; 104 ns.setNsPrefix( prefixA, uriA ).setNsPrefix( prefixB, uriB ); 105 assertEquals( null, ns.getNsURIPrefix( uriC) ); 106 assertEquals( prefixA, ns.getNsURIPrefix( uriA ) ); 107 assertEquals( prefixB, ns.getNsURIPrefix( uriB ) ); 108 } 109 110 113 public void testPrefixMappingMap() 114 { 115 PrefixMapping ns = getCrispyRope(); 116 Map map = ns.getNsPrefixMap(); 117 assertEquals( "map should have two elements", 2, map.size() ); 118 assertEquals( crispURI, map.get( "crisp" ) ); 119 assertEquals( "scheme:rope/string#", map.get( "rope" ) ); 120 } 121 122 126 public void testPrefixMappingSecret() 127 { 128 PrefixMapping ns = getCrispyRope(); 129 Map map = ns.getNsPrefixMap(); 130 131 map.put( "crisp", "with/onions" ); 132 map.put( "sandwich", "with/cheese" ); 133 assertEquals( crispURI, ns.getNsPrefixURI( "crisp" ) ); 134 assertEquals( ropeURI, ns.getNsPrefixURI( "rope" ) ); 135 assertEquals( null, ns.getNsPrefixURI( "sandwich" ) ); 136 } 137 138 private PrefixMapping getCrispyRope() 139 { 140 PrefixMapping ns = getMapping(); 141 ns.setNsPrefix( "crisp", crispURI); 142 ns.setNsPrefix( "rope", ropeURI ); 143 return ns; 144 } 145 146 150 static final String [] dontChange = 151 { 152 "", 153 "http://www.somedomain.something/whatever#", 154 "crispy:cabbage", 155 "cris:isOnInfiniteEarths", 156 "rop:tangled/web", 157 "roped:abseiling" 158 }; 159 160 165 static final String [][] expansions = 166 { 167 { "crisp:pathPart", crispURI + "pathPart" }, 168 { "rope:partPath", ropeURI + "partPath" }, 169 { "crisp:path:part", crispURI + "path:part" }, 170 }; 171 172 public void testExpandPrefix() 173 { 174 PrefixMapping ns = getMapping(); 175 ns.setNsPrefix( "crisp", crispURI ); 176 ns.setNsPrefix( "rope", ropeURI ); 177 178 for (int i = 0; i < dontChange.length; i += 1) 179 assertEquals 180 ( 181 "should be unchanged", 182 dontChange[i], 183 ns.expandPrefix( dontChange[i] ) 184 ); 185 186 for (int i = 0; i < expansions.length; i += 1) 187 assertEquals 188 ( 189 "should expand correctly", 190 expansions[i][1], 191 ns.expandPrefix( expansions[i][0] ) 192 ); 193 } 194 195 public void testUseEasyPrefix() 196 { 197 testUseEasyPrefix( "prefix mapping impl", getMapping() ); 198 testShortForm( "prefix mapping impl", getMapping() ); 199 } 200 201 public static void testUseEasyPrefix( String title, PrefixMapping ns ) 202 { 203 testUsePrefix( title, ns ); 204 testShortForm( title, ns ); 205 } 206 207 public static void testUsePrefix( String title, PrefixMapping ns ) 208 { 209 ns.setNsPrefix( "crisp", crispURI ); 210 ns.setNsPrefix( "butter", butterURI ); 211 assertEquals( title, "", ns.usePrefix( "" ) ); 212 assertEquals( title, ropeURI, ns.usePrefix( ropeURI ) ); 213 assertEquals( title, "crisp:tail", ns.usePrefix( crispURI + "tail" ) ); 214 assertEquals( title, "butter:here:we:are", ns.usePrefix( butterURI + "here:we:are" ) ); 215 } 216 217 public static void testShortForm( String title, PrefixMapping ns ) 218 { 219 ns.setNsPrefix( "crisp", crispURI ); 220 ns.setNsPrefix( "butter", butterURI ); 221 assertEquals( title, "", ns.shortForm( "" ) ); 222 assertEquals( title, ropeURI, ns.shortForm( ropeURI ) ); 223 assertEquals( title, "crisp:tail", ns.shortForm( crispURI + "tail" ) ); 224 assertEquals( title, "butter:here:we:are", ns.shortForm( butterURI + "here:we:are" ) ); 225 } 226 227 public void testEasyQName() 228 { 229 PrefixMapping ns = getMapping(); 230 String alphaURI = "http://seasonal.song/preamble/"; 231 ns.setNsPrefix( "alpha", alphaURI ); 232 assertEquals( "alpha:rowboat", ns.qnameFor( alphaURI + "rowboat" ) ); 233 } 234 235 public void testNoQNameNoPrefix() 236 { 237 PrefixMapping ns = getMapping(); 238 String alphaURI = "http://seasonal.song/preamble/"; 239 ns.setNsPrefix( "alpha", alphaURI ); 240 assertEquals( null, ns.qnameFor( "eg:rowboat" ) ); 241 } 242 243 public void testNoQNameBadLocal() 244 { 245 PrefixMapping ns = getMapping(); 246 String alphaURI = "http://seasonal.song/preamble/"; 247 ns.setNsPrefix( "alpha", alphaURI ); 248 assertEquals( null, ns.qnameFor( alphaURI + "12345" ) ); 249 } 250 251 255 public void testQnameFromEmail() 256 { 257 String uri = "http://some.long.uri/for/a/namespace#"; 258 PrefixMapping ns = getMapping(); 259 ns.setNsPrefix( "x", uri ); 260 assertEquals( null, ns.qnameFor( uri ) ); 261 assertEquals( null, ns.qnameFor( uri + "non/fiction" ) ); 262 } 263 264 265 269 public void testAddOtherPrefixMapping() 270 { 271 PrefixMapping a = getMapping(); 272 PrefixMapping b = getMapping(); 273 assertFalse( "must have two diffferent maps", a == b ); 274 a.setNsPrefix( "crisp", crispURI ); 275 a.setNsPrefix( "rope", ropeURI ); 276 b.setNsPrefix( "butter", butterURI ); 277 assertEquals( null, b.getNsPrefixURI( "crisp") ); 278 assertEquals( null, b.getNsPrefixURI( "rope") ); 279 b.setNsPrefixes( a ); 280 checkContainsMapping( b ); 281 } 282 283 private void checkContainsMapping( PrefixMapping b ) 284 { 285 assertEquals( crispURI, b.getNsPrefixURI( "crisp") ); 286 assertEquals( ropeURI, b.getNsPrefixURI( "rope") ); 287 assertEquals( butterURI, b.getNsPrefixURI( "butter") ); 288 } 289 290 294 public void testAddMap() 295 { 296 PrefixMapping b = getMapping(); 297 Map map = new HashMap(); 298 map.put( "crisp", crispURI ); 299 map.put( "rope", ropeURI ); 300 b.setNsPrefix( "butter", butterURI ); 301 b.setNsPrefixes( map ); 302 checkContainsMapping( b ); 303 } 304 305 public void testAddDefaultMap() 306 { 307 PrefixMapping pm = getMapping(); 308 PrefixMapping root = new PrefixMappingImpl(); 309 pm.setNsPrefix( "a", "aPrefix:" ); 310 pm.setNsPrefix( "b", "bPrefix:" ); 311 root.setNsPrefix( "a", "pootle:" ); 312 root.setNsPrefix( "z", "bPrefix:" ); 313 root.setNsPrefix( "c", "cootle:" ); 314 assertSame( pm, pm.withDefaultMappings( root ) ); 315 assertEquals( "aPrefix:", pm.getNsPrefixURI( "a" ) ); 316 assertEquals( null, pm.getNsPrefixURI( "z" ) ); 317 assertEquals( "bPrefix:", pm.getNsPrefixURI( "b" ) ); 318 assertEquals( "cootle:", pm.getNsPrefixURI( "c" ) ); 319 } 320 321 322 public void testSecondPrefixRetainsExistingMap() 323 { 324 PrefixMapping A = getMapping(); 325 A.setNsPrefix( "a", crispURI ); 326 A.setNsPrefix( "b", crispURI ); 327 assertEquals( crispURI, A.getNsPrefixURI( "a" ) ); 328 assertEquals( crispURI, A.getNsPrefixURI( "b" ) ); 329 } 330 331 public void testSecondPrefixReplacesReverseMap() 332 { 333 PrefixMapping A = getMapping(); 334 A.setNsPrefix( "a", crispURI ); 335 A.setNsPrefix( "b", crispURI ); 336 assertEquals( "b", A.getNsURIPrefix( crispURI ) ); 337 } 338 339 public void testSecondPrefixDeletedUncoversPreviousMap() 340 { 341 PrefixMapping A = getMapping(); 342 A.setNsPrefix( "x", crispURI ); 343 A.setNsPrefix( "y", crispURI ); 344 A.removeNsPrefix( "y" ); 345 assertEquals( "x", A.getNsURIPrefix( crispURI ) ); 346 } 347 348 351 public void testEmptyDoesNotWipeURI() 352 { 353 PrefixMapping pm = getMapping(); 354 pm.setNsPrefix( "frodo", ropeURI ); 355 pm.setNsPrefix( "", ropeURI ); 356 assertEquals( ropeURI, pm.getNsPrefixURI( "frodo" ) ); 357 } 358 359 363 public void testSameURIKeepsDefault() 364 { 365 PrefixMapping A = getMapping(); 366 A.setNsPrefix( "", crispURI ); 367 A.setNsPrefix( "crisp", crispURI ); 368 assertEquals( crispURI, A.getNsPrefixURI( "" ) ); 369 } 370 371 public void testReturnsSelf() 372 { 373 PrefixMapping A = getMapping(); 374 assertSame( A, A.setNsPrefix( "crisp", crispURI ) ); 375 assertSame( A, A.setNsPrefixes( A ) ); 376 assertSame( A, A.setNsPrefixes( new HashMap() ) ); 377 assertSame( A, A.removeNsPrefix( "rhubarb" ) ); 378 } 379 380 public void testRemovePrefix() 381 { 382 String hURI = "http://test.remove.prefixes/prefix#"; 383 String bURI = "http://other.test.remove.prefixes/prefix#"; 384 PrefixMapping A = getMapping(); 385 A.setNsPrefix( "hr", hURI ); 386 A.setNsPrefix( "br", bURI ); 387 A.removeNsPrefix( "hr" ); 388 assertEquals( null, A.getNsPrefixURI( "hr" ) ); 389 assertEquals( bURI, A.getNsPrefixURI( "br" ) ); 390 } 391 392 public void testAllowNastyNamespace() 393 { getMapping().setNsPrefix( "abc", "def" ); 395 } 396 397 public void testLock() 398 { 399 PrefixMapping A = getMapping(); 400 assertSame( A, A.lock() ); 401 402 try { A.setNsPrefix( "crisp", crispURI ); fail( "mapping should be frozen" ); } 403 catch (PrefixMapping.JenaLockedException e) { pass(); } 404 405 try { A.setNsPrefixes( A ); fail( "mapping should be frozen" ); } 406 catch (PrefixMapping.JenaLockedException e) { pass(); } 407 408 try { A.setNsPrefixes( new HashMap() ); fail( "mapping should be frozen" ); } 409 catch (PrefixMapping.JenaLockedException e) { pass(); } 410 411 try { A.removeNsPrefix( "toast" ); fail( "mapping should be frozen" ); } 412 catch (PrefixMapping.JenaLockedException e) { pass(); } 413 } 414 } 415 416 | Popular Tags |