KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > test > workbench > JalistoTestCase


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.test.workbench;
25
26 import junit.extensions.jfunc.JFuncTestCase;
27 import org.objectweb.jalisto.se.api.ClassDescription;
28 import org.objectweb.jalisto.se.api.Session;
29 import org.objectweb.jalisto.se.api.Transaction;
30 import org.objectweb.jalisto.se.api.query.IndexManager;
31 import org.objectweb.jalisto.se.api.query.QueryManager;
32 import org.objectweb.jalisto.se.JalistoFactory;
33 import org.objectweb.jalisto.se.test.data.Book;
34 import org.objectweb.jalisto.se.test.data.BookWithAuthor;
35
36 import java.util.Iterator JavaDoc;
37
38 public class JalistoTestCase extends JFuncTestCase {
39
40     public JalistoTestCase() {
41         super();
42     }
43
44     public JalistoTestCase(String JavaDoc name) {
45         super(name);
46     }
47
48     protected void setTestEngine(JalistoTestEngine testEngine) {
49         this.testEngine = testEngine;
50     }
51
52     protected void setUpOnce() throws Exception JavaDoc {
53         System.out.println("=== INIT ===");
54         super.setUpOnce();
55     }
56
57     protected void tearDownOnce() throws Exception JavaDoc {
58         super.tearDownOnce();
59         System.out.println("=== CLOSE ===");
60     }
61
62     protected void tearDown() throws Exception JavaDoc {
63         super.tearDown();
64         endTransaction();
65     }
66
67     protected static Object JavaDoc newTestCase(JalistoTestSuite suite,
68                                         JalistoTestCase testCase) {
69         return suite.getTestProxy(testCase);
70     }
71
72     protected String JavaDoc getJalistoPropertiesFilename() {
73         return testEngine.getDBPropertiesFilename();
74     }
75
76     /**
77      * ****************************** UTIL **************************************
78      */

79
80     public void initSession(boolean withErase) {
81         finishTests();
82         session = JalistoFactory.getSession(getJalistoPropertiesFilename());
83         if (withErase && session.getInternalSession().getProperties().allowsSpecialFunctionnalities()) {
84             session.eraseStorage();
85         }
86         session.openSession();
87         tx = session.currentTransaction();
88         if(tx == null) {
89             throw new IllegalStateException JavaDoc("tx == null");
90         }
91         queryManager = session.getQueryManager();
92         if (queryManager != null) {
93             indexManager = queryManager.getIndexManager();
94         }
95     }
96
97     public void define(ClassDescription meta) {
98         session.defineClass(meta);
99     }
100
101     public void cleanUp(Class JavaDoc aClass) {
102         tx.begin();
103         Iterator JavaDoc extentIt = session.getExtent(aClass).readFully().iterator();
104         while (extentIt.hasNext()) {
105             Object JavaDoc oid = extentIt.next();
106             session.deleteObjectByOid(oid);
107         }
108         tx.commit();
109     }
110
111     public void populate(int nbr) {
112         tx.begin();
113         for (int i = 0; i < nbr; i++) {
114             session.createObject(Book.newBook().toArray(), Book.class);
115             if ((i != 0) && ((i % 1000) == 0)) {
116                 tx.commit();
117                 tx.begin();
118             }
119         }
120         tx.commit();
121     }
122
123     public void populateBookWithAuthor(int nbr) {
124         tx.begin();
125         for (int i = 0; i < nbr; i++) {
126             session.createObject(BookWithAuthor.newBook().toArray(), BookWithAuthor.class);
127             if ((i != 0) && ((i % 1000) == 0)) {
128                 tx.commit();
129                 tx.begin();
130             }
131         }
132         tx.commit();
133     }
134
135     public void finishTests() {
136         endTransaction();
137         closeSession();
138     }
139
140     public void endTransaction() {
141         if ((tx != null) && (tx.isActive())) {
142             tx.rollback();
143         }
144     }
145
146     public void closeSession() {
147         endTransaction();
148         if ((session != null) && (session.isOpen())) {
149             session.closeSession();
150         }
151     }
152
153     protected Session session = null;
154     protected Transaction tx = null;
155     protected QueryManager queryManager = null;
156     protected IndexManager indexManager = null;
157
158
159     protected final Object JavaDoc[] oneObjectArray = new Object JavaDoc[1];
160     private JalistoTestEngine testEngine;
161
162     public static final int numberOfQueryExecute = 30;
163 }
164
Popular Tags