KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.ws.jaxme.junit;
17
18 import java.io.StringReader JavaDoc;
19 import java.io.StringWriter JavaDoc;
20
21 import javax.xml.bind.JAXBContext;
22 import javax.xml.bind.JAXBException;
23 import javax.xml.bind.Marshaller;
24 import javax.xml.bind.Unmarshaller;
25
26 import org.apache.ws.jaxme.impl.JMMarshallerImpl;
27 import org.apache.ws.jaxme.tests.printparse.Test;
28 import org.apache.ws.jaxme.tests.printparse.impl.TestImpl;
29 import org.xml.sax.InputSource JavaDoc;
30
31 import junit.framework.TestCase;
32
33
34 /** Test case for the
35  * <code>printMethod</code> and <code>parseMethod</code>
36  * attributes in <code>jaxb:javaType</code>.
37  */

38 public class PrintParseTest extends TestCase {
39     private String JavaDoc getNamespace() {
40         TestImpl test = new TestImpl();
41         return test.getQName().getNamespaceURI();
42     }
43
44     private String JavaDoc getPackageName() {
45         String JavaDoc testClassName = Test.class.getName();
46         int offset = testClassName.lastIndexOf('.');
47         return testClassName.substring(0, offset);
48     }
49
50     private JAXBContext getJAXBContext() throws JAXBException {
51         return JAXBContext.newInstance(getPackageName());
52     }
53
54     /** Tests the use of <code>jaxb:javaType/@print</code>.
55      */

56     public void testPrint() throws Exception JavaDoc {
57         boolean[] bools = new boolean[]{false, true};
58         int[] ints = new int[]{0,1};
59         for (int i = 0; i < bools.length; i++) {
60             Test test = new TestImpl();
61             test.setBool(bools[i]);
62             StringWriter JavaDoc sw = new StringWriter JavaDoc();
63             Marshaller m = getJAXBContext().createMarshaller();
64             m.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE);
65             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
66             m.marshal(test, sw);
67             String JavaDoc expect =
68               "<Test xmlns=\"" + getNamespace() + "\"><Bool>" + ints[i] +
69               "</Bool></Test>";
70             String JavaDoc got = sw.toString();
71             assertEquals(expect, got);
72         }
73     }
74
75     /** Tests the use of <code>jaxb:javaType/@parse</code>.
76      */

77     public void testParse() throws Exception JavaDoc {
78         boolean[] bools = new boolean[]{false, true};
79         int[] ints = new int[]{0,1};
80         for (int i = 0; i < bools.length; i++) {
81             String JavaDoc input =
82                 "<Test xmlns=\"" + getNamespace() + "\"><Bool>" + ints[i] +
83                 "</Bool></Test>";
84             Unmarshaller u = getJAXBContext().createUnmarshaller();
85             Test test = (Test) u.unmarshal(new InputSource JavaDoc(new StringReader JavaDoc(input)));
86             assertEquals(bools[i], test.isBool());
87         }
88     }
89 }
90
Popular Tags