001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.jee;
018:
019: import junit.framework.AssertionFailedError;
020: import junit.framework.TestCase;
021:
022: import org.custommonkey.xmlunit.Diff;
023: import org.xml.sax.Attributes;
024: import org.xml.sax.InputSource;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.XMLReader;
027: import org.xml.sax.helpers.XMLFilterImpl;
028:
029: import javax.xml.bind.JAXBContext;
030: import javax.xml.bind.Marshaller;
031: import javax.xml.bind.Unmarshaller;
032: import javax.xml.bind.ValidationEvent;
033: import javax.xml.bind.ValidationEventHandler;
034: import javax.xml.parsers.SAXParser;
035: import javax.xml.parsers.SAXParserFactory;
036: import javax.xml.transform.sax.SAXSource;
037: import java.io.BufferedInputStream;
038: import java.io.ByteArrayInputStream;
039: import java.io.ByteArrayOutputStream;
040: import java.io.File;
041: import java.io.FileOutputStream;
042: import java.io.IOException;
043: import java.io.InputStream;
044:
045: /**
046: * @version $Revision: 605825 $ $Date: 2007-12-19 23:56:25 -0800 $
047: */
048: public class JeeTest extends TestCase {
049: public void testEjbJar() throws Exception {
050: marshalAndUnmarshal(EjbJar.class, "ejb-jar-example1.xml");
051: }
052:
053: public void testApplication() throws Exception {
054: marshalAndUnmarshal(Application.class,
055: "application-example.xml");
056: }
057:
058: public void testApplicationClient() throws Exception {
059: marshalAndUnmarshal(ApplicationClient.class,
060: "application-client-example.xml");
061: }
062:
063: public void testWar() throws Exception {
064: marshalAndUnmarshal(WebApp.class, "web-example.xml");
065: }
066:
067: public void testTld() throws Exception {
068: marshalAndUnmarshal(TldTaglib.class, "tld-example.xml");
069: }
070:
071: public void testRar() throws Exception {
072: marshalAndUnmarshal(Connector.class, "connector-example.xml");
073: }
074:
075: private <T> void marshalAndUnmarshal(Class<T> type,
076: String xmlFileName) throws Exception {
077: SAXParserFactory factory = SAXParserFactory.newInstance();
078: factory.setNamespaceAware(true);
079: factory.setValidating(false);
080: SAXParser parser = factory.newSAXParser();
081:
082: JAXBContext ctx = JAXBContext.newInstance(type);
083: Unmarshaller unmarshaller = ctx.createUnmarshaller();
084:
085: NamespaceFilter xmlFilter = new NamespaceFilter(parser
086: .getXMLReader());
087: xmlFilter.setContentHandler(unmarshaller
088: .getUnmarshallerHandler());
089: unmarshaller.setEventHandler(new TestValidationEventHandler());
090:
091: InputStream in = this .getClass().getClassLoader()
092: .getResourceAsStream(xmlFileName);
093: String expected = readContent(in);
094:
095: SAXSource source = new SAXSource(xmlFilter, new InputSource(
096: new ByteArrayInputStream(expected.getBytes())));
097:
098: Object object = unmarshaller.unmarshal(source);
099:
100: Marshaller marshaller = ctx.createMarshaller();
101: marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
102:
103: ByteArrayOutputStream baos = new ByteArrayOutputStream();
104: marshaller.marshal(object, baos);
105:
106: byte[] bytes = baos.toByteArray();
107: String actual = new String(bytes);
108:
109: try {
110: Diff myDiff = new Diff(expected, actual);
111: assertTrue("Files are similar " + myDiff, myDiff.similar());
112: } catch (AssertionFailedError e) {
113: writeToTmpFile(bytes, xmlFileName);
114: throw e;
115: }
116: }
117:
118: public static class NamespaceFilter extends XMLFilterImpl {
119: private static final InputSource EMPTY_INPUT_SOURCE = new InputSource(
120: new ByteArrayInputStream(new byte[0]));
121:
122: public NamespaceFilter(XMLReader xmlReader) {
123: super (xmlReader);
124: }
125:
126: public InputSource resolveEntity(String publicId,
127: String systemId) throws SAXException, IOException {
128: return EMPTY_INPUT_SOURCE;
129: }
130:
131: public void startElement(String uri, String localName,
132: String qname, Attributes atts) throws SAXException {
133: super .startElement("http://java.sun.com/xml/ns/javaee",
134: localName, qname, atts);
135: }
136: }
137:
138: private void writeToTmpFile(byte[] bytes, String xmlFileName) {
139: try {
140: File tempFile = File.createTempFile("jaxb-output", "xml");
141: FileOutputStream out = new FileOutputStream(tempFile);
142: out.write(bytes);
143: out.close();
144: System.out.println("Jaxb output of " + xmlFileName
145: + " written to " + tempFile.getAbsolutePath());
146: } catch (IOException e) {
147: e.printStackTrace();
148: }
149: }
150:
151: private String readContent(InputStream in) throws IOException {
152: StringBuffer sb = new StringBuffer();
153: in = new BufferedInputStream(in);
154: int i = in.read();
155: while (i != -1) {
156: sb.append((char) i);
157: i = in.read();
158: }
159: String content = sb.toString();
160: return content;
161: }
162:
163: private static class TestValidationEventHandler implements
164: ValidationEventHandler {
165: public boolean handleEvent(ValidationEvent validationEvent) {
166: System.out.println(validationEvent.getMessage());
167: return true;
168: }
169: }
170: }
|