1   package com.atlassian.user.impl.hibernate.configuration;
2   
3   import net.sf.hibernate.SessionFactory;
4   import net.sf.hibernate.HibernateException;
5   import net.sf.hibernate.cfg.Configuration;
6   import com.atlassian.user.User;
7   import com.atlassian.user.Group;
8   import com.atlassian.user.ExternalEntity;
9   
10  /**
11   * An in-memory implementation to use for testing the Hibernate implementation of Atlassian-User.
12   */
13  public class MemoryHsqlHibernateAccessor implements HibernateAccessor
14  {
15      private SessionFactory sessionFactory;
16  
17      public synchronized SessionFactory getSessionFactory()
18      {
19          if (sessionFactory == null)
20          {
21              try
22              {
23                  sessionFactory = createSessionFactory(new Class[]{User.class, Group.class, ExternalEntity.class});
24              }
25              catch (HibernateException e)
26              {
27                  throw new RuntimeException(e);
28              }
29          }
30  
31          return sessionFactory;
32      }
33  
34      private SessionFactory createSessionFactory(Class[] classesWithMappings) throws HibernateException
35      {
36          Configuration config = new Configuration().
37              setProperty("hibernate.dialect", "net.sf.hibernate.dialect.HSQLDialect").
38              setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver").
39              setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:" + getClass()).
40              setProperty("hibernate.connection.username", "sa").
41              setProperty("hibernate.connection.password", "").
42              setProperty("hibernate.connection.pool_size", "1").
43              setProperty("hibernate.connection.autocommit", "true").
44              setProperty("hibernate.hbm2ddl.auto", "create-drop").
45              setProperty("hibernate.show_sql", "false");
46  
47          for (int i = 0; i < classesWithMappings.length; i++)
48          {
49              Class clazz = classesWithMappings[i];
50              config.addClass(clazz);
51          }
52  
53          return config.buildSessionFactory();
54      }
55  }