PageRenderTime 1255ms CodeModel.GetById 36ms RepoModel.GetById 2ms app.codeStats 0ms

/extensions/persist/test/com/google/inject/persist/jpa/EnsureJpaCanTakeObjectsInPropertiesTest.java

https://gitlab.com/metamorphiccode/guice
Java | 110 lines | 71 code | 24 blank | 15 comment | 3 complexity | 48d7726d7105f70c5cc8eb2c8fbbe4cd MD5 | raw file
  1. /**
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package com.google.inject.persist.jpa;
  15. import com.google.inject.AbstractModule;
  16. import com.google.inject.Guice;
  17. import com.google.inject.Injector;
  18. import com.google.inject.persist.PersistService;
  19. import com.google.inject.persist.UnitOfWork;
  20. import junit.framework.TestCase;
  21. import org.hibernate.cfg.Environment;
  22. import org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider;
  23. import org.hsqldb.jdbc.JDBCDataSource;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import javax.persistence.EntityManagerFactory;
  27. import javax.persistence.PersistenceException;
  28. import javax.sql.DataSource;
  29. public class EnsureJpaCanTakeObjectsInPropertiesTest extends TestCase {
  30. private Injector injector;
  31. public static class DBModule extends AbstractModule {
  32. final DataSource ds;
  33. final boolean passDataSource;
  34. DBModule(DataSource ds, boolean passDataSource) {
  35. this.ds = ds;
  36. this.passDataSource = passDataSource;
  37. }
  38. @Override
  39. protected void configure() {
  40. Map<String, Object> p = new HashMap<String, Object>();
  41. p.put(Environment.CONNECTION_PROVIDER, InjectedDataSourceConnectionProvider.class.getName());
  42. if (passDataSource) {
  43. p.put(Environment.DATASOURCE, ds);
  44. }
  45. JpaPersistModule jpaPersistModule = new JpaPersistModule("testProperties").properties(p);
  46. install(jpaPersistModule);
  47. }
  48. }
  49. @Override
  50. public void setUp() {
  51. injector = null;
  52. }
  53. @Override
  54. public final void tearDown() {
  55. if (injector == null) {
  56. return;
  57. }
  58. injector.getInstance(UnitOfWork.class).end();
  59. injector.getInstance(EntityManagerFactory.class).close();
  60. }
  61. private static DataSource getDataSource() {
  62. final JDBCDataSource dataSource = new JDBCDataSource();
  63. dataSource.setDatabase("jdbc:hsqldb:mem:persistence");
  64. dataSource.setUser("sa");
  65. dataSource.setPassword("");
  66. return dataSource;
  67. }
  68. private void startPersistService(boolean passDataSource) {
  69. final DataSource dataSource = getDataSource();
  70. injector = Guice.createInjector(new DBModule(dataSource, passDataSource));
  71. //startup persistence
  72. injector.getInstance(PersistService.class).start();
  73. }
  74. public void testWorksIfPassDataSource() {
  75. startPersistService(true);
  76. }
  77. public void testFailsIfNoDataSource() {
  78. try {
  79. startPersistService(false);
  80. fail();
  81. } catch (PersistenceException ex) {
  82. // Expected
  83. injector = null;
  84. }
  85. }
  86. }