Search in sources :

Example 1 with DataSourceStub

use of org.hibernate.testing.jdbc.DataSourceStub in project hibernate-orm by hibernate.

the class PersistenceUnitOverridesTests method testIntegrationOverridesOfPersistenceXmlDataSourceWithDriverManagerInfo.

@Test
@TestForIssue(jiraKey = "HHH-13640")
public void testIntegrationOverridesOfPersistenceXmlDataSourceWithDriverManagerInfo() {
    // mimics a DataSource defined in the persistence.xml
    final DataSourceStub dataSource = new DataSourceStub("puDataSource");
    final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() {

        @Override
        public DataSource getNonJtaDataSource() {
            return dataSource;
        }
    };
    final Map<String, Object> integrationSettings = new HashMap<>();
    integrationSettings.put(AvailableSettings.JPA_JDBC_DRIVER, ConnectionProviderBuilder.DRIVER);
    integrationSettings.put(AvailableSettings.JPA_JDBC_URL, ConnectionProviderBuilder.URL);
    integrationSettings.put(AvailableSettings.JPA_JDBC_USER, ConnectionProviderBuilder.USER);
    integrationSettings.put(AvailableSettings.JPA_JDBC_PASSWORD, ConnectionProviderBuilder.PASS);
    integrationSettings.put("hibernate.connection.init_sql", "");
    final PersistenceProvider provider = new HibernatePersistenceProvider();
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(info, integrationSettings);
    try {
        final SessionFactoryImplementor sessionFactory = emf.unwrap(SessionFactoryImplementor.class);
        final ConnectionProvider connectionProvider = sessionFactory.getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DriverManagerConnectionProviderImpl.class));
    } finally {
        emf.close();
    }
}
Also used : DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with DataSourceStub

use of org.hibernate.testing.jdbc.DataSourceStub in project hibernate-orm by hibernate.

the class PersistenceUnitOverridesTests method testPassingIntegrationJpaJdbcOverrideForJtaDataSourceProperty.

@Test
public void testPassingIntegrationJpaJdbcOverrideForJtaDataSourceProperty() {
    PersistenceProvider provider = new HibernatePersistenceProvider() {

        @Override
        public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integrationOverrides) {
            return super.createContainerEntityManagerFactory(new DelegatingPersistenceUnitInfo(info) {

                // inject a JPA JTA DataSource setting into the PU
                final DataSource puDataSource;

                final Properties puProperties;

                {
                    puDataSource = new DataSourceStub("puDataSource");
                    puProperties = new Properties();
                    puProperties.putAll(info.getProperties());
                    puProperties.put(AvailableSettings.JPA_JTA_DATASOURCE, puDataSource);
                }

                @Override
                public DataSource getJtaDataSource() {
                    return null;
                }

                @Override
                public DataSource getNonJtaDataSource() {
                    return null;
                }

                @Override
                public Properties getProperties() {
                    return puProperties;
                }
            }, integrationOverrides);
        }
    };
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(new PersistenceUnitInfoAdapter(), // a JTA DataSource (nor the reverse).  However, that is a useful thing to support
    ConnectionProviderBuilder.getJpaConnectionProviderProperties("db2"));
    try {
        final Map<String, Object> properties = emf.getProperties();
        final Object hibernateJdbcDriver = properties.get(AvailableSettings.URL);
        assertThat(hibernateJdbcDriver, notNullValue());
        final Object jpaJdbcDriver = properties.get(AvailableSettings.JPA_JDBC_URL);
        assertThat((String) jpaJdbcDriver, containsString("db2"));
        // see if the values had the affect to adjust the `ConnectionProvider` used
        final ConnectionProvider connectionProvider = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DriverManagerConnectionProviderImpl.class));
    } finally {
        emf.close();
    }
}
Also used : DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Properties(java.util.Properties) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with DataSourceStub

use of org.hibernate.testing.jdbc.DataSourceStub in project hibernate-orm by hibernate.

the class PersistenceUnitOverridesTests method testPassingIntegrationJtaDataSourceOverrideForJpaJdbcSettings.

@Test
public void testPassingIntegrationJtaDataSourceOverrideForJpaJdbcSettings() {
    final PersistenceUnitInfoAdapter puInfo = new PersistenceUnitInfoAdapter(ConnectionProviderBuilder.getJpaConnectionProviderProperties("db2"));
    final DataSource integrationDataSource = new DataSourceStub("integrationDataSource");
    final HibernatePersistenceProvider provider = new HibernatePersistenceProvider();
    // todo (6.0) : fix for Oracle see HHH-13432
    // puInfo.getProperties().setProperty( AvailableSettings.HQL_BULK_ID_STRATEGY, MultiTableBulkIdStrategyStub.class.getName() );
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(puInfo, Collections.singletonMap(AvailableSettings.JPA_JTA_DATASOURCE, integrationDataSource));
    try {
        // first let's check the DataSource used in the EMF...
        final ConnectionProvider connectionProvider = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DatasourceConnectionProviderImpl.class));
        final DatasourceConnectionProviderImpl dsCp = (DatasourceConnectionProviderImpl) connectionProvider;
        assertThat(dsCp.getDataSource(), is(integrationDataSource));
        // now let's check that it is exposed via the EMF properties
        // - note : the spec does not indicate that this should work, but
        // it worked this way in previous versions
        final Object jtaDs = emf.getProperties().get(AvailableSettings.JPA_JTA_DATASOURCE);
        assertThat(jtaDs, is(integrationDataSource));
        // Additionally, we should have set Hibernate's DATASOURCE setting
        final Object hibDs = emf.getProperties().get(AvailableSettings.JPA_JTA_DATASOURCE);
        assertThat(hibDs, is(integrationDataSource));
        // Make sure the non-jta-data-source setting was cleared or otherwise null
        final Object nonJtaDs = emf.getProperties().get(AvailableSettings.JPA_NON_JTA_DATASOURCE);
        assertThat(nonJtaDs, nullValue());
    } finally {
        emf.close();
    }
}
Also used : DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) DatasourceConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test)

