Bug 512255 - Eclipselink JPA/Auditing capablity in EE Environment fails with JNDI name parameter type

Signed-off-by: Joe Grassel <fyrewyld@gmail.com>
Reviewed-by: Will Dazey <dazeydev.3@gmail.com>
diff --git a/jpa/eclipselink.jpa.test.jse/antbuild.xml b/jpa/eclipselink.jpa.test.jse/antbuild.xml
index 133bd75..a6e1bfe 100644
--- a/jpa/eclipselink.jpa.test.jse/antbuild.xml
+++ b/jpa/eclipselink.jpa.test.jse/antbuild.xml
@@ -68,13 +68,16 @@
     
     <target name="test-run" depends="compile, weave">
         <mkdir dir="${reports.dir}"/>
+        <property name="rmi.port" value="1099"/>
         <property name="additional.jvmargs" value="-Ddummy2=dummy"/>
+        <property name="rmi" value="-Djava.naming.provider.url=rmi://localhost:${rmi.port} -Djava.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory"/>
         <junit printsummary="on" haltonfailure="false" failureproperty="fail" fork="yes" dir="${jse.classes.dir}" forkmode="once">
-            <jvmarg line="${additional.jvmargs}"/>
+            <jvmarg line="${additional.jvmargs} ${rmi}"/>
             <sysproperty key="javax.persistence.jdbc.url" value="${db.url}"/>
             <sysproperty key="javax.persistence.jdbc.driver" value="${db.driver}"/>
             <sysproperty key="javax.persistence.jdbc.user" value="${db.user}"/>
             <sysproperty key="javax.persistence.jdbc.password" value="${db.pwd}"/>
+            <sysproperty key="rmi.port" value="${rmi.port}"/>
             <classpath>
                 <path refid="run.jse.path" />
             </classpath>
diff --git a/jpa/eclipselink.jpa.test.jse/src/org/eclipse/persistence/jpa/test/basic/TestBasicPersistence.java b/jpa/eclipselink.jpa.test.jse/src/org/eclipse/persistence/jpa/test/basic/TestBasicPersistence.java
index 83c3d72..5130d9b 100644
--- a/jpa/eclipselink.jpa.test.jse/src/org/eclipse/persistence/jpa/test/basic/TestBasicPersistence.java
+++ b/jpa/eclipselink.jpa.test.jse/src/org/eclipse/persistence/jpa/test/basic/TestBasicPersistence.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2014, 2015 IBM Corporation. All rights reserved.
+ * Copyright (c) 2014, 2017 IBM Corporation. All rights reserved.
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
  * which accompanies this distribution.
@@ -12,14 +12,32 @@
  *       - 450010 : Add java se test bucket
  *     12/19/2014 - Dalia Abo Sheasha
  *       - 454917 : Added a test to use the IDENTITY strategy to generate values
+ *     02/16/2017 - Jody Grassel
+ *       - 512255 : Eclipselink JPA/Auditing capablity in EE Environment fails with JNDI name parameter type
  ******************************************************************************/
 package org.eclipse.persistence.jpa.test.basic;
 
+import java.io.Serializable;
+import java.lang.management.ManagementFactory;
+import java.rmi.Remote;
+import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.logging.Logger;
 
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXConnectorServerFactory;
+import javax.management.remote.JMXServiceURL;
+import javax.naming.InitialContext;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
+import javax.sql.DataSource;
 
+import org.eclipse.persistence.config.PersistenceUnitProperties;
 import org.eclipse.persistence.jpa.test.basic.model.Dog;
 import org.eclipse.persistence.jpa.test.basic.model.Employee;
 import org.eclipse.persistence.jpa.test.basic.model.Person;
@@ -29,19 +47,65 @@
 import org.eclipse.persistence.jpa.test.framework.EmfRunner;
 import org.eclipse.persistence.jpa.test.framework.Property;
 import org.eclipse.persistence.jpa.test.framework.SQLListener;
