KickJava   Java API By Example, From Geeks To Geeks.

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


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.Marshaller;
23
24 import org.apache.ws.jaxme.examples.misc.address.AddressCreator;
25 import org.apache.ws.jaxme.examples.misc.address.AddressPrinter;
26 import org.apache.ws.jaxme.impl.JMMarshallerImpl;
27 import org.apache.ws.jaxme.test.misc.address.Address;
28 import org.xml.sax.InputSource JavaDoc;
29
30
31 /** Basic test for marshalling and unmarshalling an
32  * Address document.
33  */

34 public class PrinterCreatorTest extends BaseTestCase {
35     /** Creates a new instance with the given name.
36      */

37     public PrinterCreatorTest(String JavaDoc pName) {
38         super(pName);
39     }
40     
41     private String JavaDoc getAddress() throws Exception JavaDoc {
42         StringWriter JavaDoc sw = new StringWriter JavaDoc();
43         AddressCreator.writeAddress(sw);
44         sw.close();
45         return "<Address xmlns=\"http://ws.apache.org/jaxme/test/misc/address\">\n" +
46         " <Name>\n" +
47         " <First>Jane</First>\n" +
48         " <Last>Doe</Last>\n" +
49         " </Name>\n" +
50         " <Postal>\n" +
51         " <Street>34 Main Street</Street>\n" +
52         " <ZIP>02215</ZIP>\n" +
53         " <City>Boston</City>\n" +
54         " <State>MA</State>\n" +
55         " </Postal>\n" +
56         " <PhoneDetails>\n" +
57         " <Phone type=\"Work\">\n" +
58         " <PhoneNumber>555.6789</PhoneNumber>\n" +
59         " </Phone>\n" +
60         " <Phone type=\"Fax\">\n" +
61         " <PhoneNumber>555.1212</PhoneNumber>\n" +
62         " </Phone>\n" +
63         " </PhoneDetails>\n" +
64         " <EmailDetails>\n" +
65         " <Email type=\"Private\">\n" +
66         " <EmailAddress>jdoe@yourcompany.com</EmailAddress>\n" +
67         " </Email>\n" +
68         " <Email type=\"Office\">\n" +
69         " <EmailAddress>josephdoe@mycompany.com</EmailAddress>\n" +
70         " </Email>\n" +
71         " <Email type=\"Office\">\n" +
72         " <EmailAddress>joseph.doe@mycompany.com</EmailAddress>\n" +
73         " </Email>\n" +
74         " </EmailDetails>\n" +
75         "</Address>";
76     }
77     
78     /** Tests marshalling a JaxMe object.
79      */

80     public void testCreate() throws Exception JavaDoc {
81         StringWriter JavaDoc sw = new StringWriter JavaDoc();
82         AddressCreator.writeAddress(sw);
83         sw.close();
84         String JavaDoc expect = "<Address xmlns=\"http://ws.apache.org/jaxme/test/misc/address\">\n" +
85         " <Name>\n" +
86         " <First>Jane</First>\n" +
87         " <Last>Doe</Last>\n" +
88         " </Name>\n" +
89         " <Postal>\n" +
90         " <Street>34 Main Street</Street>\n" +
91         " <ZIP>02215</ZIP>\n" +
92         " <City>Boston</City>\n" +
93         " <State>MA</State>\n" +
94         " </Postal>\n" +
95         " <PhoneDetails>\n" +
96         " <Phone type=\"Work\">\n" +
97         " <PhoneNumber>555.6789</PhoneNumber>\n" +
98         " </Phone>\n" +
99         " <Phone type=\"Fax\">\n" +
100         " <PhoneNumber>555.1212</PhoneNumber>\n" +
101         " </Phone>\n" +
102         " </PhoneDetails>\n" +
103         " <EmailDetails>\n" +
104         " <Email type=\"Private\">\n" +
105         " <EmailAddress>jdoe@yourcompany.com</EmailAddress>\n" +
106         " </Email>\n" +
107         " <Email type=\"Office\">\n" +
108         " <EmailAddress>josephdoe@mycompany.com</EmailAddress>\n" +
109         " </Email>\n" +
110         " <Email type=\"Office\">\n" +
111         " <EmailAddress>joseph.doe@mycompany.com</EmailAddress>\n" +
112         " </Email>\n" +
113         " </EmailDetails>\n" +
114         "</Address>";
115         String JavaDoc got = sw.toString();
116         assertEquals(expect, got);
117     }
118     
119     /** Tests unmarshalling a string into t JaxMe object.
120      */

121     public void testPrint() throws Exception JavaDoc {
122         String JavaDoc address = getAddress();
123         InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(address));
124         isource.setSystemId("testPrint.xml");
125         Address addr = AddressPrinter.getAddress(isource);
126         JAXBContext context = JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.address");
127         StringWriter JavaDoc sw = new StringWriter JavaDoc();
128         Marshaller m = context.createMarshaller();
129         m.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE);
130         m.marshal(addr, sw);
131         sw.close();
132         String JavaDoc got = sw.toString();
133         assertEquals(address, got);
134     }
135 }
136
Popular Tags