Example 4 with DataSourceStub

use of org.hibernate.testing.jdbc.DataSourceStub in project hibernate-orm by hibernate.

the class PersistenceUnitOverridesTests method testIntegrationOverridesOfPersistenceXmlDataSource.

@Test
@TestForIssue(jiraKey = "HHH-13640")
public void testIntegrationOverridesOfPersistenceXmlDataSource() {
    // mimics a DataSource defined in the persistence.xml
    final DataSourceStub dataSource = new DataSourceStub("puDataSource");
    final PersistenceUnitInfoAdapter info = new PersistenceUnitInfoAdapter() {

        @Override
        public DataSource getNonJtaDataSource() {
            return dataSource;
        }
    };
    // Now create "integration Map" that overrides the DataSource to use
    final DataSource override = new DataSourceStub("integrationDataSource");
    final Map<String, Object> integrationSettings = new HashMap<>();
    integrationSettings.put(AvailableSettings.JPA_NON_JTA_DATASOURCE, override);
    // todo (6.0) : fix for Oracle see HHH-13432
    // integrationSettings.put( AvailableSettings.HQL_BULK_ID_STRATEGY, new MultiTableBulkIdStrategyStub() );
    final PersistenceProvider provider = new HibernatePersistenceProvider();
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(info, integrationSettings);
    try {
        final Map<String, Object> properties = emf.getProperties();
        assertThat(properties.get(AvailableSettings.JPA_NON_JTA_DATASOURCE), notNullValue());
        assertThat(properties.get(AvailableSettings.JPA_NON_JTA_DATASOURCE), is(override));
        final SessionFactoryImplementor sessionFactory = emf.unwrap(SessionFactoryImplementor.class);
        final ConnectionProvider connectionProvider = sessionFactory.getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DatasourceConnectionProviderImpl.class));
        final DatasourceConnectionProviderImpl dsProvider = (DatasourceConnectionProviderImpl) connectionProvider;
        assertThat(dsProvider.getDataSource(), is(override));
    } finally {
        emf.close();
    }
}
Also used : HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) DatasourceConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with DataSourceStub

use of org.hibernate.testing.jdbc.DataSourceStub in project hibernate-orm by hibernate.

the class PersistenceUnitOverridesTests method testPassingIntegrationJpaDataSourceOverrideForJtaDataSourceElement.

@Test
public // )
void testPassingIntegrationJpaDataSourceOverrideForJtaDataSourceElement() {
    final DataSource puDataSource = new DataSourceStub("puDataSource");
    final DataSource integrationDataSource = new DataSourceStub("integrationDataSource");
    PersistenceProvider provider = new HibernatePersistenceProvider() {

        @Override
        public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integrationOverrides) {
            return super.createContainerEntityManagerFactory(new DelegatingPersistenceUnitInfo(info) {

                @Override
                public DataSource getJtaDataSource() {
                    // - as opposed using `javax.persistence.jtaDataSource` under the `properties` element
                    return puDataSource;
                }
            }, integrationOverrides);
        }
    };
    final Map integrationOverrides = new HashMap();
    // noinspection unchecked
    integrationOverrides.put(AvailableSettings.JPA_JTA_DATASOURCE, integrationDataSource);
    // todo (6.0) : fix for Oracle see HHH-13432
    // integrationOverrides.put( AvailableSettings.HQL_BULK_ID_STRATEGY, new MultiTableBulkIdStrategyStub() );
    final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(new PersistenceUnitInfoAdapter(), integrationOverrides);
    try {
        final Map<String, Object> properties = emf.getProperties();
        final Object datasource = properties.get(AvailableSettings.JPA_JTA_DATASOURCE);
        assertThat(datasource, is(integrationDataSource));
        // see if the values had the affect to adjust the `ConnectionProvider` used
        final ConnectionProvider connectionProvider = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(ConnectionProvider.class);
        assertThat(connectionProvider, instanceOf(DatasourceConnectionProviderImpl.class));
        final DatasourceConnectionProviderImpl datasourceConnectionProvider = (DatasourceConnectionProviderImpl) connectionProvider;
        assertThat(datasourceConnectionProvider.getDataSource(), is(integrationDataSource));
    } finally {
        emf.close();
    }
}
Also used : DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) HashMap(java.util.HashMap) PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) DelegatingPersistenceUnitInfo(org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo) PersistenceUnitInfoAdapter(org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter) DataSource(javax.sql.DataSource) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DataSourceStub(org.hibernate.testing.jdbc.DataSourceStub) DatasourceConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

EntityManagerFactory (jakarta.persistence.EntityManagerFactory)9 ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)9 HibernatePersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider)9 DataSourceStub (org.hibernate.testing.jdbc.DataSourceStub)9 PersistenceUnitInfoAdapter (org.hibernate.testing.util.jpa.PersistenceUnitInfoAdapter)9 Test (org.junit.Test)9 PersistenceProvider (jakarta.persistence.spi.PersistenceProvider)8 DataSource (javax.sql.DataSource)7 HashMap (java.util.HashMap)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 DatasourceConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl)5 TestForIssue (org.hibernate.testing.TestForIssue)5 DriverManagerConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl)4 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)3 Map (java.util.Map)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 DelegatingPersistenceUnitInfo (org.hibernate.testing.util.jpa.DelegatingPersistenceUnitInfo)3 Properties (java.util.Properties)1