001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: SubclassIndexTest.java,v 1.5.2.3 2008/01/07 15:14:35 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.test;
010:
011: import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;
012:
013: import java.io.File;
014: import java.io.IOException;
015:
016: import junit.framework.TestCase;
017:
018: import com.sleepycat.je.DatabaseException;
019: import com.sleepycat.je.Environment;
020: import com.sleepycat.je.EnvironmentConfig;
021: import com.sleepycat.je.util.TestUtils;
022: import com.sleepycat.persist.EntityCursor;
023: import com.sleepycat.persist.EntityStore;
024: import com.sleepycat.persist.PrimaryIndex;
025: import com.sleepycat.persist.SecondaryIndex;
026: import com.sleepycat.persist.StoreConfig;
027: import com.sleepycat.persist.model.Entity;
028: import com.sleepycat.persist.model.Persistent;
029: import com.sleepycat.persist.model.PrimaryKey;
030: import com.sleepycat.persist.model.SecondaryKey;
031:
032: public class SubclassIndexTest extends TestCase {
033:
034: private File envHome;
035: private Environment env;
036:
037: public void setUp() throws IOException {
038:
039: envHome = new File(System.getProperty(TestUtils.DEST_DIR));
040: TestUtils.removeLogFiles("Setup", envHome, false);
041: }
042:
043: public void tearDown() throws IOException {
044:
045: if (env != null) {
046: try {
047: env.close();
048: } catch (DatabaseException e) {
049: System.out.println("During tearDown: " + e);
050: }
051: }
052: try {
053: TestUtils.removeLogFiles("TearDown", envHome, false);
054: } catch (Error e) {
055: System.out.println("During tearDown: " + e);
056: }
057: envHome = null;
058: env = null;
059: }
060:
061: public void testSubclassIndex() throws DatabaseException {
062:
063: EnvironmentConfig envConfig = new EnvironmentConfig();
064: envConfig.setAllowCreate(true);
065: env = new Environment(envHome, envConfig);
066:
067: StoreConfig storeConfig = new StoreConfig();
068: storeConfig.setAllowCreate(true);
069: EntityStore store = new EntityStore(env, "foo", storeConfig);
070:
071: PrimaryIndex<String, Employee> employeesById = store
072: .getPrimaryIndex(String.class, Employee.class);
073:
074: employeesById.put(new Employee("1"));
075: employeesById.put(new Manager("2", "a"));
076: employeesById.put(new Manager("3", "a"));
077: employeesById.put(new Manager("4", "b"));
078:
079: Employee e;
080: Manager m;
081:
082: e = employeesById.get("1");
083: assertNotNull(e);
084: assertTrue(!(e instanceof Manager));
085:
086: /* Ensure DB exists BEFORE calling getSubclassIndex. [#15247] */
087: PersistTestUtils.assertDbExists(true, env, "foo",
088: Employee.class.getName(), "dept");
089:
090: /* Normal use: Subclass index for a key in the subclass. */
091: SecondaryIndex<String, String, Manager> managersByDept = store
092: .getSubclassIndex(employeesById, Manager.class,
093: String.class, "dept");
094:
095: m = managersByDept.get("a");
096: assertNotNull(m);
097: assertEquals("2", m.id);
098:
099: m = managersByDept.get("b");
100: assertNotNull(m);
101: assertEquals("4", m.id);
102:
103: EntityCursor<Manager> managers = managersByDept.entities();
104: try {
105: m = managers.next();
106: assertNotNull(m);
107: assertEquals("2", m.id);
108: m = managers.next();
109: assertNotNull(m);
110: assertEquals("3", m.id);
111: m = managers.next();
112: assertNotNull(m);
113: assertEquals("4", m.id);
114: m = managers.next();
115: assertNull(m);
116: } finally {
117: managers.close();
118: }
119:
120: /* Getting a subclass index for the entity class is also allowed. */
121: store.getSubclassIndex(employeesById, Employee.class,
122: String.class, "other");
123:
124: /* Getting a subclass index for a base class key is not allowed. */
125: try {
126: store.getSubclassIndex(employeesById, Manager.class,
127: String.class, "other");
128: fail();
129: } catch (IllegalArgumentException expected) {
130: }
131:
132: store.close();
133: env.close();
134: env = null;
135: }
136:
137: @Entity
138: private static class Employee {
139:
140: @PrimaryKey
141: String id;
142:
143: @SecondaryKey(relate=MANY_TO_ONE)
144: String other;
145:
146: Employee(String id) {
147: this .id = id;
148: }
149:
150: private Employee() {
151: }
152: }
153:
154: @Persistent
155: private static class Manager extends Employee {
156:
157: @SecondaryKey(relate=MANY_TO_ONE)
158: String dept;
159:
160: Manager(String id, String dept) {
161: super (id);
162: this .dept = dept;
163: }
164:
165: private Manager() {
166: }
167: }
168: }
|