tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: Patch from Svetlin Zarev fixing the pojo usage of @DataSourceDefinition
Date Sat, 17 Jun 2017 08:13:08 GMT
Repository: tomee
Updated Branches:
  refs/heads/master 3e29ab90b -> ea6127f73


Patch from Svetlin Zarev fixing the pojo usage of @DataSourceDefinition


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/ea6127f7
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/ea6127f7
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/ea6127f7

Branch: refs/heads/master
Commit: ea6127f73299c5ce0e7b39e68e5cab4f7e6c036d
Parents: 3e29ab9
Author: rmannibucau <rmannibucau@apache.org>
Authored: Sat Jun 17 10:13:02 2017 +0200
Committer: rmannibucau <rmannibucau@apache.org>
Committed: Sat Jun 17 10:13:02 2017 +0200

----------------------------------------------------------------------
 .../datasource/definition/DataSourceBean.java   | 21 ++-----
 .../definition/DataSourceDefinitionTest.java    | 21 +++++++
 .../definition/DataSourceServlet.java           | 65 ++++++++++++++------
 .../config/ConvertDataSourceDefinitions.java    | 14 +++++
 4 files changed, 88 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/ea6127f7/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceBean.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceBean.java
b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceBean.java
index 1234ebd..8a304d5 100644
--- a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceBean.java
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceBean.java
@@ -21,10 +21,7 @@ import javax.annotation.Resource;
 import javax.annotation.sql.DataSourceDefinition;
 import javax.ejb.LocalBean;
 import javax.ejb.Stateless;
-import javax.naming.InitialContext;
 import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.SQLException;
 
 @Stateless
 @LocalBean