+import org.junit.AfterClass;
 import org.junit.Assert;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 @RunWith(EmfRunner.class)
 public class TestBasicPersistence {
-    @Emf(createTables = DDLGen.DROP_CREATE, classes = { Dog.class, XmlFish.class, Person.class, Employee.class }, properties = {
-        @Property(name = "eclipselink.cache.shared.default", value = "false") }, mappingFiles = { "META-INF/fish-orm.xml" })
+    @Emf(createTables = DDLGen.DROP_CREATE, classes = { Dog.class, XmlFish.class, Person.class, Employee.class }, 
+            properties = {@Property(name = "eclipselink.cache.shared.default", value = "false")}, 
+            mappingFiles = { "META-INF/fish-orm.xml" })
     private EntityManagerFactory emf;
 
     @SQLListener
     List<String> _sql;
+    
+    private static final int rmiPort;
+    private static final String dsName = "mockDataSource";
+    private static final BogusDataSource mockDataSource = new BogusDataSource("tmpDataSourceImp getConnection called");
+    private static Registry rmiRegistry = null;
+    private static JMXConnectorServer connector = null;
+    
+    static {
+        int rmiPortVal = 1099;
+        
+        String rmiPortProp = System.getProperty("rmi.port");
+        if (!(rmiPortProp == null || rmiPortProp.isEmpty())) {
+            try {
+                rmiPortVal = new Integer(rmiPortProp);
+            } catch (NumberFormatException nfe) {
+                // Use default value.
+            }
+        }
+            
+        rmiPort = rmiPortVal;
+    }
+    
+    @BeforeClass
+    public static void beforeClass() throws Exception {
+        rmiRegistry = LocateRegistry.createRegistry(rmiPort);
+        
+        // Create and Bind Mock Data Source
+        rmiRegistry.bind(dsName, mockDataSource);       
 
+        connector = JMXConnectorServerFactory.newJMXConnectorServer(new JMXServiceURL("service:jmx:rmi://localhost:" + rmiPort),
+                new HashMap<String, Object>(), ManagementFactory.getPlatformMBeanServer());
+    }
+
+    @AfterClass
+    public static void afterClass() throws Exception {
+        if (rmiRegistry != null) {
+            rmiRegistry.unbind(dsName);
+        } 
+        
+        if (connector != null) {
+            connector.stop();
+        }
+    }
+    
     @Test
     public void activeTransaction() {
         Assert.assertNotNull(emf);
@@ -105,4 +169,68 @@ public void identityStrategyTest() {
             em.close();
         }
     }
+    
+    @Test
+    public void testNonJTADataSourceOverride() throws Exception {
+        if (emf == null)
+            return;
+        
+        InitialContext ic = new InitialContext();
+        Assert.assertNotNull(ic.lookup(dsName));
+        
+        EntityManager em = null;
+        boolean pass = false;
+        Map properties = new HashMap();  
+        properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, dsName);
+        properties.put("eclipselink.jdbc.exclusive-connection.mode", "Always");
+        
+        try {
+            em = emf.createEntityManager(properties);
+            em.clear();
+            em.find(Dog.class, 1);
+        } catch (RuntimeException expected) {
+            pass = expected.getMessage().indexOf("tmpDataSourceImp getConnection called") != -1;
+            if (!pass) {
+                throw expected;
+            }
+        } finally {
+            if (em != null) {
+                em.close();
+            }
+        }
+        Assert.assertTrue("Non JTA datasource was not set or accessed as expected through map of properties", pass);
+    }
+    
+    /*
+     * Taken from org.eclipse.persistence.testing.tests.jpa.validation.ValidationTestSuite
+     */
+    public static class BogusDataSource implements DataSource, Remote, Serializable {
+        private String text = "foo";
+        
+        public BogusDataSource(String text){
+            super();
+            this.text = text;
+        }
+        
+        public Connection getConnection() throws SQLException {
+            RuntimeException exception = new RuntimeException(text);
+            throw exception;
+        }
+        
+        public Connection getConnection(String username, String password) throws SQLException {
+            return getConnection();
+        }
+        
+        //rest are ignored
+        public java.io.PrintWriter getLogWriter() throws SQLException {
+            return null;
+        }
+        
+        public void setLogWriter(java.io.PrintWriter out) throws SQLException{}
+        public void setLoginTimeout(int seconds) throws SQLException{}
+        public int getLoginTimeout() throws SQLException { return 1; }
+        public <T> T unwrap(Class<T> iface) throws SQLException { return null; }
+        public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; }
+        public Logger getParentLogger() { return null; }
+    }
 }
diff --git a/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/EntityManagerImpl.java b/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/EntityManagerImpl.java
index 2581116..1ad649d 100644
--- a/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/EntityManagerImpl.java
+++ b/jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/EntityManagerImpl.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 1998, 2016 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017 IBM Corporation, Oracle and/or its affiliates. All rights reserved.
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
  * which accompanies this distribution.
@@ -33,6 +33,8 @@
  *       - 350487: JPA 2.1 Specification defined support for Stored Procedure Calls
  *     08/11/2012-2.5 Guy Pelletier
  *       - 393867: Named queries do not work when using EM level Table Per Tenant Multitenancy.
+ *     02/16/2017-2.6 Jody Grassel
+ *       - 512255: Eclipselink JPA/Auditing capablity in EE Environment fails with JNDI name parameter type
  ******************************************************************************/
 package org.eclipse.persistence.internal.jpa;
 
@@ -2583,6 +2585,7 @@ protected static ConnectionPolicy createConnectionPolicy(ServerSession serverSes
                             jndiConnector.setDataSource(dataSource);
                         } else {
                             // dataSourceName != null
+                            jndiConnector.setDataSource(null);
                             jndiConnector.setName(dataSourceName);
                         }
                     }