KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > junit > EnumerationTest


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
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.apache.ws.jaxme.junit;
18
19 import java.io.StringReader JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 import javax.xml.bind.JAXBContext;
23 import javax.xml.bind.JAXBException;
24 import javax.xml.bind.Marshaller;
25 import javax.xml.bind.Unmarshaller;
26
27 import org.apache.ws.jaxme.JMElement;
28 import org.apache.ws.jaxme.JMUnmarshaller;
29 import org.apache.ws.jaxme.impl.JAXBContextImpl;
30 import org.apache.ws.jaxme.impl.JMMarshallerImpl;
31 import org.apache.ws.jaxme.impl.JMSAXDriver;
32 import org.apache.ws.jaxme.test.misc.enumeration.AllSimpleTypes;
33 import org.apache.ws.jaxme.test.misc.enumeration.AllTypesElement;
34 import org.apache.ws.jaxme.test.misc.enumeration.MyDoubleTypeClass;
35 import org.apache.ws.jaxme.test.misc.enumeration.MyFloatTypeClass;
36 import org.apache.ws.jaxme.test.misc.enumeration.MyIntTypeClass;
37 import org.apache.ws.jaxme.test.misc.enumeration.MyLongTypeClass;
38 import org.apache.ws.jaxme.test.misc.enumeration.MyShortTypeClass;
39 import org.apache.ws.jaxme.test.misc.enumeration.MyStringTypeClass;
40 import org.apache.ws.jaxme.test.misc.enumeration.impl.AllSimpleTypesImpl;
41 import org.apache.ws.jaxme.test.misc.enumeration.impl.AllTypesElementImpl;
42 import org.apache.ws.jaxme.test.misc.enumeration.impl.AllTypesElementTypeDriver;
43 import org.xml.sax.InputSource JavaDoc;
44
45
46 /** Test suite for enumerations generated by <code>xs:enum</code>.
47  */

