001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: EvolveTest.java,v 1.6.2.6 2008/01/07 15:14:35 cwl Exp $
007: */
008: package com.sleepycat.persist.test;
009:
010: import java.io.IOException;
011:
012: import junit.framework.Test;
013:
014: import com.sleepycat.je.util.TestUtils;
015: import com.sleepycat.persist.evolve.EvolveConfig;
016: import com.sleepycat.persist.evolve.EvolveEvent;
017: import com.sleepycat.persist.evolve.EvolveListener;
018: import com.sleepycat.persist.evolve.EvolveStats;
019: import com.sleepycat.persist.impl.PersistCatalog;
020:
021: /**
022: * Runs part two of the EvolveTest. This part is run with the new/updated
023: * version of EvolveClasses in the classpath. It uses the environment and
024: * store created by EvolveTestInit. It verifies that it can read/write/evolve
025: * objects serialized using the old class format, and that it can create new
026: * objects with the new class format.
027: *
028: * @author Mark Hayes
029: */
030: public class EvolveTest extends EvolveTestBase {
031:
032: public static Test suite() throws Exception {
033:
034: return getSuite(EvolveTest.class);
035: }
036:
037: private int evolveNRead;
038: private int evolveNConverted;
039:
040: boolean useEvolvedClass() {
041: return true;
042: }
043:
044: @Override
045: public void setUp() throws IOException {
046:
047: /* Copy the log files created by EvolveTestInit. */
048: envHome = getTestInitHome(true /*evolved*/);
049: envHome.mkdirs();
050: TestUtils.removeLogFiles("Setup", envHome, false);
051: TestUtils.copyFiles(getTestInitHome(false /*evolved*/),
052: envHome);
053: }
054:
055: public void testLazyEvolve() throws Exception {
056:
057: openEnv();
058:
059: /*
060: * Open in raw mode to check unevolved raw object and formats. This
061: * is possible whether or not we can open the store further below to
062: * evolve formats without errors.
063: */
064: openRawStore();
065: caseObj.checkUnevolvedModel(rawStore.getModel(), env);
066: caseObj.readRawObjects(rawStore, false /*expectEvolved*/,
067: false /*expectUpdated*/);
068: closeRawStore();
069:
070: if (openStoreReadWrite()) {
071:
072: /*
073: * When opening read-write, formats are evolved lazily. Check by
074: * reading evolved objects.
075: */
076: caseObj
077: .checkEvolvedModel(store.getModel(), env, true /*oldTypesExist*/);
078: caseObj.readObjects(store, false /*doUpdate*/);
079: closeStore();
080:
081: /*
082: * Read raw objects again to check that the evolved objects are
083: * returned even though the stored objects were not evolved.
084: */
085: openRawStore();
086: caseObj
087: .checkEvolvedModel(rawStore.getModel(), env, true /*oldTypesExist*/);
088: caseObj.readRawObjects(rawStore, true /*expectEvolved*/,
089: false /*expectUpdated*/);
090: closeRawStore();
091:
092: /*
093: * Open read-only to ensure that the catalog does not need to
094: * change (evolve formats) unnecessarily.
095: */
096: PersistCatalog.expectNoClassChanges = true;
097: try {
098: openStoreReadOnly();
099: } finally {
100: PersistCatalog.expectNoClassChanges = false;
101: }
102: caseObj
103: .checkEvolvedModel(store.getModel(), env, true /*oldTypesExist*/);
104: caseObj.readObjects(store, false /*doUpdate*/);
105: closeStore();
106:
107: /*
108: * Open read-write to update objects and store them in evolved
109: * format.
110: */
111: openStoreReadWrite();
112: caseObj
113: .checkEvolvedModel(store.getModel(), env, true /*oldTypesExist*/);
114: caseObj.readObjects(store, true /*doUpdate*/);
115: caseObj
116: .checkEvolvedModel(store.getModel(), env, true /*oldTypesExist*/);
117: closeStore();
118:
119: /*
120: * Check raw objects again after the evolved objects were stored.
121: */
122: openRawStore();
123: caseObj
124: .checkEvolvedModel(rawStore.getModel(), env, true /*oldTypesExist*/);
125: caseObj.readRawObjects(rawStore, true /*expectEvolved*/,
126: true /*expectUpdated*/);
127: closeRawStore();
128: }
129:
130: closeAll();
131: }
132:
133: public void testEagerEvolve() throws Exception {
134:
135: /* If the store cannot be opened, this test is not appropriate. */
136: if (caseObj.getStoreOpenException() != null) {
137: return;
138: }
139:
140: EvolveConfig config = new EvolveConfig();
141: config.setEvolveListener(new EvolveListener() {
142: public boolean evolveProgress(EvolveEvent event) {
143: EvolveStats stats = event.getStats();
144: evolveNRead = stats.getNRead();
145: evolveNConverted = stats.getNConverted();
146: return true;
147: }
148: });
149:
150: openEnv();
151:
152: openStoreReadWrite();
153:
154: /*
155: * Evolve and expect that the expected number of entities are
156: * converted.
157: */
158: int nExpected = caseObj.getNRecordsExpected();
159: evolveNRead = 0;
160: evolveNConverted = 0;
161: PersistCatalog.unevolvedFormatsEncountered = false;
162: EvolveStats stats = store.evolve(config);
163: if (nExpected > 0) {
164: assertTrue(PersistCatalog.unevolvedFormatsEncountered);
165: }
166: assertTrue(evolveNRead == nExpected);
167: assertTrue(evolveNConverted == nExpected);
168: assertTrue(evolveNConverted >= evolveNRead);
169: assertEquals(evolveNRead, stats.getNRead());
170: assertEquals(evolveNConverted, stats.getNConverted());
171:
172: /* Evolve again and expect that no entities are converted. */
173: evolveNRead = 0;
174: evolveNConverted = 0;
175: PersistCatalog.unevolvedFormatsEncountered = false;
176: stats = store.evolve(config);
177: assertTrue(!PersistCatalog.unevolvedFormatsEncountered);
178: assertTrue(evolveNRead == 0);
179: assertTrue(evolveNConverted == 0);
180: assertEquals(0, stats.getNRead());
181: assertEquals(0, stats.getNConverted());
182:
183: /* Ensure that we can read all entities without evolution. */
184: PersistCatalog.unevolvedFormatsEncountered = false;
185: caseObj.readObjects(store, false /*doUpdate*/);
186: assertTrue(!PersistCatalog.unevolvedFormatsEncountered);
187:
188: /*
189: * When automatic unused type deletion is implemented in the future the
190: * oldTypesExist parameters below should be changed to false.
191: */
192:
193: /* Open again and try an update. */
194: caseObj
195: .checkEvolvedModel(store.getModel(), env, true /*oldTypesExist*/);
196: caseObj.readObjects(store, true /*doUpdate*/);
197: caseObj
198: .checkEvolvedModel(store.getModel(), env, true /*oldTypesExist*/);
199: closeStore();
200:
201: /* Open read-only and double check that everything is OK. */
202: openStoreReadOnly();
203: caseObj
204: .checkEvolvedModel(store.getModel(), env, true /*oldTypesExist*/);
205: caseObj.readObjects(store, false /*doUpdate*/);
206: caseObj
207: .checkEvolvedModel(store.getModel(), env, true /*oldTypesExist*/);
208: closeStore();
209:
210: /* Check raw objects. */
211: openRawStore();
212: caseObj
213: .checkEvolvedModel(rawStore.getModel(), env, true /*oldTypesExist*/);
214: caseObj
215: .readRawObjects(rawStore, true /*expectEvolved*/, true /*expectUpdated*/);
216:
217: /*
218: * Test copy raw object to new store via convertRawObject. In this
219: * test we can pass false for oldTypesExist because newStore starts
220: * with the new/evolved class model.
221: */
222: openNewStore();
223: caseObj.copyRawObjects(rawStore, newStore);
224: caseObj.readObjects(newStore, true /*doUpdate*/);
225: caseObj
226: .checkEvolvedModel(newStore.getModel(), env, false /*oldTypesExist*/);
227: closeNewStore();
228: closeRawStore();
229:
230: closeAll();
231: }
232: }
|