PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/test/MockHibernateModule.java

http://sigma-h.googlecode.com/
Java | 75 lines | 53 code | 12 blank | 10 comment | 9 complexity | 5699061d2675cc1f84cf4804bad52518 MD5 | raw file
 1/*
 2 * All Sigmah code is released under the GNU General Public License v3
 3 * See COPYRIGHT.txt and LICENSE.txt.
 4 */
 5
 6package org.sigmah.test;
 7
 8import java.io.File;
 9
10import javax.persistence.EntityManager;
11import javax.persistence.EntityManagerFactory;
12
13import org.hibernate.ejb.Ejb3Configuration;
14import org.sigmah.server.dao.hibernate.HibernateModule;
15import org.sigmah.server.domain.PersistentClasses;
16
17import com.google.inject.Provider;
18import com.google.inject.Singleton;
19
20public class MockHibernateModule extends HibernateModule {
21    private static EntityManagerFactory emf = null;
22
23
24    @Override
25    protected void configureEmf() {
26        bind(EntityManagerFactory.class).toProvider(new Provider<EntityManagerFactory>() {
27            @Override
28            public EntityManagerFactory get() {
29                // we are assuming that the tests do not affect the database schema, so there is no
30                // need to restart hibernate for each test class, and we save quite a bit of time
31                if (emf == null) {
32                	
33                	// clear out the default h2 databse if it exists
34                	File h2db = new File("activitityinfo-test.h2");
35                	if(h2db.exists()) {
36                		if(!h2db.delete()) {
37                			throw new RuntimeException("Could not delete the testing database activityinfo.h2 prior to starting tests. Maybe there is another" +
38                					" test running?");
39                		}
40                	}
41                	
42                    // we want to avoid a full scan of WEB-INF/classes during hibernate
43                    // startup for tests. So we avoid the normal persistence.xml config
44                    // and build the configuration manually.
45                    Ejb3Configuration cfg = new Ejb3Configuration();
46                    for(Class entityClass : PersistentClasses.LIST) {
47                        cfg.addAnnotatedClass(entityClass);
48                    }
49                    emf = cfg.configure(getConfigurationFilePath()) //add a regular hibernate.cfg.xml
50                            .buildEntityManagerFactory(); //Create the entity manager factory
51                    System.err.println("GUICE: EntityManagerFACTORY created");
52                }
53                return emf;
54            }
55        }).in(Singleton.class);
56    }
57
58    private String getConfigurationFilePath() {
59        String db = "h2";
60        if(System.getProperty("testDatabase") != null) {
61            db = System.getProperty("testDatabase");
62        }
63        String cfgFile = "/hibernate-tests-" + db + ".cfg.xml";
64        if( getClass().getResourceAsStream(cfgFile) == null ) {
65            throw new Error("Cannot find hibernate cfg file for testing: " + cfgFile);
66        }
67        return cfgFile;
68    }
69
70    @Override
71    protected void configureEm() {
72        bind(EntityManager.class).toProvider(EntityManagerProvider.class)
73                .in(TestScoped.class);
74    }
75}