01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.geronimo.test;
19:
20: import java.io.StringWriter;
21: import java.io.StringReader;
22:
23: import javax.xml.bind.JAXBContext;
24: import javax.xml.bind.Marshaller;
25: import javax.xml.bind.Unmarshaller;
26:
27: import org.apache.geronimo.test.generated.Account;
28:
29: /**
30: * Performs basic tests using the JAXB 2.0 API.
31: */
32: public class JAXBTest {
33:
34: private static final String EXPECTED_OUTPUT = "<FirstName>foo</FirstName><LastName>bar</LastName>";
35:
36: private static final String INPUT = "<foo:Account xmlns:foo=\"http://geronimo.apache.org\">\r\n"
37: + "<FirstName>first</FirstName>\r\n"
38: + "<LastName>last</LastName>\r\n" + "</foo:Account>\r\n";
39:
40: JAXBContext jc = null;
41:
42: public JAXBTest() throws Exception {
43: this .jc = JAXBContext
44: .newInstance("org.apache.geronimo.test.generated");
45: }
46:
47: public String getImplementation() {
48: return this .jc.getClass().getName();
49: }
50:
51: public void testMarshall() throws Exception {
52:
53: Account bean = new Account();
54: bean.setFirstName("foo");
55: bean.setLastName("bar");
56:
57: Marshaller m = this .jc.createMarshaller();
58: StringWriter writer = new StringWriter();
59: m.marshal(bean, writer);
60: writer.flush();
61:
62: String xml = writer.toString();
63: System.out.println(xml);
64:
65: if (xml.indexOf(EXPECTED_OUTPUT) == -1) {
66: throw new Exception("Unexpected xml generated");
67: }
68: }
69:
70: public void testUnmarshall() throws Exception {
71:
72: Unmarshaller m = this .jc.createUnmarshaller();
73: Account bean = (Account) m.unmarshal(new StringReader(INPUT));
74:
75: if (!(bean.getFirstName().equals("first") && bean.getLastName()
76: .equals("last"))) {
77: throw new Exception("Unexpected data");
78: }
79: }
80:
81: }
|