commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r1508275 - in /commons/proper/proxy/branches/version-2.0-work: core/src/main/java/org/apache/commons/proxy2/interceptor/ core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ core/src/main/java/org/apache/commons/proxy2/intercep...
Date Tue, 30 Jul 2013 02:08:40 GMT
Author: jcarman
Date: Tue Jul 30 02:08:40 2013
New Revision: 1508275

URL: http://svn.apache.org/r1508275
Log:
Implementing new StubInterceptorBuilder functionality.

Added:
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ArgumentMatcher.java
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/InvocationMatcher.java
      - copied, changed from r1508076, commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/MethodNameMatcher.java
      - copied, changed from r1508076, commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/ReturnTypeMatcher.java
      - copied, changed from r1508076, commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java
    commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.java
    commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptorBuilder.java
    commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java
    commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterface.java
    commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.java
Removed:
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java
Modified:
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java
    commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java
    commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java
    commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java
    commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java

Modified: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java?rev=1508275&r1=1508274&r2=1508275&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java
Tue Jul 30 02:08:40 2013
@@ -18,6 +18,7 @@
 package org.apache.commons.proxy2.interceptor;
 
 import org.apache.commons.proxy2.Interceptor;
+import org.apache.commons.proxy2.ObjectProvider;
 import org.apache.commons.proxy2.provider.ObjectProviderUtils;
 
 public final class InterceptorUtils
@@ -31,6 +32,11 @@ public final class InterceptorUtils
         return new ObjectProviderInterceptor(ObjectProviderUtils.constant(value));
     }
 
+    public static Interceptor provider(ObjectProvider<?> provider)
+    {
+        return new ObjectProviderInterceptor(provider);
+    }
+
 //----------------------------------------------------------------------------------------------------------------------
 // Constructors
 //----------------------------------------------------------------------------------------------------------------------

Modified: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java?rev=1508275&r1=1508274&r2=1508275&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java
Tue Jul 30 02:08:40 2013
@@ -21,13 +21,14 @@ import org.apache.commons.lang3.tuple.Im
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.commons.proxy2.Interceptor;
 import org.apache.commons.proxy2.Invocation;
