001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestNsPrefix.java,v 1.12 2008/01/02 12:08:14 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.db.test;
008:
009: /**
010: *
011: * This tests basic open/create operations on the modelRDB.
012: *
013: * To run, you must have a mySQL database operational on
014: * localhost with a database name of "test" and allow use
015: * by a user named "test" with an empty password.
016: *
017: * (based in part on model tests written earlier by bwm and kers)
018: *
019: * @author csayers
020: * @version 0.1
021: */
022:
023: import com.hp.hpl.jena.db.*;
024: import com.hp.hpl.jena.rdf.model.test.ModelTestBase;
025: import com.hp.hpl.jena.shared.PrefixMapping;
026: import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;
027: import com.hp.hpl.jena.shared.test.AbstractTestPrefixMapping;
028:
029: import junit.framework.*;
030:
031: public class TestNsPrefix extends ModelTestBase {
032:
033: public TestNsPrefix(String name) {
034: super (name);
035: }
036:
037: public static TestSuite suite() {
038: return new TestSuite(TestNsPrefix.class);
039: }
040:
041: protected void setUp() throws java.lang.Exception {
042: }
043:
044: protected void tearDown() throws java.lang.Exception {
045: }
046:
047: protected PrefixMapping getMapping() {
048: IDBConnection conn = TestConnection
049: .makeAndCleanTestConnection();
050: return ModelRDB.createModel(conn);
051: }
052:
053: public void testSinglePrefix() throws java.lang.Exception {
054: String testPrefix = "testPrefix";
055: String testURI = "http://someTestURI#";
056: IDBConnection conn = TestConnection
057: .makeAndCleanTestConnection();
058: ModelRDB m = ModelRDB.createModel(conn);
059: assertEquals(0, m.getNsPrefixMap().size()); // brand new model should have no prefixes
060: m.setNsPrefix(testPrefix, testURI);
061: assertEquals(1, m.getNsPrefixMap().size());
062: assertEquals(testURI, m.getNsPrefixURI(testPrefix));
063: m.close();
064: conn.close();
065: }
066:
067: public void testDuplicatePrefix() throws java.lang.Exception {
068: String testPrefix = "testPrefix";
069: String testURI = "http://someTestURI#";
070: IDBConnection conn = TestConnection
071: .makeAndCleanTestConnection();
072: ModelRDB m = ModelRDB.createModel(conn);
073: assertEquals(0, m.getNsPrefixMap().size()); // brand new model should have no prefixes
074: m.setNsPrefix(testPrefix, testURI);
075: m.setNsPrefix(testPrefix, testURI);
076: assertEquals(1, m.getNsPrefixMap().size());
077: assertEquals(testURI, m.getNsPrefixURI(testPrefix));
078: m.close();
079: conn.close();
080: }
081:
082: public void testChangingPrefixMapping() throws java.lang.Exception {
083: String testPrefix = "testPrefix";
084: String testURI = "http://someTestURI#";
085: String someOtherTestURI = "http://someOtherTestURI#";
086: IDBConnection conn = TestConnection
087: .makeAndCleanTestConnection();
088: ModelRDB m = ModelRDB.createModel(conn);
089: assertEquals(0, m.getNsPrefixMap().size()); // brand new model should have no prefixes
090: m.setNsPrefix(testPrefix, testURI);
091: m.setNsPrefix(testPrefix, someOtherTestURI);
092: assertEquals(1, m.getNsPrefixMap().size());
093: assertDiffer(testURI, m.getNsPrefixURI(testPrefix));
094: assertEquals(someOtherTestURI, m.getNsPrefixURI(testPrefix));
095: m.close();
096: conn.close();
097: }
098:
099: public void testPersistenceOfPrefixes() throws java.lang.Exception {
100: String testPrefix = "testPrefix";
101: String testURI = "http://someTestURI#";
102: IDBConnection conn = TestConnection
103: .makeAndCleanTestConnection();
104: ModelRDB m = ModelRDB.createModel(conn);
105: assertEquals(0, m.getNsPrefixMap().size()); // brand new model should have no prefixes
106: m.setNsPrefix(testPrefix, testURI);
107: assertEquals(1, m.getNsPrefixMap().size());
108: assertEquals(testURI, m.getNsPrefixURI(testPrefix));
109: m.close();
110:
111: // Now create a different model and check there is no prefix
112: // and that removing it has no effect on the first model.
113: ModelRDB m2 = ModelRDB.createModel(conn, "myName");
114: assertEquals(0, m2.getNsPrefixMap().size());
115: m2.remove();
116: m2.close();
117:
118: // Now reopen the first Model and check the prefix was persisted
119: ModelRDB m3 = ModelRDB.open(conn);
120: assertEquals(1, m3.getNsPrefixMap().size());
121: assertEquals(testURI, m3.getNsPrefixURI(testPrefix));
122: m3.close();
123:
124: conn.close();
125: }
126:
127: public void testIdependenceOfPrefixes() throws java.lang.Exception {
128: String testPrefix1 = "testPrefix1";
129: String testURI1 = "http://someTestURI1#";
130: String testPrefix2 = "testPrefix2";
131: String testURI2 = "http://someTestURI2#";
132: String testPrefix3 = "testPrefix3";
133: String testURI3 = "http://someTestURI3#";
134: IDBConnection conn = TestConnection
135: .makeAndCleanTestConnection();
136:
137: // Create a first model with a set of prefixes
138: ModelRDB m = ModelRDB.createModel(conn);
139: assertEquals(0, m.getNsPrefixMap().size()); // brand new model should have no prefixes
140: m.setNsPrefix(testPrefix1, testURI1);
141: m.setNsPrefix(testPrefix2, testURI2);
142: assertEquals(2, m.getNsPrefixMap().size());
143: assertEquals(testURI1, m.getNsPrefixURI(testPrefix1));
144: assertEquals(testURI2, m.getNsPrefixURI(testPrefix2));
145:
146: // Create a second model with an overlapping set of prefixes
147: ModelRDB m2 = ModelRDB.createModel(conn, "secondGraph");
148: assertEquals(0, m2.getNsPrefixMap().size()); // brand new model should have no prefixes
149: m2.setNsPrefix(testPrefix2, testURI2);
150: m2.setNsPrefix(testPrefix3, testURI3);
151:
152: // Verify second model has the correct contents
153: assertEquals(2, m2.getNsPrefixMap().size());
154: assertEquals(testURI2, m2.getNsPrefixURI(testPrefix2));
155: assertEquals(testURI3, m2.getNsPrefixURI(testPrefix3));
156:
157: // Verify that first model was unchanged.
158: assertEquals(2, m.getNsPrefixMap().size());
159: assertEquals(testURI1, m.getNsPrefixURI(testPrefix1));
160: assertEquals(testURI2, m.getNsPrefixURI(testPrefix2));
161:
162: // Now remove the second model
163: m2.remove();
164: m2.close();
165: m2 = null;
166:
167: // Verify that first model was still unchanged.
168: assertEquals(2, m.getNsPrefixMap().size());
169: assertEquals(testURI1, m.getNsPrefixURI(testPrefix1));
170: assertEquals(testURI2, m.getNsPrefixURI(testPrefix2));
171:
172: // Now close and reopen the first Model and check the prefix was persisted
173: m.close();
174: m = null;
175:
176: ModelRDB m3 = ModelRDB.open(conn);
177: assertEquals(2, m3.getNsPrefixMap().size());
178: assertEquals(testURI1, m3.getNsPrefixURI(testPrefix1));
179: assertEquals(testURI2, m3.getNsPrefixURI(testPrefix2));
180: m3.close();
181:
182: conn.close();
183: }
184:
185: public void testPersistedChangedPrefixes()
186: throws java.lang.Exception {
187: String testPrefix1 = "testPrefix1";
188: String testURI1 = "http://someTestURI/1#";
189: String testURI1b = "http://someTestURI/1b#";
190: String testPrefix2 = "testPrefix2";
191: String testURI2 = "http://someTestURI/2#";
192: String testPrefix3 = "testPrefix3";
193: String testURI3 = "http://someTestURI/3#";
194: IDBConnection conn = TestConnection
195: .makeAndCleanTestConnection();
196:
197: ModelRDB m = ModelRDB.createModel(conn);
198: assertEquals(0, m.getNsPrefixMap().size()); // brand new model should have no prefixes
199: m.setNsPrefix(testPrefix1, testURI1);
200: m.setNsPrefix(testPrefix2, testURI2);
201: assertEquals(2, m.getNsPrefixMap().size());
202: assertEquals(testURI1, m.getNsPrefixURI(testPrefix1));
203: assertEquals(testURI2, m.getNsPrefixURI(testPrefix2));
204: m.close();
205: m = null;
206:
207: // Now reopen the first Model and check the prefixes were persisted
208: ModelRDB m2 = ModelRDB.open(conn);
209: assertEquals(2, m2.getNsPrefixMap().size());
210: assertEquals(testURI1, m2.getNsPrefixURI(testPrefix1));
211: assertEquals(testURI2, m2.getNsPrefixURI(testPrefix2));
212:
213: // Now change one prefix and add a third
214: m2.setNsPrefix(testPrefix1, testURI1b);
215: m2.setNsPrefix(testPrefix3, testURI3);
216:
217: assertEquals(3, m2.getNsPrefixMap().size());
218: assertEquals(testURI1b, m2.getNsPrefixURI(testPrefix1));
219: assertEquals(testURI2, m2.getNsPrefixURI(testPrefix2));
220: assertEquals(testURI3, m2.getNsPrefixURI(testPrefix3));
221:
222: m2.close();
223: m2 = null;
224:
225: // Now reopen again and check it's all still as expected.
226: ModelRDB m3 = ModelRDB.open(conn);
227: assertEquals(3, m3.getNsPrefixMap().size());
228: assertEquals(testURI1b, m3.getNsPrefixURI(testPrefix1));
229: assertEquals(testURI2, m3.getNsPrefixURI(testPrefix2));
230: assertEquals(testURI3, m3.getNsPrefixURI(testPrefix3));
231:
232: m3.close();
233: conn.close();
234: }
235:
236: public void testCopyPersistentPrefixMapping()
237: throws java.lang.Exception {
238: String testPrefix1 = "testPrefix1";
239: String testPrefix2 = "testPrefix2";
240: String testURI1 = "http://someTestURI/1#";
241: String testURI2 = "http://someTestURI/2#";
242: IDBConnection conn = TestConnection
243: .makeAndCleanTestConnection();
244: ModelRDB m = ModelRDB.createModel(conn);
245: assertEquals(0, m.getNsPrefixMap().size()); // brand new model should have no prefixes
246: m.setNsPrefix(testPrefix1, testURI1);
247: m.setNsPrefix(testPrefix2, testURI2);
248: assertEquals(2, m.getNsPrefixMap().size());
249: assertEquals(testURI1, m.getNsPrefixURI(testPrefix1));
250: assertEquals(testURI2, m.getNsPrefixURI(testPrefix2));
251:
252: // Now create a second model, copy the prefix mapping and make sure it matches
253: ModelRDB m2 = ModelRDB.createModel(conn, "secondModel");
254: assertEquals(0, m2.getNsPrefixMap().size());
255: m2.setNsPrefixes(m.getNsPrefixMap());
256: assertEquals(2, m2.getNsPrefixMap().size());
257: assertEquals(testURI1, m2.getNsPrefixURI(testPrefix1));
258: assertEquals(testURI2, m2.getNsPrefixURI(testPrefix2));
259:
260: m.close();
261: m2.close();
262:
263: conn.close();
264: }
265:
266: public void testCopyMemoryPrefixMapping()
267: throws java.lang.Exception {
268: String testPrefix1 = "testPrefix1";
269: String testPrefix2 = "testPrefix2";
270: String testURI1 = "http://someTestURI/1#";
271: String testURI2 = "http://someTestURI/2#";
272:
273: PrefixMapping myMap = new PrefixMappingImpl();
274: myMap.setNsPrefix(testPrefix1, testURI1);
275: myMap.setNsPrefix(testPrefix2, testURI2);
276:
277: assertEquals(2, myMap.getNsPrefixMap().size());
278: assertEquals(testURI1, myMap.getNsPrefixURI(testPrefix1));
279: assertEquals(testURI2, myMap.getNsPrefixURI(testPrefix2));
280:
281: IDBConnection conn = TestConnection
282: .makeAndCleanTestConnection();
283: ModelRDB m = ModelRDB.createModel(conn);
284: assertEquals(0, m.getNsPrefixMap().size()); // brand new model should have no prefixes
285: m.setNsPrefixes(myMap);
286: assertEquals(2, m.getNsPrefixMap().size());
287: assertEquals(testURI1, m.getNsPrefixURI(testPrefix1));
288: assertEquals(testURI2, m.getNsPrefixURI(testPrefix2));
289:
290: m.close();
291: conn.close();
292: }
293:
294: public void testSecondPrefixDoesNotRemoveSharedURI()
295: throws Exception {
296: IDBConnection conn = TestConnection
297: .makeAndCleanTestConnection();
298: ModelRDB m = ModelRDB.createModel(conn);
299: String someURI = "urn:x-hp:db-unit-testing:xxx";
300: m.setNsPrefix("p1", someURI);
301: m.setNsPrefix("p2", someURI);
302: m.close();
303: conn.close();
304: IDBConnection conn2 = TestConnection.makeTestConnection();
305: ModelRDB m2 = ModelRDB.open(conn2);
306: assertEquals(someURI, m2.getNsPrefixURI("p1"));
307: assertEquals(someURI, m2.getNsPrefixURI("p2"));
308: m2.close();
309: conn2.close();
310: }
311:
312: }
313:
314: /*
315: (c) Copyright 2002, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
316: All rights reserved.
317:
318: Redistribution and use in source and binary forms, with or without
319: modification, are permitted provided that the following conditions
320: are met:
321:
322: 1. Redistributions of source code must retain the above copyright
323: notice, this list of conditions and the following disclaimer.
324:
325: 2. Redistributions in binary form must reproduce the above copyright
326: notice, this list of conditions and the following disclaimer in the
327: documentation and/or other materials provided with the distribution.
328:
329: 3. The name of the author may not be used to endorse or promote products
330: derived from this software without specific prior written permission.
331:
332: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
333: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
334: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
335: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
336: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
337: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
338: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
339: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
340: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
341: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
342: */
|