1 9 package org.jboss.portal.test.core; 10 11 import java.sql.Connection ; 12 import java.sql.Statement ; 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.List ; 16 import java.util.Properties ; 17 18 import junit.framework.TestCase; 19 20 import org.jboss.portal.core.impl.role.RoleModuleImpl; 21 import org.jboss.portal.core.impl.user.UserModuleImpl; 22 import org.jboss.portal.core.modules.RoleModule; 23 import org.jboss.portal.core.modules.UserModule; 24 import org.hibernate.SessionFactory; 25 import org.hibernate.Session; 26 import org.hibernate.Transaction; 27 import org.hibernate.dialect.Dialect; 28 import org.hibernate.cfg.Configuration; 29 30 34 public abstract class ModelTestCase 35 extends TestCase 36 { 37 public ModelTestCase(String name) 38 { 39 super(name); 40 } 41 42 protected SessionFactory factory; 43 protected Session session; 44 protected Transaction tx; 45 protected UserModule userModule; 46 protected RoleModule roleModule; 47 48 public void setUp() throws Exception 49 { 50 Configuration cfg = new Configuration(); 51 52 cfg 53 .addResource("org/jboss/portal/core/impl/user/UserImpl.hbm.xml", Thread.currentThread() 54 .getContextClassLoader()); 55 cfg.addResource("org/jboss/portal/core/impl/role/RoleImpl.hbm.xml", Thread.currentThread() 56 .getContextClassLoader()); 57 cfg.addResource("mapping.hbm.xml", Thread.currentThread().getContextClassLoader()); 58 59 Properties props = new Properties (); 60 props.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.HSQLDialect"); 61 props.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver"); 62 props.setProperty("hibernate.connection.url", "jdbc:hsqldb:test"); 63 props.setProperty("hibernate.connection.username", "sa"); 64 props.setProperty("hibernate.connection.password", ""); 65 props.setProperty("hibernate.connection.pool_size", "1"); 66 props.setProperty("hibernate.cache.provider_class", "net.sf.hibernate.cache.TreeCacheProvider"); 67 cfg.setProperties(props); 68 69 73 factory = cfg.buildSessionFactory(); 74 75 List lst = new ArrayList (); 76 Dialect dialect = Dialect.getDialect(props); 77 lst.addAll(Arrays.asList(cfg.generateDropSchemaScript(dialect))); 78 lst.addAll(Arrays.asList(cfg.generateSchemaCreationScript(dialect))); 79 80 Session tmp = factory.openSession(); 82 Connection conn = tmp.connection(); 83 Statement st = conn.createStatement(); 84 for (int i = 0; i < lst.size(); i++) 85 { 86 String s = (String ) lst.get(i); 87 if (!s.startsWith("alter table")) 88 { 89 st.executeQuery(s); 90 } 91 } 92 tmp.close(); 93 94 session = factory.openSession(); 96 tx = session.beginTransaction(); 97 98 populate(); 100 nextSession(); 101 102 userModule = new UserModuleImpl() 104 { 105 protected Session getSession() 106 { 107 return session; 108 } 109 }; 110 111 roleModule = new RoleModuleImpl() 113 { 114 protected Session getSession() 115 { 116 return session; 117 } 118 }; 119 } 120 121 protected void tearDown() throws Exception 122 { 123 tx.commit(); 124 session.close(); 125 factory.close(); 126 } 127 128 protected void nextSession() throws Exception 129 { 130 tx.commit(); 131 session.close(); 132 session = factory.openSession(); 133 tx = session.beginTransaction(); 134 } 135 136 protected void populate() throws Exception 137 { 138 } 139 } | Popular Tags |