summaryrefslogtreecommitdiff
path: root/db-4.8.30/test/scr024/src/com/sleepycat/persist/test/EvolveTest.java
blob: c76a63a76bbd5590e96241162dd4a40447812624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2000-2009 Oracle.  All rights reserved.
 *
 * $Id$
 */
package com.sleepycat.persist.test;

import java.io.IOException;

import junit.framework.Test;

import com.sleepycat.persist.evolve.EvolveConfig;
import com.sleepycat.persist.evolve.EvolveEvent;
import com.sleepycat.persist.evolve.EvolveListener;
import com.sleepycat.persist.evolve.EvolveStats;
import com.sleepycat.persist.impl.PersistCatalog;
import com.sleepycat.util.test.SharedTestUtils;

/**
 * Runs part two of the EvolveTest.  This part is run with the new/updated
 * version of EvolveClasses in the classpath.  It uses the environment and
 * store created by EvolveTestInit.  It verifies that it can read/write/evolve
 * objects serialized using the old class format, and that it can create new
 * objects with the new class format.
 *
 * @author Mark Hayes
 */
public class EvolveTest extends EvolveTestBase {

    /* Toggle to use listener every other test case. */
    private static boolean useEvolveListener;

    public static Test suite()
        throws Exception {

        return getSuite(EvolveTest.class);
    }

    private int evolveNRead;
    private int evolveNConverted;

    boolean useEvolvedClass() {
        return true;
    }

    @Override
    public void tearDown() {
        try { super.tearDown(); } catch (Throwable e) { }
    }

    @Override
    public void setUp()
        throws IOException {

        /* Copy the log files created by EvolveTestInit. */
        envHome = getTestInitHome(true /*evolved*/);
        envHome.mkdirs();
        SharedTestUtils.emptyDir(envHome);
        SharedTestUtils.copyFiles(getTestInitHome(false /*evolved*/), envHome);
    }

    public void testLazyEvolve()
        throws Exception {

        openEnv();

        /*
         * Open in raw mode to check unevolved raw object and formats.  This
         * is possible whether or not we can open the store further below to
         * evolve formats without errors.
         */
        openRawStore();
        caseObj.checkUnevolvedModel(rawStore.getModel(), env);
        caseObj.readRawObjects
            (rawStore, false /*expectEvolved*/, false /*expectUpdated*/);
        closeRawStore();

        if (openStoreReadWrite()) {

            /*
             * When opening read-write, formats are evolved lazily.  Check by
             * reading evolved objects.
             */
            caseObj.checkEvolvedModel
                (store.getModel(), env, true /*oldTypesExist*/);
            caseObj.readObjects(store, false /*doUpdate*/);
            closeStore();

            /*
             * Read raw objects again to check that the evolved objects are
             * returned even though the stored objects were not evolved.
             */
            openRawStore();
            caseObj.checkEvolvedModel
                (rawStore.getModel(), env, true /*oldTypesExist*/);
            caseObj.readRawObjects
                (rawStore, true /*expectEvolved*/, false /*expectUpdated*/);
            closeRawStore();

            /*
             * Open read-only to ensure that the catalog does not need to
             * change (evolve formats) unnecessarily.
             */
            PersistCatalog.expectNoClassChanges = true;
            try {
                openStoreReadOnly();
            } finally {
                PersistCatalog.expectNoClassChanges = false;
            }
            caseObj.checkEvolvedModel
                (store.getModel(), env, true /*oldTypesExist*/);
            caseObj.readObjects(store, false /*doUpdate*/);
            closeStore();

            /*
             * Open read-write to update objects and store them in evolved
             * format.
             */
            openStoreReadWrite();
            caseObj.checkEvolvedModel
                (store.getModel(), env, true /*oldTypesExist*/);
            caseObj.readObjects(store, true /*doUpdate*/);
            caseObj.checkEvolvedModel
                (store.getModel(), env, true /*oldTypesExist*/);
            closeStore();

            /*
             * Check raw objects again after the evolved objects were stored.
             */
            openRawStore();
            caseObj.checkEvolvedModel
                (rawStore.getModel(), env, true /*oldTypesExist*/);
            caseObj.readRawObjects
                (rawStore, true /*expectEvolved*/, true /*expectUpdated*/);
            closeRawStore();
        }

        closeAll();
    }

