KickJava   Java API By Example, From Geeks To Geeks.

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


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.impl.JMMarshallerImpl;
29 import org.xml.sax.InputSource JavaDoc;
30
31 import junit.framework.TestCase;
32
33
34 /** <p>A base class for JUnit tests.</p>
35  *
36  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
37  * @version $Id: BaseTestCase.java,v 1.10 2005/04/28 23:57:53 jochen Exp $
38  */

39 public abstract class BaseTestCase extends TestCase {
40     protected BaseTestCase() {
41     }
42
43     protected BaseTestCase(String JavaDoc pName) {
44         super(pName);
45     }
46
47     protected BaseTestCase(Class JavaDoc c) {
48         this(c.getName());
49     }
50
51     protected String JavaDoc getNamespaceURI(JMElement pElement) {
52         return pElement.getQName().getNamespaceURI();
53     }
54
55     protected String JavaDoc getPackageName(Class JavaDoc pClass) {
56         String JavaDoc className = pClass.getName();
57         int offset = className.lastIndexOf('.');
58         if (offset == -1) {
59             throw new IllegalStateException JavaDoc("Unable to parse package name: " + className);
60         } else {
61             return className.substring(0, offset);
62         }
63     }
64
65     protected JAXBContext getJAXBContext(Class JavaDoc pClass) throws JAXBException {
66         return JAXBContext.newInstance(getPackageName(pClass));
67     }
68
69     // Asserts equality of the two given byte arrays.
70
protected void assertEquals(byte[] pExpect, byte[] pGot) {
71         if (pExpect.length != pGot.length) {
72             fail("Expected " + pExpect.length + " bytes, got " + pGot.length);
73         } else {
74             for (int i = 0; i < pExpect.length; i++) {
75                 if (pExpect[i] != pGot[i]) {
76                     fail("Expected byte " + ((int) pExpect[i]) + " at offset " + i +
77                             ", got byte " + ((int) pGot[i]));
78                 }
79             }
80         }
81     }
82
83     protected void unmarshalMarshalUnmarshal(Class JavaDoc pClass, String JavaDoc pXML, boolean pIndenting)
84             throws Exception JavaDoc {
85         Object JavaDoc o = unmarshal(pClass, pXML);
86         assertNotNull(o);
87         String JavaDoc s = marshal(o, pClass, pIndenting);
88         assertEquals(pXML, s);
89         Object JavaDoc comp = unmarshal(pClass, s);
90         assertNotNull(comp);
91     }
92
93     protected void unmarshalMarshalUnmarshal(Class JavaDoc pClass, String JavaDoc pXML) throws Exception JavaDoc {
94         unmarshalMarshalUnmarshal(pClass, pXML, true);
95     }
96
97     protected Object JavaDoc unmarshal(Class JavaDoc pClass, String JavaDoc pXML) throws JAXBException {
98         JAXBContext context = JAXBContext.newInstance(getPackageName(pClass));
99         Unmarshaller unmarshaller = context.createUnmarshaller();
100         return unmarshaller.unmarshal(new InputSource JavaDoc(new StringReader JavaDoc(pXML)));
101     }
102
103     protected String JavaDoc marshal(Object JavaDoc o, Class JavaDoc pClass) throws JAXBException {
104         return marshal(o, pClass, true);
105     }
106
107     protected String JavaDoc marshal(Object JavaDoc o, Class JavaDoc pClass, boolean pIndenting) throws JAXBException {
108         JAXBContext context = JAXBContext.newInstance(getPackageName(pClass));
109         Marshaller marshaller = context.createMarshaller();
110         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, pIndenting ? Boolean.TRUE : Boolean.FALSE);
111         marshaller.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE);
112         StringWriter JavaDoc sw = new StringWriter JavaDoc();
113         marshaller.marshal(o, sw);
114         return sw.toString();
115     }
116 }
117
Popular Tags