You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2007/01/23 00:51:01 UTC

svn commit: r498854 [1/2] - in /incubator/openejb/trunk/openejb3/container/openejb-jee/src: main/java/org/apache/openejb/jee/jpa/ main/java/org/apache/openejb/jee/oej2/ test/java/org/apache/openejb/jee/jpa/ test/java/org/apache/openejb/jee/oej2/ test/r...

Author: dblevins
Date: Mon Jan 22 15:50:50 2007
New Revision: 498854

URL: http://svn.apache.org/viewvc?view=rev&rev=498854
Log:
Ability to unmarshal and marshal the jpa entity mappings xml file

Added:
    incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/JpaJaxbUtil.java
    incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/Oej2JaxbUtil.java
      - copied, changed from r498545, incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/JaxbUtil.java
    incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/
    incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/JaxbTest.java
    incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/resources/jpa-mapping-full.xml   (with props)
Removed:
    incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/JaxbUtil.java
Modified:
    incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/ConversionTest.java
    incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/OpenejbJarTest.java

Added: incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/JpaJaxbUtil.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/JpaJaxbUtil.java?view=auto&rev=498854
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/JpaJaxbUtil.java (added)
+++ incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/JpaJaxbUtil.java Mon Jan 22 15:50:50 2007
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.jee.jpa;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.InputSource;
+import org.apache.openejb.jee.oej2.NamespaceFilter;
+
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.ValidationEventHandler;
+import javax.xml.bind.ValidationEvent;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.parsers.SAXParser;
+import javax.xml.transform.sax.SAXSource;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JpaJaxbUtil {
+
+    public static <T>String marshal(Class<T> type, Object object) throws JAXBException {
+        JAXBContext ctx2 = JAXBContext.newInstance(type);
+        Marshaller marshaller = ctx2.createMarshaller();
+
+        marshaller.setProperty("jaxb.formatted.output", true);
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        marshaller.marshal(object, baos);
+
+        return new String(baos.toByteArray());
+    }
+
+    public static <T>Object unmarshal(Class<T> type, InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
+        InputSource inputSource = new InputSource(in);
+
+        SAXParserFactory factory = SAXParserFactory.newInstance();
+        factory.setNamespaceAware(true);
+        factory.setValidating(false);
+        SAXParser parser = factory.newSAXParser();
+
+        JAXBContext ctx = JAXBContext.newInstance(type);
+        Unmarshaller unmarshaller = ctx.createUnmarshaller();
+        unmarshaller.setEventHandler(new ValidationEventHandler(){
+            public boolean handleEvent(ValidationEvent validationEvent) {
+                System.out.println(validationEvent);
+                return false;
+            }
+        });
+
+        return unmarshaller.unmarshal(inputSource);
+    }
+}

Copied: incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/Oej2JaxbUtil.java (from r498545, incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/JaxbUtil.java)
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/Oej2JaxbUtil.java?view=diff&rev=498854&p1=incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/JaxbUtil.java&r1=498545&p2=incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/Oej2JaxbUtil.java&r2=498854
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/JaxbUtil.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oej2/Oej2JaxbUtil.java Mon Jan 22 15:50:50 2007
@@ -24,6 +24,8 @@
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.JAXBElement;
+import javax.xml.bind.ValidationEventHandler;
+import javax.xml.bind.ValidationEvent;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParserFactory;
 import javax.xml.parsers.SAXParser;
@@ -35,7 +37,7 @@
 /**
  * @version $Rev$ $Date$
  */