48 public class EnumerationTest extends BaseTestCase {
49     private JAXBContextImpl factory;
50
51     /** Creates a new test instance with the given name.
52      */

53     public EnumerationTest(String JavaDoc arg) { super(arg); }
54
55     public void setUp() throws JAXBException {
56         factory = (JAXBContextImpl) JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.enumeration");
57     }
58
59     private JAXBContextImpl getFactory() {
60         return factory;
61     }
62
63     private AllSimpleTypes getAllSimpleTypesElement() {
64         AllSimpleTypes element = new AllSimpleTypesImpl();
65         element.setStringElem(MyStringTypeClass.FOO);
66         element.setIntElem(MyIntTypeClass.INT3);
67         element.setLongElem(MyLongTypeClass.LONG_NEGATIVE);
68         element.setShortElem(MyShortTypeClass.SHORT_POSITIVE);
69         element.setDoubleElem(MyDoubleTypeClass.DOUBLE_POSITIVE);
70         element.setFloatElem(MyFloatTypeClass.FLOAT_NEGATIVE);
71         return element;
72     }
73
74     private AllTypesElement getAllTypesElement() throws JAXBException {
75         AllTypesElement element = (AllTypesElement) getFactory().getManager(AllTypesElement.class).getElementJ();
76         element.setAllSimpleTypesElement(getAllSimpleTypesElement());
77         return element;
78     }
79
80     private String JavaDoc getAllTypesElementString() {
81         AllTypesElementImpl elem = new AllTypesElementImpl();
82         String JavaDoc uri = elem.getQName().getNamespaceURI();
83         return
84         "<ex:AllTypesElement xmlns:ex=\"" + uri + "\">\n" +
85         " <ex:AllSimpleTypesElement>\n" +
86         " <ex:StringElem>FOO</ex:StringElem>\n" +
87         " <ex:IntElem>3</ex:IntElem>\n" +
88         " <ex:LongElem>-23987982739273989</ex:LongElem>\n" +
89         " <ex:ShortElem>3468</ex:ShortElem>\n" +
90         " <ex:DoubleElem>3249239847982.234</ex:DoubleElem>\n" +
91         " <ex:FloatElem>-24234.234</ex:FloatElem>\n" +
92         " </ex:AllSimpleTypesElement>\n" +
93         "</ex:AllTypesElement>";
94     }
95
96     private void verifyAllSimpleTypesElement(AllSimpleTypes pElement) {
97         assertEquals(MyStringTypeClass.FOO, pElement.getStringElem());
98         assertEquals("FOO", pElement.getStringElem().getValue());
99         assertEquals("FOO", pElement.getStringElem().toString());
100         assertEquals(MyIntTypeClass.INT3, pElement.getIntElem());
101         assertEquals(3, pElement.getIntElem().getValue());
102         assertEquals("INT3", pElement.getIntElem().getName());
103         assertEquals("3", pElement.getIntElem().toString());
104         assertEquals(MyLongTypeClass.LONG_NEGATIVE, pElement.getLongElem());
105         assertEquals(-23987982739273989L, pElement.getLongElem().getValue());
106         assertEquals("LONG_NEGATIVE", pElement.getLongElem().getName());
107         assertEquals("-23987982739273989", pElement.getLongElem().toString());
108         assertEquals(MyShortTypeClass.SHORT_POSITIVE, pElement.getShortElem());
109         assertEquals(3468, pElement.getShortElem().getValue());
110         assertEquals("SHORT_POSITIVE", pElement.getShortElem().getName());
111         assertEquals("3468", pElement.getShortElem().toString());
112         assertEquals(MyDoubleTypeClass.DOUBLE_POSITIVE, pElement.getDoubleElem());
113         assertTrue(3249239847982.234 == pElement.getDoubleElem().getValue());
114         assertEquals("DOUBLE_POSITIVE", pElement.getDoubleElem().getName());
115         assertEquals("3249239847982.234", pElement.getDoubleElem().toString());
116         assertEquals(MyFloatTypeClass.FLOAT_NEGATIVE, pElement.getFloatElem());
117         assertTrue(Float.parseFloat("-24234.234") == pElement.getFloatElem().getValue());
118         assertEquals("FLOAT_NEGATIVE", pElement.getFloatElem().getName());
119         assertEquals("-24234.234", pElement.getFloatElem().toString());
120     }
121
122     private void verifyAllTypesElement(AllTypesElement pElement) {
123         verifyAllSimpleTypesElement(pElement.getAllSimpleTypesElement());
124     }
125
126     /** Test for unmarshalling of simple elements.
127      */

128     public void testUnmarshalSimpleElements() throws Exception JavaDoc {
129         JMUnmarshaller jmUnmarshaller = (JMUnmarshaller) getFactory().createUnmarshaller();
130         StringReader JavaDoc sr = new StringReader JavaDoc(getAllTypesElementString());
131         AllTypesElement result = (AllTypesElement) jmUnmarshaller.unmarshal(new InputSource JavaDoc(sr));
132         verifyAllSimpleTypesElement(result.getAllSimpleTypesElement());
133     }
134
135     /** Test for unmarshalling of complex elements.
136      */

137     public void testUnmarshalComplexElements() throws Exception JavaDoc {
138         JAXBContext myFactory = getFactory();
139         Unmarshaller unmarshaller = myFactory.createUnmarshaller();
140         StringReader JavaDoc sr = new StringReader JavaDoc(getAllTypesElementString());
141         AllTypesElement result = (AllTypesElement) unmarshaller.unmarshal(new InputSource JavaDoc(sr));
142         verifyAllTypesElement(result);
143     }
144
145     /** Test for marshalling of simple elements.
146      */

147     public void testMarshalSimpleElements() throws Exception JavaDoc {
148         StringWriter JavaDoc sw = new StringWriter JavaDoc();
149         AllTypesElement element = getAllTypesElement();
150         Marshaller m = getFactory().createMarshaller();
151         m.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE);
152         m.marshal(element, sw);
153         String JavaDoc expected = getAllTypesElementString();
154         String JavaDoc got = sw.toString();
155         assertEquals(expected, got);
156     }
157
158     /** Test for marshalling of complex elements.
159      */

160     public void testMarshalComplexElements() throws Exception JavaDoc {
161         JAXBContext myFactory = getFactory();
162         Marshaller marshaller = myFactory.createMarshaller();
163         marshaller.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE);
164         StringWriter JavaDoc sw = new StringWriter JavaDoc();
165         JMElement jmElement = (JMElement) getAllTypesElement();
166         assertNotNull(jmElement.getQName());
167         marshaller.marshal(jmElement, sw);
168         assertEquals(getAllTypesElementString(), sw.toString());
169     }
170
171     /** Test for proper creation of prefixes.
172      */

173     public void testPrefixes() throws Exception JavaDoc {
174         AllTypesElementImpl el = new AllTypesElementImpl();
175         JMSAXDriver driver = new AllTypesElementTypeDriver();
176         assertEquals("ex", driver.getPreferredPrefix(el.getQName().getNamespaceURI()));
177         assertNull(driver.getPreferredPrefix("dummyURI"));
178     }
179 }
180
Popular Tags