Author: rmannibucau
Date: Sun May 13 22:52:57 2012
New Revision: 1338001

URL: http://svn.apache.org/viewvc?rev=1338001&view=rev
Log:
global should be global even for datasource

Added:
    
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionJndiTest.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java?rev=1338001&r1=1338000&r2=1338001&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
 Sun May 13 22:52:57 2012
@@ -882,7 +882,9 @@ public class AutoConfig implements Dynam
                     && 
resource.getProperties().getProperty(ORIGIN_FLAG).equals(ORIGIN_ANNOTATION)) {
                 properties.remove(ORIGIN_FLAG);
 
-                resourceInfo.id = module.getModuleId() + "/" + resourceInfo.id;
+                if (!(resourceInfo.id.startsWith("global") || 
resourceInfo.id.startsWith("/global"))) {
+                    resourceInfo.id = module.getModuleId() + "/" + 
resourceInfo.id;
+                }
 
                 if (properties.get("JdbcUrl") == null) {
                     final String url = getVendorUrl(properties);

Added: 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionJndiTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionJndiTest.java?rev=1338001&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionJndiTest.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionJndiTest.java
 Sun May 13 22:52:57 2012
@@ -0,0 +1,127 @@
+/*
+ * 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.assembler.classic;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import javax.annotation.Resource;
+import javax.annotation.sql.DataSourceDefinition;
+import javax.annotation.sql.DataSourceDefinitions;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
+import javax.ejb.Stateless;
+import javax.sql.DataSource;
+import org.apache.commons.dbcp.DelegatingConnection;
+import org.apache.commons.dbcp.managed.ManagedConnection;
+import org.apache.commons.dbcp.managed.PoolableManagedConnection;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.junit.Module;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.resource.jdbc.DataSourceFactory;
+import org.apache.openejb.spi.ContainerSystem;
+import org.hsqldb.jdbc.JDBCConnection;
+import org.hsqldb.jdbc.JDBCDataSource;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.junit.Assert.assertThat;
+
+@RunWith(ApplicationComposer.class)
+public class DataSourceDefinitionJndiTest {
+
+    @EJB
+    private DatasourceDefinitionsBean multipleDatasources;
+
+    @Module
+    public Class<?>[] app() throws Exception {
+        return new Class<?>[]{DatasourceDefinitionsBean.class};
+    }
+
+
+    @DataSourceDefinitions({
+            @DataSourceDefinition(
+                    name = "java:global/foo",
+                    className = "org.hsqldb.jdbc.JDBCDataSource",
+                    user = "sa",
+                    password = "",
+                    url = "jdbc:hsqldb:mem:dsdjt1"
+            ),
+            @DataSourceDefinition(
+                    name = "java:app/foo",
+                    className = "org.hsqldb.jdbc.JDBCDataSource",
+                    user = "sa",
+                    password = "",
+                    url = "jdbc:hsqldb:mem:dsdjt2"
+            )
+    })
+    @Stateless
+    public static class DatasourceDefinitionsBean {
+        @Resource(name = "java:app/foo")
+        private DataSource app;
+        @Resource(name = "java:global/foo")
+        private DataSource global;
+
+        public DataSource getApp() {
+            return app;
+        }
+
+        public DataSource getGlobal() {
+            return global;
+        }
+    }
+
+    @Test
+    public void checkInjections() throws Exception {
+        final DataSource global = multipleDatasources.getGlobal();
+        check(global, "dsdjt1");
+        final DataSource app = multipleDatasources.getApp();
+        check(app, "dsdjt2");
+    }
+
+    @Test
+    public void checkGlobal() throws Exception {
+        final DataSource ds = (DataSource) 
SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext().lookup("java:global/foo");
+        check(ds, "dsdjt1");
+    }
+
+    private void check(final DataSource ds, final String name) throws 
SQLException, NoSuchMethodException, InvocationTargetException, 
IllegalAccessException {
+        // the first "cast part" is not important, we just want to check the 
jdbc url is ok
+        assertThat(ds, 
instanceOf(DataSourceFactory.DbcpManagedDataSource.class));
+        final DataSourceFactory.DbcpManagedDataSource dbcp = 
(DataSourceFactory.DbcpManagedDataSource) ds;
+        final Connection connection = dbcp.getConnection();
+        assertThat(connection, instanceOf(ManagedConnection.class));
+        final ManagedConnection mc = (ManagedConnection) connection;
+        final Method getInnermostDelegateInternal = 
DelegatingConnection.class.getDeclaredMethod("getInnermostDelegateInternal");
+        getInnermostDelegateInternal.setAccessible(true);
+        final Connection delegate = (Connection) 
getInnermostDelegateInternal.invoke(mc);
+        assertThat(delegate, instanceOf(JDBCConnection.class));
+        final Method getURL = JDBCConnection.class.getDeclaredMethod("getURL");
+        getURL.setAccessible(true);
+        assertEquals("jdbc:hsqldb:mem:" + name, getURL.invoke(delegate));
+    }
+}


Reply via email to