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.util.test;
008:
009: import java.util.Iterator;
010: import junit.framework.*;
011: import org.apache.commons.logging.*;
012:
013: import com.hp.hpl.jena.util.LocationMapper;
014: import com.hp.hpl.jena.util.FileManager;
015: import com.hp.hpl.jena.rdf.model.Model;
016:
017: /** TestLocationMapper
018: *
019: * @author Andy Seaborne
020: * @version $Id: TestLocationMapper.java,v 1.10 2008/01/02 12:08:35 andy_seaborne Exp $
021: */
022:
023: public class TestLocationMapper extends TestCase {
024: static Log log = LogFactory.getLog(TestLocationMapper.class);
025: static final String testingDir = TestFileManager.testingDir;
026: static final String filename1 = "file:test";
027: static final String notFilename = "zzzz";
028: static final String filename2 = "file:" + testingDir
029: + "/location-mapping-test-file";
030: static final String mapping = "location-mapping-test.n3;"
031: + testingDir + "/location-mapping-test.n3";
032:
033: public TestLocationMapper(String name) {
034: super (name);
035: }
036:
037: public static TestSuite suite() {
038: return new TestSuite(TestLocationMapper.class);
039: }
040:
041: public void testLocationMapper() {
042: LocationMapper locMap = new LocationMapper(mapping);
043: String alt = locMap.altMapping(filename1);
044: assertNotNull(alt);
045: assertEquals(alt, filename2);
046: }
047:
048: public void testLocationMapperMiss() {
049: LocationMapper locMap = new LocationMapper(mapping);
050: String alt = locMap.altMapping(notFilename);
051: assertNotNull(alt);
052: assertEquals(alt, notFilename);
053: }
054:
055: public void testLocationMapperURLtoFile() {
056: LocationMapper locMap = new LocationMapper(mapping);
057: String alt = locMap.altMapping("http://example.org/file");
058: assertNotNull(alt);
059: assertEquals(alt, "file:" + testingDir
060: + "/location-mapping-test-file");
061: }
062:
063: public void testLocationMapperFromModel() {
064: Model model = FileManager.get().loadModel(
065: testingDir + "/location-mapping-test.n3");
066: LocationMapper loc = new LocationMapper(model);
067:
068: // Light test that the two location mappers are "the same"
069: LocationMapper locMap = new LocationMapper(mapping);
070: for (Iterator iter = loc.listAltEntries(); iter.hasNext();) {
071: String e = (String) iter.next();
072: String v1 = locMap.getAltEntry(e);
073: String v2 = loc.getAltEntry(e);
074: assertEquals("Different entries", v1, v2);
075: }
076: for (Iterator iter = loc.listAltPrefixes(); iter.hasNext();) {
077: String e = (String) iter.next();
078: String v1 = locMap.getAltPrefix(e);
079: String v2 = loc.getAltPrefix(e);
080: assertEquals("Different entries", v1, v2);
081: }
082: }
083:
084: public void testLocationMapperClone1() {
085: LocationMapper locMap1 = new LocationMapper(mapping);
086: // See testLocationMapperURLtoFile
087: // String alt = locMap.altMapping("http://example.org/file") ;
088: // assertNotNull(alt) ;
089: // assertEquals(alt, "file:"+testingDir+"/location-mapping-test-file") ;
090:
091: LocationMapper locMap2 = new LocationMapper(locMap1);
092: // Remove from original
093: locMap1.removeAltEntry("http://example.org/file");
094: String alt = locMap2.altMapping("http://example.org/file");
095: assertNotNull(alt);
096: assertEquals(alt, "file:" + testingDir
097: + "/location-mapping-test-file");
098: }
099:
100: public void testLocationMapperClone2() {
101: LocationMapper locMap1 = new LocationMapper(mapping);
102: // See testLocationMapperURLtoFile
103: // String alt = locMap.altMapping("http://example.org/file") ;
104: // assertNotNull(alt) ;
105: // assertEquals(alt, "file:"+testingDir+"/location-mapping-test-file") ;
106:
107: LocationMapper locMap2 = new LocationMapper(locMap1);
108:
109: // Change this one
110: locMap2.addAltPrefix("http://example.org/OTHER", "file:OTHER");
111: {
112: String alt = locMap2
113: .altMapping("http://example.org/OTHER/f");
114: assertNotNull(alt);
115: assertEquals(alt, "file:OTHER/f");
116: }
117: // Not the other
118: {
119: String alt = locMap1
120: .altMapping("http://example.org/OTHER/f");
121: assertNotNull(alt);
122: // Did not change
123: assertEquals(alt, "http://example.org/OTHER/f");
124: }
125: }
126:
127: public void testLocationMapperEquals1() {
128: LocationMapper locMap1 = new LocationMapper(mapping);
129: LocationMapper locMap2 = new LocationMapper(mapping);
130: assertEquals(locMap1, locMap2);
131: assertEquals(locMap1.hashCode(), locMap2.hashCode());
132: }
133:
134: public void testLocationMapperEquals2() {
135: LocationMapper locMap1 = new LocationMapper(mapping);
136: LocationMapper locMap2 = new LocationMapper(mapping);
137: locMap2.addAltEntry("file:nowhere", "file:somewhere");
138: assertFalse(locMap1.equals(locMap2));
139: assertFalse(locMap2.equals(locMap1));
140: }
141:
142: public void testLocationMapperToModel1() {
143: LocationMapper locMap1 = new LocationMapper(mapping);
144: LocationMapper locMap2 = new LocationMapper(locMap1.toModel());
145: assertEquals(locMap1, locMap2);
146: assertEquals(locMap1.hashCode(), locMap2.hashCode());
147: }
148:
149: public void testLocationMapperToModel2() {
150: LocationMapper locMap1 = new LocationMapper(mapping);
151: LocationMapper locMap2 = new LocationMapper(mapping);
152: locMap1 = new LocationMapper(locMap1.toModel());
153: locMap2.addAltEntry("file:nowhere", "file:somewhere");
154: assertFalse(locMap1.equals(locMap2));
155: assertFalse(locMap2.equals(locMap1));
156: }
157: }
158:
159: /*
160: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
161: * All rights reserved.
162: *
163: * Redistribution and use in source and binary forms, with or without
164: * modification, are permitted provided that the following conditions
165: * are met:
166: * 1. Redistributions of source code must retain the above copyright
167: * notice, this list of conditions and the following disclaimer.
168: * 2. Redistributions in binary form must reproduce the above copyright
169: * notice, this list of conditions and the following disclaimer in the
170: * documentation and/or other materials provided with the distribution.
171: * 3. The name of the author may not be used to endorse or promote products
172: * derived from this software without specific prior written permission.
173: *
174: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
176: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
177: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
178: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
179: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
180: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
181: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
182: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
183: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
184: */
|