1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.axiom.support;
18  
19  import junit.framework.TestCase;
20  import org.apache.axiom.om.OMAbstractFactory;
21  import org.apache.axiom.om.OMElement;
22  import org.apache.axiom.om.OMFactory;
23  import org.apache.axiom.om.OMNamespace;
24  
25  import javax.xml.namespace.QName;
26  import java.util.Locale;
27  
28  public class AxiomUtilsTest extends TestCase {
29  
30      private OMElement element;
31  
32      protected void setUp() throws Exception {
33          OMFactory factory = OMAbstractFactory.getOMFactory();
34          OMNamespace namespace = factory.createOMNamespace("http://www.springframework.org", "prefix");
35          element = factory.createOMElement("element", namespace);
36      }
37  
38      public void testToNamespaceDeclared() throws Exception {
39          QName qName = new QName(element.getNamespace().getNamespaceURI(), "localPart");
40          OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
41          assertNotNull("Invalid namespace", namespace);
42          assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
43      }
44  
45      public void testToNamespaceUndeclared() throws Exception {
46          QName qName = new QName("http://www.example.com", "localPart");
47          OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
48          assertNotNull("Invalid namespace", namespace);
49          assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
50          assertFalse("Invalid prefix", "prefix".equals(namespace.getPrefix()));
51      }
52  
53      public void testToNamespacePrefixDeclared() throws Exception {
54          QName qName = new QName(element.getNamespace().getNamespaceURI(), "localPart", "prefix");
55          OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
56          assertNotNull("Invalid namespace", namespace);
57          assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
58          assertEquals("Invalid prefix", "prefix", namespace.getPrefix());
59      }
60  
61      public void testToNamespacePrefixUndeclared() throws Exception {
62          QName qName = new QName("http://www.example.com", "localPart", "otherPrefix");
63          OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
64          assertNotNull("Invalid namespace", namespace);
65          assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
66          assertEquals("Invalid prefix", qName.getPrefix(), namespace.getPrefix());
67      }
68  
69      public void testToLanguage() throws Exception {
70          assertEquals("Invalid conversion", "fr-CA", AxiomUtils.toLanguage(Locale.CANADA_FRENCH));
71          assertEquals("Invalid conversion", "en", AxiomUtils.toLanguage(Locale.ENGLISH));
72      }
73  
74      public void testToLocale() throws Exception {
75          assertEquals("Invalid conversion", Locale.CANADA_FRENCH, AxiomUtils.toLocale("fr-CA"));
76          assertEquals("Invalid conversion", Locale.ENGLISH, AxiomUtils.toLocale("en"));
77      }
78  }