KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > test > SubclassIndexTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: SubclassIndexTest.java,v 1.5 2006/11/16 04:18:21 mark Exp $
7  */

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