001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: ConvertAndAddTest.java,v 1.1.2.4 2008/01/07 15:14:35 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.test;
010:
011: import java.io.File;
012: import java.io.IOException;
013:
014: import junit.framework.TestCase;
015:
016: import com.sleepycat.je.DatabaseException;
017: import com.sleepycat.je.Environment;
018: import com.sleepycat.je.EnvironmentConfig;
019: import com.sleepycat.je.util.TestUtils;
020: import com.sleepycat.persist.EntityStore;
021: import com.sleepycat.persist.PrimaryIndex;
022: import com.sleepycat.persist.StoreConfig;
023: import com.sleepycat.persist.evolve.Conversion;
024: import com.sleepycat.persist.evolve.Converter;
025: import com.sleepycat.persist.evolve.Mutations;
026: import com.sleepycat.persist.model.Entity;
027: import com.sleepycat.persist.model.EntityModel;
028: import com.sleepycat.persist.model.PrimaryKey;
029:
030: /**
031: * Test a bug fix where an IndexOutOfBoundsException occurs when adding a field
032: * and converting another field, where the latter field is alphabetically
033: * higher than the former. This is also tested by
034: * EvolveClasses.FieldAddAndConvert, but that class does not test evolving an
035: * entity that was created by catalog version 0. [#15797]
036: *
037: * A modified version of this program was run manually with JE 3.2.30 to
038: * produce a log, which is the result of the testSetup() test. The sole log
039: * file was renamed from 00000000.jdb to ConvertAndAddTest.jdb and added to CVS
040: * in this directory. When that log file is opened here, the bug is
041: * reproduced. The modifications to this program for 3.2.30 are:
042: *
043: * + X in testSetup
044: * + X out testConvertAndAddField
045: * + don't remove log files in tearDown
046: * + @Entity version is 0
047: * + removed field MyEntity.a
048: *
049: * This test should be excluded from the BDB build because it uses a stored JE
050: * log file and it tests a fix for a bug that was never present in BDB.
051: *
052: * @author Mark Hayes
053: */
054: public class ConvertAndAddTest extends TestCase {
055:
056: private static final String STORE_NAME = "test";
057:
058: private File envHome;
059: private Environment env;
060:
061: public void setUp() throws IOException {
062:
063: envHome = new File(System.getProperty(TestUtils.DEST_DIR));
064: TestUtils.removeLogFiles("Setup", envHome, false);
065: }
066:
067: public void tearDown() throws IOException {
068:
069: if (env != null) {
070: try {
071: env.close();
072: } catch (DatabaseException e) {
073: System.out.println("During tearDown: " + e);
074: }
075: }
076: try {
077: TestUtils.removeLogFiles("TearDown", envHome, false);
078: } catch (Error e) {
079: System.out.println("During tearDown: " + e);
080: }
081: envHome = null;
082: env = null;
083: }
084:
085: private EntityStore open(boolean addConverter)
086: throws DatabaseException {
087:
088: EnvironmentConfig envConfig = new EnvironmentConfig();
089: envConfig.setAllowCreate(true);
090: env = new Environment(envHome, envConfig);
091:
092: Mutations mutations = new Mutations();
093: mutations.addConverter(new Converter(MyEntity.class.getName(),
094: 0, "b", new MyConversion()));
095:
096: StoreConfig storeConfig = new StoreConfig();
097: storeConfig.setAllowCreate(true);
098: storeConfig.setMutations(mutations);
099: return new EntityStore(env, "foo", storeConfig);
100: }
101:
102: private void close(EntityStore store) throws DatabaseException {
103:
104: store.close();
105: env.close();
106: env = null;
107: }
108:
109: public void testConvertAndAddField() throws DatabaseException,
110: IOException {
111:
112: /* Copy log file resource to log file zero. */
113: TestUtils.loadLog(getClass(), "ConvertAndAddTest.jdb", envHome);
114:
115: EntityStore store = open(true /*addConverter*/);
116:
117: PrimaryIndex<Long, MyEntity> index = store.getPrimaryIndex(
118: Long.class, MyEntity.class);
119:
120: MyEntity entity = index.get(1L);
121: assertNotNull(entity);
122: assertEquals(123, entity.b);
123:
124: close(store);
125: }
126:
127: public void xtestSetup() throws DatabaseException {
128:
129: EntityStore store = open(false /*addConverter*/);
130:
131: PrimaryIndex<Long, MyEntity> index = store.getPrimaryIndex(
132: Long.class, MyEntity.class);
133:
134: MyEntity entity = new MyEntity();
135: entity.key = 1;
136: entity.b = 123;
137: index.put(entity);
138:
139: close(store);
140: }
141:
142: @Entity(version=1)
143: static class MyEntity {
144:
145: @PrimaryKey
146: long key;
147:
148: int a; // added in version 1
149: int b;
150:
151: private MyEntity() {
152: }
153: }
154:
155: public static class MyConversion implements Conversion {
156:
157: public void initialize(EntityModel model) {
158: }
159:
160: public Object convert(Object fromValue) {
161: return fromValue;
162: }
163:
164: @Override
165: public boolean equals(Object o) {
166: return o instanceof MyConversion;
167: }
168: }
169: }
|