    public void testEagerEvolve()
        throws Exception {

        /* If the store cannot be opened, this test is not appropriate. */
        if (caseObj.getStoreOpenException() != null) {
            return;
        }

        EvolveConfig config = new EvolveConfig();

        /*
         * Use listener every other time to ensure that the stats are returned
         * correctly when no listener is configured. [#17024]
         */
        useEvolveListener = !useEvolveListener;
        if (useEvolveListener) {
            config.setEvolveListener(new EvolveListener() {
                public boolean evolveProgress(EvolveEvent event) {
                    EvolveStats stats = event.getStats();
                    evolveNRead = stats.getNRead();
                    evolveNConverted = stats.getNConverted();
                    return true;
                }
            });
        }

        openEnv();

        openStoreReadWrite();

        /*
         * Evolve and expect that the expected number of entities are
         * converted.
         */
        int nExpected = caseObj.getNRecordsExpected();
        evolveNRead = 0;
        evolveNConverted = 0;
        PersistCatalog.unevolvedFormatsEncountered = false;
        EvolveStats stats = store.evolve(config);
        if (nExpected > 0) {
            assertTrue(PersistCatalog.unevolvedFormatsEncountered);
        }
        assertTrue(stats.getNRead() == nExpected);
        assertTrue(stats.getNConverted() == nExpected);
        assertTrue(stats.getNConverted() >= stats.getNRead());
        if (useEvolveListener) {
            assertEquals(evolveNRead, stats.getNRead());
            assertEquals(evolveNConverted, stats.getNConverted());
        }

        /* Evolve again and expect that no entities are converted. */
        evolveNRead = 0;
        evolveNConverted = 0;
        PersistCatalog.unevolvedFormatsEncountered = false;
        stats = store.evolve(config);
        assertTrue(!PersistCatalog.unevolvedFormatsEncountered);
        assertEquals(0, stats.getNRead());
        assertEquals(0, stats.getNConverted());
        if (useEvolveListener) {
            assertTrue(evolveNRead == 0);
            assertTrue(evolveNConverted == 0);
        }

        /* Ensure that we can read all entities without evolution. */
        PersistCatalog.unevolvedFormatsEncountered = false;
        caseObj.readObjects(store, false /*doUpdate*/);
        assertTrue(!PersistCatalog.unevolvedFormatsEncountered);

        /*
         * When automatic unused type deletion is implemented in the future the
         * oldTypesExist parameters below should be changed to false.
         */

        /* Open again and try an update. */
        caseObj.checkEvolvedModel
            (store.getModel(), env, true /*oldTypesExist*/);
        caseObj.readObjects(store, true /*doUpdate*/);
        caseObj.checkEvolvedModel
            (store.getModel(), env, true /*oldTypesExist*/);
        closeStore();

        /* Open read-only and double check that everything is OK. */
        openStoreReadOnly();
        caseObj.checkEvolvedModel
            (store.getModel(), env, true /*oldTypesExist*/);
        caseObj.readObjects(store, false /*doUpdate*/);
        caseObj.checkEvolvedModel
            (store.getModel(), env, true /*oldTypesExist*/);
        closeStore();

        /* Check raw objects. */
        openRawStore();
        caseObj.checkEvolvedModel
            (rawStore.getModel(), env, true /*oldTypesExist*/);
        caseObj.readRawObjects
            (rawStore, true /*expectEvolved*/, true /*expectUpdated*/);

        /*
         * Test copy raw object to new store via convertRawObject.  In this
         * test we can pass false for oldTypesExist because newStore starts
         * with the new/evolved class model.
         */
        openNewStore();
        caseObj.copyRawObjects(rawStore, newStore);
        caseObj.readObjects(newStore, true /*doUpdate*/);
        caseObj.checkEvolvedModel
            (newStore.getModel(), env, false /*oldTypesExist*/);
        closeNewStore();
        closeRawStore();

        closeAll();
    }
}