KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > service > namespace > QNameTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.service.namespace;
18
19 import java.util.Collection JavaDoc;
20 import java.util.HashSet JavaDoc;
21
22 import junit.framework.TestCase;
23
24
25
26 /**
27  * @see org.alfresco.service.namespace.QName
28  *
29  * @author David Caruana
30  */

31 public class QNameTest extends TestCase
32 {
33
34     public QNameTest(String JavaDoc name)
35     {
36         super(name);
37     }
38
39
40     public void testInvalidQName() throws Exception JavaDoc
41     {
42         try
43         {
44             QName qname = QName.createQName("");
45             fail("Missing local name was not caught");
46         }
47         catch (InvalidQNameException e)
48         {
49         }
50
51         try
52         {
53             QName qname = QName.createQName("invalid{}name");
54             fail("Namespace not at start was not caught");
55         }
56         catch (InvalidQNameException e)
57         {
58         }
59
60         try
61         {
62             QName qname = QName.createQName("{name");
63             fail("Missing closing namespace token was not caught");
64         }
65         catch (InvalidQNameException e)
66         {
67         }
68
69         try
70         {
71             QName qname = QName.createQName("{}");
72             fail("Missing local name after namespace was not caught");
73         }
74         catch (InvalidQNameException e)
75         {
76         }
77
78         try
79         {
80             QName qname = QName.createQName("{}name");
81         }
82         catch (InvalidQNameException e)
83         {
84             fail("Empty namespace is valid");
85         }
86
87         try
88         {
89             QName qname = QName.createQName("{namespace}name");
90             assertEquals("namespace", qname.getNamespaceURI());
91             assertEquals("name", qname.getLocalName());
92         }
93         catch (InvalidQNameException e)
94         {
95             fail("Valid namespace has been thrown out");
96         }
97
98         try
99         {
100             QName qname = QName.createQName((String JavaDoc) null, (String JavaDoc) null);
101             fail("Null name was not caught");
102         }
103         catch (InvalidQNameException e)
104         {
105         }
106
107         try
108         {
109             QName qname = QName.createQName((String JavaDoc) null, "");
110             fail("Empty name was not caught");
111         }
112         catch (InvalidQNameException e)
113         {
114         }
115         
116     }
117
118
119     public void testConstruction()
120     {
121         QName qname1 = QName.createQName("namespace1", "name1");
122         assertEquals("namespace1", qname1.getNamespaceURI());
123         assertEquals("name1", qname1.getLocalName());
124
125         QName qname2 = QName.createQName("{namespace2}name2");
126         assertEquals("namespace2", qname2.getNamespaceURI());
127         assertEquals("name2", qname2.getLocalName());
128
129         QName qname3 = QName.createQName(null, "name3");
130         assertEquals("", qname3.getNamespaceURI());
131
132         QName qname4 = QName.createQName("", "name4");
133         assertEquals("", qname4.getNamespaceURI());
134
135         QName qname5 = QName.createQName("{}name5");
136         assertEquals("", qname5.getNamespaceURI());
137
138         QName qname6 = QName.createQName("name6");
139         assertEquals("", qname6.getNamespaceURI());
140     }
141
142
143     public void testStringRepresentation()
144     {
145         QName qname1 = QName.createQName("namespace", "name1");
146         assertEquals("{namespace}name1", qname1.toString());
147
148         QName qname2 = QName.createQName("", "name2");
149         assertEquals("{}name2", qname2.toString());
150
151         QName qname3 = QName.createQName("{namespace}name3");
152         assertEquals("{namespace}name3", qname3.toString());
153
154         QName qname4 = QName.createQName("{}name4");
155         assertEquals("{}name4", qname4.toString());
156
157         QName qname5 = QName.createQName("name5");
158         assertEquals("{}name5", qname5.toString());
159     }
160
161
162     public void testEquality()
163     {
164         QName qname1 = QName.createQName("namespace", "name");
165         QName qname2 = QName.createQName("namespace", "name");
166         QName qname3 = QName.createQName("{namespace}name");
167         assertEquals(qname1, qname2);
168         assertEquals(qname1, qname3);
169         assertEquals(qname1.hashCode(), qname2.hashCode());
170         assertEquals(qname1.hashCode(), qname3.hashCode());
171
172         QName qname4 = QName.createQName("", "name");
173         QName qname5 = QName.createQName("", "name");
174         QName qname6 = QName.createQName(null, "name");
175         assertEquals(qname4, qname5);
176         assertEquals(qname4, qname6);
177         assertEquals(qname4.hashCode(), qname5.hashCode());
178         assertEquals(qname4.hashCode(), qname6.hashCode());
179
180         QName qname7 = QName.createQName("namespace", "name");
181         QName qname8 = QName.createQName("namespace", "differentname");
182         assertFalse(qname7.equals(qname8));
183         assertFalse(qname7.hashCode() == qname8.hashCode());
184
185         QName qname9 = QName.createQName("namespace", "name");
186         QName qname10 = QName.createQName("differentnamespace", "name");
187         assertFalse(qname9.equals(qname10));
188         assertFalse(qname9.hashCode() == qname10.hashCode());
189     }
190
191
192     public void testPrefix()
193     {
194         try
195         {
196             QName noResolver = QName.createQName(NamespaceService.ALFRESCO_PREFIX, "alfresco prefix", null);
197             fail("Null resolver was not caught");
198         }
199         catch (IllegalArgumentException JavaDoc e)
200         {
201         }
202
203         NamespacePrefixResolver mockResolver = new MockNamespacePrefixResolver();
204         QName qname1 = QName.createQName(NamespaceService.ALFRESCO_PREFIX, "alfresco prefix", mockResolver);
205         assertEquals(NamespaceService.ALFRESCO_URI, qname1.getNamespaceURI());
206         QName qname2 = QName.createQName("", "default prefix", mockResolver);
207         assertEquals(NamespaceService.DEFAULT_URI, qname2.getNamespaceURI());
208         QName qname3 = QName.createQName(null, "null default prefix", mockResolver);
209         assertEquals(NamespaceService.DEFAULT_URI, qname3.getNamespaceURI());
210
211         try
212         {
213             QName qname4 = QName.createQName("garbage", "garbage prefix", mockResolver);
214             fail("Invalid Prefix was not caught");
215         }
216         catch (NamespaceException e)
217         {
218         }
219     }
220
221     
222     private static class MockNamespacePrefixResolver
223         implements NamespacePrefixResolver
224     {
225
226         public String JavaDoc getNamespaceURI(String JavaDoc prefix)
227         {
228             if (prefix.equals(NamespaceService.DEFAULT_PREFIX))
229             {
230                 return NamespaceService.DEFAULT_URI;
231             }
232             else if (prefix.equals(NamespaceService.ALFRESCO_PREFIX))
233             {
234                 return NamespaceService.ALFRESCO_URI;
235             }
236             throw new NamespaceException("Prefix " + prefix + " not registered");
237         }
238
239         public Collection JavaDoc<String JavaDoc> getPrefixes(String JavaDoc namespaceURI)
240         {
241             throw new NamespaceException("URI " + namespaceURI + " not registered");
242         }
243
244         public Collection JavaDoc<String JavaDoc> getPrefixes()
245         {
246             HashSet JavaDoc<String JavaDoc> prefixes = new HashSet JavaDoc<String JavaDoc>();
247             prefixes.add(NamespaceService.DEFAULT_PREFIX);
248             prefixes.add(NamespaceService.ALFRESCO_PREFIX);
249             return prefixes;
250         }
251         
252         public Collection JavaDoc<String JavaDoc> getURIs()
253         {
254             HashSet JavaDoc<String JavaDoc> uris = new HashSet JavaDoc<String JavaDoc>();
255             uris.add(NamespaceService.DEFAULT_URI);
256             uris.add(NamespaceService.ALFRESCO_URI);
257             return uris;
258         }
259         
260     }
261     
262 }
263
Popular Tags