@@ -33,20 +30,14 @@ public class DataSourceBean {
     @Resource(name = "jdbc/database")
     private DataSource dataSource;
 
-    public String getUrlFromInjectedDataSource() throws SQLException {
-        return getDataSourceUrl(dataSource);
-    }
-
-    public String getUrlFromLookedUpDataSource() throws Exception {
-        final InitialContext initialContext = new InitialContext();
-        final DataSource ds = (DataSource) initialContext.lookup("java:comp/env/jdbc/database");
+    @Resource(name = "jdbc/pojodb")
+    private DataSource dataSourceFromPojo;
 
-        return getDataSourceUrl(ds);
+    public DataSource getDataSource() {
+        return dataSource;
     }
 
-    private String getDataSourceUrl(DataSource dataSource) throws SQLException {
-        try (Connection connection = dataSource.getConnection()) {
-            return connection.getMetaData().getURL();
-        }
+    public DataSource getDataSourceFromPojo() {
+        return dataSourceFromPojo;
     }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/ea6127f7/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceDefinitionTest.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceDefinitionTest.java
b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceDefinitionTest.java
index 2691dc1..a5bb716 100644
--- a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceDefinitionTest.java
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceDefinitionTest.java
@@ -65,6 +65,7 @@ public class DataSourceDefinitionTest {
                 .addClass(DataSourceDefinitionTest.class)
                 .addClass(DataSourceServlet.class)
                 .addClass(DataSourceBean.class)
+                .addClass(DataSourcePojo.class)
                 .addClass(Runner.class)
                 .addAsLibraries(JarLocation.jarLocation(Test.class))
                 .addAsWebInfResource(new ClassLoaderAsset(CONTENT_LOCATION_EJB_JAR_XML),
RESOURCE_EJB_JAR_XML)
@@ -89,10 +90,30 @@ public class DataSourceDefinitionTest {
     }
 
     @Test
+    public void testDataSourceFromPojoInjectionInServlet() throws IOException {
+        validateTest("testDataSourceFromPojoInjectionInServlet");
+    }
+
+    @Test
+    public void testDataSourceFromPojoLookUpFromServlet() throws IOException {
+        validateTest("testDataSourceFromPojoLookUpFromServlet");
+    }
+
+    @Test
     public void testDataSourceLookUpFromEjb() throws IOException {
         validateTest("testDataSourceLookUpFromEjb");
     }
 
+    @Test
+    public void testDataSourceFromPojoInjectionInEjbDSFromPojo() throws IOException {
+        validateTest("testDataSourceFromPojoInjectionInEjbDSFromPojo");
+    }
+
+    @Test
+    public void testDataSourceFromPojoLookUpFromEjb() throws IOException {
+        validateTest("testDataSourceFromPojoLookUpFromEjb");
+    }
+
     private void validateTest(String testName) throws IOException {
         final String expectedOutput = testName + "=true";
 

http://git-wip-us.apache.org/repos/asf/tomee/blob/ea6127f7/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceServlet.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceServlet.java
b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceServlet.java
index bd8664e..319777d 100644
--- a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceServlet.java
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/datasource/definition/DataSourceServlet.java
@@ -21,6 +21,7 @@ package org.apache.openejb.arquillian.tests.datasource.definition;
 import javax.annotation.Resource;
 import javax.ejb.EJB;
 import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -34,10 +35,14 @@ import java.sql.Connection;
 import java.sql.SQLException;
 
 public class DataSourceServlet extends HttpServlet {
-    private static final String EXPECTED_URL = "jdbc:hsqldb:mem:expected";
+    private static final String EXPECTED_URL_BEAN = "jdbc:hsqldb:mem:expected";
+    private static final String EXPECTED_URL_POJO = "jdbc:hsqldb:mem:pojo";
 
     @Resource(name = "jdbc/database")
-    DataSource dataSource;
+    DataSource dataSourceFromManagedBean;
+
+    @Resource(name = "jdbc/pojodb")
+    DataSource dataSourceFromPojo;
 
     @EJB
     DataSourceBean dataSourceBean;
@@ -59,33 +64,57 @@ public class DataSourceServlet extends HttpServlet {
     }
 
     public void testDataSourceInjectionInEjb() throws SQLException {
-        final String dataSourceUrl = dataSourceBean.getUrlFromInjectedDataSource();
-        if (!EXPECTED_URL.equals(dataSourceUrl)) {
-            throw new IllegalStateException("[BEAN - INJECTION] Unexpected URL: " + dataSourceUrl);
-        }
+        final DataSource ds = dataSourceBean.getDataSource();
+        verifyDataSource(ds, EXPECTED_URL_BEAN, "BEAN, INJECTION, DEF_FROM_MANAGED_BEAN");
     }
 
     public void testDataSourceLookUpFromEjb() throws Exception {
-        final String dataSourceUrl = dataSourceBean.getUrlFromLookedUpDataSource();
-        if (!EXPECTED_URL.equals(dataSourceUrl)) {
-            throw new IllegalStateException("[BEAN - LOOKUP] Unexpected URL: " + dataSourceUrl);
-        }
+        final DataSource ds = dataSourceBean.getDataSource();
+        verifyDataSource(ds, EXPECTED_URL_BEAN, "BEAN, LOOKUP, DEF_FROM_MANAGED_BEAN");
+    }
+
+    public void testDataSourceFromPojoInjectionInEjbDSFromPojo() throws SQLException {
+        final DataSource ds = dataSourceBean.getDataSourceFromPojo();
+        verifyDataSource(ds, EXPECTED_URL_POJO, "BEAN, INJECTION, DEF_FROM_POJO");
     }
 
+    public void testDataSourceFromPojoLookUpFromEjb() throws Exception {
+        final DataSource ds = dataSourceBean.getDataSourceFromPojo();
+        verifyDataSource(ds, EXPECTED_URL_POJO, "BEAN, LOOKUP, DEF_FROM_POJO");
+    }
+
+
     public void testDataSourceInjectionInServlet() throws SQLException {
-        final String dataSourceUrl = getDataSourceUrl(dataSource);
-        if (!EXPECTED_URL.equals(dataSourceUrl)) {
-            throw new IllegalStateException("[SERVLET - INJECTION] Unexpected URL: " + dataSourceUrl);
-        }
+        verifyDataSource(dataSourceFromManagedBean, EXPECTED_URL_BEAN, "SERVLET, INJECTION,
DEF_FROM_MANAGED_BEAN");
     }
 
     public void testDataSourceLookUpFromServlet() throws Exception {
+        final DataSource ds = lookUpDataSource("java:comp/env/jdbc/database");
+        verifyDataSource(ds, EXPECTED_URL_BEAN, "SERVLET, LOOKUP, DEF_FROM_MANAGED_BEAN");
+    }
+
+    public void testDataSourceFromPojoInjectionInServlet() throws SQLException {
+        verifyDataSource(dataSourceFromPojo, EXPECTED_URL_POJO, "SERVLET, INJECTION, DEF_FROM_POJO");
+    }
+
+    public void testDataSourceFromPojoLookUpFromServlet() throws Exception {
+        final DataSource ds = lookUpDataSource("java:comp/env/jdbc/pojodb");
+        verifyDataSource(ds, EXPECTED_URL_POJO, "SERVLET, LOOKUP, DEF_FROM_POJO");
+    }
+
+    private DataSource lookUpDataSource(String name) throws NamingException {
         final InitialContext initialContext = new InitialContext();
-        final DataSource ds = (DataSource) initialContext.lookup("java:comp/env/jdbc/database");
+        return (DataSource) initialContext.lookup(name);
+    }
+
+    private void verifyDataSource(DataSource dataSource, String expectedUrl, String context)
throws SQLException {
+        final String dataSourceUrl = getDataSourceUrl(dataSource);
 
-        final String dataSourceUrl = getDataSourceUrl(ds);
-        if (!EXPECTED_URL.equals(dataSourceUrl)) {
-            throw new IllegalStateException("[SERVLET - LOOKUP] Unexpected URL: " + dataSourceUrl);
+        if (!expectedUrl.equals(dataSourceUrl)) {
+            throw new IllegalStateException("[" + context + "] "
+                    + "Expected={URL=" + EXPECTED_URL_BEAN + "} "
+                    + "Actual={URL=" + dataSourceUrl + "}"
+            );
         }
     }
 

http://git-wip-us.apache.org/repos/asf/tomee/blob/ea6127f7/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java
b/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java
index 568e95a..d7bb6f3 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java
@@ -29,6 +29,7 @@ import org.apache.openejb.util.PropertyPlaceHolderHelper;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 
 /**
@@ -42,6 +43,7 @@ public class ConvertDataSourceDefinitions extends BaseConvertDefinitions
{
         final List<JndiConsumer> jndiConsumers = collectConsumers(appModule);
 
         final KeyedCollection<String, DataSource> dataSources = new KeyedCollection<String,
DataSource>();
+        final KeyedCollection<String, DataSource> dataSourcesFromCompManagedBeans =
new KeyedCollection<String, DataSource>();
 
         for (final JndiConsumer consumer : jndiConsumers) {
             if (consumer == null) {
@@ -52,12 +54,24 @@ public class ConvertDataSourceDefinitions extends BaseConvertDefinitions
{
                 /*
                  * TOMEE-2053: It may contain invalid datasource definitions
                  * because it is never updated with content from the ejb-jar.xml
+                 * Wait until all other consumers have been processed, to safely
+                 * decide which data sources to transfer;
                  */
+
+                dataSourcesFromCompManagedBeans.addAll(consumer.getDataSource());
                 continue;
             }
             dataSources.addAll(consumer.getDataSource());
         }
 
+        final Map<String, DataSource> dataSourcesMap = dataSources.toMap();
+        for(DataSource dataSource : dataSourcesFromCompManagedBeans){
+            //Interested only in DataSources that come from non-JndiConsumers
+            if(!dataSourcesMap.containsKey(dataSource.getKey())){
+                dataSources.add(dataSource);
+            }
+        }
+
         for (final DataSource dataSource : dataSources) {
             appModule.getResources().add(toResource(dataSource));
         }


Mime
View raw message