KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > test > EvolveTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: EvolveTest.java,v 1.6 2006/10/30 21:14:55 bostic Exp $
7  */

8 package com.sleepycat.persist.test;
9
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 import junit.framework.Test;
14
15 import com.sleepycat.je.util.TestUtils;
16 import com.sleepycat.persist.impl.PersistCatalog;
17 import com.sleepycat.persist.evolve.EvolveConfig;
18 import com.sleepycat.persist.evolve.EvolveEvent;
19 import com.sleepycat.persist.evolve.EvolveStats;
20 import com.sleepycat.persist.evolve.EvolveListener;
21
22 /**
23  * Runs part two of the EvolveTest. This part is run with the new/updated
24  * version of EvolveClasses in the classpath. It uses the environment and
25  * store created by EvolveTestInit. It verifies that it can read/write/evolve
26  * objects serialized using the old class format, and that it can create new
27  * objects with the new class format.
28  *
29  * @author Mark Hayes
30  */

31 public class EvolveTest extends EvolveTestBase {
32
33     public static Test suite()
34         throws Exception JavaDoc {
35
36         return getSuite(EvolveTest.class);
37     }
38
39     private int evolveNRead;
40     private int evolveNConverted;
41
42     @Override JavaDoc
43     public void setUp()
44         throws IOException JavaDoc {
45
46         /* Copy the log files created by EvolveTestInit. */
47         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
48         TestUtils.removeLogFiles("Setup", envHome, false);
49         TestUtils.copyFiles(getTestInitHome(), envHome);
50     }
51
52     public void testLazyEvolve()
53         throws Exception JavaDoc {
54
55         openEnv();
56
57         /*
58          * Open in raw mode to check unevolved raw object and formats. This
59          * is possible whether or not we can open the store further below to
60          * evolve formats without errors.
61          */

62         openRawStore();
63         caseObj.checkUnevolvedModel(rawStore.getModel(), env);
64         caseObj.readRawObjects
65             (rawStore, false /*expectEvolved*/, false /*expectUpdated*/);
66         closeRawStore();
67
68         if (openStoreReadWrite()) {
69
70             /*
71              * When opening read-write, formats are evolved lazily. Check by
72              * reading evolved objects.
73              */

74             caseObj.checkEvolvedModel
75                 (store.getModel(), env, true /*oldTypesExist*/);
76             caseObj.readObjects(store, false /*doUpdate*/);
77             closeStore();
78
79             /*
80              * Read raw objects again to check that the evolved objects are
81              * returned even though the stored object were not evolved.
82              */

83             openRawStore();
84             caseObj.checkEvolvedModel
85                 (rawStore.getModel(), env, true /*oldTypesExist*/);
86             caseObj.readRawObjects
87                 (rawStore, true /*expectEvolved*/, false /*expectUpdated*/);
88             closeRawStore();
89
90             /*
91              * Open read-only to ensure that the catalog does not need to
92              * change (evolve formats) unnecessarily.
93              */

94             PersistCatalog.expectNoClassChanges = true;
95             try {
96                 openStoreReadOnly();
97             } finally {
98                 PersistCatalog.expectNoClassChanges = false;
99             }
100             caseObj.checkEvolvedModel
101                 (store.getModel(), env, true /*oldTypesExist*/);
102             caseObj.readObjects(store, false /*doUpdate*/);
103             closeStore();
104
105             /*
106              * Open read-write to update objects and store them in evolved
107              * format.
108              */

109             openStoreReadWrite();
110             caseObj.checkEvolvedModel
111                 (store.getModel(), env, true /*oldTypesExist*/);
112             caseObj.readObjects(store, true /*doUpdate*/);
113             caseObj.checkEvolvedModel
114                 (store.getModel(), env, true /*oldTypesExist*/);
115             closeStore();
116
117             /*
118              * Check raw objects again after the evolved objects were stored.
119              */

120             openRawStore();
121             caseObj.checkEvolvedModel
122                 (rawStore.getModel(), env, true /*oldTypesExist*/);
123             caseObj.readRawObjects
124                 (rawStore, true /*expectEvolved*/, true /*expectUpdated*/);
125             closeRawStore();
126         }
127
128         closeAll();
129     }
130
131     public void testEagerEvolve()
132         throws Exception JavaDoc {
133         
134         /* If the store cannot be opened, this test is not appropriate. */
135         if (caseObj.getStoreOpenException() != null) {
136             return;
137         }
138
139         EvolveConfig config = new EvolveConfig();
140         config.setEvolveListener(new EvolveListener() {
141             public boolean evolveProgress(EvolveEvent event) {
142                 EvolveStats stats = event.getStats();
143                 evolveNRead = stats.getNRead();
144                 evolveNConverted = stats.getNConverted();
145                 return true;
146             }
147         });
148
149         openEnv();
150
151         openStoreReadWrite();
152
153         /*
154          * Evolve and expect that the expected number of entities are
155          * converted.
156          */

157         int nExpected = caseObj.getNRecordsExpected();
158         evolveNRead = 0;
159         evolveNConverted = 0;
160         EvolveStats stats = store.evolve(config);
161         assertTrue(evolveNRead == nExpected);
162         assertTrue(evolveNConverted == nExpected);
163         assertTrue(evolveNConverted >= evolveNRead);
164         assertEquals(evolveNRead, stats.getNRead());
165         assertEquals(evolveNConverted, stats.getNConverted());
166
167         /* Evolve again and expect that no entities are converted. */
168         evolveNRead = 0;
169         evolveNConverted = 0;
170         stats = store.evolve(config);
171         assertTrue(evolveNRead == 0);
172         assertTrue(evolveNConverted == 0);
173         assertEquals(0, stats.getNRead());
174         assertEquals(0, stats.getNConverted());
175
176         /*
177          * When automatic unused type deletion is implemented in the future the
178          * oldTypesExist parameters below should be changed to false.
179          */

180
181         /* Open again and try an update. */
182         caseObj.checkEvolvedModel
183             (store.getModel(), env, true /*oldTypesExist*/);
184         caseObj.readObjects(store, true /*doUpdate*/);
185         caseObj.checkEvolvedModel
186             (store.getModel(), env, true /*oldTypesExist*/);
187         closeStore();
188
189         /* Open read-only and double check that everything is OK. */
190         openStoreReadOnly();
191         caseObj.checkEvolvedModel
192             (store.getModel(), env, true /*oldTypesExist*/);
193         caseObj.readObjects(store, false /*doUpdate*/);
194         caseObj.checkEvolvedModel
195             (store.getModel(), env, true /*oldTypesExist*/);
196         closeStore();
197
198         /* Check raw objects. */
199         openRawStore();
200         caseObj.checkEvolvedModel
201             (rawStore.getModel(), env, true /*oldTypesExist*/);
202         caseObj.readRawObjects
203             (rawStore, true /*expectEvolved*/, true /*expectUpdated*/);
204
205         /*
206          * Test copy raw object to new store via convertRawObject. In this
207          * test we can pass false for oldTypesExist because newStore starts
208          * with the new/evolved class model.
209          */

210         openNewStore();
211         caseObj.copyRawObjects(rawStore, newStore);
212         caseObj.readObjects(newStore, true /*doUpdate*/);
213         caseObj.checkEvolvedModel
214             (newStore.getModel(), env, false /*oldTypesExist*/);
215         closeNewStore();
216         closeRawStore();
217
218         closeAll();
219     }
220 }
221
Popular Tags