-public class JaxbUtil {
+public class Oej2JaxbUtil {
 
     public static <T>String marshal(Class<T> type, Object object) throws JAXBException {
         JAXBContext ctx2 = JAXBContext.newInstance(type);
@@ -59,6 +61,13 @@
 
         JAXBContext ctx = JAXBContext.newInstance(type);
         Unmarshaller unmarshaller = ctx.createUnmarshaller();
+        unmarshaller.setEventHandler(new ValidationEventHandler(){
+            public boolean handleEvent(ValidationEvent validationEvent) {
+                System.out.println(validationEvent);
+                return false;
+            }
+        });
+
 
         NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
         xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());

Added: incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/JaxbTest.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/JaxbTest.java?view=auto&rev=498854
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/JaxbTest.java (added)
+++ incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/JaxbTest.java Mon Jan 22 15:50:50 2007
@@ -0,0 +1,69 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.jee.jpa;
+
+import junit.framework.TestCase;
+import org.apache.openejb.jee.oej2.Oej2JaxbUtil;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.BufferedInputStream;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JaxbTest extends TestCase {
+
+    public void testEntityMappings() throws Exception {
+        unmarshalAndMarshal(EntityMappings.class, "jpa-mapping-full.xml");
+    }
+
+    private <T> void unmarshalAndMarshal(Class<T> type, java.lang.String xmlFileName) throws Exception {
+        unmarshalAndMarshal(type, xmlFileName, xmlFileName);
+    }
+
+    private <T> void unmarshalAndMarshal(Class<T> type, java.lang.String xmlFileName, java.lang.String expectedFile) throws Exception {
+
+        Object object = JpaJaxbUtil.unmarshal(type, getInputStream(xmlFileName));
+
+        String actual = JpaJaxbUtil.marshal(type, object);
+
+        if (xmlFileName.equals(expectedFile)) {
+            String sourceXml = readContent(getInputStream(xmlFileName));
+            assertEquals(sourceXml, actual);
+        } else {
+            String expected = readContent(getInputStream(expectedFile));
+            assertEquals(expected, actual);
+        }
+    }
+
+    private <T>InputStream getInputStream(String xmlFileName) {
+        return getClass().getClassLoader().getResourceAsStream(xmlFileName);
+    }
+
+    private String readContent(InputStream in) throws IOException {
+        StringBuffer sb = new StringBuffer();
+        in = new BufferedInputStream(in);
+        int i = in.read();
+        while (i != -1) {
+            sb.append((char) i);
+            i = in.read();
+        }
+        return sb.toString();
+    }
+
+}

Modified: incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/ConversionTest.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/ConversionTest.java?view=diff&rev=498854&r1=498853&r2=498854
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/ConversionTest.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/ConversionTest.java Mon Jan 22 15:50:50 2007
@@ -31,7 +31,7 @@
 public class ConversionTest extends TestCase {
 
     public void testConversion() throws Exception {
-        JAXBElement element = (JAXBElement) JaxbUtil.unmarshal(OpenejbJarType.class, getInputStream("openejb-jar-2-full.xml"));
+        JAXBElement element = (JAXBElement) Oej2JaxbUtil.unmarshal(OpenejbJarType.class, getInputStream("openejb-jar-2-full.xml"));
         OpenejbJarType o2 = (OpenejbJarType) element.getValue();
 
         GeronimoEjbJarType g2 = new GeronimoEjbJarType();
@@ -48,16 +48,16 @@
         }
 
         JAXBElement root = new JAXBElement(new QName("http://geronimo.apache.org/xml/ns/j2ee/ejb/openejb-2.0","ejb-jar"), GeronimoEjbJarType.class, g2);
-        String result = JaxbUtil.marshal(GeronimoEjbJarType.class, root);
+        String result = Oej2JaxbUtil.marshal(GeronimoEjbJarType.class, root);
         assertEquals(readContent(getInputStream("geronimo-openejb-converted.xml")), result);
 
     }
 
     private <T> void unmarshalAndMarshal(Class<T> type, java.lang.String xmlFileName, java.lang.String expectedFile) throws Exception {
 
-        Object object = JaxbUtil.unmarshal(type, getInputStream(xmlFileName));
+        Object object = Oej2JaxbUtil.unmarshal(type, getInputStream(xmlFileName));
 
-        java.lang.String actual = JaxbUtil.marshal(type, object);
+        java.lang.String actual = Oej2JaxbUtil.marshal(type, object);
 
         if (xmlFileName.equals(expectedFile)) {
             String sourceXml = readContent(getInputStream(xmlFileName));

Modified: incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/OpenejbJarTest.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/OpenejbJarTest.java?view=diff&rev=498854&r1=498853&r2=498854
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/OpenejbJarTest.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-jee/src/test/java/org/apache/openejb/jee/oej2/OpenejbJarTest.java Mon Jan 22 15:50:50 2007
@@ -28,12 +28,6 @@
  */
 public class OpenejbJarTest extends TestCase {
 
-    public void testNothing() {
-    }
-
-    /**
-     * @throws Exception
-     */
     public void testValidOpenejbJar() throws Exception {
         unmarshalAndMarshal(OpenejbJarType.class, "openejb-jar-2-full.xml");
     }
@@ -52,9 +46,9 @@
 
     private <T> void unmarshalAndMarshal(Class<T> type, java.lang.String xmlFileName, java.lang.String expectedFile) throws Exception {
 
-        Object object = JaxbUtil.unmarshal(type, getInputStream(xmlFileName));
+        Object object = Oej2JaxbUtil.unmarshal(type, getInputStream(xmlFileName));
 
-        String actual = JaxbUtil.marshal(type, object);
+        String actual = Oej2JaxbUtil.marshal(type, object);
 
         if (xmlFileName.equals(expectedFile)) {
             String sourceXml = readContent(getInputStream(xmlFileName));