+import org.apache.commons.proxy2.interceptor.matcher.InvocationMatcher;
 
 import java.io.Serializable;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 /**
- * A {@link SwitchInterceptor} maintains a list of {@link InvocationMatcher}/{@link Interceptor}
pairs.  Each
+ * A {@link SwitchInterceptor} maintains a list of {@link org.apache.commons.proxy2.interceptor.matcher.InvocationMatcher}/{@link
Interceptor} pairs.  Each
  * invocation will be checked against the registered InvocationMatchers.  If one matches
the current invocation, then
  * the corresponding Interceptor will be called.  If no InvocationMatchers match, then the
invocation will merely
  * {@link org.apache.commons.proxy2.Invocation#proceed()} method is called.

Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ArgumentMatcher.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ArgumentMatcher.java?rev=1508275&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ArgumentMatcher.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ArgumentMatcher.java
Tue Jul 30 02:08:40 2013
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.proxy2.interceptor.matcher;
+
+public interface ArgumentMatcher
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Other Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    boolean matches(Object argument);
+}

Copied: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/InvocationMatcher.java
(from r1508076, commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java)
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/InvocationMatcher.java?p2=commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/InvocationMatcher.java&p1=commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java&r1=1508076&r2=1508275&rev=1508275&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InvocationMatcher.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/InvocationMatcher.java
Tue Jul 30 02:08:40 2013
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.commons.proxy2.interceptor;
+package org.apache.commons.proxy2.interceptor.matcher;
 
 import org.apache.commons.proxy2.Invocation;
 

Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java?rev=1508275&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java
Tue Jul 30 02:08:40 2013
@@ -0,0 +1,47 @@
+package org.apache.commons.proxy2.interceptor.matcher.argument;
+
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.proxy2.interceptor.matcher.ArgumentMatcher;
+
+public class ArgumentMatcherUtils
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Static Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    public static ArgumentMatcher any()
+    {
+        return new ArgumentMatcher()
+        {
+            @Override
+            public boolean matches(Object argument)
+            {
+                return true;
+            }
+        };
+    }
+
+    public static ArgumentMatcher eq(final Object value)
+    {
+        return new ArgumentMatcher()
+        {
+            @Override
+            public boolean matches(Object argument)
+            {
+                return ObjectUtils.equals(argument, value);
+            }
+        };
+    }
+
+    public static ArgumentMatcher isInstance(final Class<?> type)
+    {
+        return new ArgumentMatcher()
+        {
+            @Override
+            public boolean matches(Object argument)
+            {
+                return type.isInstance(argument);
+            }
+        };
+    }
+}

Copied: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/MethodNameMatcher.java
(from r1508076, commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java)
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/MethodNameMatcher.java?p2=commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/MethodNameMatcher.java&p1=commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java&r1=1508076&r2=1508275&rev=1508275&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/MethodNameMatcher.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/MethodNameMatcher.java
Tue Jul 30 02:08:40 2013
@@ -15,10 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.commons.proxy2.interceptor.matcher;
+package org.apache.commons.proxy2.interceptor.matcher.invocation;
 
 import org.apache.commons.proxy2.Invocation;
-import org.apache.commons.proxy2.interceptor.InvocationMatcher;
+import org.apache.commons.proxy2.interceptor.matcher.InvocationMatcher;
 
 /**
  * A {@link MethodNameMatcher} simply checks to see that the method name of the invocation
matches the target method

Copied: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/ReturnTypeMatcher.java
(from r1508076, commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java)
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/ReturnTypeMatcher.java?p2=commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/ReturnTypeMatcher.java&p1=commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java&r1=1508076&r2=1508275&rev=1508275&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/ReturnTypeMatcher.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/invocation/ReturnTypeMatcher.java
Tue Jul 30 02:08:40 2013
@@ -15,10 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.commons.proxy2.interceptor.matcher;
+package org.apache.commons.proxy2.interceptor.matcher.invocation;
 
 import org.apache.commons.proxy2.Invocation;
-import org.apache.commons.proxy2.interceptor.InvocationMatcher;
+import org.apache.commons.proxy2.interceptor.matcher.InvocationMatcher;
 
 public class ReturnTypeMatcher implements InvocationMatcher
 {

Modified: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java?rev=1508275&r1=1508274&r2=1508275&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java
Tue Jul 30 02:08:40 2013
@@ -18,7 +18,7 @@
 package org.apache.commons.proxy2.interceptor;
 
 import org.apache.commons.proxy2.Invocation;
-import org.apache.commons.proxy2.interceptor.matcher.MethodNameMatcher;
+import org.apache.commons.proxy2.interceptor.matcher.invocation.MethodNameMatcher;
 import org.apache.commons.proxy2.util.AbstractTestCase;
 import org.apache.commons.proxy2.util.Echo;
 import org.apache.commons.proxy2.util.MockInvocation;

Modified: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java?rev=1508275&r1=1508274&r2=1508275&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestMethodNameMatcher.java
Tue Jul 30 02:08:40 2013
@@ -17,6 +17,7 @@
 
 package org.apache.commons.proxy2.interceptor.matcher;
 
+import org.apache.commons.proxy2.interceptor.matcher.invocation.MethodNameMatcher;
 import org.apache.commons.proxy2.util.AbstractTestCase;
 import org.apache.commons.proxy2.util.Echo;
 import org.apache.commons.proxy2.util.MockInvocation;

Modified: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java?rev=1508275&r1=1508274&r2=1508275&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/matcher/TestReturnTypeMatcher.java
Tue Jul 30 02:08:40 2013
@@ -18,7 +18,7 @@
 package org.apache.commons.proxy2.interceptor.matcher;
 
 import org.apache.commons.proxy2.Invocation;
-import org.apache.commons.proxy2.interceptor.InvocationMatcher;
+import org.apache.commons.proxy2.interceptor.matcher.invocation.ReturnTypeMatcher;
 import org.apache.commons.proxy2.util.AbstractTestCase;
 import org.apache.commons.proxy2.util.Echo;
 import org.apache.commons.proxy2.util.MockInvocation;

Added: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.java?rev=1508275&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.java
Tue Jul 30 02:08:40 2013
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.proxy2.stub;
+
+import org.apache.commons.proxy2.ObjectProvider;
+import org.apache.commons.proxy2.interceptor.InterceptorUtils;
+import org.apache.commons.proxy2.interceptor.matcher.ArgumentMatcher;
+import org.apache.commons.proxy2.interceptor.matcher.argument.ArgumentMatcherUtils;
+
+public abstract class Behavior<T>
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Fields
+//----------------------------------------------------------------------------------------------------------------------
+
+    private TrainingContext trainingContext;
+
+//----------------------------------------------------------------------------------------------------------------------
+// Abstract Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    protected abstract void train(T stub);
+
+//----------------------------------------------------------------------------------------------------------------------
+// Other Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    protected <R> R any()
+    {
+        recordArgumentMatcher(ArgumentMatcherUtils.any());
+        return null;
+    }
+
+    private void recordArgumentMatcher(ArgumentMatcher matcher)
+    {
+        trainingContext.addArgumentMatcher(matcher);
+    }
+
+    protected <R> R eq(R value)
+    {
+        recordArgumentMatcher(ArgumentMatcherUtils.eq(value));
+        return null;
+    }
+
+    protected <R> R isInstance(Class<R> type)
+    {
+        recordArgumentMatcher(ArgumentMatcherUtils.isInstance(type));
+        return null;
+    }
+
+    void train(TrainingContext context, T stub)
+    {
+        this.trainingContext = context;
+        train(stub);
+    }
+
+    protected <R> When<R> when(R expression)
+    {
+        return new When();
+    }
+
+//----------------------------------------------------------------------------------------------------------------------
+// Inner Classes
+//----------------------------------------------------------------------------------------------------------------------
+
+    protected class When<R>
+    {
+        protected Behavior<T> thenReturn(R value)
+        {
+            trainingContext.setInterceptor(InterceptorUtils.constant(value));
+            return Behavior.this;
+        }
+
+        protected Behavior<T> thenReturn(ObjectProvider<R> provider)
+        {
+            trainingContext.setInterceptor(InterceptorUtils.provider(provider));
+            return Behavior.this;
+        }
+    }
+}

Added: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptorBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptorBuilder.java?rev=1508275&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptorBuilder.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptorBuilder.java
Tue Jul 30 02:08:40 2013
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.proxy2.stub;
+
+import org.apache.commons.proxy2.Interceptor;
+import org.apache.commons.proxy2.Invoker;
+import org.apache.commons.proxy2.ProxyFactory;
+import org.apache.commons.proxy2.ProxyUtils;
+import org.apache.commons.proxy2.interceptor.SwitchInterceptor;
+
+import java.lang.reflect.Method;
+
+public class StubInterceptorBuilder
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Fields
+//----------------------------------------------------------------------------------------------------------------------
+
+    private final ProxyFactory proxyFactory;
+    private final SwitchInterceptor interceptor = new SwitchInterceptor();
+
+//----------------------------------------------------------------------------------------------------------------------
+// Constructors
+//----------------------------------------------------------------------------------------------------------------------
+
+    public StubInterceptorBuilder(ProxyFactory proxyFactory)
+    {
+        this.proxyFactory = proxyFactory;
+    }
+
+//----------------------------------------------------------------------------------------------------------------------
+// Other Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    public Interceptor build()
+    {
+        return interceptor;
+    }
+
+    public <T> StubInterceptorBuilder configure(Class<T> type, Behavior<T>
behavior)
+    {
+        final TrainingContext context = new TrainingContext(interceptor);
+        T stub = proxyFactory.createInvokerProxy(new TrainingContextInvoker(context), type);
+        behavior.train(context, stub);
+        return this;
+    }
+
+//----------------------------------------------------------------------------------------------------------------------
+// Inner Classes
+//----------------------------------------------------------------------------------------------------------------------
+
+    private static class TrainingContextInvoker implements Invoker
+    {
+        private final TrainingContext trainingContext;
+
+        private TrainingContextInvoker(TrainingContext trainingContext)
+        {
+            this.trainingContext = trainingContext;
+        }
+
+        @Override
+        public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable
+        {
+            trainingContext.stubMethodInvoked(method, arguments);
+            return ProxyUtils.nullValue(method.getReturnType());
+        }
+    }
+}

Added: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java?rev=1508275&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java
Tue Jul 30 02:08:40 2013
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.proxy2.stub;
+
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.proxy2.Interceptor;
+import org.apache.commons.proxy2.Invocation;
+import org.apache.commons.proxy2.interceptor.SwitchInterceptor;
+import org.apache.commons.proxy2.interceptor.matcher.ArgumentMatcher;
+import org.apache.commons.proxy2.interceptor.matcher.InvocationMatcher;
+import org.apache.commons.proxy2.invoker.RecordedInvocation;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+
+public class TrainingContext
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Fields
+//----------------------------------------------------------------------------------------------------------------------
+
+    private List<ArgumentMatcher> argumentMatchers = new LinkedList<ArgumentMatcher>();
+    private InvocationMatcher matcher;
+    private Interceptor interceptor;
+    private final SwitchInterceptor switchInterceptor;
+
+//----------------------------------------------------------------------------------------------------------------------
+// Constructors
+//----------------------------------------------------------------------------------------------------------------------
+
+    public TrainingContext(SwitchInterceptor switchInterceptor)
+    {
+        this.switchInterceptor = switchInterceptor;
+    }
+
+//----------------------------------------------------------------------------------------------------------------------
+// Other Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    public void addArgumentMatcher(ArgumentMatcher argumentMatcher)
+    {
+        argumentMatchers.add(argumentMatcher);
+    }
+
+    public void setInterceptor(Interceptor interceptor)
+    {
+        this.interceptor = interceptor;
+        addCase();
+    }
+
+    private void addCase()
+    {
+        if (matcher != null && interceptor != null)
+        {
+            switchInterceptor.when(matcher).then(interceptor);
+            matcher = null;
+            interceptor = null;
+            argumentMatchers.clear();
+        }
+    }
+
+    public void stubMethodInvoked(Method method, Object[] arguments)
+    {
+        final ArgumentMatcher[] matchersArray = argumentMatchers.toArray(new ArgumentMatcher[argumentMatchers.size()]);
+        argumentMatchers.clear();
+        final RecordedInvocation invocation = new RecordedInvocation(method, arguments);
+        if (ArrayUtils.isEmpty(matchersArray))
+        {
+            this.matcher = new ExactArgumentsMatcher(invocation);
+        }
+        else if (matchersArray.length == arguments.length)
+        {
+            this.matcher = new ArgumentMatchersMatcher(invocation, matchersArray);
+        }
+        else
+        {
+            throw new IllegalStateException("Either use exact arguments or argument matchers,
but not both.");
+        }
+        addCase();
+    }
+
+//----------------------------------------------------------------------------------------------------------------------
+// Inner Classes
+//----------------------------------------------------------------------------------------------------------------------
+
+    private static class ArgumentMatchersMatcher implements InvocationMatcher
+    {
+        private final RecordedInvocation recordedInvocation;
+        private final ArgumentMatcher[] matchers;
+
+        private ArgumentMatchersMatcher(RecordedInvocation recordedInvocation, ArgumentMatcher[]
matchers)
+        {
+            this.recordedInvocation = recordedInvocation;
+            this.matchers = matchers;
+        }
+
+        @Override
+        public boolean matches(Invocation invocation)
+        {
+            return invocation.getMethod().equals(recordedInvocation.getInvokedMethod()) &&
+                    allArgumentsMatch(invocation.getArguments());
+        }
+
+        private boolean allArgumentsMatch(Object[] arguments)
+        {
+            for (int i = 0; i < arguments.length; i++)
+            {
+                Object argument = arguments[i];
+                if (!matchers[i].matches(argument))
+                {
+                    return false;
+                }
+            }
+            return true;
+        }
+    }
+
+    private static class ExactArgumentsMatcher implements InvocationMatcher
+    {
+        private final RecordedInvocation recordedInvocation;
+
+        private ExactArgumentsMatcher(RecordedInvocation recordedInvocation)
+        {
+            this.recordedInvocation = recordedInvocation;
+        }
+
+        @Override
+        public boolean matches(Invocation invocation)
+        {
+            return invocation.getMethod().equals(recordedInvocation.getInvokedMethod()) &&
+                    Arrays.equals(invocation.getArguments(), recordedInvocation.getArguments());
+        }
+    }
+}

Added: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterface.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterface.java?rev=1508275&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterface.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterface.java
Tue Jul 30 02:08:40 2013
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.proxy2.stub;
+
+public interface StubInterface
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Other Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    public String one(String value);
+    public String three(String arg1, String arg2);
+    public String two(String value);
+}

Added: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.java?rev=1508275&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.java
Tue Jul 30 02:08:40 2013
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.proxy2.stub;
+
+import org.apache.commons.proxy2.Interceptor;
+import org.apache.commons.proxy2.ProxyFactory;
+import org.apache.commons.proxy2.cglib.CglibProxyFactory;
+import org.apache.commons.proxy2.invoker.NullInvoker;
+import org.apache.commons.proxy2.provider.ObjectProviderUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class TestStubInterceptorBuilder
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Fields
+//----------------------------------------------------------------------------------------------------------------------
+
+    private ProxyFactory proxyFactory;
+    private StubInterface target;
+
+//----------------------------------------------------------------------------------------------------------------------
+// Other Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+    @Before
+    public void setUp()
+    {
+        this.proxyFactory = new CglibProxyFactory();
+        this.target = proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, StubInterface.class);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testMixingArgumentMatchingStrategies()
+    {
+        StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
+        builder.configure(StubInterface.class, new Behavior<StubInterface>()
+        {
+            @Override
+            protected void train(StubInterface stub)
+            {
+                when(stub.three(isInstance(String.class), "World")).thenReturn(ObjectProviderUtils.constant("World"));
+            }
+        });
+    }
+
+    @Test
+    public void testWithArgumentMatchers()
+    {
+        StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
+        Interceptor interceptor = builder.configure(StubInterface.class, new Behavior<StubInterface>()
+        {
+            @Override
+            protected void train(StubInterface stub)
+            {
+                when(stub.one(isInstance(String.class))).thenReturn(ObjectProviderUtils.constant("World"));
+            }
+        }).build();
+
+        final StubInterface proxy = proxyFactory.createInterceptorProxy(target, interceptor,
StubInterface.class);
+        assertEquals("World", proxy.one("Hello"));
+        assertEquals("World", proxy.one("Whatever"));
+    }
+
+    @Test
+    public void testWithMismatchedArgument()
+    {
+        StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
+        Interceptor interceptor = builder.configure(StubInterface.class, new Behavior<StubInterface>()
+        {
+            @Override
+            protected void train(StubInterface stub)
+            {
+                when(stub.one(eq("Hello"))).thenReturn("World");
+            }
+        }).build();
+        final StubInterface proxy = proxyFactory.createInterceptorProxy(target, interceptor,
StubInterface.class);
+        assertEquals("World", proxy.one("Hello"));
+        assertEquals(null, proxy.one("Whatever"));
+    }
+
+    @Test
+    public void testWithMultipleMethodsTrained()
+    {
+        StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
+        Interceptor interceptor = builder.configure(StubInterface.class, new Behavior<StubInterface>()
+        {
+            @Override
+            protected void train(StubInterface stub)
+            {
+                when(stub.one("Hello")).thenReturn("World");
+                when(stub.two("Foo")).thenReturn("Bar");
+            }
+        }).build();
+
+        final StubInterface proxy = proxyFactory.createInterceptorProxy(target, interceptor,
StubInterface.class);
+        assertEquals("World", proxy.one("Hello"));
+        assertEquals("Bar", proxy.two("Foo"));
+    }
+
+    @Test
+    public void testWithSingleMethodTrained()
+    {
+        StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
+        Interceptor interceptor = builder.configure(StubInterface.class, new Behavior<StubInterface>()
+        {
+            @Override
+            protected void train(StubInterface stub)
+            {
+                when(stub.one("Hello")).thenReturn("World");
+            }
+        }).build();
+
+        final StubInterface proxy = proxyFactory.createInterceptorProxy(target, interceptor,
StubInterface.class);
+        assertEquals("World", proxy.one("Hello"));
+        assertEquals(null, proxy.two("Whatever"));
+        assertEquals(null, proxy.one("Mismatch!"));
+    }
+}



Mime
View raw message