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.jpa;
018:
019: import junit.framework.TestCase;
020:
021: import javax.xml.bind.JAXBContext;
022: import javax.xml.bind.Unmarshaller;
023: import javax.xml.bind.ValidationEventHandler;
024: import javax.xml.bind.ValidationEvent;
025: import javax.xml.bind.Marshaller;
026: import java.io.InputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.BufferedInputStream;
029: import java.io.IOException;
030: import java.io.ByteArrayInputStream;
031: import java.net.URL;
032:
033: import org.apache.openejb.jee.jpa.unit.Persistence;
034: import org.custommonkey.xmlunit.Diff;
035:
036: /**
037: * @version $Revision: 600204 $ $Date: 2007-12-01 14:35:03 -0800 $
038: */
039: public class PersistenceXmlTest extends TestCase {
040:
041: /**
042: * TODO: What does the test test out? The comparision using assertEquals doesn't seem to work well with xml files.
043: *
044: * @throws Exception
045: */
046: public void testAll() throws Exception {
047: // Persistence persistence = new Persistence();
048: // persistence.setVersion("1.0");
049: // Persistence.PersistenceUnit persistenceUnit = new Persistence.PersistenceUnit();
050: // persistenceUnit.setDescription("description");
051: // persistenceUnit.setExcludeUnlistedClasses(true);
052: // persistenceUnit.setJtaDataSource("jtadatasource");
053: // persistenceUnit.setName("name");
054: // persistenceUnit.setNonJtaDataSource("nonjtadatasource");
055: // persistenceUnit.setProvider("org.acme.Provider");
056: // persistenceUnit.setTransactionType(TransactionType.JTA);
057: // persistenceUnit.getClazz().add("org.acme.Person");
058: // persistenceUnit.getClazz().add("org.acme.Animal");
059: // persistenceUnit.getJarFile().add("jarfile1");
060: // persistenceUnit.getJarFile().add("jarfile2");
061: // persistenceUnit.getMappingFile().add("mappingfile1");
062: // persistenceUnit.getMappingFile().add("mappingfile2");
063: //
064: // Persistence.PersistenceUnit.Properties properties = new Persistence.PersistenceUnit.Properties();
065: // properties.setProperty("foo","oof");
066: // properties.setProperty("bar","rab");
067: // properties.setProperty("baz","zab");
068: // persistenceUnit.setProperties(properties);
069: //
070: // persistenceUnit.setProperties(properties);
071: // persistence.getPersistenceUnit().add(persistenceUnit);
072:
073: JAXBContext ctx = JAXBContext.newInstance(Persistence.class);
074: Unmarshaller unmarshaller = ctx.createUnmarshaller();
075:
076: URL resource = this .getClass().getClassLoader().getResource(
077: "persistence-example.xml");
078: InputStream in = resource.openStream();
079: java.lang.String expected = readContent(in);
080:
081: Persistence element = (Persistence) unmarshaller
082: .unmarshal(new ByteArrayInputStream(expected.getBytes()));
083: unmarshaller.setEventHandler(new TestValidationEventHandler());
084: System.out.println("unmarshalled");
085:
086: Marshaller marshaller = ctx.createMarshaller();
087: marshaller.setProperty("jaxb.formatted.output", true);
088:
089: ByteArrayOutputStream baos = new ByteArrayOutputStream();
090: marshaller.marshal(element, baos);
091:
092: String actual = new String(baos.toByteArray());
093:
094: Diff myDiff = new Diff(expected, actual);
095: assertTrue("Files are similar " + myDiff, myDiff.similar());
096: }
097:
098: private java.lang.String readContent(InputStream in)
099: throws IOException {
100: StringBuffer sb = new StringBuffer();
101: in = new BufferedInputStream(in);
102: int i = in.read();
103: while (i != -1) {
104: sb.append((char) i);
105: i = in.read();
106: }
107: java.lang.String content = sb.toString();
108: return content;
109: }
110:
111: private static class TestValidationEventHandler implements
112: ValidationEventHandler {
113: public boolean handleEvent(ValidationEvent validationEvent) {
114: System.out.println(validationEvent.getMessage());
115: return true;
116: }
117: }
118: }
|