This project has retired. For details please refer to its Attic page.
PrefixesTest xref
View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.rdf;
19  
20  import org.junit.Assert;
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  /**
30   * Reference Test class for {@link Prefixes}
31   */
32  public class PrefixesTest {
33  
34      private Prefixes p;
35  
36      @Before
37      public void setUp() {
38          p = new Prefixes();
39      }
40  
41      @Test
42      public void testEmptyPrefixes() {
43          Assert.assertTrue(p.isEmpty());
44          Assert.assertTrue(p.allPrefixes().isEmpty());
45      }
46  
47      @Test
48      public void testUndefinedPrefix() {
49          Assert.assertFalse(p.hasPrefix("ex"));
50          Assert.assertFalse(p.hasNamespaceIRI("ex"));
51          Assert.assertNull(p.getNamespaceIRIFor("ex"));
52      }
53  
54      @Test
55      public void testCannotAbbreviateUndefined() {
56          Assert.assertFalse(p.canAbbreviate("http://example.com/foo"));
57      }
58  
59      @Test
60      public void testCannotExpandUndefined() {
61          Assert.assertFalse(p.canExpand("ex:foo"));
62      }
63  
64      @Test
65      public void testAddPrefix() {
66          p.add("ex", "http://example.com/");
67          Assert.assertFalse(p.isEmpty());
68          Assert.assertEquals(Collections.singleton("ex"), p.allPrefixes());
69      }
70  
71      @Test
72      public void testCheckForDeclaredPrefix() {
73          p.add("ex", "http://example.com/");
74          Assert.assertTrue(p.hasPrefix("ex"));
75          Assert.assertTrue(p.hasNamespaceIRI("http://example.com/"));
76          Assert.assertEquals("http://example.com/", p.getNamespaceIRIFor("ex"));
77      }
78  
79      @Test
80      public void testCanExpandDeclaredPrefix() {
81          p.add("ex", "http://example.com/");
82          Assert.assertTrue(p.canExpand("ex:foo"));
83          Assert.assertTrue(p.canExpand("ex:"));
84          Assert.assertEquals(RDFUtils.iri("http://example.com/foo"), p.expand("ex:foo"));
85          Assert.assertEquals(RDFUtils.iri("http://example.com/"), p.expand("ex:"));
86      }
87  
88      @Test
89      public void testCanContractDeclaredNamespace() {
90          p.add("ex", "http://example.com/");
91          Assert.assertTrue(p.canAbbreviate("http://example.com/foo"));
92          Assert.assertTrue(p.canAbbreviate("http://example.com/"));
93          Assert.assertEquals("ex:foo", p.abbreviate("http://example.com/foo"));
94          Assert.assertEquals("ex:", p.abbreviate("http://example.com/"));
95      }
96  
97      @Test
98      public void testExpandOnlyAcceptsCURIEs() {
99          try {
100             p.expand("@");
101             Assert.fail("Should have thrown IllegalArgumentException because argument is not a valid CURIE");
102         } catch (IllegalArgumentException ex) {
103             // expected
104         }
105     }
106 
107     @Test
108     public void testCanExpandOnlyAcceptsCURIEs() {
109         try {
110             p.expand("@");
111             Assert.fail("Should have thrown IllegalArgumentException because argument is not a valid CURIE");
112         } catch (IllegalArgumentException ex) {
113             // expected
114         }
115     }
116 
117     @Test
118     public void testEmptyPrefix() {
119         p.add("", "http://example.com/");
120         Assert.assertFalse(p.isEmpty());
121         Assert.assertEquals(Collections.singleton(""), p.allPrefixes());
122         Assert.assertTrue(p.hasPrefix(""));
123         Assert.assertEquals(":foo", p.abbreviate("http://example.com/foo"));
124         Assert.assertEquals(RDFUtils.iri("http://example.com/foo"), p.expand(":foo"));
125         Assert.assertEquals(":", p.abbreviate("http://example.com/"));
126         Assert.assertEquals(RDFUtils.iri("http://example.com/"), p.expand(":"));
127     }
128 
129     @Test
130     public void testCannotAddPrefixTwice() {
131         p.add("ex", "http://example.com/");
132         try {
133             p.add("ex", "http://other.example.com/");
134             Assert.fail("Should have failed because of duplicate assignment of 'ex' prefix");
135         } catch (IllegalStateException ex) {
136             // expected
137         }
138     }
139 
140     @Test
141     public void testCanReAssignToSameIRI() {
142         p.add("ex", "http://example.com/");
143         p.add("ex", "http://example.com/");
144         // should NOT throw IllegalStateException
145     }
146 
147     @Test
148     public void testRemovePrefixResultsInEmptyMapping() {
149         p.add("ex", "http://example.com/");
150         p.removePrefix("ex");
151         Assert.assertTrue(p.isEmpty());
152         Assert.assertFalse(p.hasPrefix("ex"));
153         Assert.assertFalse(p.hasNamespaceIRI("http://example.com/"));
154     }
155 
156     @Test
157     public void testCanAddAfterRemoving() {
158         p.add("ex", "http://example.com/");
159         p.removePrefix("ex");
160         p.add("ex", "http://other.example.com/");
161         Assert.assertEquals("http://other.example.com/", p.getNamespaceIRIFor("ex"));
162     }
163 
164     @Test
165     public void testMergeEmptyPrefixes() {
166         p.add(new Prefixes());
167         Assert.assertTrue(p.isEmpty());
168     }
169 
170     @Test
171     public void testMergePrefixesWithoutConflict() {
172         p.add("ex", "http://example.com/");
173         p.add(Prefixes.create1("foaf", "http://xmlns.com/foaf/"));
174         Set<String> prefixes = p.allPrefixes();
175         Assert.assertTrue(prefixes.contains("ex"));
176         Assert.assertTrue(prefixes.contains("foaf"));
177         Assert.assertEquals(2, prefixes.size());
178     }
179 
180     @Test
181     public void testCreate1() {
182         p = Prefixes.create1("ex", "http://example.com/");
183         Assert.assertEquals(1, p.allPrefixes().size());
184         Assert.assertEquals("http://example.com/", p.getNamespaceIRIFor("ex"));
185     }
186 
187     @Test
188     public void testMergePrefixesWithConflictRaisesException() {
189         p.add("ex", "http://example.com/");
190         Prefixes p2 = Prefixes.create1("ex", "http://xmlns.com/foaf/");
191         try {
192             p.add(p2);
193             Assert.fail("Should have failed because ex is assigned twice");
194         } catch (IllegalStateException ex) {
195             // expected
196         }
197     }
198 
199     @Test
200     public void testMergePrefixesWithConflictButSameNamespace() {
201         p.add("ex", "http://example.com/");
202         p.add(Prefixes.create1("ex", "http://example.com/"));
203         Set<String> prefixes = p.allPrefixes();
204         Assert.assertTrue(prefixes.contains("ex"));
205         Assert.assertEquals(1, prefixes.size());
206     }
207 
208     @Test
209     public void testCreateSubset() {
210         p.add("ex", "http://example.com/");
211         p.add("foaf", "http://xmlns.com/foaf/");
212         Prefixes subset = p.createSubset("ex");
213         Assert.assertEquals(1, subset.allPrefixes().size());
214         Assert.assertTrue(subset.hasPrefix("ex"));
215     }
216 
217     @Test
218     public void testCreateSubsetWithUndefinedPrefixThrowsException() {
219         try {
220             p.createSubset("ex");
221             Assert.fail("Should have failed, p has no mapping for ex");
222         } catch (IllegalArgumentException ex) {
223             // expected
224         }
225     }
226 
227     @Test
228     public void testAsMapEmpty() {
229         Assert.assertTrue(p.asMap().isEmpty());
230     }
231 
232     @Test
233     public void testAsMapIsUnmodifiable() {
234         try {
235             p.asMap().put("ex", "http://example.com/");
236             Assert.fail("Should have failed, result of asMap() is supposed to be unmodifiable");
237         } catch (UnsupportedOperationException ex) {
238             // expected
239         }
240     }
241 
242     @Test
243     public void testAddVolatile() {
244         p.addVolatile("ex", "http://example.com/");
245         Assert.assertTrue(p.allPrefixes().contains("ex"));
246         Assert.assertEquals("http://example.com/", p.getNamespaceIRIFor("ex"));
247     }
248 
249     @Test
250     public void testAddVolatileNeverFails() {
251         p.add("ex", "http://example.com/");
252         p.addVolatile("ex", "http://other.example.com/");
253         p.addVolatile("foaf", "http://xmlns.com/foaf/");
254         p.addVolatile("foaf", "http://foaf.example.com/");
255         Assert.assertEquals(2, p.allPrefixes().size());
256         Assert.assertTrue(p.hasPrefix("foaf"));
257         Assert.assertTrue(p.hasPrefix("ex"));
258     }
259 
260     @Test
261     public void testRemoveVolatilePrefix() {
262         p.addVolatile("ex", "http://example.com/");
263         p.removePrefix("ex");
264         Assert.assertTrue(p.isEmpty());
265         Assert.assertFalse(p.isVolatile("ex"));
266     }
267 
268     @Test
269     public void testIsVolatile() {
270         p.add("ex", "http://example.com/");
271         p.addVolatile("foaf", "http://xmlns.com/foaf/");
272         Assert.assertTrue(p.isVolatile("foaf"));
273         Assert.assertFalse(p.isVolatile("ex"));
274     }
275 
276     @Test
277     public void testUndefinedPrefixIsNotVolatile() {
278         Assert.assertFalse(p.isVolatile("ex"));
279     }
280 
281     @Test
282     public void testAddVolatileDoesNotOverwriteHardMapping() {
283         p.add("ex", "http://example.com/");
284         p.addVolatile("ex", "http://other.example.com/");
285         Assert.assertEquals("http://example.com/", p.getNamespaceIRIFor("ex"));
286         Assert.assertFalse(p.isVolatile("ex"));
287     }
288 
289     @Test
290     public void testAddVolatileDoesNotOverwriteVolatileMapping() {
291         p.addVolatile("ex", "http://example.com/");
292         p.addVolatile("ex", "http://other.example.com/");
293         Assert.assertEquals("http://example.com/", p.getNamespaceIRIFor("ex"));
294         Assert.assertTrue(p.isVolatile("ex"));
295     }
296 
297     @Test
298     public void testAddHardOverwritesVolatileMapping() {
299         p.addVolatile("ex", "http://other.example.com/");
300         p.add("ex", "http://example.com/");
301         Assert.assertEquals("http://example.com/", p.getNamespaceIRIFor("ex"));
302         Assert.assertFalse(p.isVolatile("ex"));
303     }
304 
305     @Test
306     public void testMergeWithVolatile() {
307         p.add("a", "http://p1.example.com/");
308         p.addVolatile("b", "http://p2.example.com/");
309         p.addVolatile("c", "http://p3.example.com/");
310         p.addVolatile("d", "http://p4.example.com/");
311         Prefixes q = new Prefixes();
312         q.addVolatile("a", "http://q1.example.com/");
313         p.add("b", "http://q2.example.com/");
314         p.addVolatile("c", "http://q3.example.com/");
315         p.addVolatile("e", "http://q5.example.com/");
316         p.add(q);
317         Assert.assertEquals(new HashSet<String>(Arrays.asList("a", "b", "c", "d", "e")), p.allPrefixes());
318         Assert.assertEquals("http://p1.example.com/", p.getNamespaceIRIFor("a"));
319         Assert.assertEquals("http://q2.example.com/", p.getNamespaceIRIFor("b"));
320         Assert.assertEquals("http://p3.example.com/", p.getNamespaceIRIFor("c"));
321         Assert.assertEquals("http://p4.example.com/", p.getNamespaceIRIFor("d"));
322         Assert.assertEquals("http://q5.example.com/", p.getNamespaceIRIFor("e"));
323     }
324 
325     @Test
326     public void testAddPrefixesAsVolatile() {
327         p.addVolatile("ex", "http://example.com/");
328         Prefixes q = new Prefixes();
329         q.add("ex", "http://other.example.com/");
330         p.addVolatile(q);
331         Assert.assertEquals("http://example.com/", p.getNamespaceIRIFor("ex"));
332     }
333 
334     @Test
335     public void testIncompatiblePrefixesInMergeAreDetected() {
336         Prefixes p1 = PopularPrefixes.createSubset("rdf");
337         Prefixes p2 = Prefixes.create1("rdf", "http://example.com/rdf#");
338         try {
339             p1.add(p2);
340             Assert.fail("Should fail because of different mappings for rdf prefix");
341         } catch (IllegalStateException ex) {
342             // expected
343         }
344     }
345 
346     @Test
347     public void testNewPrefixesFromOtherPrefixesAreIndependent() {
348         Prefixes p2 = new Prefixes(p);
349         p2.add("ex", "http://example.org/");
350         Assert.assertTrue(p.isEmpty());
351     }
352 
353 }