You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jl...@apache.org on 2021/01/25 16:23:57 UTC
[tomee] 02/02: Make sure we can also read jakarta persistence.xml
by default
This is an automated email from the ASF dual-hosted git repository.
jlmonteiro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git
commit be713f800e57c50fc8ea3518a9fc3bf7530cc759
Author: Jean-Louis Monteiro <jl...@tomitribe.com>
AuthorDate: Mon Jan 25 17:23:30 2021 +0100
Make sure we can also read jakarta persistence.xml by default
---
.../jee/jpa/unit/JaxbPersistenceFactory.java | 6 ++-
.../apache/openejb/jee/jpa/PersistenceXmlTest.java | 36 +++++++++++++
.../src/test/resources/persistence_2.0-jakarta.xml | 63 ++++++++++++++++++++++
3 files changed, 104 insertions(+), 1 deletion(-)
diff --git a/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/unit/JaxbPersistenceFactory.java b/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/unit/JaxbPersistenceFactory.java
index 1ca0617..6caa2dc 100644
--- a/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/unit/JaxbPersistenceFactory.java
+++ b/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/unit/JaxbPersistenceFactory.java
@@ -80,7 +80,11 @@ public class JaxbPersistenceFactory {
@Override
protected String eeUri(final String uri) {
- return "http://xmlns.jcp.org/xml/ns/persistence".equals(uri) ? "http://java.sun.com/xml/ns/persistence": uri;
+ // there should not be any other namespace, but let's see if we can match all of them
+ // http://java.sun.com/xml/ns/persistence
+ // http://xmlns.jcp.org/xml/ns/persistence
+ // https://jakarta.ee/xml/ns/persistence
+ return uri != null && uri.contains("/persistence") ? PERSISTENCE_SCHEMA: uri;
}
@Override
diff --git a/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/PersistenceXmlTest.java b/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/PersistenceXmlTest.java
index cc0dc4f..2d1d317 100644
--- a/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/PersistenceXmlTest.java
+++ b/container/openejb-jee/src/test/java/org/apache/openejb/jee/jpa/PersistenceXmlTest.java
@@ -20,6 +20,7 @@ package org.apache.openejb.jee.jpa;
import junit.framework.TestCase;
import org.apache.openejb.jee.JAXBContextFactory;
+import org.apache.openejb.jee.jpa.unit.JaxbPersistenceFactory;
import org.apache.openejb.jee.jpa.unit.Persistence;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
@@ -97,6 +98,41 @@ public class PersistenceXmlTest extends TestCase {
assertTrue("Files are similar " + myDiff, myDiff.similar());
}
+ public void testPersistenceJakarta() throws Exception {
+
+ // make sure we can still parse previous versions
+ {
+ final URL resource = this.getClass().getClassLoader().getResource("persistence_2.0-example.xml");
+ final InputStream in = resource.openStream();
+
+ final Persistence element = JaxbPersistenceFactory.getPersistence(Persistence.class, in);
+
+ assertNotNull(element);
+ System.out.println("unmarshalled " + element);
+ }
+ {
+ final URL resource = this.getClass().getClassLoader().getResource("persistence-example.xml");
+ final InputStream in = resource.openStream();
+
+ final Persistence element = JaxbPersistenceFactory.getPersistence(Persistence.class, in);
+
+ assertNotNull(element);
+ System.out.println("unmarshalled " + element);
+ }
+
+ // try new jakarta namespace
+ {
+ final URL resource = this.getClass().getClassLoader().getResource("persistence_2.0-jakarta.xml");
+ final InputStream in = resource.openStream();
+
+ final Persistence element = JaxbPersistenceFactory.getPersistence(Persistence.class, in);
+
+ assertNotNull(element);
+ System.out.println("unmarshalled " + element);
+ }
+
+ }
+
private java.lang.String readContent(InputStream in) throws IOException {
final StringBuilder sb = new StringBuilder();
in = new BufferedInputStream(in);
diff --git a/container/openejb-jee/src/test/resources/persistence_2.0-jakarta.xml b/container/openejb-jee/src/test/resources/persistence_2.0-jakarta.xml
new file mode 100644
index 0000000..04f1240
--- /dev/null
+++ b/container/openejb-jee/src/test/resources/persistence_2.0-jakarta.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+
+ 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.
+-->
+
+<!-- $Rev$ -->
+<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd" version="3.0">
+
+ <persistence-unit transaction-type="JTA" name="foo">
+ <description>description</description>
+ <provider>org.acme.Provider</provider>
+ <jta-data-source>jtadatasource</jta-data-source>
+ <non-jta-data-source>nonjtadatasource</non-jta-data-source>
+ <mapping-file>mappingfile1</mapping-file>
+ <mapping-file>mappingfile2</mapping-file>
+ <jar-file>jarfile1</jar-file>
+ <jar-file>jarfile2</jar-file>
+ <class>org.acme.Person</class>
+ <class>org.acme.Animal</class>
+ <exclude-unlisted-classes>true</exclude-unlisted-classes>
+ <shared-cache-mode>ALL</shared-cache-mode>
+ <validation-mode>AUTO</validation-mode>
+ <properties>
+ <property value="rab" name="bar"/>
+ <property value="zab" name="baz"/>
+ <property value="oof" name="foo"/>
+ </properties>
+ </persistence-unit>
+ <persistence-unit transaction-type="RESOURCE_LOCAL" name="bar">
+ <description>description2</description>
+ <provider>org.acme.Provider2</provider>
+ <jta-data-source>jtadatasource2</jta-data-source>
+ <non-jta-data-source>nonjtadatasource2</non-jta-data-source>
+ <mapping-file>mappingfile12</mapping-file>
+ <mapping-file>mappingfile22</mapping-file>
+ <jar-file>jarfile12</jar-file>
+ <jar-file>jarfile22</jar-file>
+ <class>org.acme.Person2</class>
+ <class>org.acme.Animal2</class>
+ <exclude-unlisted-classes>true</exclude-unlisted-classes>
+ <properties>
+ <property value="rab2" name="bar2"/>
+ <property value="oof2" name="foo2"/>
+ <property value="zab2" name="baz2"/>
+ </properties>
+ </persistence-unit>
+</persistence>