summaryrefslogtreecommitdiff
path: root/src/test/java/com/amazon/carbonado/gen
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2007-04-01 00:00:07 +0000
committerBrian S. O'Neill <bronee@gmail.com>2007-04-01 00:00:07 +0000
commit2094a1aa099c8a6614bf3dd27dcca04c87ec1a6c (patch)
treea0d815a5944c76e10dfdad54d12078911d54f315 /src/test/java/com/amazon/carbonado/gen
parent518a7f8bd19d8ab635596f37c2263668d928f616 (diff)
Move Storable code generation support to separate package.
Diffstat (limited to 'src/test/java/com/amazon/carbonado/gen')
-rw-r--r--src/test/java/com/amazon/carbonado/gen/StorableInterceptorFactory.java202
-rw-r--r--src/test/java/com/amazon/carbonado/gen/TestStorableSerializer.java115
-rw-r--r--src/test/java/com/amazon/carbonado/gen/TestWrappedStorableFactory.java164
3 files changed, 481 insertions, 0 deletions
diff --git a/src/test/java/com/amazon/carbonado/gen/StorableInterceptorFactory.java b/src/test/java/com/amazon/carbonado/gen/StorableInterceptorFactory.java
new file mode 100644
index 0000000..527ce21
--- /dev/null
+++ b/src/test/java/com/amazon/carbonado/gen/StorableInterceptorFactory.java
@@ -0,0 +1,202 @@
+/*
+ * 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.gen;
+
+import java.util.Map;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.UndeclaredThrowableException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.ref.Reference;
+import java.lang.ref.SoftReference;
+
+import org.cojen.util.ClassInjector;
+import org.cojen.util.WeakIdentityMap;
+import org.cojen.classfile.TypeDesc;
+import org.cojen.classfile.ClassFile;
+import org.cojen.classfile.Modifiers;
+import org.cojen.classfile.MethodInfo;
+import org.cojen.classfile.CodeBuilder;
+
+import com.amazon.carbonado.Storable;
+
+/**
+ * StorableInterceptorFactory creates instances of Storables that delegate
+ * calls to a proxy.
+ *
+ * <p>If the base class for the interceptor is abstract and has any methods implemented, those
+ * methods will be invoked directly and no further action taken.
+ *
+ * <p>Any methods which are not implemented will be delegated to the proxy which is provided to the
+ * constructor.
+ *
+ * @author Don Schneider
+ */
+public class StorableInterceptorFactory<S extends Storable> {
+
+ public static final String PROXY = "mProxy$";
+
+ private static Map<Object, Reference<StorableInterceptorFactory>>
+ cCache = new WeakIdentityMap();
+
+ /**
+ * @param interceptorType the handler type to be invoked for accessors and
+ * mutators, which should just be the type of S.
+ */
+ public static <S extends Storable> StorableInterceptorFactory<S> getInstance(
+ Class<? extends S> interceptorType, Class<S> userType, boolean shortCircuit)
+ {
+ synchronized (cCache) {
+ StorableInterceptorFactory<S> factory;
+ String key = interceptorType.getName() + userType.getName() + (shortCircuit?"S":"P");
+ Reference<StorableInterceptorFactory> ref = cCache.get(key);
+ if (null != ref) {
+ factory = ref.get();
+ if (factory != null) {
+ return factory;
+ }
+ }
+ factory = new StorableInterceptorFactory<S>(interceptorType, userType, shortCircuit);
+ cCache.put(key, new SoftReference<StorableInterceptorFactory>(factory));
+ return factory;
+ }
+ }
+
+ private final Constructor<? extends S> mConstructor;
+
+ private StorableInterceptorFactory(final Class<? extends S> interceptorType,
+ Class<S> userType,
+ boolean doShortCircuit) {
+ Class storableClass = generateStorable(interceptorType, userType, doShortCircuit);
+ try {
+ mConstructor = storableClass.getConstructor(userType);
+ }
+ catch (NoSuchMethodException e) {
+ throw new UndeclaredThrowableException(e);
+ }
+ }
+
+ private static <T extends Storable> Class<? extends T>
+ generateStorable(Class<? extends T> interceptorType,
+ Class<T> userType,
+ boolean doShortCircuit)
+ {
+ TypeDesc interceptorTypeDesc = TypeDesc.forClass(interceptorType);
+ TypeDesc userTypeDesc = TypeDesc.forClass(userType);
+
+ ClassInjector ci = ClassInjector.create(interceptorType.getName(), null);
+ ClassFile cf = CodeBuilderUtil.createStorableClassFile(
+ ci,
+ interceptorType,
+ false,
+ StorableInterceptorFactory.class.getName());
+
+ // private final Storable mProxy$;
+ cf.addField(Modifiers.PRIVATE.toFinal(true), PROXY, userTypeDesc);
+
+ final TypeDesc[] ctorParams = {userTypeDesc};
+ // Add public constructor:
+ {
+ final int storableHandler = 0;
+ MethodInfo mi = cf.addConstructor(Modifiers.PUBLIC, ctorParams);
+ CodeBuilder b = new CodeBuilder(mi);
+ b.loadThis();
+ try {
+ interceptorType.getConstructor(new Class[] {userType});
+ b.loadLocal(b.getParameter(storableHandler));
+ b.invokeSuperConstructor(ctorParams);
+ }
+ catch (NoSuchMethodException e) {
+ b.invokeSuperConstructor(null);
+ }
+
+
+ //// this.storableHandler = storableHandler
+ CodeBuilderUtil.assertParameterNotNull(b, storableHandler);
+ b.loadThis();
+ b.loadLocal(b.getParameter(storableHandler));
+ b.storeField(PROXY, userTypeDesc);
+
+ b.returnVoid();
+ }
+
+
+ // Add delegation for all abstract methods. It is the responsibility of the implementor
+ // to delegate the non-abstract methods
+
+ for (Method method : interceptorType.getMethods()) {
+ if (Modifier.isAbstract(method.getModifiers())) {
+ MethodInfo mi = cf.addMethod(method);
+ CodeBuilder b = new CodeBuilder(mi);
+
+ // If we're asked to short circuit, we don't bother proxying. This is useful
+ // for creating a "visitor" -- that is, only implement a few "set" methods
+ if (!doShortCircuit) {
+ b.loadThis();
+ b.loadField(PROXY, userTypeDesc);
+ for (int i = 0; i < method.getParameterTypes().length; i++) {
+ b.loadLocal(b.getParameter(i));
+ }
+ b.invoke(method);
+ }
+
+ if (void.class == method.getReturnType()) {
+ b.returnVoid();
+ } else {
+ b.returnValue(TypeDesc.forClass(method.getReturnType()));
+ }
+ }
+ }
+
+ Class result = ci.defineClass(cf);
+ return (Class<? extends T>) result;
+ }
+
+ /**
+ * Create a new proxied storable instance which delegates to the given
+ * proxies. All methods are fully delegated.
+ *
+ * @param storable to use as a proxy
+ */
+ public S create(Storable storable) {
+ try {
+ return mConstructor.newInstance(storable);
+ }
+ catch (InstantiationException e) {
+ InternalError error = new InternalError();
+ error.initCause(e);
+ throw error;
+ } catch (IllegalAccessException e) {
+ InternalError error = new InternalError();
+ error.initCause(e);
+ throw error;
+ } catch (InvocationTargetException e) {
+ Throwable cause = e.getCause();
+ if (cause instanceof RuntimeException) {
+ throw (RuntimeException) cause;
+ }
+ if (cause instanceof Error) {
+ throw (Error) cause;
+ }
+ InternalError error = new InternalError();
+ error.initCause(cause == null ? e : cause);
+ throw error;
+ }
+ }
+}
diff --git a/src/test/java/com/amazon/carbonado/gen/TestStorableSerializer.java b/src/test/java/com/amazon/carbonado/gen/TestStorableSerializer.java
new file mode 100644
index 0000000..2786dce
--- /dev/null
+++ b/src/test/java/com/amazon/carbonado/gen/TestStorableSerializer.java
@@ -0,0 +1,115 @@
+/*
+ * 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.gen;
+
+import java.io.*;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import com.amazon.carbonado.*;
+import com.amazon.carbonado.lob.*;
+
+import com.amazon.carbonado.repo.toy.ToyRepository;
+import com.amazon.carbonado.stored.*;
+
+/**
+ * Test case for {@link StorableSerializer}.
+ *
+ * @author Brian S O'Neill
+ */
+public class TestStorableSerializer extends TestCase {
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(TestStorableSerializer.class);
+ }
+
+ private Repository mRepository;
+
+ public TestStorableSerializer(String name) {
+ super(name);
+ }
+
+ protected void setUp() {
+ mRepository = new ToyRepository();
+ }
+
+ protected void tearDown() {
+ mRepository.close();
+ mRepository = null;
+ }
+
+ public void testReadAndWrite() throws Exception {
+ Storage<StorableTestBasic> storage = mRepository.storageFor(StorableTestBasic.class);
+ StorableTestBasic stb = storage.prepare();
+ stb.setId(50);
+ stb.setStringProp("hello");
+ stb.setIntProp(100);
+ stb.setLongProp(999);
+ stb.setDoubleProp(2.718281828d);
+
+ StorableSerializer<StorableTestBasic> serializer =
+ StorableSerializer.forType(StorableTestBasic.class);
+
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ DataOutputStream dout = new DataOutputStream(bout);
+
+ serializer.write(stb, (DataOutput) dout);
+ dout.flush();
+
+ byte[] bytes = bout.toByteArray();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
+ DataInputStream din = new DataInputStream(bin);
+
+ StorableTestBasic stb2 = serializer.read(storage, (DataInput) din);
+
+ assertEquals(stb, stb2);
+ }
+
+ /*
+ public void testReadAndWriteLobs() throws Exception {
+ Storage<StorableWithLobs> storage = mRepository.storageFor(StorableWithLobs.class);
+ StorableWithLobs s = storage.prepare();
+ s.setBlobValue(new ByteArrayBlob("Hello Blob".getBytes()));
+ s.setClobValue(new StringClob("Hello Clob"));
+
+ StorableSerializer<StorableWithLobs> serializer =
+ StorableSerializer.forType(StorableWithLobs.class);
+
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ DataOutputStream dout = new DataOutputStream(bout);
+
+ serializer.write(s, (DataOutput) dout);
+ dout.flush();
+
+ byte[] bytes = bout.toByteArray();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
+ DataInputStream din = new DataInputStream(bin);
+
+ StorableWithLobs s2 = serializer.read(storage, (DataInput) din);
+
+ assertEquals(s, s2);
+ }
+ */
+}
diff --git a/src/test/java/com/amazon/carbonado/gen/TestWrappedStorableFactory.java b/src/test/java/com/amazon/carbonado/gen/TestWrappedStorableFactory.java
new file mode 100644
index 0000000..40135f1
--- /dev/null
+++ b/src/test/java/com/amazon/carbonado/gen/TestWrappedStorableFactory.java
@@ -0,0 +1,164 @@
+/*
+ * 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.gen;
+
+import java.lang.reflect.Method;
+
+import junit.framework.TestSuite;
+
+import com.amazon.carbonado.Cursor;
+import com.amazon.carbonado.Repository;
+import com.amazon.carbonado.Storable;
+import com.amazon.carbonado.Storage;
+import com.amazon.carbonado.TestStorableBase;
+import com.amazon.carbonado.TestStorables;
+
+import com.amazon.carbonado.stored.StorableTestBasic;
+import com.amazon.carbonado.stored.STBContainer;
+
+/**
+ *
+ *
+ * @author Brian S O'Neill
+ * @author Don Schneider
+ */
+public class TestWrappedStorableFactory extends TestStorableBase {
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(TestWrappedStorableFactory.class);
+ }
+
+ public TestWrappedStorableFactory() {
+ super();
+ }
+
+ /**
+ * Test setAndGet
+ */
+ public void test_proxiedSetAndGet() throws Exception {
+ Class<? extends StorableTestBasic> wrapperClass = StorableGenerator
+ .getWrappedClass(StorableTestBasic.class);
+
+ TestStorables.InvocationTracker props = new TestStorables.InvocationTracker("props");
+ TestStorables.InvocationTracker handler = new TestStorables.InvocationTracker("handler");
+
+ StorableTestBasic wrapper = wrapperClass
+ .getConstructor(WrappedSupport.class, Storable.class)
+ .newInstance(handler, props);
+
+ setPrimaryKeyProperties(wrapper);
+ setBasicProperties(wrapper);
+
+ wrapper.getStringProp();
+ wrapper.getIntProp();
+ wrapper.getLongProp();
+ wrapper.getDoubleProp();
+ wrapper.getId();
+
+ for (Method method : Storable.class.getMethods()) {
+ if (method.getParameterTypes().length > 0) {
+ if (method.getParameterTypes()[0] != String.class) {
+ method.invoke(wrapper, wrapper);
+ }
+ } else {
+ method.invoke(wrapper, (Object[]) null);
+ }
+ }
+
+ props.assertTrack(TestStorables.ALL_GET_METHODS
+ | TestStorables.ALL_SET_METHODS
+ // Copy is called on wrapped storable because wrapped storage is null
+ | TestStorables.sCopy
+ | TestStorables.sToStringKeyOnly
+ | TestStorables.sHasDirtyProperties
+ | TestStorables.sEqualKeys
+ | TestStorables.sEqualProperties
+ | TestStorables.sMarkPropertiesClean
+ | TestStorables.sMarkAllPropertiesClean
+ | TestStorables.sMarkPropertiesDirty
+ | TestStorables.sMarkAllPropertiesDirty
+ | TestStorables.ALL_COPY_PROP_METHODS);
+
+ handler.assertTrack(TestStorables.sTryLoad
+ | TestStorables.sLoad
+ | TestStorables.sInsert
+ | TestStorables.sTryInsert
+ | TestStorables.sUpdate
+ | TestStorables.sTryUpdate
+ | TestStorables.sDelete
+ | TestStorables.sTryDelete);
+ }
+
+ /**
+ * Verify that storables from joined property are also wrapped.
+ */
+ public void test_wrappedJoin() throws Exception {
+ Storage<StorableTestBasic> stbStorage =
+ getRepository().storageFor(StorableTestBasic.class);
+ StorableTestBasic stb = stbStorage.prepare();
+ //assertEquals(stbStorage, stb.storage());
+ //assertTrue(stb.storage().getClass().getName().indexOf("IndexedStorage") > 0);
+
+ stb.setId(1);
+ stb.initBasicProperties();
+ stb.setStringProp("Hello");
+ stb.insert();
+
+ stb = stbStorage.prepare();
+ stb.setId(2);
+ stb.initBasicProperties();
+ stb.setStringProp("Hello");
+ stb.insert();
+
+ stb = stbStorage.prepare();
+ stb.setId(3);
+ stb.initBasicProperties();
+ stb.setStringProp("World");
+ stb.insert();
+
+ Storage<STBContainer> containerStorage = getRepository().storageFor(STBContainer.class);
+
+ STBContainer container = containerStorage.prepare();
+ container.setName("A");
+ container.setCategory("Hello");
+ container.setCount(2);
+ container.insert();
+
+ container = containerStorage.prepare();
+ container.setName("B");
+ container.setCategory("World");
+ container.setCount(1);
+ container.insert();
+
+ // Test wrapping of query results
+ /*
+ container = containerStorage.prepare();
+ container.setName("A");
+ container.load();
+ Cursor<StorableTestBasic> cursor = container.getContained().fetch();
+ while (cursor.hasNext()) {
+ stb = cursor.next();
+ assertTrue(stb.storage().getClass().getName().indexOf("IndexedStorage") > 0);
+ }
+ */
+ }
+}