From 2a973c1f7347c9a4dee944029cc9e9e77b1fefa4 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 15 Oct 2006 18:03:23 +0000 Subject: Added basic index tests. --- .../carbonado/repo/indexed/TestCapabilities.java | 215 +++++++++++++++++++++ .../repo/indexed/TestIndexEntryGenerator.java | 114 +++++++++++ .../stored/StorableTestBasicCompoundIndexed.java | 39 ++++ 3 files changed, 368 insertions(+) create mode 100644 src/test/java/com/amazon/carbonado/repo/indexed/TestCapabilities.java create mode 100644 src/test/java/com/amazon/carbonado/repo/indexed/TestIndexEntryGenerator.java create mode 100644 src/test/java/com/amazon/carbonado/stored/StorableTestBasicCompoundIndexed.java (limited to 'src') diff --git a/src/test/java/com/amazon/carbonado/repo/indexed/TestCapabilities.java b/src/test/java/com/amazon/carbonado/repo/indexed/TestCapabilities.java new file mode 100644 index 0000000..6a6440b --- /dev/null +++ b/src/test/java/com/amazon/carbonado/repo/indexed/TestCapabilities.java @@ -0,0 +1,215 @@ +/* + * Copyright 2006 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.amazon.carbonado.repo.indexed; + +import java.util.Arrays; +import java.util.List; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.amazon.carbonado.Repository; +import com.amazon.carbonado.capability.IndexInfo; +import com.amazon.carbonado.capability.IndexInfoCapability; +import com.amazon.carbonado.capability.StorableInfoCapability; +import com.amazon.carbonado.info.Direction; + +import com.amazon.carbonado.TestUtilities; +import com.amazon.carbonado.stored.StorableTestBasic; +import com.amazon.carbonado.stored.StorableTestBasicIndexed; +import com.amazon.carbonado.stored.StorableTestBasicCompoundIndexed; + +/** + * + * + * @author Brian S O'Neill + */ +public class TestCapabilities extends TestCase { + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static TestSuite suite() { + return new TestSuite(TestCapabilities.class); + } + + private Repository mRepository; + + public TestCapabilities(String name) { + super(name); + } + + protected void setUp() throws Exception { + super.setUp(); + mRepository = TestUtilities.buildTempRepository("indexed"); + } + + protected void tearDown() throws Exception { + super.tearDown(); + mRepository.close(); + mRepository = null; + } + + public void testStorableInfoCapability() throws Exception { + StorableInfoCapability cap = mRepository.getCapability(StorableInfoCapability.class); + assertNotNull(cap); + + String[] names = cap.getUserStorableTypeNames(); + assertEquals(0, names.length); + + mRepository.storageFor(StorableTestBasic.class); + + names = cap.getUserStorableTypeNames(); + assertEquals(1, names.length); + assertEquals(StorableTestBasic.class.getName(), names[0]); + + mRepository.storageFor(StorableTestBasicIndexed.class); + names = cap.getUserStorableTypeNames(); + assertEquals(2, names.length); + List list = Arrays.asList(names); + assertTrue(list.contains(StorableTestBasic.class.getName())); + assertTrue(list.contains(StorableTestBasicIndexed.class.getName())); + + mRepository.storageFor(StorableTestBasicCompoundIndexed.class); + names = cap.getUserStorableTypeNames(); + assertEquals(3, names.length); + list = Arrays.asList(names); + assertTrue(list.contains(StorableTestBasic.class.getName())); + assertTrue(list.contains(StorableTestBasicIndexed.class.getName())); + assertTrue(list.contains(StorableTestBasicCompoundIndexed.class.getName())); + } + + public void testIndexInfoCapability() throws Exception { + IndexInfoCapability cap = mRepository.getCapability(IndexInfoCapability.class); + assertNotNull(cap); + + // StorableTestBasic + IndexInfo[] infos = cap.getIndexInfo(StorableTestBasic.class); + assertEquals(1, infos.length); + assertContainsIndex(infos, + StorableTestBasic.class.getName(), + true, + new String[] {"id"}, + new Direction[] {Direction.ASCENDING}); + + // StorableTestBasicIndexed + infos = cap.getIndexInfo(StorableTestBasicIndexed.class); + assertEquals(5, infos.length); + assertContainsIndex(infos, + StorableTestBasicIndexed.class.getName(), + true, + new String[] {"id"}, + new Direction[] {Direction.ASCENDING}); + assertContainsIndex(infos, + null, + true, + new String[] {"stringProp", "id"}, + new Direction[] {Direction.ASCENDING, Direction.ASCENDING}); + assertContainsIndex(infos, + null, + true, + new String[] {"intProp", "id"}, + new Direction[] {Direction.ASCENDING, Direction.ASCENDING}); + assertContainsIndex(infos, + null, + true, + new String[] {"longProp", "id"}, + new Direction[] {Direction.ASCENDING, Direction.ASCENDING}); + assertContainsIndex(infos, + null, + true, + new String[] {"doubleProp", "id"}, + new Direction[] {Direction.ASCENDING, Direction.ASCENDING}); + + // StorableTestBasicCompoundIndexed + infos = cap.getIndexInfo(StorableTestBasicCompoundIndexed.class); + assertEquals(5, infos.length); + assertContainsIndex(infos, + StorableTestBasicCompoundIndexed.class.getName(), + true, + new String[] {"id"}, + new Direction[] {Direction.ASCENDING}); + assertContainsIndex(infos, + null, + true, + new String[] {"stringProp", "intProp", "id"}, + new Direction[] {Direction.ASCENDING, Direction.ASCENDING, Direction.ASCENDING}); + assertContainsIndex(infos, + null, + true, + new String[] {"intProp", "stringProp", "id"}, + new Direction[] {Direction.ASCENDING, Direction.ASCENDING, Direction.ASCENDING}); + assertContainsIndex(infos, + null, + true, + new String[] {"doubleProp", "longProp", "id"}, + new Direction[] {Direction.DESCENDING, Direction.ASCENDING, Direction.ASCENDING}); + assertContainsIndex(infos, + null, + true, + new String[] {"stringProp", "doubleProp"}, + new Direction[] {Direction.ASCENDING, Direction.ASCENDING}); + } + + private void assertContainsIndex(IndexInfo[] infos, + String name, + boolean unique, + String[] properties, + Direction[] directions) + { + findIndex: + for (IndexInfo info : infos) { + if (name == null || name.equals(info.getName())) { + if (name == null) { + if (unique != info.isUnique() || + properties.length != info.getPropertyNames().length || + directions.length != info.getPropertyDirections().length) { + continue findIndex; + } + } else { + assertEquals(unique, info.isUnique()); + assertEquals(properties.length, info.getPropertyNames().length); + assertEquals(directions.length, info.getPropertyDirections().length); + } + + for (int i=0; i indexDesc = newStorableIndex(test); + IndexEntryGenerator builder = IndexEntryGenerator.getInstance(indexDesc); + + Class s = builder.getIndexEntryClass(); + + validateIndexEntry(test, s); + exerciseStorable(s); + + StorableTestBasic master = + mRepository.storageFor(StorableTestBasic.class).prepare(); + populate(master); + master.insert(); + + Storable index = mRepository.storageFor(s).prepare(); + builder.setAllProperties(index, master); + index.insert(); + + Storable indexChecker = mRepository.storageFor(s).prepare(); + builder.setAllProperties(indexChecker, master); + assertTrue(indexChecker.tryLoad()); + + StorableTestBasic masterChecker = builder.loadMaster(indexChecker); + assertEquals(master, masterChecker); + + assertTrue(builder.isConsistent(index, master)); + masterChecker = + mRepository.storageFor(StorableTestBasic.class).prepare(); + master.copyAllProperties(masterChecker); + assertTrue(builder.isConsistent(index, masterChecker)); + masterChecker.setId(-42); + assertFalse(builder.isConsistent(index, masterChecker)); + + } + } + + /** + * @param test + * @return StorableInfo for this test + */ + private StorableIndex newStorableIndex(TestSyntheticStorableBuilders.TestDef test) { + StorableInfo info = StorableIntrospector.examine(test.mClass); + final Map allProps = info.getAllProperties(); + + TestSyntheticStorableBuilders.IndexDef[] indexDefinitions = test.mProps; + StorableProperty[] props = new StorableProperty[indexDefinitions.length]; + Direction[] dirs = new Direction[indexDefinitions.length]; + int i = 0; + for (TestSyntheticStorableBuilders.IndexDef p : indexDefinitions) { + props[i] = allProps.get(p.getProp()); + dirs[i] = p.getDir(); + i++; + } + StorableIndex index = new StorableIndex(props, dirs); + return index; + } + + +} diff --git a/src/test/java/com/amazon/carbonado/stored/StorableTestBasicCompoundIndexed.java b/src/test/java/com/amazon/carbonado/stored/StorableTestBasicCompoundIndexed.java new file mode 100644 index 0000000..638782f --- /dev/null +++ b/src/test/java/com/amazon/carbonado/stored/StorableTestBasicCompoundIndexed.java @@ -0,0 +1,39 @@ +/* + * Copyright 2006 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.amazon.carbonado.stored; + +import com.amazon.carbonado.*; + +/** + * + * @author Brian S O'Neill + */ +@Indexes({ + @Index({"-stringProp", "-intProp"}), + @Index({"+intProp", "stringProp"}), + @Index("id"), + @Index("-doubleProp"), + @Index({"-doubleProp", "+longProp"}) +}) +@PrimaryKey("id") +@AlternateKeys({ + @Key({"stringProp", "doubleProp"}) +}) +public abstract class StorableTestBasicCompoundIndexed extends StorableTestBasic { +} -- cgit v1.2.3