1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: SubclassIndexTest.java,v 1.1 2008/02/07 17:12:32 mark Exp $
7 */
8
9package com.sleepycat.persist.test;
10
11import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;
12
13import java.io.File;
14import java.io.IOException;
15
16import junit.framework.TestCase;
17
18import com.sleepycat.db.DatabaseException;
19import com.sleepycat.db.Environment;
20import com.sleepycat.db.EnvironmentConfig;
21import com.sleepycat.persist.EntityCursor;
22import com.sleepycat.persist.EntityStore;
23import com.sleepycat.persist.PrimaryIndex;
24import com.sleepycat.persist.SecondaryIndex;
25import com.sleepycat.persist.StoreConfig;
26import com.sleepycat.persist.model.Entity;
27import com.sleepycat.persist.model.Persistent;
28import com.sleepycat.persist.model.PrimaryKey;
29import com.sleepycat.persist.model.SecondaryKey;
30import com.sleepycat.util.test.SharedTestUtils;
31import com.sleepycat.util.test.TestEnv;
32
33public class SubclassIndexTest extends TestCase {
34
35    private File envHome;
36    private Environment env;
37
38    public void setUp()
39        throws IOException {
40
41        envHome = new File(System.getProperty(SharedTestUtils.DEST_DIR));
42        SharedTestUtils.emptyDir(envHome);
43    }
44
45    public void tearDown()
46        throws IOException {
47
48        if (env != null) {
49            try {
50                env.close();
51            } catch (DatabaseException e) {
52                System.out.println("During tearDown: " + e);
53            }
54        }
55        try {
56            SharedTestUtils.emptyDir(envHome);
57        } catch (Error e) {
58            System.out.println("During tearDown: " + e);
59        }
60        envHome = null;
61        env = null;
62    }
63
64    public void testSubclassIndex()
65        throws IOException, DatabaseException {
66
67        EnvironmentConfig envConfig = TestEnv.BDB.getConfig();
68        envConfig.setAllowCreate(true);
69        env = new Environment(envHome, envConfig);
70
71        StoreConfig storeConfig = new StoreConfig();
72        storeConfig.setAllowCreate(true);
73        EntityStore store = new EntityStore(env, "foo", storeConfig);
74
75        PrimaryIndex<String, Employee> employeesById =
76            store.getPrimaryIndex(String.class, Employee.class);
77
78        employeesById.put(new Employee("1"));
79        employeesById.put(new Manager("2", "a"));
80        employeesById.put(new Manager("3", "a"));
81        employeesById.put(new Manager("4", "b"));
82
83        Employee e;
84        Manager m;
85
86        e = employeesById.get("1");
87        assertNotNull(e);
88        assertTrue(!(e instanceof Manager));
89
90        /* Ensure DB exists BEFORE calling getSubclassIndex. [#15247] */
91        PersistTestUtils.assertDbExists
92            (true, env, "foo", Employee.class.getName(), "dept");
93
94        /* Normal use: Subclass index for a key in the subclass. */
95        SecondaryIndex<String, String, Manager> managersByDept =
96            store.getSubclassIndex
97                (employeesById, Manager.class, String.class, "dept");
98
99        m = managersByDept.get("a");
100        assertNotNull(m);
101        assertEquals("2", m.id);
102
103        m = managersByDept.get("b");
104        assertNotNull(m);
105        assertEquals("4", m.id);
106
107        EntityCursor<Manager> managers = managersByDept.entities();
108        try {
109            m = managers.next();
110            assertNotNull(m);
111            assertEquals("2", m.id);
112            m = managers.next();
113            assertNotNull(m);
114            assertEquals("3", m.id);
115            m = managers.next();
116            assertNotNull(m);
117            assertEquals("4", m.id);
118            m = managers.next();
119            assertNull(m);
120        } finally {
121            managers.close();
122        }
123
124        /* Getting a subclass index for the entity class is also allowed. */
125        store.getSubclassIndex
126            (employeesById, Employee.class, String.class, "other");
127
128        /* Getting a subclass index for a base class key is not allowed. */
129        try {
130            store.getSubclassIndex
131                (employeesById, Manager.class, String.class, "other");
132            fail();
133        } catch (IllegalArgumentException expected) {
134        }
135
136        store.close();
137        env.close();
138        env = null;
139    }
140
141    @Entity
142    private static class Employee {
143
144        @PrimaryKey
145        String id;
146
147        @SecondaryKey(relate=MANY_TO_ONE)
148        String other;
149
150        Employee(String id) {
151            this.id = id;
152        }
153
154        private Employee() {}
155    }
156
157    @Persistent
158    private static class Manager extends Employee {
159
160        @SecondaryKey(relate=MANY_TO_ONE)
161        String dept;
162
163        Manager(String id, String dept) {
164            super(id);
165            this.dept = dept;
166        }
167
168        private Manager() {}
169    }
170}
171