diff options
author | Nigel Magnay <nigel.magnay@gmail.com> | 2014-10-06 15:50:27 +0100 |
---|---|---|
committer | Nigel Magnay <nigel.magnay@gmail.com> | 2014-10-07 15:15:09 +0100 |
commit | d8d58191efd47e0f58fc90c33a68c424a0de35ab (patch) | |
tree | 285b8ead370b0a2a8d30b890145b366842fb8c30 /extensions | |
parent | aa2f2dc625cb5cc26a2782479790dd80a00353cc (diff) | |
download | guice-d8d58191efd47e0f58fc90c33a68c424a0de35ab.tar.gz |
Introduce a test that shows a JPA provider being passed a datasource
from guice in a map, rather than from the persistence.xml file.
Note that this requires a modern version of hibernate since older versions
did not correctly process passed objects.
Signed-off-by: Nigel Magnay <nigel.magnay@gmail.com>
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/persist/test/META-INF/persistence.xml | 14 | ||||
-rw-r--r-- | extensions/persist/test/com/google/inject/persist/jpa/EnsureJpaCanTakeObjectsInPropertiesTest.java | 111 |
2 files changed, 125 insertions, 0 deletions
diff --git a/extensions/persist/test/META-INF/persistence.xml b/extensions/persist/test/META-INF/persistence.xml index 3b08af05..881c7fd8 100644 --- a/extensions/persist/test/META-INF/persistence.xml +++ b/extensions/persist/test/META-INF/persistence.xml @@ -25,5 +25,19 @@ </properties> </persistence-unit> + <persistence-unit name="testProperties" transaction-type="RESOURCE_LOCAL"> + <provider>org.hibernate.ejb.HibernatePersistence</provider> + + <class>com.google.inject.persist.jpa.JpaTestEntity</class> + <class>com.google.inject.persist.jpa.JpaParentTestEntity</class> + <exclude-unlisted-classes>true</exclude-unlisted-classes> + + <properties> + <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> + <property name="hibernate.id.new_generator_mappings" value="true"/> + <property name="hibernate.hbm2ddl.auto" value="create-drop"/> + </properties> + </persistence-unit> + </persistence> diff --git a/extensions/persist/test/com/google/inject/persist/jpa/EnsureJpaCanTakeObjectsInPropertiesTest.java b/extensions/persist/test/com/google/inject/persist/jpa/EnsureJpaCanTakeObjectsInPropertiesTest.java new file mode 100644 index 00000000..c5421a51 --- /dev/null +++ b/extensions/persist/test/com/google/inject/persist/jpa/EnsureJpaCanTakeObjectsInPropertiesTest.java @@ -0,0 +1,111 @@ +/** + * Licensed 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 com.google.inject.persist.jpa; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; +import com.google.inject.persist.UnitOfWork; + +import junit.framework.TestCase; + +import org.hibernate.cfg.Environment; +import org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider; +import org.hsqldb.jdbc.JDBCDataSource; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceException; +import javax.sql.DataSource; + +public class EnsureJpaCanTakeObjectsInPropertiesTest extends TestCase { + + private Injector injector; + + public static class DBModule extends AbstractModule { + + final DataSource ds; + final boolean passDataSource; + + DBModule(DataSource ds, boolean passDataSource) { + this.ds = ds; + this.passDataSource = passDataSource; + } + + @Override + protected void configure() { + Map<String, Object> p = new HashMap<String, Object>(); + + p.put(Environment.CONNECTION_PROVIDER, InjectedDataSourceConnectionProvider.class.getName()); + if (passDataSource) { + p.put(Environment.DATASOURCE, ds); + } + + JpaPersistModule jpaPersistModule = new JpaPersistModule("testProperties").properties(p); + + install(jpaPersistModule); + } + } + + @Override + public void setUp() { + injector = null; + } + + @Override + public final void tearDown() { + if (injector == null) { + return; + } + + injector.getInstance(UnitOfWork.class).end(); + injector.getInstance(EntityManagerFactory.class).close(); + } + + private static JDBCDataSource getDataSource() { + final JDBCDataSource dataSource = new JDBCDataSource(); + dataSource.setDatabase("jdbc:hsqldb:mem:persistence"); + dataSource.setUser("sa"); + dataSource.setPassword(""); + return dataSource; + } + + private void startPersistService(boolean passDataSource) { + final JDBCDataSource dataSource = getDataSource(); + + injector = Guice.createInjector(new DBModule(dataSource, passDataSource)); + + //startup persistence + injector.getInstance(PersistService.class).start(); + } + + public void testWorksIfPassDataSource() { + startPersistService(true); + } + + public void testFailsIfNoDataSource() { + try { + startPersistService(false); + fail(); + } catch (PersistenceException ex) { + // Expected + injector = null; + } + } + +} |