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
15package com.google.inject.persist.jpa;
16
17import com.google.inject.AbstractModule;
18import com.google.inject.Guice;
19import com.google.inject.Injector;
20import com.google.inject.persist.PersistService;
21import com.google.inject.persist.UnitOfWork;
22
23import junit.framework.TestCase;
24
25import org.hibernate.cfg.Environment;
26import org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider;
27import org.hsqldb.jdbc.JDBCDataSource;
28
29import java.util.HashMap;
30import java.util.Map;
31
32import javax.persistence.EntityManagerFactory;
33import javax.persistence.PersistenceException;
34import javax.sql.DataSource;
35
36public class EnsureJpaCanTakeObjectsInPropertiesTest extends TestCase {
37
38  private Injector injector;
39
40  public static class DBModule extends AbstractModule {
41
42    final DataSource ds;
43    final boolean passDataSource;
44
45    DBModule(DataSource ds, boolean passDataSource) {
46      this.ds = ds;
47      this.passDataSource = passDataSource;
48    }
49
50    @Override
51    protected void configure() {
52      Map<String, Object> p = new HashMap<String, Object>();
53
54      p.put(Environment.CONNECTION_PROVIDER, InjectedDataSourceConnectionProvider.class.getName());
55      if (passDataSource) {
56        p.put(Environment.DATASOURCE, ds);
57      }
58
59      JpaPersistModule jpaPersistModule = new JpaPersistModule("testProperties").properties(p);
60
61      install(jpaPersistModule);
62    }
63  }
64
65  @Override
66  public void setUp() {
67    injector = null;
68  }
69
70  @Override
71  public final void tearDown() {
72    if (injector == null) {
73      return;
74    }
75
76    injector.getInstance(UnitOfWork.class).end();
77    injector.getInstance(EntityManagerFactory.class).close();
78  }
79
80  private static DataSource getDataSource() {
81    final JDBCDataSource dataSource = new JDBCDataSource();
82    dataSource.setDatabase("jdbc:hsqldb:mem:persistence");
83    dataSource.setUser("sa");
84    dataSource.setPassword("");
85    return dataSource;
86  }
87
88  private void startPersistService(boolean passDataSource) {
89    final DataSource dataSource = getDataSource();
90
91    injector = Guice.createInjector(new DBModule(dataSource, passDataSource));
92
93    //startup persistence
94    injector.getInstance(PersistService.class).start();
95  }
96  public void testWorksIfPassDataSource() {
97    startPersistService(true);
98  }
99
100  public void testFailsIfNoDataSource() {
101    try {
102      startPersistService(false);
103      fail();
104    } catch (PersistenceException ex) {
105      // Expected
106      injector = null;
107    }
108  }
109
110}
111