Author: jcarman Date: Mon Jul 29 16:08:49 2013 New Revision: 1508119 URL: http://svn.apache.org/r1508119 Log: Adding "utils" classes
Added: 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/ObjectProviderInterceptor.java commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/provider/ObjectProviderUtils.java commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestInterceptorUtils.java commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestObjectProviderInterceptor.java commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/provider/TestObjectProviderUtils.java Removed: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ConstantInterceptor.java commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestConstantInterceptor.java Modified: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java Added: 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=1508119&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java Mon Jul 29 16:08:49 2013 @@ -0,0 +1,42 @@ +/* + * 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; + +import org.apache.commons.proxy2.Interceptor; +import org.apache.commons.proxy2.provider.ObjectProviderUtils; + +public final class InterceptorUtils +{ +//---------------------------------------------------------------------------------------------------------------------- +// Static Methods +//---------------------------------------------------------------------------------------------------------------------- + + public static Interceptor constant(Object value) + { + return new ObjectProviderInterceptor(ObjectProviderUtils.constant(value)); + } + +//---------------------------------------------------------------------------------------------------------------------- +// Constructors +//---------------------------------------------------------------------------------------------------------------------- + + private InterceptorUtils() + { + + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ObjectProviderInterceptor.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ObjectProviderInterceptor.java?rev=1508119&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ObjectProviderInterceptor.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ObjectProviderInterceptor.java Mon Jul 29 16:08:49 2013 @@ -0,0 +1,55 @@ +/* + * 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; + +import org.apache.commons.lang3.Validate; +import org.apache.commons.proxy2.Interceptor; +import org.apache.commons.proxy2.Invocation; +import org.apache.commons.proxy2.ObjectProvider; + +/** + * A {@link ObjectProviderInterceptor} merely returns the value returned from + * {@link org.apache.commons.proxy2.ObjectProvider#getObject()}. + */ +public class ObjectProviderInterceptor implements Interceptor +{ +//---------------------------------------------------------------------------------------------------------------------- +// Fields +//---------------------------------------------------------------------------------------------------------------------- + + private final ObjectProvider<?> provider; + +//---------------------------------------------------------------------------------------------------------------------- +// Constructors +//---------------------------------------------------------------------------------------------------------------------- + + public ObjectProviderInterceptor(ObjectProvider<?> provider) + { + this.provider = Validate.notNull(provider, "Provider cannot be null."); + } + +//---------------------------------------------------------------------------------------------------------------------- +// Interceptor Implementation +//---------------------------------------------------------------------------------------------------------------------- + + @Override + public Object intercept(Invocation invocation) throws Throwable + { + return provider.getObject(); + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/provider/ObjectProviderUtils.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/provider/ObjectProviderUtils.java?rev=1508119&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/provider/ObjectProviderUtils.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/provider/ObjectProviderUtils.java Mon Jul 29 16:08:49 2013 @@ -0,0 +1,61 @@ +/* + * 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.provider; + +import org.apache.commons.proxy2.ObjectProvider; + +public final class ObjectProviderUtils +{ +//---------------------------------------------------------------------------------------------------------------------- +// Static Methods +//---------------------------------------------------------------------------------------------------------------------- + + public static <T> ObjectProvider<T> bean(Class<T> beanClass) + { + return new BeanProvider<T>(beanClass); + } + + public static <T extends Cloneable> ObjectProvider<T> cloning(T prototype) + { + return new CloningProvider<T>(prototype); + } + + public static <T> ObjectProvider<T> constant(T value) + { + return new ConstantProvider<T>(value); + } + + public static <T> ObjectProvider<T> nullValue() + { + return new NullProvider<T>(); + } + + public static <T> ObjectProvider<T> singleton(ObjectProvider<T> inner) + { + return new SingletonProvider<T>(inner); + } + +//---------------------------------------------------------------------------------------------------------------------- +// Constructors +//---------------------------------------------------------------------------------------------------------------------- + + private ObjectProviderUtils() + { + + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestInterceptorUtils.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestInterceptorUtils.java?rev=1508119&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestInterceptorUtils.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestInterceptorUtils.java Mon Jul 29 16:08:49 2013 @@ -0,0 +1,37 @@ +/* + * 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; + +import org.apache.commons.proxy2.Interceptor; +import org.apache.commons.proxy2.Invocation; +import org.apache.commons.proxy2.util.AbstractTestCase; +import org.apache.commons.proxy2.util.Echo; +import org.apache.commons.proxy2.util.MockInvocation; + +import java.lang.reflect.Method; + +public class TestInterceptorUtils extends AbstractTestCase +{ + public void testConstant() throws Throwable + { + Interceptor interceptor = InterceptorUtils.constant("Hello!"); + Method method = Echo.class.getMethod("echoBack", String.class); + Invocation invocation = new MockInvocation(method, "World!"); + assertEquals("Hello!", interceptor.intercept(invocation)); + } +} Added: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestObjectProviderInterceptor.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestObjectProviderInterceptor.java?rev=1508119&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestObjectProviderInterceptor.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestObjectProviderInterceptor.java Mon Jul 29 16:08:49 2013 @@ -0,0 +1,46 @@ +/* + * 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; + +import org.apache.commons.proxy2.provider.ObjectProviderUtils; +import org.apache.commons.proxy2.util.AbstractTestCase; + +public class TestObjectProviderInterceptor extends AbstractTestCase +{ +//---------------------------------------------------------------------------------------------------------------------- +// Other Methods +//---------------------------------------------------------------------------------------------------------------------- + + public void testIntercept() throws Throwable + { + ObjectProviderInterceptor interceptor = new ObjectProviderInterceptor(ObjectProviderUtils.constant("Hello!")); + assertEquals("Hello!", interceptor.intercept(null)); + } + + public void testWithNullProvider() + { + try + { + new ObjectProviderInterceptor(null); + } + catch (NullPointerException e) + { + assertEquals("Provider cannot be null.", e.getMessage()); + } + } +} 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=1508119&r1=1508118&r2=1508119&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 Mon Jul 29 16:08:49 2013 @@ -25,6 +25,8 @@ import org.apache.commons.proxy2.util.Mo import java.lang.reflect.Method; +import static org.apache.commons.proxy2.interceptor.InterceptorUtils.constant; + public class TestSwitchInterceptor extends AbstractTestCase { //---------------------------------------------------------------------------------------------------------------------- @@ -34,8 +36,8 @@ public class TestSwitchInterceptor exten public void testWithMultipleAdvices() throws Throwable { SwitchInterceptor interceptor = new SwitchInterceptor(); - interceptor.when(new MethodNameMatcher("echo")).then(new ConstantInterceptor("bar")); - interceptor.when(new MethodNameMatcher("echoBack")).then(new ConstantInterceptor("baz")); + interceptor.when(new MethodNameMatcher("echo")).then(constant("bar")); + interceptor.when(new MethodNameMatcher("echoBack")).then(constant("baz")); Method method = Echo.class.getMethod("echoBack", String.class); Invocation invocation = new MockInvocation(method, "foo", "foo"); assertEquals("baz", interceptor.intercept(invocation)); @@ -51,7 +53,7 @@ public class TestSwitchInterceptor exten public void testWithSingleAdviceWhichDoesNotMatch() throws Throwable { - SwitchInterceptor interceptor = new SwitchInterceptor().when(new MethodNameMatcher("echoBackZZZZ")).then(new ConstantInterceptor("bar")); + SwitchInterceptor interceptor = new SwitchInterceptor().when(new MethodNameMatcher("echoBackZZZZ")).then(constant("bar")); Method method = Echo.class.getMethod("echoBack", String.class); Invocation invocation = new MockInvocation(method, "foo", "foo"); assertEquals("foo", interceptor.intercept(invocation)); @@ -59,7 +61,7 @@ public class TestSwitchInterceptor exten public void testWithSingleAdviceWhichMatches() throws Throwable { - SwitchInterceptor interceptor = new SwitchInterceptor().when(new MethodNameMatcher("echoBack")).then(new ConstantInterceptor("bar")); + SwitchInterceptor interceptor = new SwitchInterceptor().when(new MethodNameMatcher("echoBack")).then(constant("bar")); Method method = Echo.class.getMethod("echoBack", String.class); Invocation invocation = new MockInvocation(method, "foo", "foo"); assertEquals("bar", interceptor.intercept(invocation)); Added: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/provider/TestObjectProviderUtils.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/provider/TestObjectProviderUtils.java?rev=1508119&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/provider/TestObjectProviderUtils.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/provider/TestObjectProviderUtils.java Mon Jul 29 16:08:49 2013 @@ -0,0 +1,51 @@ +/* + * 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.provider; + +import org.apache.commons.proxy2.util.AbstractTestCase; +import org.apache.commons.proxy2.util.EchoImpl; + +import java.util.Date; + +public class TestObjectProviderUtils extends AbstractTestCase +{ + public void testBean() throws Exception + { + assertTrue(ObjectProviderUtils.bean(EchoImpl.class) instanceof BeanProvider); + } + + public void testCloning() throws Exception + { + assertTrue(ObjectProviderUtils.cloning(new Date()) instanceof CloningProvider); + } + + public void testConstant() throws Exception + { + assertTrue(ObjectProviderUtils.constant("Hello") instanceof ConstantProvider); + } + + public void testNullValue() throws Exception + { + assertTrue(ObjectProviderUtils.nullValue() instanceof NullProvider); + } + + public void testSingleton() throws Exception + { + assertTrue(ObjectProviderUtils.singleton(new ConstantProvider<Object>("Hello")) instanceof SingletonProvider); + } +}