KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > test > core > suite > RestructureTestCase


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.test.core.suite;
25
26 import junit.framework.Test;
27 import org.objectweb.jalisto.se.JalistoFactory;
28 import org.objectweb.jalisto.se.test.data.Book;
29 import org.objectweb.jalisto.se.test.workbench.JalistoTestCase;
30 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Random JavaDoc;
35
36 public class RestructureTestCase extends JalistoTestCase {
37
38     public RestructureTestCase() {
39     }
40
41     public RestructureTestCase(String JavaDoc name) {
42         super(name);
43     }
44
45     public static Test suite() {
46         JalistoTestSuite suite = new JalistoTestSuite();
47         RestructureTestCase tc = (RestructureTestCase) newTestCase(suite, new RestructureTestCase());
48
49         tc.newSession();
50         tc.define();
51
52         tc.cleanUp();
53
54         tc.populate(500);
55
56         tc.stabilityTest(50, 100);
57         tc.reorganize();
58
59         tc.cleanUp();
60         tc.reorganize();
61
62         tc.finishTests();
63
64         return suite;
65     }
66
67     /**
68      * **************************************************************************************
69      */

70
71     public void newSession() {
72         System.gc();
73         session = JalistoFactory.getSession(getJalistoPropertiesFilename());
74         session.openSession();
75         tx = session.currentTransaction();
76     }
77
78     public void define() {
79         session.defineClass(Book.getMetaDescription());
80     }
81
82     public void reorganize() {
83         if (session.getInternalSession().getProperties().allowsSpecialFunctionnalities()) {
84             session.closeSession();
85             session.reorganize();
86             session.openSession();
87         } else {
88             System.out.println("not allows reorganize functionnality");
89         }
90     }
91
92     public void cleanUp() {
93         oids = new ArrayList JavaDoc();
94         tx.begin();
95         Iterator JavaDoc extent = session.getExtent(Book.class).readFully().iterator();
96         while (extent.hasNext()) {
97             Object JavaDoc oid = extent.next();
98             session.deleteObjectByOid(oid);
99         }
100         tx.commit();
101     }
102
103     public void populate(int nbrBook) {
104         tx.begin();
105         for (int i = 0; i < nbrBook; i++) {
106             oids.add(session.createObject(Book.newBook().toArray(), Book.class));
107         }
108         tx.commit();
109     }
110
111     public void stabilityTest(int nbrCreate, int nbrIt) {
112         Random JavaDoc random = new Random JavaDoc();
113
114         for (int c = 0; c < nbrIt; c++) {
115             tx.begin();
116             for (int i = 0; i < nbrCreate; i++) {
117                 Object JavaDoc[] bookInArray = Book.newBook().toArray();
118                 Object JavaDoc oid = session.createObject(bookInArray, Book.class);
119                 oids.add(oid);
120             }
121             tx.commit();
122
123             tx.begin();
124             for (int i = 0; i < nbrCreate; i++) {
125                 int index = random.nextInt(oids.size());
126                 Object JavaDoc oid = oids.get(index);
127                 session.readObjectByOid(oid);
128             }
129             tx.commit();
130         }
131     }
132
133     public void finishTests() {
134         if ((session != null) && (session.isOpen())) {
135             session.closeSession();
136         }
137     }
138
139     ArrayList JavaDoc oids = null;
140 }
141
Popular Tags