From e8ef7742ea599f2506abac821dacb242b83919b4 Mon Sep 17 00:00:00 2001 From: Jonathan Gallimore Date: Fri, 16 Nov 2018 12:39:20 +0000 Subject: [PATCH 1/2] Add simple test to ensure overrides from openejb-cmp-orm.xml work --- .../openejb/core/LegacyInterfaceTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/container/openejb-core/src/test/java/org/apache/openejb/core/LegacyInterfaceTest.java b/container/openejb-core/src/test/java/org/apache/openejb/core/LegacyInterfaceTest.java index a7f74942d58..f450f2c9b52 100644 --- a/container/openejb-core/src/test/java/org/apache/openejb/core/LegacyInterfaceTest.java +++ b/container/openejb-core/src/test/java/org/apache/openejb/core/LegacyInterfaceTest.java @@ -24,6 +24,7 @@ import org.apache.openejb.config.AppModule; import org.apache.openejb.config.ConfigurationFactory; import org.apache.openejb.config.EjbModule; +import org.apache.openejb.config.PersistenceModule; import org.apache.openejb.core.ivm.naming.InitContextFactory; import org.apache.openejb.jee.CmpField; import org.apache.openejb.jee.ContainerTransaction; @@ -34,6 +35,7 @@ import org.apache.openejb.jee.QueryMethod; import org.apache.openejb.jee.SingletonBean; import org.apache.openejb.jee.TransAttribute; +import org.apache.openejb.jee.jpa.*; import org.junit.AfterClass; import javax.ejb.CreateException; @@ -140,6 +142,80 @@ public void test() throws Exception { } + public void testCustomCmpMappings() throws Exception { + + System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName()); + + final ConfigurationFactory config = new ConfigurationFactory(); + final Assembler assembler = new Assembler(); + + assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class)); + assembler.createSecurityService(config.configureService(SecurityServiceInfo.class)); + + final EjbJar ejbJar = new EjbJar(); + ejbJar.addEnterpriseBean(new SingletonBean(MySingletonBean.class)); + ejbJar.addEnterpriseBean(new EntityBean(MyBmpBean.class, PersistenceType.BEAN)); + + final EntityBean cmp = ejbJar.addEnterpriseBean(new EntityBean(MyCmpBean.class, PersistenceType.CONTAINER)); + cmp.setPrimKeyClass(Integer.class.getName()); + cmp.setPrimkeyField("id"); + cmp.getCmpField().add(new CmpField("id")); + cmp.getCmpField().add(new CmpField("name")); + final Query query = new Query(); + query.setQueryMethod(new QueryMethod("findByPrimaryKey", Integer.class.getName())); + query.setEjbQl("SELECT OBJECT(DL) FROM License DL"); + cmp.getQuery().add(query); + final List transactions = ejbJar.getAssemblyDescriptor().getContainerTransaction(); + + transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyBmpBean", "*")); + transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyCmpBean", "*")); + transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MySingletonBean", "*")); + + final File f = new File("test").getAbsoluteFile(); + if (!f.exists() && !f.mkdirs()) { + throw new Exception("Failed to create test directory: " + f); + } + + final EntityMappings entityMappings = new EntityMappings(); + + final Entity entity = new Entity(); + entity.setClazz("openejb.org.apache.openejb.core.MyCmpBean"); + entity.setName("MyCmpBean"); + entity.setDescription("MyCmpBean"); + entity.setAttributes(new Attributes()); + + final NamedQuery namedQuery = new NamedQuery(); + namedQuery.setQuery("SELECT OBJECT(DL) FROM License DL"); + entity.getNamedQuery().add(namedQuery); + + final Id id = new Id(); + id.setName("id"); + entity.getAttributes().getId().add(id); + + final Basic basic = new Basic(); + basic.setName("name"); + final Column column = new Column(); + column.setName("wNAME"); + column.setLength(300); + basic.setColumn(column); + entity.getAttributes().getBasic().add(basic); + + entityMappings.getEntity().add(entity); + + final AppModule module = new AppModule(this.getClass().getClassLoader(), f.getAbsolutePath()); + final EjbModule ejbModule = new EjbModule(ejbJar); + ejbModule.getAltDDs().put("openejb-cmp-orm.xml", entityMappings); + module.getEjbModules().add(ejbModule); + + assertNull(module.getCmpMappings()); + assembler.createApplication(config.configureApplication(module)); + assertNotNull(module.getCmpMappings()); + final List basicList = module.getCmpMappings().getEntityMap().get("openejb.org.apache.openejb.core.MyCmpBean").getAttributes().getBasic(); + assertEquals(1, basicList.size()); + assertEquals(300, basicList.get(0).getColumn().getLength().intValue()); + assertEquals("wNAME", basicList.get(0).getColumn().getName()); + } + @LocalHome(MyLocalHome.class) @RemoteHome(MyRemoteHome.class) public static abstract class MyCmpBean implements javax.ejb.EntityBean { From f668add70884ff2038e15b0eb52bff8547c7c3dc Mon Sep 17 00:00:00 2001 From: Jonathan Gallimore Date: Fri, 16 Nov 2018 15:06:17 +0000 Subject: [PATCH 2/2] Adding further basic CMP tests --- .gitignore | 1 + .../arquillian/tests/cmp/CmpMappingTest.java | 58 +++++++++++++++++++ .../arquillian/tests/cmp/CmpServlet.java | 30 ++++++++++ .../arquillian/tests/cmp/MyCmpBean.java | 53 +++++++++++++++++ .../arquillian/tests/cmp/MyLocalHome.java | 14 +++++ .../arquillian/tests/cmp/MyLocalObject.java | 7 +++ .../arquillian/tests/cmp/MyRemoteHome.java | 14 +++++ .../arquillian/tests/cmp/MyRemoteObject.java | 9 +++ .../openejb/arquillian/tests/cmp/ejb-jar.xml | 54 +++++++++++++++++ .../arquillian/tests/cmp/openejb-cmp-orm.xml | 32 ++++++++++ .../openejb/config/ReadDescriptors.java | 3 +- .../openejb/config/ReadDescriptorsTest.java | 10 ++++ .../openejb/config/test-openejb-cmp-orm.xml | 32 ++++++++++ 13 files changed, 316 insertions(+), 1 deletion(-) create mode 100644 arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/CmpMappingTest.java create mode 100644 arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/CmpServlet.java create mode 100644 arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyCmpBean.java create mode 100644 arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyLocalHome.java create mode 100644 arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyLocalObject.java create mode 100644 arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyRemoteHome.java create mode 100644 arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyRemoteObject.java create mode 100644 arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/resources/org/apache/openejb/arquillian/tests/cmp/ejb-jar.xml create mode 100644 arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/resources/org/apache/openejb/arquillian/tests/cmp/openejb-cmp-orm.xml create mode 100644 container/openejb-core/src/test/resources/org/apache/openejb/config/test-openejb-cmp-orm.xml diff --git a/.gitignore b/.gitignore index 548c9c39922..fa39aa3040f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ quick.bat /temp /report.txt nb-configuration.xml +.factorypath diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/CmpMappingTest.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/CmpMappingTest.java new file mode 100644 index 00000000000..4c8cf41b964 --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/CmpMappingTest.java @@ -0,0 +1,58 @@ +/* + * 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.arquillian.tests.cmp; + +import org.apache.ziplock.IO; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.net.URL; + +/** + * @version $Rev$ $Date$ + */ +@RunWith(Arquillian.class) +public class CmpMappingTest { + + @ArquillianResource + private URL url; + + @Deployment(testable = false) + public static WebArchive createDeployment() { + WebArchive archive = ShrinkWrap.create(WebArchive.class, CmpMappingTest.class.getSimpleName() + ".war") + .addClasses(CmpServlet.class, MyCmpBean.class, MyLocalHome.class, MyLocalObject.class, MyRemoteHome.class, MyRemoteObject.class) + .addAsWebInfResource(new ClassLoaderAsset("org/apache/openejb/arquillian/tests/cmp/openejb-cmp-orm.xml"), "openejb-cmp-orm.xml") + .addAsWebInfResource(new ClassLoaderAsset("org/apache/openejb/arquillian/tests/cmp/ejb-jar.xml"), "ejb-jar.xml"); + + System.out.println(archive.toString(true)); + return archive; + } + + @Test + @RunAsClient + public void checkCmpJpaEntityORMMappings() throws Exception { + final String output = IO.readString(new URL(url.toExternalForm())); + System.out.println(output); + } +} diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/CmpServlet.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/CmpServlet.java new file mode 100644 index 00000000000..c843a9cde3e --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/CmpServlet.java @@ -0,0 +1,30 @@ +package org.apache.openejb.arquillian.tests.cmp; + +import org.apache.openejb.assembler.classic.AppInfo; +import org.apache.openejb.assembler.classic.Assembler; +import org.apache.openejb.loader.SystemInstance; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Collection; + +@WebServlet(name="Cmp", urlPatterns = "/*") +public class CmpServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + final Assembler assembler = SystemInstance.get().getComponent(Assembler.class); + final Collection deployedApplications = assembler.getDeployedApplications(); + + for (final AppInfo deployedApplication : deployedApplications) { + if ("CmpMappingTest".equals(deployedApplication.appId)) { + final String cmpMappingsXml = deployedApplication.cmpMappingsXml; + resp.getWriter().write(cmpMappingsXml == null ? "null" : cmpMappingsXml); + } + } + } +} diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyCmpBean.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyCmpBean.java new file mode 100644 index 00000000000..20ecd76fda2 --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyCmpBean.java @@ -0,0 +1,53 @@ +package org.apache.openejb.arquillian.tests.cmp; + +import javax.ejb.CreateException; +import javax.ejb.EntityBean; +import javax.ejb.EntityContext; +import javax.ejb.LocalHome; +import javax.ejb.RemoteHome; +import javax.ejb.RemoveException; + +@LocalHome(MyLocalHome.class) +@RemoteHome(MyRemoteHome.class) +public abstract class MyCmpBean implements EntityBean { + + // CMP + public abstract Integer getId(); + + public abstract void setId(Integer id); + + public abstract String getName(); + + public abstract void setName(String number); + + public void doit() { + } + + public Integer ejbCreateObject(final String id) throws CreateException { + return null; + } + + public void ejbPostCreateObject(final String id) { + } + + public void setEntityContext(final EntityContext ctx) { + } + + public void unsetEntityContext() { + } + + public void ejbActivate() { + } + + public void ejbPassivate() { + } + + public void ejbLoad() { + } + + public void ejbStore() { + } + + public void ejbRemove() throws RemoveException { + } +} \ No newline at end of file diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyLocalHome.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyLocalHome.java new file mode 100644 index 00000000000..8eb489ee47c --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyLocalHome.java @@ -0,0 +1,14 @@ +package org.apache.openejb.arquillian.tests.cmp; + +public interface MyLocalHome extends javax.ejb.EJBLocalHome { + + public MyLocalObject createObject(String name) + throws javax.ejb.CreateException; + + public MyLocalObject findByPrimaryKey(Integer primarykey) + throws javax.ejb.FinderException; + + public java.util.Collection findEmptyCollection() + throws javax.ejb.FinderException; + +} diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyLocalObject.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyLocalObject.java new file mode 100644 index 00000000000..c0075713168 --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyLocalObject.java @@ -0,0 +1,7 @@ +package org.apache.openejb.arquillian.tests.cmp; + +public interface MyLocalObject extends javax.ejb.EJBLocalObject { + + public void doit(); + +} diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyRemoteHome.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyRemoteHome.java new file mode 100644 index 00000000000..9153ad60696 --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyRemoteHome.java @@ -0,0 +1,14 @@ +package org.apache.openejb.arquillian.tests.cmp; + +public interface MyRemoteHome extends javax.ejb.EJBHome { + + public MyRemoteObject createObject(String name) + throws javax.ejb.CreateException, java.rmi.RemoteException; + + public MyRemoteObject findByPrimaryKey(Integer primarykey) + throws javax.ejb.FinderException, java.rmi.RemoteException; + + public java.util.Collection findEmptyCollection() + throws javax.ejb.FinderException, java.rmi.RemoteException; + +} diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyRemoteObject.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyRemoteObject.java new file mode 100644 index 00000000000..c7452897aff --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/cmp/MyRemoteObject.java @@ -0,0 +1,9 @@ +package org.apache.openejb.arquillian.tests.cmp; + +import java.rmi.RemoteException; + +public interface MyRemoteObject extends javax.ejb.EJBObject { + + public void doit() throws RemoteException; + +} diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/resources/org/apache/openejb/arquillian/tests/cmp/ejb-jar.xml b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/resources/org/apache/openejb/arquillian/tests/cmp/ejb-jar.xml new file mode 100644 index 00000000000..d9b2065783e --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/resources/org/apache/openejb/arquillian/tests/cmp/ejb-jar.xml @@ -0,0 +1,54 @@ + + + + + + MyCmpBean + org.apache.openejb.arquillian.tests.cmp.MyCmpBean + Container + java.lang.Integer + false + + name + + id + + + findByPrimaryKey + + java.lang.Integer + + + SELECT OBJECT(DL) FROM License DL + + + + + + + MyCmpBean + * + + Supports + + + diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/resources/org/apache/openejb/arquillian/tests/cmp/openejb-cmp-orm.xml b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/resources/org/apache/openejb/arquillian/tests/cmp/openejb-cmp-orm.xml new file mode 100644 index 00000000000..19cf79d6026 --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/resources/org/apache/openejb/arquillian/tests/cmp/openejb-cmp-orm.xml @@ -0,0 +1,32 @@ + + + + + MyCmpBean + + SELECT OBJECT(DL) FROM License DL + + + + + + + + + \ No newline at end of file diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/ReadDescriptors.java b/container/openejb-core/src/main/java/org/apache/openejb/config/ReadDescriptors.java index 814ebf152cb..171a563b48f 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/config/ReadDescriptors.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/ReadDescriptors.java @@ -548,7 +548,8 @@ else if ("ALL".equalsIgnoreCase(beanDiscoveryMode) && beans.isTrim()) { return current; } - private void readCmpOrm(final EjbModule ejbModule) throws OpenEJBException { + // package scoped for testing + void readCmpOrm(final EjbModule ejbModule) throws OpenEJBException { final Object data = ejbModule.getAltDDs().get("openejb-cmp-orm.xml"); if (data != null && !(data instanceof EntityMappings)) { if (data instanceof URL) { diff --git a/container/openejb-core/src/test/java/org/apache/openejb/config/ReadDescriptorsTest.java b/container/openejb-core/src/test/java/org/apache/openejb/config/ReadDescriptorsTest.java index c8d029d958c..a46e9a92bae 100644 --- a/container/openejb-core/src/test/java/org/apache/openejb/config/ReadDescriptorsTest.java +++ b/container/openejb-core/src/test/java/org/apache/openejb/config/ReadDescriptorsTest.java @@ -19,6 +19,8 @@ import org.apache.openejb.config.sys.Resource; import org.apache.openejb.config.sys.Resources; +import org.apache.openejb.jee.EjbJar; +import org.apache.openejb.jee.jpa.EntityMappings; import org.junit.Assert; import org.junit.Test; @@ -92,4 +94,12 @@ public void testLazyResource() { Assert.assertNull(res.getProperties().getProperty("InitializeAfterDeployment")); } + @Test + public void testReadCmpOrmDescriptor() throws Exception { + final EjbModule ejbModule = new EjbModule(new EjbJar()); + ejbModule.getAltDDs().put("openejb-cmp-orm.xml", getClass().getResource("test-openejb-cmp-orm.xml")); + new ReadDescriptors().readCmpOrm(ejbModule); + Assert.assertNotNull(ejbModule.getAltDDs().get("openejb-cmp-orm.xml")); + Assert.assertTrue(EntityMappings.class.isInstance(ejbModule.getAltDDs().get("openejb-cmp-orm.xml"))); + } } diff --git a/container/openejb-core/src/test/resources/org/apache/openejb/config/test-openejb-cmp-orm.xml b/container/openejb-core/src/test/resources/org/apache/openejb/config/test-openejb-cmp-orm.xml new file mode 100644 index 00000000000..f26eb72d0ce --- /dev/null +++ b/container/openejb-core/src/test/resources/org/apache/openejb/config/test-openejb-cmp-orm.xml @@ -0,0 +1,32 @@ + + + + + MyCmpBean + + SELECT OBJECT(DL) FROM License DL + + + + + + + + + \ No newline at end of file