Setup integration test with testng and tapestry services.
Hi everyone, I'm trying to setup integration test with testng and tapestry services. Lance kindly helped me get some of this up and running on my personal project, but my day job is requiring a little bit more. So far I have the following code. but experiencing the following two issues. Issue 1. (SerializationSupport.java:38) - Setting a new service proxy provider when there's already an existing provider. This may indicate that you have multiple IoC Registries. (I'm aware of the cause, I just don't know how to fix it) Issue 2. The h2 in mem database doesn't clear between test, is there a way to clear the data without having to session.delete() the content manually? public class AppModuleTest { public static void bind(ServiceBinder binder) { binder.bind(SearchService.class, SearchServiceImpl.class); binder.bind(UserInfoService.class, UserInfoServiceImpl.class); } public static Messages buildMessages() { return Mockito.mock(Messages.class); } public static Request buildRequest() { Request request = Mockito.mock(Request.class); when(request.isXHR()).thenReturn(false); return request; } public static void contributeHibernateSessionSource(OrderedConfigurationHibernateConfigurer configuration) { configuration.add(test, new TestHibernateConfigurer()); configuration.addInstance(test, ETSSHibernateConfigurer.class); } public static void contributeHibernateEntityPackageManager(org.apache.tapestry5.ioc.ConfigurationString config) { config.add(org.healthresearch.etss.entities); } @Scope(ScopeConstants.PERTHREAD) public static FullTextSession buildFullTextSession(HibernateSessionManager sessionManager) { return Search.getFullTextSession(sessionManager.getSession()); } public static void contributeApplicationDefaults(MappedConfigurationString, Object config) { config.add(HibernateSymbols.DEFAULT_CONFIGURATION, false); } } public class TestHibernateConfigurer implements HibernateConfigurer { @Override public void configure(Configuration config) { config.setProperty(hibernate.dialect, org.hibernate.dialect.H2Dialect); config.setProperty(hibernate.connection.driver_class, org.h2.Driver); config.setProperty(hibernate.connection.url, jdbc:h2:mem:test); config.setProperty(hibernate.hbm2ddl.auto, update); config.setProperty(hibernate.show_sql, false); config.setProperty(hibernate.format_sql, true); config.setProperty(hibernate.hbm2ddl.import_files, xxx); config.setProperty(hibernate.search.default.directory_provider, RAMDirectoryProvider.class.getName()); } } public abstract class AbstractHibernateTest { private HibernateSessionManager sessionManager; private SearchService searchService; private Session session; @BeforeClass public void abstractBefore() { System.out.println(abstractBefore); Registry registry = RegistryBuilder.buildAndStartupRegistry(AppModuleTest.class, HibernateCoreModule.class); this.sessionManager = registry.getService(HibernateSessionManager.class); this.searchService = registry.getService(SearchService.class); this.session = sessionManager.getSession(); before(registry); } protected abstract void before(Registry registry) ; public HibernateSessionManager getSessionManager() { return sessionManager; } public Session getSession() { return session; } public SearchService getSearchService() { return searchService; } public void setDelete(Object object) { session.delete(object); sessionManager.commit(); } public int getResultSize(Class? clazz) { return session.createCriteria(clazz).list().size(); } public void getCommit() { session.flush(); sessionManager.commit(); } } public class SampleTest extends AbstractHibernateTest { @Override public void before(Registry registry) { } @Test public void testDatabase() { UserProfile userProfile = new UserProfile(); userProfile.setShortname(gmc07); getSession().save(userProfile); getCommit(); assertEquals(getResultSize(UserProfile.class), 1); setDelete(userProfile); assertEquals(getResultSize(UserProfile.class), 0); } } public class ProfileTest extends AbstractHibernateTest { @Override protected void before(Registry registry) { } @Test private void firstTest() { UserProfile userProfile = new UserProfile(); userProfile.setShortname(jrr06); getSession().save(userProfile); getCommit(); getResultSize(UserProfile.class); assertEquals(getResultSize(UserProfile.class), 1); FullTextQuery query = getSearchService().search(UserProfile.class, jrr06
Re: Setup integration test with testng and tapestry services.
To nuke the H2 db, this is what I use (it's JPA, adjust for Hibernate): // based on http://www.objectpartners.com/2010/11/09/unit-testing-your-persistence-tier-code/ public void clearDatabase() throws SQLException { EntityTransaction transaction = em.getTransaction(); if (!transaction.isActive()) transaction.begin(); Connection c = em.unwrap(Connection.class); Statement s = c.createStatement(); s.execute(SET REFERENTIAL_INTEGRITY FALSE); SetString tables = new HashSetString(); ResultSet rs = s.executeQuery(select table_name + from INFORMATION_SCHEMA.tables + where table_type='TABLE' and table_schema='PUBLIC'); while (rs.next()) { // if we don't skip over the sequence table, we'll start getting The sequence table information is not complete // exceptions if (!rs.getString(1).startsWith(DUAL_) !rs.getString(1).equals(SEQUENCE)) { tables.add(rs.getString(1)); } } rs.close(); for (String table : tables) { s.executeUpdate(DELETE FROM + table); } transaction.commit(); s.execute(SET REFERENTIAL_INTEGRITY TRUE); s.close(); } Kalle On Fri, Aug 29, 2014 at 6:14 AM, George Christman gchrist...@cardaddy.com wrote: Hi everyone, I'm trying to setup integration test with testng and tapestry services. Lance kindly helped me get some of this up and running on my personal project, but my day job is requiring a little bit more. So far I have the following code. but experiencing the following two issues. Issue 1. (SerializationSupport.java:38) - Setting a new service proxy provider when there's already an existing provider. This may indicate that you have multiple IoC Registries. (I'm aware of the cause, I just don't know how to fix it) Issue 2. The h2 in mem database doesn't clear between test, is there a way to clear the data without having to session.delete() the content manually? public class AppModuleTest { public static void bind(ServiceBinder binder) { binder.bind(SearchService.class, SearchServiceImpl.class); binder.bind(UserInfoService.class, UserInfoServiceImpl.class); } public static Messages buildMessages() { return Mockito.mock(Messages.class); } public static Request buildRequest() { Request request = Mockito.mock(Request.class); when(request.isXHR()).thenReturn(false); return request; } public static void contributeHibernateSessionSource(OrderedConfigurationHibernateConfigurer configuration) { configuration.add(test, new TestHibernateConfigurer()); configuration.addInstance(test, ETSSHibernateConfigurer.class); } public static void contributeHibernateEntityPackageManager(org.apache.tapestry5.ioc.ConfigurationString config) { config.add(org.healthresearch.etss.entities); } @Scope(ScopeConstants.PERTHREAD) public static FullTextSession buildFullTextSession(HibernateSessionManager sessionManager) { return Search.getFullTextSession(sessionManager.getSession()); } public static void contributeApplicationDefaults(MappedConfigurationString, Object config) { config.add(HibernateSymbols.DEFAULT_CONFIGURATION, false); } } public class TestHibernateConfigurer implements HibernateConfigurer { @Override public void configure(Configuration config) { config.setProperty(hibernate.dialect, org.hibernate.dialect.H2Dialect); config.setProperty(hibernate.connection.driver_class, org.h2.Driver); config.setProperty(hibernate.connection.url, jdbc:h2:mem:test); config.setProperty(hibernate.hbm2ddl.auto, update); config.setProperty(hibernate.show_sql, false); config.setProperty(hibernate.format_sql, true); config.setProperty(hibernate.hbm2ddl.import_files, xxx); config.setProperty(hibernate.search.default.directory_provider, RAMDirectoryProvider.class.getName()); } } public abstract class AbstractHibernateTest { private HibernateSessionManager sessionManager; private SearchService searchService; private Session session; @BeforeClass public void abstractBefore() { System.out.println(abstractBefore); Registry registry = RegistryBuilder.buildAndStartupRegistry(AppModuleTest.class, HibernateCoreModule.class); this.sessionManager = registry.getService(HibernateSessionManager.class); this.searchService = registry.getService(SearchService.class); this.session = sessionManager.getSession(); before(registry); } protected abstract void before(Registry registry) ; public HibernateSessionManager getSessionManager() { return sessionManager; } public Session getSession() { return session
Re: Setup integration test with testng and tapestry services.
You should call registry.shutdown() in @AfterClass and registry.cleanupThread() in @After.
Re: Setup integration test with testng and tapestry services.
Thanks guys for your input. Lance, I tried the following configuration without success, but then tried the second configuration and succeeded. Could you take a quick peak to be sure I'm doing it correctly. -- cleanup in @AfterMethod = This failed with the following exception Failed: org.hibernate.SessionException: Session is closed -- cleanup in @AfterTest = Failed:java.lang.IllegalStateException: Method org.apache.tapestry5.ioc.internal.RegisteryImpl.cleanupThread(RegistryImpl.java.519) may no longer be involked -- I found success moving before to @BeforeMethod and moving registry shutdown into @AfterMethod. public class RegistryBuilderTest { protected final Registry buildRegistry(Class... moduleClasses) { RegistryBuilder builder = new RegistryBuilder(); builder.add(moduleClasses); return builder.build(); } } public class SampleTest extends RegistryBuilderTest { private Registry registry; private HibernateSessionManager sessionManager; private Session session; @BeforeClass protected void before() { registry = buildRegistry(AppModuleTest.class, HibernateCoreModule.class); sessionManager = registry.getService(HibernateSessionManager.class); session = sessionManager.getSession(); } @Test public void test1() { System.out.println(test1); UserProfile userProfile = new UserProfile(); userProfile.setShortname(test1); session.save(userProfile); session.flush(); sessionManager.commit(); assertEquals(getResultSize(UserProfile.class), 1); } @Test public void test2() { System.out.println(test2); UserProfile userProfile = new UserProfile(); userProfile.setShortname(test2); session.save(userProfile); session.flush(); sessionManager.commit(); assertEquals(getResultSize(UserProfile.class), 1); } public int getResultSize(Class? clazz) { return session.createCriteria(clazz).list().size(); } @AfterClass public void shutDown() { System.out.println(shutDown); registry.shutdown(); } @AfterMethod public void cleanupThread() { System.out.println(cleanupThread); registry.cleanupThread(); } } Working configuration public class SampleTest extends RegistryBuilderTest { private Registry registry; private HibernateSessionManager sessionManager; private Session session; @BeforeMethod protected void before() { registry = buildRegistry(AppModuleTest.class, HibernateCoreModule.class); sessionManager = registry.getService(HibernateSessionManager.class); session = sessionManager.getSession(); } @Test public void test1() { System.out.println(test1); UserProfile userProfile = new UserProfile(); userProfile.setShortname(test1); session.save(userProfile); session.flush(); sessionManager.commit(); assertEquals(getResultSize(UserProfile.class), 1); } @Test public void test2() { System.out.println(test2); UserProfile userProfile = new UserProfile(); userProfile.setShortname(test2); session.save(userProfile); session.flush(); sessionManager.commit(); assertEquals(getResultSize(UserProfile.class), 1); } public int getResultSize(Class? clazz) { return session.createCriteria(clazz).list().size(); } @AfterMethod public void cleanupThread() { System.out.println(cleanupThread); registry.cleanupThread(); registry.shutdown(); } } On Fri, Aug 29, 2014 at 11:33 AM, Lance Java lance.j...@googlemail.com wrote: From the h2 docs here: http://www.h2database.com/html/features.html#in_memory_databases In some cases, only one connection to a in-memory database is required. This means the database to be opened is private. In this case, the database URL is jdbc:h2:mem: Opening two connections within the same virtual machine means opening two different (private) databases. Sometimes multiple connections to the same in-memory database are required. In this case, the database URL must include a name. Example: jdbc:h2:mem:db1. Accessing the same database using this URL only works within the same virtual machine and class loader environment. So, if you use a connection url of jdbc:h2:mem: AND you make sure to call registry.cleanupThread() in the @After of each test. You will get a new database for every test. -- George Christman www.CarDaddy.com P.O. Box 735 Johnstown, New York