001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: EvolveCase.java,v 1.8.2.4 2008/01/07 15:14:35 cwl Exp $
007: */
008: package com.sleepycat.persist.test;
009:
010: import java.util.Iterator;
011: import java.util.List;
012:
013: import junit.framework.TestCase;
014:
015: import com.sleepycat.je.DatabaseException;
016: import com.sleepycat.je.Environment;
017: import com.sleepycat.persist.EntityStore;
018: import com.sleepycat.persist.StoreConfig;
019: import com.sleepycat.persist.evolve.Mutations;
020: import com.sleepycat.persist.model.ClassMetadata;
021: import com.sleepycat.persist.model.EntityModel;
022: import com.sleepycat.persist.model.Persistent;
023: import com.sleepycat.persist.raw.RawStore;
024: import com.sleepycat.persist.raw.RawType;
025:
026: @Persistent
027: abstract class EvolveCase {
028:
029: static final String STORE_NAME = "foo";
030:
031: transient boolean updated;
032:
033: Mutations getMutations() {
034: return null;
035: }
036:
037: void configure(EntityModel model, StoreConfig config) {
038: }
039:
040: String getStoreOpenException() {
041: return null;
042: }
043:
044: int getNRecordsExpected() {
045: return 1;
046: }
047:
048: void checkUnevolvedModel(EntityModel model, Environment env) {
049: }
050:
051: void checkEvolvedModel(EntityModel model, Environment env,
052: boolean oldTypesExist) {
053: }
054:
055: void writeObjects(EntityStore store) throws DatabaseException {
056: }
057:
058: void readObjects(EntityStore store, boolean doUpdate)
059: throws DatabaseException {
060: }
061:
062: void readRawObjects(RawStore store, boolean expectEvolved,
063: boolean expectUpdated) throws DatabaseException {
064: }
065:
066: void copyRawObjects(RawStore rawStore, EntityStore newStore)
067: throws DatabaseException {
068: }
069:
070: /**
071: * Checks for equality and prints the entire values rather than
072: * abbreviated values like TestCase.assertEquals does.
073: */
074: static void checkEquals(Object expected, Object got) {
075: if ((expected != null) ? (!expected.equals(got))
076: : (got != null)) {
077: TestCase.fail("Expected:\n" + expected + "\nBut got:\n"
078: + got);
079: }
080: }
081:
082: /**
083: * Asserts than an entity database exists or does not exist.
084: */
085: static void assertDbExists(boolean expectExists, Environment env,
086: String entityClassName) {
087: assertDbExists(expectExists, env, entityClassName, null);
088: }
089:
090: /**
091: * Checks that an entity class exists or does not exist.
092: */
093: static void checkEntity(boolean exists, EntityModel model,
094: Environment env, String className, int version,
095: String secKeyName) {
096: if (exists) {
097: TestCase.assertNotNull(model.getEntityMetadata(className));
098: ClassMetadata meta = model.getClassMetadata(className);
099: TestCase.assertNotNull(meta);
100: TestCase.assertEquals(version, meta.getVersion());
101: TestCase.assertTrue(meta.isEntityClass());
102:
103: RawType raw = model.getRawType(className);
104: TestCase.assertNotNull(raw);
105: TestCase.assertEquals(version, raw.getVersion());
106: } else {
107: TestCase.assertNull(model.getEntityMetadata(className));
108: TestCase.assertNull(model.getClassMetadata(className));
109: TestCase.assertNull(model.getRawType(className));
110: }
111:
112: assertDbExists(exists, env, className);
113: if (secKeyName != null) {
114: assertDbExists(exists, env, className, secKeyName);
115: }
116: }
117:
118: /**
119: * Checks that a non-entity class exists or does not exist.
120: */
121: static void checkNonEntity(boolean exists, EntityModel model,
122: Environment env, String className, int version) {
123: if (exists) {
124: ClassMetadata meta = model.getClassMetadata(className);
125: TestCase.assertNotNull(meta);
126: TestCase.assertEquals(version, meta.getVersion());
127: TestCase.assertTrue(!meta.isEntityClass());
128:
129: RawType raw = model.getRawType(className);
130: TestCase.assertNotNull(raw);
131: TestCase.assertEquals(version, raw.getVersion());
132: } else {
133: TestCase.assertNull(model.getClassMetadata(className));
134: TestCase.assertNull(model.getRawType(className));
135: }
136:
137: TestCase.assertNull(model.getEntityMetadata(className));
138: assertDbExists(false, env, className);
139: }
140:
141: /**
142: * Asserts than a database expectExists or does not exist. If keyName is
143: * null, checks an entity database. If keyName is non-null, checks a
144: * secondary database.
145: */
146: static void assertDbExists(boolean expectExists, Environment env,
147: String entityClassName, String keyName) {
148: PersistTestUtils.assertDbExists(expectExists, env, STORE_NAME,
149: entityClassName, keyName);
150: }
151:
152: static void checkVersions(EntityModel model, String name,
153: int version) {
154: checkVersions(model, new String[] { name },
155: new int[] { version });
156: }
157:
158: static void checkVersions(EntityModel model, String name1,
159: int version1, String name2, int version2) {
160: checkVersions(model, new String[] { name1, name2 }, new int[] {
161: version1, version2 });
162: }
163:
164: private static void checkVersions(EntityModel model,
165: String[] names, int[] versions) {
166: List<RawType> all = model.getAllRawTypeVersions(names[0]);
167: TestCase.assertNotNull(all);
168:
169: assert names.length == versions.length;
170: TestCase.assertEquals(names.length, all.size());
171:
172: Iterator<RawType> iter = all.iterator();
173: for (int i = 0; i < names.length; i += 1) {
174: RawType type = iter.next();
175: TestCase.assertEquals(versions[i], type.getVersion());
176: TestCase.assertEquals(names[i], type.getClassName());
177: }
178: }
179: }
|