001: /*
002: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package com.hp.hpl.jena.n3.test;
008:
009: import junit.framework.TestCase;
010: import junit.framework.TestSuite;
011:
012: import com.hp.hpl.jena.n3.IRIResolver;
013: import com.hp.hpl.jena.n3.JenaURIException;
014:
015: /** com.hp.hpl.jena.query.util.test.TestCaseURI
016: *
017: * @author Andy Seaborne
018: * @version $Id: TestResolver.java,v 1.6 2008/01/02 12:06:53 andy_seaborne Exp $
019: */
020:
021: public class TestResolver extends TestCase {
022: public static TestSuite suite() {
023: TestSuite ts = new TestSuite(TestResolver.class);
024: ts.setName("TestURI");
025: return ts;
026: }
027:
028: public void testBase1() {
029: IRIResolver resolver = new IRIResolver();
030: assertNotNull(resolver.getBaseIRI());
031: String base = resolver.getBaseIRI();
032: assertTrue(base.indexOf(':') > 0);
033: }
034:
035: public void testBase2() {
036: IRIResolver resolver = new IRIResolver("x");
037: assertNotNull(resolver.getBaseIRI());
038: // Active when IRI library integrated - currently the resolver takes a raw base string.
039: // String base = resolver.getBaseIRI() ;
040: // assertTrue(base.indexOf(':') > 0 ) ;
041: }
042:
043: public void testBase3() {
044: String b = IRIResolver.resolveGlobal("x");
045: IRIResolver resolver = new IRIResolver(b);
046: assertNotNull(resolver.getBaseIRI());
047: String base = resolver.getBaseIRI();
048: assertTrue(base.indexOf(':') > 0);
049: }
050:
051: public void testBadBase1() {
052: execException("%G", JenaURIException.class);
053: }
054:
055: public void testBadBase2() {
056: execException("/%G", JenaURIException.class);
057: }
058:
059: public void testBadBase3() {
060: execException("file:/%/", JenaURIException.class);
061: }
062:
063: public void testBadBase4() {
064: execException("http://example.org/%", JenaURIException.class);
065: }
066:
067: public void testBadChoice1() {
068: chooseException("%G", JenaURIException.class);
069: }
070:
071: public void testBadChoice2() {
072: chooseException("/%G", JenaURIException.class);
073: }
074:
075: public void testBadChoice3() {
076: chooseException("file:/%/", JenaURIException.class);
077: }
078:
079: public void testChoice1() {
080: choose("file:a");
081: }
082:
083: public void testChoice2() {
084: choose("file:a");
085: }
086:
087: // ---- Basic
088:
089: public void testURI_1() {
090: execTest("", "http://example.org/", "http://example.org/");
091: }
092:
093: public void testURI_2() {
094: execTest("", "http://example.org/xyz_2007",
095: "http://example.org/xyz_2007");
096: }
097:
098: public void testURI_3() {
099: execTest("", "http://example.org/xyz 2007",
100: "http://example.org/xyz 2007");
101: }
102:
103: public void testURI_4() {
104: execTest("", "http://example.org/xyz__2007",
105: "http://example.org/xyz__2007");
106: }
107:
108: public void testURI_5() {
109: execTest("", "http://example.org/xyz__abc",
110: "http://example.org/xyz__abc");
111: }
112:
113: // ---- Relative URIs
114:
115: public void testURI_relX_1() {
116: execTest("x", "http://example.org/ns", "http://example.org/x");
117: }
118:
119: public void testURI_relX_2() {
120: execTest("x", "http://example.org/", "http://example.org/x");
121: }
122:
123: public void testURI2_relHashX_1() {
124: execTest("#x", "http://example.org/ns",
125: "http://example.org/ns#x");
126: }
127:
128: public void testURI2_relHashX_2() {
129: execTest("#x", "http://example.org/ns/",
130: "http://example.org/ns/#x");
131: }
132:
133: public void testURI_blank_1() {
134: execTest("", "http://example.org/ns", "http://example.org/ns");
135: }
136:
137: public void testURI_blank_2() {
138: execTest("", "http://example.org/ns/", "http://example.org/ns/");
139: }
140:
141: public void testURI_hash_1() {
142: execTest("#", "http://example.org/ns", "http://example.org/ns#");
143: }
144:
145: public void testURI_hash_2() {
146: execTest("#", "http://example.org/ns/",
147: "http://example.org/ns/#");
148: }
149:
150: public void testBaseHash_1() {
151: execTest("x", "http://example.org/ns#", "http://example.org/x");
152: }
153:
154: public void testBaseHash_2() {
155: execTest("x", "http://example.org#", "http://example.org/x");
156: }
157:
158: public void testBaseHash_3() {
159: execTest("#", "base:x", "base:x#");
160: }
161:
162: // // Java5: exception
163: // // Java6 & GNUclasspath: correctly get "base:#"
164: // public void testBaseHash_4() { execTest("#", "base:", "base:#") ; }
165:
166: public void testScheme_1() {
167: execTest("x", "base:", "base:x");
168: }
169:
170: public void testScheme_2() {
171: execTest("/x", "base:", "base:/x");
172: }
173:
174: public void testScheme_3() {
175: execTestMatch("x", "file:", "^file:///.*/x$");
176: }
177:
178: public void testScheme_4() {
179: execTestMatch("file:x", null, "^file:///.*/x$");
180: }
181:
182: // public void testURI_file_1() { execTestMatch("file:x", "http://example.org/ns", "^file:///.*/x$") ; }
183:
184: public void testURI_file_2() {
185: execTest("x", "file:///A/B/C", "file:///A/B/x");
186: }
187:
188: public void testURI_file_3() {
189: execTest("x", "file:///A/B/", "file:///A/B/x");
190: }
191:
192: // ---- Absolute URIs are left alone
193:
194: public void testURI_abs_1() {
195: execTest("http://host/x", "http://example.org/ns",
196: "http://host/x");
197: }
198:
199: public void testURI_abs_2() {
200: execTest("file:///x", "http://example.org/ns", "file:///x");
201: }
202:
203: public void testURI_abs_3() {
204: execTest("tag:foo", "http://example.org/ns", "tag:foo");
205: }
206:
207: public void testURI_abs_4() {
208: execTest("tag:/foo", "http://example.org/ns", "tag:/foo");
209: }
210:
211: public void testURI_abs_5() {
212: execTest("tag:/foo/", "http://example.org/ns", "tag:/foo/");
213: }
214:
215: public void testURI_abs_6() {
216: execTest("scheme99:/foo/", "http://example.org/ns",
217: "scheme99:/foo/");
218: }
219:
220: // Null base
221:
222: public void testURI_nullBase_1() {
223: execTest("scheme99:/foo/", null, "scheme99:/foo/");
224: }
225:
226: // resolved against current directory
227: public void testURI_nullBase_2() {
228: execTestMatch("foo", null, ".*/foo");
229: }
230:
231: // ---- Hierarchical URIs
232:
233: public void testHierURI_1() {
234: execTest("../foo", "file:///dir/file", "file:///foo");
235: }
236:
237: public void testHierURI_2() {
238: execTest("../foo", "http://host/dir/file.html",
239: "http://host/foo");
240: }
241:
242: public void testHierURI_3() {
243: execTest("../foo", "http://host/dir/", "http://host/foo");
244: }
245:
246: public void testHierURI_4() {
247: execTest("../foo", "http://host/", "http://host/foo");
248: }
249:
250: public void testHierURI_5() {
251: execTest("../foo", "http://host/xyz", "http://host/foo");
252: }
253:
254: public void testHierURI_6() {
255: execTest(".", "http://host/xyz", "http://host/");
256: }
257:
258: public void testHierURI_7() {
259: execTest(".", "http://host/xyz/", "http://host/xyz/");
260: }
261:
262: public void testHierURI_8() {
263: execTest(".", "http://host/", "http://host/");
264: }
265:
266: public void testHierURI_9() {
267: execTest(".", "file:///dir/file", "file:///dir/");
268: }
269:
270: // ---- File URIs
271:
272: public void testFileURI_1() {
273: execFileTest("file:///foo", "file:///foo");
274: }
275:
276: public void testFileURI_2() {
277: execFileTest("file://foo", "file://foo");
278: }
279:
280: public void testFileURI_3() {
281: execFileTest("file:/foo", "file:///foo");
282: }
283:
284: // Bad.
285: public void testBad_1() {
286: execException("%G", "http://example.org/",
287: JenaURIException.class);
288: }
289:
290: public void testBad_2() {
291: execException("foo", "http://example.org/%HH",
292: JenaURIException.class);
293: }
294:
295: public void testBad_3() {
296: execException("bar", "http://example.org/%3",
297: JenaURIException.class);
298: }
299:
300: public void testBaseEmpty() {
301: execTestMatch("x", "", "^file:///.*/x$");
302: }
303:
304: // Resolved against current directory.
305: public void testBaseNull() {
306: execTestMatch("x", null, ".*/x");
307: }
308:
309: public void testRelBase_1() {
310: execTestMatch("x", "ns", ".*/x");
311: }
312:
313: public void testRelBase_2() {
314: execTestMatch("x", "/ns", ".*/x");
315: }
316:
317: // ---- Opaque
318:
319: public void testURI_opaque_1() {
320: execTest("#x", "tag:A", "tag:A#x");
321: }
322:
323: public void testURI_opaque_2() {
324: execTest("#x", "urn:x-jena:A", "urn:x-jena:A#x");
325: }
326:
327: // public void testURI_opaque_3() { execException("#x", "urn:x-jena:A", RelativeURIException.class) ; }
328:
329: // ---- Opaque file URLs
330:
331: // Should these be errors? Yes.
332: //public void testURI_file_4() { execTest("x", "file:A", "file:Ax") ; }
333: public void testURI_file_4() {
334: execTestMatch("x", "file:A", "^file:///.*/x");
335: }
336:
337: public void testURI_file_5() {
338: execTestMatch("#x", "file:A", "^file:///.*/A#x");
339: }
340:
341: //public void testURI_file_5() { execException("#x", "file:A", RelativeURIException.class) ; }
342:
343: //public void testURI_file_6() { execTest("foo", "file:///xyz abc/", "file:///xyz abc/foo" ) ; }
344:
345: public void testURI_file_7() {
346: execTestMatch("file:foo", "file:xyz", "^file:///.*foo$");
347: }
348:
349: public void testURI_file_8() {
350: execTestMatch("file:foo", "file:a b", "^file:///.*foo$");
351: }
352:
353: // File URLs - test aren't exact as the depend where they are run.
354:
355: public void testFileURI_rel_1() {
356: execTestFileRelURI("file:foo");
357: }
358:
359: public void testFileURI_rel_2() {
360: execTestFileRelURI("file:foo/bar");
361: }
362:
363: public void testFileURI_rel_3() {
364: execTestFileRelURI("file:foo/");
365: }
366:
367: public void testFileURI_rel_4() {
368: execTestFileRelURI("file:foo/bar/");
369: }
370:
371: private void execTest(String u, String base, String result) {
372: IRIResolver resolver = new IRIResolver(base);
373: String res = resolver.resolve(u);
374:
375: if (result == null) {
376: assertNull("(" + u + "," + base + ") => <null> :: Got: "
377: + res, res);
378: return;
379: }
380:
381: assertNotNull("(" + u + "," + base + ") => " + result
382: + " :: Got: <null>", res);
383: assertTrue("(" + u + "," + base + ") => " + result
384: + " :: Got: " + res, res.equals(result));
385: }
386:
387: // A test for resolved names that depend on where the tests are run.
388: private void execTestMatch(String u, String base,
389: String resultPattern) {
390: IRIResolver resolver = new IRIResolver(base);
391: String res = resolver.resolve(u);
392:
393: if (resultPattern == null) {
394: assertNull("(" + u + "," + base + ") => <null> :: Got: "
395: + res, res);
396: return;
397: }
398:
399: boolean r = res.matches(resultPattern);
400: assertTrue("Does not match: " + res + " -- " + resultPattern, r);
401: }
402:
403: private void execFileTest(String fn1, String fn2) {
404: String s = IRIResolver.resolveFileURL(fn1);
405: assertEquals(s, fn2);
406: }
407:
408: private void execTestFileRelURI(String fn) {
409: String relName = fn.substring("file:".length());
410: String s = IRIResolver.resolveFileURL(fn);
411: assertTrue("Lost relative name: (" + fn + "=>" + s + ")", s
412: .endsWith(relName));
413: assertTrue("Not absolute: (" + fn + "=>" + s + ")", s
414: .startsWith("file:///"));
415: }
416:
417: private void execException(String u, String base, Class ex) {
418: // 1.5.0-ism
419: //String s = ex.getSimpleName() ;
420: String s = ex.getName();
421:
422: // Tidy it up.
423: int i = s.lastIndexOf('.');
424: if (i >= 0)
425: s = s.substring(i + 1);
426:
427: try {
428: IRIResolver resolver = new IRIResolver(base);
429: String res = resolver.resolve(u);
430: if (res == null)
431: fail("(" + u + "," + base
432: + ") => <null> :: Expected exception: " + s);
433: else
434: fail("(" + u + "," + base + ") => " + res
435: + " :: Expected exception: " + s);
436: } catch (Exception ex2) {
437: // Shoudl test whether ex2 is a subclass of ex
438: assertEquals(ex, ex2.getClass());
439: }
440: }
441:
442: private void execException(String base, Class ex) {
443: // 1.5.0-ism
444: //String s = ex.getSimpleName() ;
445: String s = ex.getName();
446:
447: // Tidy it up.
448: int i = s.lastIndexOf('.');
449: if (i >= 0)
450: s = s.substring(i + 1);
451:
452: try {
453: new IRIResolver(base);
454: fail("(" + base + ") => OK :: Expected exception: " + s);
455: } catch (Exception ex2) {
456: // Shoudl test whether ex2 is a subclass of ex
457: assertEquals(ex, ex2.getClass());
458: }
459: }
460:
461: private void choose(String base) {
462:
463: IRIResolver.chooseBaseURI(base);
464:
465: }
466:
467: private void chooseException(String base, Class ex) {
468: // 1.5.0-ism
469: //String s = ex.getSimpleName() ;
470: String s = ex.getName();
471:
472: // Tidy it up.
473: int i = s.lastIndexOf('.');
474: if (i >= 0)
475: s = s.substring(i + 1);
476:
477: try {
478: IRIResolver.chooseBaseURI(base);
479: fail("(" + base + ") => OK :: Expected exception: " + s);
480: } catch (Exception ex2) {
481: // Shoudl test whether ex2 is a subclass of ex
482: assertEquals(ex, ex2.getClass());
483: }
484: }
485: // private void execTestGlobal(String u, String result)
486: // {
487: // String res = IRIResolver.resolveGlobal(u) ;
488: // if (result == null )
489: // {
490: // assertNull("("+u+") => <null> :: Got: "+res, res) ;
491: // return ;
492: // }
493: //
494: // assertNotNull("("+u+") => "+result+" :: Got: <null>", res) ;
495: // assertTrue("("+u+") => "+result+" :: Got: "+res, res.equals(result)) ;
496: // }
497: }
498:
499: /*
500: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
501: * All rights reserved.
502: *
503: * Redistribution and use in source and binary forms, with or without
504: * modification, are permitted provided that the following conditions
505: * are met:
506: * 1. Redistributions of source code must retain the above copyright
507: * notice, this list of conditions and the following disclaimer.
508: * 2. Redistributions in binary form must reproduce the above copyright
509: * notice, this list of conditions and the following disclaimer in the
510: * documentation and/or other materials provided with the distribution.
511: * 3. The name of the author may not be used to endorse or promote products
512: * derived from this software without specific prior written permission.
513: *
514: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
515: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
516: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
517: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
518: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
519: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
520: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
521: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
522: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
523: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
524: */
|