1 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 ; 14 import java.io.IOException ; 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 envHome; 35 private Environment env; 36 37 public void setUp() 38 throws IOException { 39 40 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 41 TestUtils.removeLogFiles("Setup", envHome, false); 42 } 43 44 public void tearDown() 45 throws IOException { 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 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 , Employee> employeesById = 75 store.getPrimaryIndex(String .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 90 PersistTestUtils.assertDbExists 91 (true, env, "foo", Employee.class.getName(), "dept"); 92 93 94 SecondaryIndex<String , String , Manager> managersByDept = 95 store.getSubclassIndex 96 (employeesById, Manager.class, String .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 124 store.getSubclassIndex 125 (employeesById, Employee.class, String .class, "other"); 126 127 128 try { 129 store.getSubclassIndex 130 (employeesById, Manager.class, String .class, "other"); 131 fail(); 132 } catch (IllegalArgumentException 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 id; 145 146 @SecondaryKey(relate=MANY_TO_ONE) 147 String other; 148 149 Employee(String 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 dept; 161 162 Manager(String id, String dept) { 163 super(id); 164 this.dept = dept; 165 } 166 167 private Manager() {} 168 } 169 } 170 | Popular Tags |