KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > test > TestNsPrefix


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestNsPrefix.java,v 1.8 2005/02/21 12:03:18 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.db.test;
8
9 /**
10  *
11  * This tests basic open/create operations on the modelRDB.
12  *
13  * To run, you must have a mySQL database operational on
14  * localhost with a database name of "test" and allow use
15  * by a user named "test" with an empty password.
16  *
17  * (based in part on model tests written earlier by bwm and kers)
18  *
19  * @author csayers
20  * @version 0.1
21 */

22
23 import com.hp.hpl.jena.db.*;
24 import com.hp.hpl.jena.shared.PrefixMapping;
25 import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;
26 import com.hp.hpl.jena.shared.test.AbstractTestPrefixMapping;
27
28 import junit.framework.*;
29
30 public class TestNsPrefix extends AbstractTestPrefixMapping {
31         
32     public TestNsPrefix( String JavaDoc name )
33         { super( name ); }
34     
35     public static TestSuite suite()
36         { return new TestSuite( TestNsPrefix.class ); }
37     
38     protected void setUp() throws java.lang.Exception JavaDoc {
39     }
40     
41     protected void tearDown() throws java.lang.Exception JavaDoc {
42     }
43         
44     protected PrefixMapping getMapping()
45         {
46         IDBConnection conn = TestConnection.makeAndCleanTestConnection();
47         return ModelRDB.createModel( conn );
48         }
49     
50     public void testSinglePrefix() throws java.lang.Exception JavaDoc {
51         String JavaDoc testPrefix = "testPrefix";
52         String JavaDoc testURI = "http://someTestURI#";
53         IDBConnection conn = TestConnection.makeAndCleanTestConnection();
54         ModelRDB m = ModelRDB.createModel(conn);
55         assertTrue( m.getNsPrefixMap().size() == 0 ); // brand new model should have no prefixes
56
m.setNsPrefix(testPrefix,testURI);
57         assertTrue( m.getNsPrefixMap().size() == 1);
58         assertTrue( m.getNsPrefixURI(testPrefix).compareTo(testURI) ==0 );
59         m.close();
60         conn.close();
61     }
62
63     public void testDuplicatePrefix() throws java.lang.Exception JavaDoc {
64         String JavaDoc testPrefix = "testPrefix";
65         String JavaDoc testURI = "http://someTestURI#";
66         IDBConnection conn = TestConnection.makeAndCleanTestConnection();
67         ModelRDB m = ModelRDB.createModel(conn);
68         assertTrue( m.getNsPrefixMap().size() == 0 ); // brand new model should have no prefixes
69
m.setNsPrefix(testPrefix,testURI);
70         m.setNsPrefix(testPrefix,testURI);
71         assertTrue( m.getNsPrefixMap().size() == 1);
72         assertTrue( m.getNsPrefixURI(testPrefix).compareTo(testURI) ==0 );
73         m.close();
74         conn.close();
75     }
76
77     public void testChangingPrefixMapping() throws java.lang.Exception JavaDoc {
78         String JavaDoc testPrefix = "testPrefix";
79         String JavaDoc testURI = "http://someTestURI#";
80         String JavaDoc someOtherTestURI = "http://someOtherTestURI#";
81         IDBConnection conn = TestConnection.makeAndCleanTestConnection();
82         ModelRDB m = ModelRDB.createModel(conn);
83         assertTrue( m.getNsPrefixMap().size() == 0 ); // brand new model should have no prefixes
84
m.setNsPrefix(testPrefix,testURI);
85         m.setNsPrefix(testPrefix,someOtherTestURI);
86         assertTrue( m.getNsPrefixMap().size() == 1);
87         assertTrue( m.getNsPrefixURI(testPrefix).compareTo(testURI) !=0 );
88         assertTrue( m.getNsPrefixURI(testPrefix).compareTo(someOtherTestURI) ==0 );
89         m.close();
90         conn.close();
91     }
92
93     public void testPersistenceOfPrefixes() throws java.lang.Exception JavaDoc {
94         String JavaDoc testPrefix = "testPrefix";
95         String JavaDoc testURI = "http://someTestURI#";
96         IDBConnection conn = TestConnection.makeAndCleanTestConnection();
97         ModelRDB m = ModelRDB.createModel(conn);
98         assertTrue( m.getNsPrefixMap().size() == 0 ); // brand new model should have no prefixes
99
m.setNsPrefix(testPrefix,testURI);
100         assertTrue( m.getNsPrefixMap().size() == 1);
101         assertTrue( m.getNsPrefixURI(testPrefix).compareTo(testURI) ==0 );
102         m.close();
103         
104         // Now create a different model and check there is no prefix
105
// and that removing it has no effect on the first model.
106
ModelRDB m2 = ModelRDB.createModel(conn,"myName");
107         assertTrue( m2.getNsPrefixMap().size() == 0);
108         m2.remove();
109         m2.close();
110         
111         // Now reopen the first Model and check the prefix was persisted
112
ModelRDB m3 = ModelRDB.open(conn);
113         assertTrue( m3.getNsPrefixMap().size() == 1);
114         assertTrue( m3.getNsPrefixURI(testPrefix).compareTo(testURI) ==0 );
115         m3.close();
116         
117         conn.close();
118     }
119     
120     public void testIdependenceOfPrefixes() throws java.lang.Exception JavaDoc {
121         String JavaDoc testPrefix1 = "testPrefix1";
122         String JavaDoc testURI1 = "http://someTestURI1#";
123         String JavaDoc testPrefix2 = "testPrefix2";
124         String JavaDoc testURI2 = "http://someTestURI2#";
125         String JavaDoc testPrefix3 = "testPrefix3";
126         String JavaDoc testURI3 = "http://someTestURI3#";
127         IDBConnection conn = TestConnection.makeAndCleanTestConnection();
128         
129         // Create a first model with a set of prefixes
130
ModelRDB m = ModelRDB.createModel(conn);
131         assertTrue( m.getNsPrefixMap().size() == 0 ); // brand new model should have no prefixes
132
m.setNsPrefix(testPrefix1,testURI1);
133         m.setNsPrefix(testPrefix2,testURI2);
134         assertTrue( m.getNsPrefixMap().size() == 2);
135         assertTrue( m.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
136         assertTrue( m.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
137         
138         // Create a second model with an overlapping set of prefixes
139
ModelRDB m2 = ModelRDB.createModel(conn,"secondGraph");
140         assertTrue( m2.getNsPrefixMap().size() == 0 ); // brand new model should have no prefixes
141
m2.setNsPrefix(testPrefix2,testURI2);
142         m2.setNsPrefix(testPrefix3,testURI3);
143         
144         // Verify second model has the correct contents
145
assertTrue( m2.getNsPrefixMap().size() == 2);
146         assertTrue( m2.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
147         assertTrue( m2.getNsPrefixURI(testPrefix3).compareTo(testURI3) ==0 );
148         
149         // Verify that first model was unchanged.
150
assertTrue( m.getNsPrefixMap().size() == 2);
151         assertTrue( m.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
152         assertTrue( m.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
153         
154         // Now remove the second model
155
m2.remove();
156         m2.close();
157         m2 = null;
158         
159         // Verify that first model was still unchanged.
160
assertTrue( m.getNsPrefixMap().size() == 2);
161         assertTrue( m.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
162         assertTrue( m.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
163         
164         // Now close and reopen the first Model and check the prefix was persisted
165
m.close();
166         m = null;
167         
168         ModelRDB m3 = ModelRDB.open(conn);
169         assertTrue( m3.getNsPrefixMap().size() == 2);
170         assertTrue( m3.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
171         assertTrue( m3.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
172         m3.close();
173         
174         conn.close();
175     }
176     
177     public void testPersistedChangedPrefixes() throws java.lang.Exception JavaDoc {
178         String JavaDoc testPrefix1 = "testPrefix1";
179         String JavaDoc testURI1 = "http://someTestURI/1#";
180         String JavaDoc testURI1b = "http://someTestURI/1b#";
181         String JavaDoc testPrefix2 = "testPrefix2";
182         String JavaDoc testURI2 = "http://someTestURI/2#";
183         String JavaDoc testPrefix3 = "testPrefix3";
184         String JavaDoc testURI3 = "http://someTestURI/3#";
185         IDBConnection conn = TestConnection.makeAndCleanTestConnection();
186         
187         ModelRDB m = ModelRDB.createModel(conn);
188         assertTrue( m.getNsPrefixMap().size() == 0 ); // brand new model should have no prefixes
189
m.setNsPrefix(testPrefix1,testURI1);
190         m.setNsPrefix(testPrefix2,testURI2);
191         assertTrue( m.getNsPrefixMap().size() == 2);
192         assertTrue( m.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
193         assertTrue( m.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
194         m.close();
195         m=null;
196         
197         // Now reopen the first Model and check the prefixes were persisted
198
ModelRDB m2 = ModelRDB.open(conn);
199         assertTrue( m2.getNsPrefixMap().size() == 2);
200         assertTrue( m2.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
201         assertTrue( m2.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
202         
203         // Now change one prefix and add a third
204
m2.setNsPrefix(testPrefix1,testURI1b);
205         m2.setNsPrefix(testPrefix3, testURI3);
206
207         assertTrue( m2.getNsPrefixMap().size() == 3);
208         assertTrue( m2.getNsPrefixURI(testPrefix1).compareTo(testURI1b) ==0 );
209         assertTrue( m2.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
210         assertTrue( m2.getNsPrefixURI(testPrefix3).compareTo(testURI3) ==0 );
211
212         m2.close();
213         m2 = null;
214         
215         // Now reopen again and check it's all still as expected.
216
ModelRDB m3 = ModelRDB.open(conn);
217         assertTrue( m3.getNsPrefixMap().size() == 3);
218         assertTrue( m3.getNsPrefixURI(testPrefix1).compareTo(testURI1b) ==0 );
219         assertTrue( m3.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
220         assertTrue( m3.getNsPrefixURI(testPrefix3).compareTo(testURI3) ==0 );
221         
222         m3.close();
223         conn.close();
224     }
225
226     public void testCopyPersistentPrefixMapping() throws java.lang.Exception JavaDoc {
227         String JavaDoc testPrefix1 = "testPrefix1";
228         String JavaDoc testPrefix2 = "testPrefix2";
229         String JavaDoc testURI1 = "http://someTestURI/1#";
230         String JavaDoc testURI2 = "http://someTestURI/2#";
231         IDBConnection conn = TestConnection.makeAndCleanTestConnection();
232         ModelRDB m = ModelRDB.createModel(conn);
233         assertTrue( m.getNsPrefixMap().size() == 0 ); // brand new model should have no prefixes
234
m.setNsPrefix(testPrefix1,testURI1);
235         m.setNsPrefix(testPrefix2,testURI2);
236         assertTrue( m.getNsPrefixMap().size() == 2);
237         assertTrue( m.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
238         assertTrue( m.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
239         
240         // Now create a second model, copy the prefix mapping and make sure it matches
241
ModelRDB m2 = ModelRDB.createModel(conn, "secondModel");
242         assertTrue( m2.getNsPrefixMap().size() == 0);
243         m2.setNsPrefixes(m.getNsPrefixMap());
244         assertTrue( m2.getNsPrefixMap().size() == 2);
245         assertTrue( m2.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
246         assertTrue( m2.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
247
248         m.close();
249         m2.close();
250         
251         conn.close();
252     }
253
254
255     public void testCopyMemoryPrefixMapping() throws java.lang.Exception JavaDoc {
256         String JavaDoc testPrefix1 = "testPrefix1";
257         String JavaDoc testPrefix2 = "testPrefix2";
258         String JavaDoc testURI1 = "http://someTestURI/1#";
259         String JavaDoc testURI2 = "http://someTestURI/2#";
260         
261         PrefixMapping myMap = new PrefixMappingImpl();
262         myMap.setNsPrefix(testPrefix1, testURI1);
263         myMap.setNsPrefix(testPrefix2, testURI2);
264
265         assertTrue( myMap.getNsPrefixMap().size() == 2);
266         assertTrue( myMap.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
267         assertTrue( myMap.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
268         
269         IDBConnection conn = TestConnection.makeAndCleanTestConnection();
270         ModelRDB m = ModelRDB.createModel(conn);
271         assertTrue( m.getNsPrefixMap().size() == 0 ); // brand new model should have no prefixes
272
m.setNsPrefixes(myMap);
273         assertTrue( m.getNsPrefixMap().size() == 2);
274         assertTrue( m.getNsPrefixURI(testPrefix1).compareTo(testURI1) ==0 );
275         assertTrue( m.getNsPrefixURI(testPrefix2).compareTo(testURI2) ==0 );
276         
277         m.close();
278         conn.close();
279     }
280
281 }
282         
283
284 /*
285     (c) Copyright 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
286     All rights reserved.
287
288     Redistribution and use in source and binary forms, with or without
289     modification, are permitted provided that the following conditions
290     are met:
291
292     1. Redistributions of source code must retain the above copyright
293        notice, this list of conditions and the following disclaimer.
294
295     2. Redistributions in binary form must reproduce the above copyright
296        notice, this list of conditions and the following disclaimer in the
297        documentation and/or other materials provided with the distribution.
298
299     3. The name of the author may not be used to endorse or promote products
300        derived from this software without specific prior written permission.
301
302     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
303     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
304     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
305     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
306     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
307     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
308     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
309     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
310     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
311     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
312 */

313
Popular Tags