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");
Set<String> tables = new HashSet<String>();
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 <[email protected]>
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(OrderedConfiguration<HibernateConfigurer>
> configuration) {
> configuration.add("test", new TestHibernateConfigurer());
> configuration.addInstance("test", ETSSHibernateConfigurer.class);
> }
>
> public static void
>
> contributeHibernateEntityPackageManager(org.apache.tapestry5.ioc.Configuration<String>
> 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(MappedConfiguration<String, 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", UserProfileSearchServiceImpl.SEARCH_PROPERTIES);
> assertEquals(query.getResultSize(), 1);
>
> setDelete(userProfile);
> }
>
> }
>