From ee3de4cabc79aabf21197b24af6d233e5e4a50a4 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 30 Aug 2006 21:12:19 +0000 Subject: Added small set of tests that don't need an actual Repository instance. --- .../carbonado/spi/TestConversionComparator.java | 179 +++++++++++ .../com/amazon/carbonado/spi/TestCursorList.java | 224 ++++++++++++++ .../amazon/carbonado/spi/TestTriggerManager.java | 334 +++++++++++++++++++++ 3 files changed, 737 insertions(+) create mode 100644 src/test/java/com/amazon/carbonado/spi/TestConversionComparator.java create mode 100644 src/test/java/com/amazon/carbonado/spi/TestCursorList.java create mode 100644 src/test/java/com/amazon/carbonado/spi/TestTriggerManager.java (limited to 'src/test/java/com/amazon/carbonado/spi') diff --git a/src/test/java/com/amazon/carbonado/spi/TestConversionComparator.java b/src/test/java/com/amazon/carbonado/spi/TestConversionComparator.java new file mode 100644 index 0000000..5cd7caa --- /dev/null +++ b/src/test/java/com/amazon/carbonado/spi/TestConversionComparator.java @@ -0,0 +1,179 @@ +/* + * 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.spi; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.amazon.carbonado.spi.ConversionComparator; + +/** + * Test cases for {@link ConversionComparator}. + * + * @author Brian S O'Neill + */ +public class TestConversionComparator extends TestCase { + private static final int BOOLEAN_CODE = 0; + private static final int BYTE_CODE = 1; + private static final int SHORT_CODE = 2; + private static final int CHAR_CODE = 3; + private static final int INT_CODE = 4; + private static final int FLOAT_CODE = 5; + private static final int LONG_CODE = 6; + private static final int DOUBLE_CODE = 7; + + private static final Class[] PRIMITIVE_CLASSES = { + boolean.class, byte.class, short.class, char.class, + int.class, float.class, long.class, double.class + }; + + private static final Class[] BOXED_PRIMITIVE_CLASSES = { + Boolean.class, Byte.class, Short.class, Character.class, + Integer.class, Float.class, Long.class, Double.class + }; + + // States which primitive conversions are allowed. + private static final boolean[][] PRIMITIVE_MATRIX = { + // from... + // boolean byte short char int float long double + { true, false, false, false, false, false, false, false }, // to boolean + { false, true, false, false, false, false, false, false }, // to byte + { false, true, true, false, false, false, false, false }, // to short + { false, false, false, true, false, false, false, false }, // to char + { false, true, true, false, true, false, false, false }, // to int + { false, true, true, false, false, true, false, false }, // to float + { false, true, true, false, true, false, true, false }, // to long + { false, true, true, false, true, true, false, true }, // to double + }; + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static TestSuite suite() { + return new TestSuite(TestConversionComparator.class); + } + + public TestConversionComparator(String name) { + super(name); + } + + public void test_isConversionPossible_basics() { + ConversionComparator cc = new ConversionComparator(Object.class); + assertEquals(true, cc.isConversionPossible(Object.class)); + assertEquals(false, cc.isConversionPossible(String.class)); + assertEquals(false, cc.isConversionPossible(boolean.class)); + assertEquals(false, cc.isConversionPossible(Integer.class)); + assertEquals(false, cc.isConversionPossible(int.class)); + + cc = new ConversionComparator(String.class); + assertEquals(true, cc.isConversionPossible(Object.class)); + assertEquals(true, cc.isConversionPossible(String.class)); + assertEquals(false, cc.isConversionPossible(boolean.class)); + assertEquals(false, cc.isConversionPossible(Integer.class)); + assertEquals(false, cc.isConversionPossible(int.class)); + + cc = new ConversionComparator(boolean.class); + assertEquals(true, cc.isConversionPossible(Object.class)); + assertEquals(false, cc.isConversionPossible(String.class)); + assertEquals(true, cc.isConversionPossible(boolean.class)); + assertEquals(false, cc.isConversionPossible(Integer.class)); + assertEquals(false, cc.isConversionPossible(int.class)); + + cc = new ConversionComparator(Integer.class); + assertEquals(true, cc.isConversionPossible(Object.class)); + assertEquals(false, cc.isConversionPossible(String.class)); + assertEquals(false, cc.isConversionPossible(boolean.class)); + assertEquals(true, cc.isConversionPossible(Integer.class)); + assertEquals(true, cc.isConversionPossible(int.class)); + + cc = new ConversionComparator(int.class); + assertEquals(true, cc.isConversionPossible(Object.class)); + assertEquals(false, cc.isConversionPossible(String.class)); + assertEquals(false, cc.isConversionPossible(boolean.class)); + assertEquals(true, cc.isConversionPossible(Integer.class)); + assertEquals(true, cc.isConversionPossible(int.class)); + } + + public void test_isConversionPossible_primitives() { + test_isConversionPossible_primitives(false, false); + test_isConversionPossible_primitives(false, true); + test_isConversionPossible_primitives(true, false); + test_isConversionPossible_primitives(true, true); + } + + private void test_isConversionPossible_primitives(boolean fromBoxed, boolean toBoxed) { + for (int fromCode = BOOLEAN_CODE; fromCode <= DOUBLE_CODE; fromCode++) { + ConversionComparator cc = new ConversionComparator + (fromBoxed ? BOXED_PRIMITIVE_CLASSES[fromCode] : PRIMITIVE_CLASSES[fromCode]); + for (int toCode = BOOLEAN_CODE; toCode <= DOUBLE_CODE; toCode++) { + boolean expected = PRIMITIVE_MATRIX[toCode][fromCode]; + Class toType = toBoxed ? BOXED_PRIMITIVE_CLASSES[toCode] : PRIMITIVE_CLASSES[toCode]; + assertEquals(expected, cc.isConversionPossible(toType)); + } + } + } + + public void test_compare() { + ConversionComparator cc = new ConversionComparator(Object.class); + assertEquals(true, cc.compare(Object.class, String.class) < 0); + assertEquals(true, cc.compare(String.class, Object.class) > 0); + assertEquals(0, cc.compare(Object.class, Object.class)); + assertEquals(0, cc.compare(String.class, String.class)); + assertEquals(0, cc.compare(String.class, String.class)); + assertEquals(0, cc.compare(int.class, Number.class)); + + cc = new ConversionComparator(String.class); + assertEquals(true, cc.compare(String.class, Object.class) < 0); + assertEquals(true, cc.compare(Object.class, String.class) > 0); + assertEquals(0, cc.compare(String.class, String.class)); + assertEquals(true, cc.compare(int.class, String.class) > 0); + + cc = new ConversionComparator(Integer.class); + assertEquals(true, cc.compare(String.class, Object.class) > 0); + assertEquals(true, cc.compare(Object.class, String.class) < 0); + assertEquals(true, cc.compare(Object.class, Number.class) > 0); + assertEquals(true, cc.compare(Integer.class, Number.class) < 0); + assertEquals(true, cc.compare(int.class, Number.class) > 0); + assertEquals(true, cc.compare(long.class, Number.class) > 0); + assertEquals(true, cc.compare(long.class, Long.class) < 0); + + cc = new ConversionComparator(int.class); + assertEquals(true, cc.compare(String.class, Object.class) > 0); + assertEquals(true, cc.compare(Object.class, String.class) < 0); + assertEquals(true, cc.compare(Object.class, Number.class) > 0); + assertEquals(true, cc.compare(Integer.class, Number.class) < 0); + assertEquals(true, cc.compare(int.class, Number.class) < 0); + assertEquals(true, cc.compare(long.class, Number.class) < 0); + assertEquals(true, cc.compare(long.class, Long.class) < 0); + + cc = new ConversionComparator(Byte.class); + assertEquals(true, cc.compare(int.class, Number.class) > 0); + assertEquals(true, cc.compare(long.class, Number.class) > 0); + assertEquals(true, cc.compare(long.class, Integer.class) < 0); + + cc = new ConversionComparator(byte.class); + assertEquals(true, cc.compare(short.class, int.class) < 0); + assertEquals(true, cc.compare(long.class, int.class) > 0); + + cc = new ConversionComparator(java.util.Date.class); + assertEquals(true, cc.compare(Object.class, Comparable.class) > 0); + assertEquals(0, cc.compare(java.io.Serializable.class, Comparable.class)); + } +} diff --git a/src/test/java/com/amazon/carbonado/spi/TestCursorList.java b/src/test/java/com/amazon/carbonado/spi/TestCursorList.java new file mode 100644 index 0000000..605e981 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/spi/TestCursorList.java @@ -0,0 +1,224 @@ +/* + * 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.spi; + +import com.amazon.carbonado.Cursor; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.amazon.carbonado.cursor.EmptyCursorFactory; + +/** + * Test case for TransactionManager.CursorList. + * + * @author Brian S O'Neill + */ +public class TestCursorList extends TestCase { + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static TestSuite suite() { + return new TestSuite(TestCursorList.class); + } + + TransactionManager.CursorList mList; + + public TestCursorList(String name) { + super(name); + } + + protected void setUp() { + mList = new TransactionManager.CursorList(); + } + + public void testRegisterFew() { + assertEquals(0, mList.size()); + + { + Cursor cursor = EmptyCursorFactory.newEmptyCursor(); + mList.register(cursor, null); + assertEquals(1, mList.size()); + assertEquals(cursor, mList.getCursor(0)); + assertEquals(null, mList.getValue(0)); + Object value = mList.unregister(cursor); + assertEquals(0, mList.size()); + assertEquals(null, value); + } + + { + Cursor cursor_1 = EmptyCursorFactory.newEmptyCursor(); + Cursor cursor_2 = EmptyCursorFactory.newEmptyCursor(); + mList.register(cursor_1, null); + assertEquals(1, mList.size()); + mList.register(cursor_2, null); + assertEquals(2, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(cursor_2, mList.getCursor(1)); + assertEquals(null, mList.getValue(0)); + assertEquals(null, mList.getValue(1)); + + Object value = mList.unregister(cursor_2); + assertEquals(1, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(null, value); + mList.unregister(cursor_2); + assertEquals(1, mList.size()); + mList.unregister(cursor_1); + assertEquals(0, mList.size()); + } + + // unregister in reverse + { + Cursor cursor_1 = EmptyCursorFactory.newEmptyCursor(); + Cursor cursor_2 = EmptyCursorFactory.newEmptyCursor(); + mList.register(cursor_1, null); + mList.register(cursor_2, null); + + mList.unregister(cursor_1); + assertEquals(1, mList.size()); + assertEquals(cursor_2, mList.getCursor(0)); + + mList.unregister(cursor_1); + assertEquals(1, mList.size()); + mList.unregister(cursor_2); + assertEquals(0, mList.size()); + } + } + + public void testRegisterFewValue() { + Cursor cursor_1 = EmptyCursorFactory.newEmptyCursor(); + Cursor cursor_2 = EmptyCursorFactory.newEmptyCursor(); + String value_1 = "1"; + String value_2 = "2"; + + mList.register(cursor_1, value_1); + assertEquals(1, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(value_1, mList.getValue(0)); + + mList.register(cursor_2, value_2); + assertEquals(2, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(value_1, mList.getValue(0)); + assertEquals(cursor_2, mList.getCursor(1)); + assertEquals(value_2, mList.getValue(1)); + + Object value = mList.unregister(cursor_2); + assertEquals(1, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(value_1, mList.getValue(0)); + assertEquals(value_2, value); + + value = mList.unregister(cursor_2); + assertEquals(1, mList.size()); + assertEquals(null, value); + value = mList.unregister(cursor_1); + assertEquals(0, mList.size()); + assertEquals(value_1, value); + } + + // Tests that the array expands properly. + public void testRegisterMany() { + final int count = 50; + Cursor[] cursors = new Cursor[count]; + for (int i=0; i(); + afterTriggers = new ArrayList(); + failedTriggers = new ArrayList(); + } + + @Override + protected void tearDown() { + } + + List beforeTriggers; + List afterTriggers; + List failedTriggers; + + public void testAddAndRemove() { + TriggerManager set = new TriggerManager(); + Trigger trigger = new TestTrigger(); + + assertNull(set.getInsertTrigger()); + assertNull(set.getUpdateTrigger()); + assertNull(set.getDeleteTrigger()); + + assertTrue(set.addTrigger(trigger)); + assertNotNull(set.getInsertTrigger()); + assertNotNull(set.getUpdateTrigger()); + assertNotNull(set.getDeleteTrigger()); + + assertFalse(set.addTrigger(trigger)); + assertNotNull(set.getInsertTrigger()); + assertNotNull(set.getUpdateTrigger()); + assertNotNull(set.getDeleteTrigger()); + + assertTrue(set.removeTrigger(trigger)); + assertNull(set.getInsertTrigger()); + assertNull(set.getUpdateTrigger()); + assertNull(set.getDeleteTrigger()); + + assertFalse(set.removeTrigger(trigger)); + assertNull(set.getInsertTrigger()); + assertNull(set.getUpdateTrigger()); + assertNull(set.getDeleteTrigger()); + + Trigger trigger2 = new TestTrigger(); + assertTrue(set.addTrigger(trigger)); + assertTrue(set.addTrigger(trigger2)); + assertNotNull(set.getInsertTrigger()); + assertNotNull(set.getUpdateTrigger()); + assertNotNull(set.getDeleteTrigger()); + + assertTrue(set.removeTrigger(trigger)); + assertNotNull(set.getInsertTrigger()); + assertNotNull(set.getUpdateTrigger()); + assertNotNull(set.getDeleteTrigger()); + assertTrue(set.removeTrigger(trigger2)); + assertNull(set.getInsertTrigger()); + assertNull(set.getUpdateTrigger()); + assertNull(set.getDeleteTrigger()); + } + + public void testBeforeAndAfterOps() throws Exception { + TriggerManager set = new TriggerManager(); + TestTrigger trigger = new TestTrigger(); + set.addTrigger(trigger); + Dummy d = new Dummy(); + + Object state = set.getInsertTrigger().beforeInsert(d); + assertEquals(1, trigger.beforeInsertCount); + assertEquals(0, trigger.beforeUpdateCount); + assertEquals(0, trigger.beforeDeleteCount); + + set.getInsertTrigger().afterInsert(d, state); + assertEquals(1, trigger.afterInsertCount); + assertEquals(0, trigger.afterUpdateCount); + assertEquals(0, trigger.afterDeleteCount); + + state = set.getUpdateTrigger().beforeUpdate(d); + assertEquals(1, trigger.beforeUpdateCount); + assertEquals(0, trigger.beforeDeleteCount); + + set.getUpdateTrigger().afterUpdate(d, state); + assertEquals(1, trigger.afterUpdateCount); + assertEquals(0, trigger.afterDeleteCount); + + state = set.getDeleteTrigger().beforeDelete(d); + assertEquals(1, trigger.beforeDeleteCount); + + set.getDeleteTrigger().afterDelete(d, state); + assertEquals(1, trigger.afterDeleteCount); + } + + public void testBeforeAndFailedOps() throws Exception { + TriggerManager set = new TriggerManager(); + TestTrigger trigger = new TestTrigger(); + set.addTrigger(trigger); + Dummy d = new Dummy(); + + Object state = set.getInsertTrigger().beforeInsert(d); + assertEquals(1, trigger.beforeInsertCount); + assertEquals(0, trigger.beforeUpdateCount); + assertEquals(0, trigger.beforeDeleteCount); + + set.getInsertTrigger().failedInsert(d, state); + assertEquals(1, trigger.failedInsertCount); + assertEquals(0, trigger.failedUpdateCount); + assertEquals(0, trigger.failedDeleteCount); + + state = set.getUpdateTrigger().beforeUpdate(d); + assertEquals(1, trigger.beforeUpdateCount); + assertEquals(0, trigger.beforeDeleteCount); + + set.getUpdateTrigger().failedUpdate(d, state); + assertEquals(1, trigger.failedUpdateCount); + assertEquals(0, trigger.failedDeleteCount); + + state = set.getDeleteTrigger().beforeDelete(d); + assertEquals(1, trigger.beforeDeleteCount); + + set.getDeleteTrigger().failedDelete(d, state); + assertEquals(1, trigger.failedDeleteCount); + } + + public void testExecutionOrder() throws Exception { + TriggerManager set = new TriggerManager(); + TestTrigger trigger = new TestTrigger(null); + TestTrigger trigger2 = new TestTrigger(); + set.addTrigger(trigger); + set.addTrigger(trigger2); + Dummy d = new Dummy(); + + // Insert + { + Object state = set.getInsertTrigger().beforeInsert(d); + assertEquals(2, beforeTriggers.size()); + assertEquals(trigger2, beforeTriggers.get(0)); + assertEquals(trigger, beforeTriggers.get(1)); + + set.getInsertTrigger().afterInsert(d, state); + assertEquals(2, afterTriggers.size()); + assertEquals(trigger, afterTriggers.get(0)); + assertEquals(trigger2, afterTriggers.get(1)); + + state = set.getInsertTrigger().beforeInsert(d); + set.getInsertTrigger().failedInsert(d, state); + assertEquals(2, failedTriggers.size()); + assertEquals(trigger, failedTriggers.get(0)); + assertEquals(trigger2, failedTriggers.get(1)); + } + + beforeTriggers.clear(); + afterTriggers.clear(); + failedTriggers.clear(); + + // Update + { + Object state = set.getUpdateTrigger().beforeUpdate(d); + assertEquals(2, beforeTriggers.size()); + assertEquals(trigger2, beforeTriggers.get(0)); + assertEquals(trigger, beforeTriggers.get(1)); + + set.getUpdateTrigger().afterUpdate(d, state); + assertEquals(2, afterTriggers.size()); + assertEquals(trigger, afterTriggers.get(0)); + assertEquals(trigger2, afterTriggers.get(1)); + + state = set.getUpdateTrigger().beforeUpdate(d); + set.getUpdateTrigger().failedUpdate(d, state); + assertEquals(2, failedTriggers.size()); + assertEquals(trigger, failedTriggers.get(0)); + assertEquals(trigger2, failedTriggers.get(1)); + } + + beforeTriggers.clear(); + afterTriggers.clear(); + failedTriggers.clear(); + + // Delete + { + Object state = set.getDeleteTrigger().beforeDelete(d); + assertEquals(2, beforeTriggers.size()); + assertEquals(trigger2, beforeTriggers.get(0)); + assertEquals(trigger, beforeTriggers.get(1)); + + set.getDeleteTrigger().afterDelete(d, state); + assertEquals(2, afterTriggers.size()); + assertEquals(trigger, afterTriggers.get(0)); + assertEquals(trigger2, afterTriggers.get(1)); + + state = set.getDeleteTrigger().beforeDelete(d); + set.getDeleteTrigger().failedDelete(d, state); + assertEquals(2, failedTriggers.size()); + assertEquals(trigger, failedTriggers.get(0)); + assertEquals(trigger2, failedTriggers.get(1)); + } + } + + class TestTrigger extends Trigger { + final Object stateObj; + + int beforeInsertCount; + int afterInsertCount; + int failedInsertCount; + + int beforeUpdateCount; + int afterUpdateCount; + int failedUpdateCount; + + int beforeDeleteCount; + int afterDeleteCount; + int failedDeleteCount; + + TestTrigger() { + this.stateObj = new Object(); + } + + TestTrigger(Object stateObj) { + this.stateObj = stateObj; + } + + @Override + public Object beforeInsert(S storable) { + beforeInsertCount++; + beforeTriggers.add(this); + return stateObj; + } + + @Override + public void afterInsert(S storable, Object state) { + Assert.assertEquals(stateObj, state); + afterTriggers.add(this); + afterInsertCount++; + } + + @Override + public void failedInsert(S storable, Object state) { + Assert.assertEquals(stateObj, state); + failedTriggers.add(this); + failedInsertCount++; + } + + @Override + public Object beforeUpdate(S storable) { + beforeUpdateCount++; + beforeTriggers.add(this); + return stateObj; + } + + @Override + public void afterUpdate(S storable, Object state) { + Assert.assertEquals(stateObj, state); + afterTriggers.add(this); + afterUpdateCount++; + } + + @Override + public void failedUpdate(S storable, Object state) { + Assert.assertEquals(stateObj, state); + failedTriggers.add(this); + failedUpdateCount++; + } + + @Override + public Object beforeDelete(S storable) { + beforeDeleteCount++; + beforeTriggers.add(this); + return stateObj; + } + + @Override + public void afterDelete(S storable, Object state) { + Assert.assertEquals(stateObj, state); + afterTriggers.add(this); + afterDeleteCount++; + } + + @Override + public void failedDelete(S storable, Object state) { + Assert.assertEquals(stateObj, state); + failedTriggers.add(this); + failedDeleteCount++; + } + + } +} -- cgit v1.2.3