KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > test > core > ModelTestCase


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.test.core;
10
11 import java.sql.Connection JavaDoc;
12 import java.sql.Statement JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Properties JavaDoc;
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 /**
31  * @author <a HREF="mailto:julien@jboss.org">Julien Viet </a>
32  * @version $Revision: 1.3 $
33  */

34 public abstract class ModelTestCase
35    extends TestCase
36 {
37    public ModelTestCase(String JavaDoc 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 JavaDoc
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 JavaDoc props = new Properties JavaDoc();
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       //
70
// SchemaExport export = new SchemaExport(cfg);
71
// export.create(false, true);
72

73       factory = cfg.buildSessionFactory();
74
75       List JavaDoc lst = new ArrayList JavaDoc();
76       Dialect dialect = Dialect.getDialect(props);
77       lst.addAll(Arrays.asList(cfg.generateDropSchemaScript(dialect)));
78       lst.addAll(Arrays.asList(cfg.generateSchemaCreationScript(dialect)));
79
80       // Open session and execute all create table statements
81
Session tmp = factory.openSession();
82       Connection JavaDoc conn = tmp.connection();
83       Statement JavaDoc st = conn.createStatement();
84       for (int i = 0; i < lst.size(); i++)
85       {
86          String JavaDoc s = (String JavaDoc) lst.get(i);
87          if (!s.startsWith("alter table"))
88          {
89             st.executeQuery(s);
90          }
91       }
92       tmp.close();
93
94       // Set up session and tx
95
session = factory.openSession();
96       tx = session.beginTransaction();
97
98       // Populate
99
populate();
100       nextSession();
101
102       // Setup user module
103
userModule = new UserModuleImpl()
104       {
105          protected Session getSession()
106          {
107             return session;
108          }
109       };
110
111       // Setup role module
112
roleModule = new RoleModuleImpl()
113       {
114          protected Session getSession()
115          {
116             return session;
117          }
118       };
119    }
120
121    protected void tearDown() throws Exception JavaDoc
122    {
123       tx.commit();
124       session.close();
125       factory.close();
126    }
127
128    protected void nextSession() throws Exception JavaDoc
129    {
130       tx.commit();
131       session.close();
132       session = factory.openSession();
133       tx = session.beginTransaction();
134    }
135
136    protected void populate() throws Exception JavaDoc
137    {
138    }
139 }
Popular Tags