From d46d3d66a28382f4212f68d453dc7d4d87ec1a43 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 30 Sep 2019 07:58:18 -0500 Subject: [PATCH] HHH-13432 - EntityManagerFactory no longer exposes "javax.persistence.nonJtaDataSource" --- .../orm/test/bootstrap/DataSourceStub.java | 88 +++++++++++++++++++ .../bootstrap/PersistenceUnitInfoTests.java | 54 ++++++++++++ .../PersistenceUnitOverridesTests.java | 85 ++---------------- 3 files changed, 149 insertions(+), 78 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/DataSourceStub.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/PersistenceUnitInfoTests.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/DataSourceStub.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/DataSourceStub.java new file mode 100644 index 0000000000..708c963113 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/DataSourceStub.java @@ -0,0 +1,88 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.bootstrap; + +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.logging.Logger; +import javax.sql.DataSource; + +import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; + +import org.hibernate.testing.env.ConnectionProviderBuilder; + +/** + * @author Steve Ebersole + */ +public class DataSourceStub implements DataSource { + private final String id; + private final DriverManagerConnectionProviderImpl connectionProvider; + private PrintWriter printWriter; + + DataSourceStub(String id) { + this.id = id; + connectionProvider = new DriverManagerConnectionProviderImpl(); + connectionProvider.configure( ConnectionProviderBuilder.getConnectionProviderProperties() ); + + printWriter = null; + } + + public String getId() { + return id; + } + + @Override + public Connection getConnection() throws SQLException { + return connectionProvider.getConnection(); + } + + @Override + public Connection getConnection(String username, String password) { + throw new UnsupportedOperationException(); + } + + @Override + public PrintWriter getLogWriter() { + return printWriter; + } + + @Override + public void setLogWriter(PrintWriter out) { + this.printWriter = out; + } + + @Override + public void setLoginTimeout(int seconds) { + } + + @Override + public int getLoginTimeout() { + return -1; + } + + @Override + public Logger getParentLogger() { + return Logger.getGlobal(); + } + + @Override + public T unwrap(Class iface) { + //noinspection unchecked + return (T) this; + } + + @Override + public boolean isWrapperFor(Class iface) { + return iface.isAssignableFrom( getClass() ); + } + + @Override + public String toString() { + return "DataSourceImpl(" + id + ")"; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/PersistenceUnitInfoTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/PersistenceUnitInfoTests.java new file mode 100644 index 0000000000..3fe098bdb7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/PersistenceUnitInfoTests.java @@ -0,0 +1,54 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.bootstrap; + +import java.util.Collections; +import java.util.Map; +import javax.persistence.EntityManagerFactory; +import javax.persistence.spi.PersistenceProvider; +import javax.sql.DataSource; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.jpa.HibernatePersistenceProvider; + +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author Steve Ebersole + */ +public class PersistenceUnitInfoTests extends BaseUnitTestCase { + @Test + @TestForIssue( jiraKey = "HHH-13432" ) + @FailureExpected( jiraKey = "HHH-13432" ) + public void testJtaDataExposedAsProperty() { + final DataSource puDataSource = new DataSourceStub( "puDataSource" ); + final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() { + + @Override + public DataSource getNonJtaDataSource() { + return puDataSource; + } + }; + + final PersistenceProvider provider = new HibernatePersistenceProvider(); + + final EntityManagerFactory emf = provider.createContainerEntityManagerFactory( + info, + Collections.emptyMap() + ); + + final Map properties = emf.getProperties(); + final Object o = properties.get( AvailableSettings.JPA_JTA_DATASOURCE ); + assertEquals( o, puDataSource ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/PersistenceUnitOverridesTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/PersistenceUnitOverridesTests.java index 41e45d1fa3..6cc59d058f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/PersistenceUnitOverridesTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/PersistenceUnitOverridesTests.java @@ -6,13 +6,9 @@ */ package org.hibernate.orm.test.bootstrap; -import java.io.PrintWriter; -import java.sql.Connection; -import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.Properties; -import java.util.logging.Logger; import javax.persistence.EntityManagerFactory; import javax.persistence.spi.PersistenceProvider; import javax.persistence.spi.PersistenceUnitInfo; @@ -100,7 +96,7 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase { final Properties puProperties; { - puDataSource = new DataSourceImpl( "puDataSource" ); + puDataSource = new DataSourceStub( "puDataSource" ); puProperties = new Properties(); puProperties.putAll( info.getProperties() ); @@ -161,7 +157,7 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase { final DataSource puDataSource; { - puDataSource = new DataSourceImpl( "puDataSource" ); + puDataSource = new DataSourceStub( "puDataSource" ); } @Override @@ -209,8 +205,8 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase { "have precedence over integration settings, which is also incorrect" ) public void testPassingIntegrationJpaDataSourceOverrideForJtaDataSourceElement() { - final DataSource puDataSource = new DataSourceImpl( "puDataSource" ); - final DataSource integrationDataSource = new DataSourceImpl( "integrationDataSource" ); + final DataSource puDataSource = new DataSourceStub( "puDataSource" ); + final DataSource integrationDataSource = new DataSourceStub( "integrationDataSource" ); PersistenceProvider provider = new HibernatePersistenceProvider() { @Override @@ -263,7 +259,7 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase { public void testIntegrationOverridesOfPersistenceXmlDataSource() { // mimics a DataSource defined in the persistence.xml - final DataSourceImpl dataSource = new DataSourceImpl( "puDataSource" ); + final DataSourceStub dataSource = new DataSourceStub( "puDataSource" ); final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() { @Override @@ -274,7 +270,7 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase { // Now create "integration Map" that overrides the DataSource to use - final DataSource override = new DataSourceImpl( "integrationDataSource" ); + final DataSource override = new DataSourceStub( "integrationDataSource" ); final Map integrationSettings = new HashMap<>(); integrationSettings.put( AvailableSettings.JPA_NON_JTA_DATASOURCE, override ); @@ -308,7 +304,7 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase { public void testIntegrationOverridesOfPersistenceXmlDataSourceWithDriverManagerInfo() { // mimics a DataSource defined in the persistence.xml - final DataSourceImpl dataSource = new DataSourceImpl( "puDataSource" ); + final DataSourceStub dataSource = new DataSourceStub( "puDataSource" ); final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() { @Override @@ -335,71 +331,4 @@ public class PersistenceUnitOverridesTests extends BaseUnitTestCase { assertThat( connectionProvider, instanceOf( DriverManagerConnectionProviderImpl.class ) ); } - private static class DataSourceImpl implements DataSource { - private final String id; - private final DriverManagerConnectionProviderImpl connectionProvider; - private PrintWriter printWriter; - - DataSourceImpl(String id) { - this.id = id; - connectionProvider = new DriverManagerConnectionProviderImpl(); - connectionProvider.configure( ConnectionProviderBuilder.getConnectionProviderProperties() ); - - printWriter = null; - } - - public String getId() { - return id; - } - - @Override - public Connection getConnection() throws SQLException { - return connectionProvider.getConnection(); - } - - @Override - public Connection getConnection(String username, String password) { - throw new UnsupportedOperationException(); - } - - @Override - public PrintWriter getLogWriter() { - return printWriter; - } - - @Override - public void setLogWriter(PrintWriter out) { - this.printWriter = out; - } - - @Override - public void setLoginTimeout(int seconds) { - } - - @Override - public int getLoginTimeout() { - return -1; - } - - @Override - public Logger getParentLogger() { - return Logger.getGlobal(); - } - - @Override - public T unwrap(Class iface) { - //noinspection unchecked - return (T) this; - } - - @Override - public boolean isWrapperFor(Class iface) { - return iface.isAssignableFrom( getClass() ); - } - - @Override - public String toString() { - return "DataSourceImpl(" + id + ")"; - } - } }