Added:
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/util/AnnotationMocker.java
/trunk/dependency-shot-core/src/test/java/cx/ath/mancel01/dependencyshot/test/annotation
/trunk/dependency-shot-core/src/test/java/cx/ath/mancel01/dependencyshot/test/annotation/AnnotationMockerTest.java
=======================================
--- /dev/null
+++
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/util/AnnotationMocker.java
Fri Mar 25 11:11:37 2011
@@ -0,0 +1,331 @@
+/*
+ * Copyright 2011 mathieuancelin.
+ *
+ * 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.
+ * under the License.
+ */
+package cx.ath.mancel01.dependencyshot.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ *
+ * @author mathieuancelin
+ */
+public class AnnotationMocker {
+
+ private static AnnotationMockImpl mock;
+
+ public static <Z extends Annotation> AnnotationMockSet<Z>
forAnnotation(Class<Z> annotation) {
+ return AnnotationMockImpl.newMock(annotation);
+ }
+
+ public static <T> GenericAnnotationMockWith<T> set(T call) {
+ return new GenericAnnotationMockWith<T>() {
+
+ @Override
+ public void with(T value) {
+ mock.withObject(value);
+ }
+ };
+ }
+
+ public static class AnnotationMockImpl<T>
+ implements
+ AnnotationMock<T>,
+ AnnotationMockSet<T>,
+ AnnotationMockWith<T>,
+ InvocationHandler {
+
+ private final Map<String, Object> values = new HashMap<String,
Object>();
+ private final Class<T> anno;
+ private Method method;
+
+ private AnnotationMockImpl(Class<T> anno) {
+ this.anno = anno;
+ }
+
+ public static <S extends Annotation> AnnotationMockImpl<S>
newMock(Class<S> anno) {
+ return new AnnotationMockImpl<S>(anno);
+ }
+
+ @Override
+ public AnnotationMockWith<T> set(String method) {
+ try {
+ this.method = anno.getMethod(method);
+ } catch (Exception ex) {
+ throw new RuntimeException(ex);
+ }
+ return this;
+ }
+
+ @Override
+ public AnnotationMockWith<T> set(Method method) {
+ this.method = method;
+ return this;
+ }
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
+ mock = this;
+ this.method = method;
+ if (method.getName().equals("toString")) {
+ StringBuilder builder = new StringBuilder();
+ builder.append(anno.getSimpleName());
+ builder.append(" : \n");
+ for (String name : values.keySet()) {
+ builder.append(" ");
+ builder.append(name);
+ builder.append(" : ");
+ builder.append(values.get(name));
+ builder.append("\n");
+ }
+ return builder.toString();
+ } else {
+ if (values.containsKey(method.getName())) {
+ return values.get(method.getName());
+ } else {
+ try {
+ Object value =
anno.getMethod(method.getName()).getDefaultValue();
+ if (value != null) {
+ return value;
+ }
+ } catch (Exception e) {
+
+ }
+ return null;
+ //throw new RuntimeException("Method " +
method.getName() + "isn't set ...");
+ }
+ }
+ }
+
+ @Override
+ public T make() {
+ return (T) Proxy.newProxyInstance(
+ getClass().getClassLoader(),
+ new Class[]{anno},
+ this);
+ }
+
+ @Override
+ public T mock() {
+ return (T) Proxy.newProxyInstance(
+ getClass().getClassLoader(),
+ new Class[]{anno}, this);
+ }
+
+ AnnotationMockSet<T> withObject(Object o) {
+ if (method != null) {
+ values.put(method.getName(), o);
+ } else {
+ throw new RuntimeException("Field is not set ...");
+ }
+ method = null;
+ return this;
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(byte b) {
+ return withObject(b);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(short s) {
+ return withObject(s);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(int i) {
+ return withObject(i);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(long l) {
+ return withObject(l);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(float f) {
+ return withObject(f);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(double d) {
+ return withObject(d);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(char c) {
+ return withObject(c);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(boolean b) {
+ return withObject(b);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(String str) {
+ return withObject(str);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(Class clazz) {
+ return withObject(clazz);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(Annotation anno) {
+ return withObject(anno);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(Enum e) {
+ return withObject(e);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(byte[] b) {
+ return withObject(b);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(short[] s) {
+ return withObject(s);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(int[] i) {
+ return withObject(i);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(long[] l) {
+ return withObject(l);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(float[] f) {
+ return withObject(f);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(double[] d) {
+ return withObject(d);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(char[] c) {
+ return withObject(c);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(boolean[] b) {
+ return withObject(b);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(String[] str) {
+ return withObject(str);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(Class[] clazz) {
+ return withObject(clazz);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(Annotation[] anno) {
+ return withObject(anno);
+ }
+
+ @Override
+ public AnnotationMockSet<T> with(Enum[] e) {
+ return withObject(e);
+ }
+ }
+
+ public static interface AnnotationMock<T> {
+
+ T make();
+
+ T mock();
+ }
+
+ public static interface AnnotationMockSet<T> extends AnnotationMock<T>
{
+
+ AnnotationMockWith<T> set(Method method);
+
+ AnnotationMockWith<T> set(String method);
+ }
+
+ public static interface GenericAnnotationMockWith<T> {
+ void with(T value);
+ }
+
+ public static interface AnnotationMockWith<T> extends
AnnotationMock<T> {
+
+ AnnotationMockSet<T> with(byte b);
+
+ AnnotationMockSet<T> with(short s);
+
+ AnnotationMockSet<T> with(int i);
+
+ AnnotationMockSet<T> with(long l);
+
+ AnnotationMockSet<T> with(float f);
+
+ AnnotationMockSet<T> with(double d);
+
+ AnnotationMockSet<T> with(char c);
+
+ AnnotationMockSet<T> with(boolean b);
+
+ AnnotationMockSet<T> with(String str);
+
+ AnnotationMockSet<T> with(Class clazz);
+
+ AnnotationMockSet<T> with(Annotation anno);
+
+ AnnotationMockSet<T> with(Enum e);
+
+ AnnotationMockSet<T> with(byte[] b);
+
+ AnnotationMockSet<T> with(short[] s);
+
+ AnnotationMockSet<T> with(int[] i);
+
+ AnnotationMockSet<T> with(long[] l);
+
+ AnnotationMockSet<T> with(float[] f);
+
+ AnnotationMockSet<T> with(double[] d);
+
+ AnnotationMockSet<T> with(char[] c);
+
+ AnnotationMockSet<T> with(boolean[] b);
+
+ AnnotationMockSet<T> with(String[] str);
+
+ AnnotationMockSet<T> with(Class[] clazz);
+
+ AnnotationMockSet<T> with(Annotation[] anno);
+
+ AnnotationMockSet<T> with(Enum[] e);
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-core/src/test/java/cx/ath/mancel01/dependencyshot/test/annotation/AnnotationMockerTest.java
Fri Mar 25 11:11:37 2011
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2011 mathieuancelin.
+ *
+ * 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.
+ * under the License.
+ */
+package cx.ath.mancel01.dependencyshot.test.annotation;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import static cx.ath.mancel01.dependencyshot.util.AnnotationMocker.set;
+
+import cx.ath.mancel01.dependencyshot.util.AnnotationMocker;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import javax.inject.Named;
+import javax.inject.Qualifier;
+import javax.jws.WebService;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ * @author mathieuancelin
+ */
+public class AnnotationMockerTest {
+ static final String hello = "Hello";
+ static final String kevin = "Kevin";
+ static final String mathieu = "Mathieu";
+ static final String ns = "www.serli.com";
+ static final String service = "Service";
+ static final String port = "PortType";
+ static final String wsdl = "www.serli.com?wsdl";
+ static final String itf = "MyInterface";
+
+ @Test
+ public void testAnnotationMock() {
+
+ Named named = AnnotationMocker.forAnnotation(Named.class)
+ .set("value").with(hello)
+ .make();
+ Named mockNamed =
AnnotationMocker.forAnnotation(Named.class).mock();
+ set(mockNamed.value()).with(kevin);
+
+ MyNamed myNamed1 =
AnnotationMocker.forAnnotation(MyNamed.class).mock();
+ set(myNamed1.ok()).with(Boolean.FALSE);
+ MyNamed myNamed2 =
AnnotationMocker.forAnnotation(MyNamed.class).make();
+
+ WebService ws = AnnotationMocker.forAnnotation(WebService.class)
+ .set("name").with(hello)
+ .set("targetNamespace").with(ns)
+ .set("serviceName").with(service)
+ .set("portName").with(port)
+ .set("wsdlLocation").with(wsdl)
+ .set("endpointInterface").with(itf)
+ .make();
+
+ Assert.assertEquals(hello, ws.name());
+ Assert.assertEquals(ns, ws.targetNamespace());
+ Assert.assertEquals(service, ws.serviceName());
+ Assert.assertEquals(port, ws.portName());
+ Assert.assertEquals(wsdl, ws.wsdlLocation());
+ Assert.assertEquals(itf, ws.endpointInterface());
+ Assert.assertEquals(kevin, mockNamed.value());
+ Assert.assertEquals(mathieu, myNamed2.value());
+ Assert.assertFalse(myNamed1.ok());
+ Assert.assertEquals(hello, named.value());
+ }
+
+ @Qualifier
+ @Documented
+ @Retention(RUNTIME)
+ public @interface MyNamed {
+
+ String value() default AnnotationMockerTest.mathieu;
+ boolean ok() default true;
+ }
+}