http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/handler/MockFrameworkHandler.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/handler/MockFrameworkHandler.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/handler/MockFrameworkHandler.java new file mode 100644 index 0000000..32b1284 --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/handler/MockFrameworkHandler.java @@ -0,0 +1,72 @@ +/**************************************************************** + * 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.james.mpt.onami.test.handler; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.james.mpt.onami.test.annotation.MockFramework; +import org.apache.james.mpt.onami.test.annotation.MockType; +import org.apache.james.mpt.onami.test.reflection.ClassHandler; +import org.apache.james.mpt.onami.test.reflection.HandleException; + +/** + * Handler class to handle all {@link MockFramework} annotations. + * + * @see org.apache.onami.test.reflection.ClassVisitor + * @see MockFramework + */ +public final class MockFrameworkHandler + implements ClassHandler<MockFramework> +{ + + private static final Logger LOGGER = Logger.getLogger( MockFrameworkHandler.class.getName() ); + + private MockType mockType; + + /** + * @return the mockType + */ + public MockType getMockType() + { + return mockType; + } + + /** + * {@inheritDoc} + */ + public void handle( MockFramework annotation, Class<?> element ) + throws HandleException + { + if ( mockType != null && mockType != annotation.value() ) + { + throw new HandleException( "Inconsistent mock framework found. " + "Mock framework already set [set: " + + mockType + " now found: " + annotation.value() + "]" ); + } + + if ( LOGGER.isLoggable( Level.FINER ) ) + { + LOGGER.finer( " Found MockFramework: " + annotation.value() ); + } + + mockType = annotation.value(); + } + +}
http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/handler/MockHandler.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/handler/MockHandler.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/handler/MockHandler.java new file mode 100644 index 0000000..04df8c0 --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/handler/MockHandler.java @@ -0,0 +1,178 @@ +/**************************************************************** + * 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.james.mpt.onami.test.handler; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.Map.Entry; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.james.mpt.onami.test.annotation.Mock; +import org.apache.james.mpt.onami.test.mock.MockEngine; +import org.apache.james.mpt.onami.test.reflection.FieldHandler; +import org.apache.james.mpt.onami.test.reflection.HandleException; + + +/** + * Handler class to handle all {@link Mock} annotations. + * + * @see org.apache.onami.test.reflection.ClassVisitor + * @see Mock + */ +public final class MockHandler + implements FieldHandler<Mock> +{ + + private static final Logger LOGGER = Logger.getLogger( MockHandler.class.getName() ); + + private final HashMap<Field, Object> mockedObjects = new HashMap<Field, Object>( 1 ); + + /** + * Return the mocked objects. + * + * @param engine the {@link MockEngine} + * @return the map of mocked objects + */ + public HashMap<Field, Object> getMockedObject( MockEngine engine ) + { + createMockedObjectBymockFramekork( engine ); + return mockedObjects; + } + + private void createMockedObjectBymockFramekork( MockEngine engine ) + { + for ( Entry<Field, Object> entry : mockedObjects.entrySet() ) + { + if ( entry.getValue() instanceof Class<?> ) + { + Field field = entry.getKey(); + Mock mock = field.getAnnotation( Mock.class ); + mockedObjects.put( entry.getKey(), engine.createMock( (Class<?>) entry.getValue(), mock.type() ) ); + } + } + } + + + /** + * Invoked when the visitor founds an element with a {@link Mock} annotation. + * @param annotation The {@link Mock} annotation type + * @param element the {@link Mock} annotated fiels + * @throws HandleException when an error occurs. + */ + @SuppressWarnings( "unchecked" ) + public void handle( final Mock annotation, final Field element ) + throws HandleException + { + final Class<? super Object> type = (Class<? super Object>) element.getDeclaringClass(); + + if ( LOGGER.isLoggable( Level.FINER ) ) + { + LOGGER.finer( " Found annotated field: " + element ); + } + if ( annotation.providedBy().length() > 0 ) + { + Class<?> providedClass = type; + if ( annotation.providerClass() != Object.class ) + { + providedClass = annotation.providerClass(); + } + try + { + Method method = providedClass.getMethod( annotation.providedBy() ); + + if ( !element.getType().isAssignableFrom( method.getReturnType() ) ) + { + throw new HandleException( "Impossible to mock %s due to compatibility type, method provider %s#%s returns %s", + element.getDeclaringClass().getName(), + providedClass.getName(), + annotation.providedBy(), + method.getReturnType().getName() ); + } + try + { + Object mocked = getMockProviderForType( element.getType(), method, type ); + mockedObjects.put( element, mocked ); + } + catch ( Throwable t ) + { + throw new HandleException( "Impossible to mock %s, method provider %s#%s raised an error: %s", + element.getDeclaringClass().getName(), + providedClass.getName(), + annotation.providedBy(), + t ); + } + } + catch ( SecurityException e ) + { + throw new HandleException( "Impossible to mock %s, impossible to access to method provider %s#%s: %s", + element.getDeclaringClass().getName(), + providedClass.getName(), + annotation.providedBy(), + e ); + } + catch ( NoSuchMethodException e ) + { + throw new HandleException( "Impossible to mock %s, the method provider %s#%s doesn't exist.", + element.getDeclaringClass().getName(), + providedClass.getName(), + annotation.providedBy() ); + } + } + else + { + mockedObjects.put( element, element.getType() ); + } + } + + @SuppressWarnings( "unchecked" ) + private <T> T getMockProviderForType( T t, Method method, Class<?> cls ) + throws HandleException + { + if ( method.getReturnType() == t ) + { + try + { + if ( LOGGER.isLoggable( Level.FINER ) ) + { + LOGGER.finer( " ...invoke Provider method for Mock: " + method.getName() ); + } + if ( !Modifier.isPublic( method.getModifiers() ) || !Modifier.isStatic( method.getModifiers() ) ) + { + throw new HandleException( "Impossible to invoke method %s#%s. The method shuld be 'static public %s %s()", + cls.getName(), + method.getName(), + method.getReturnType().getName(), + method.getName() ); + } + + return (T) method.invoke( cls ); + } + catch ( Exception e ) + { + throw new RuntimeException( e ); + } + } + throw new HandleException( "The method: %s should return type %s", method, t ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/MockEngine.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/MockEngine.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/MockEngine.java new file mode 100644 index 0000000..b1bd7b8 --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/MockEngine.java @@ -0,0 +1,50 @@ +/**************************************************************** + * 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.james.mpt.onami.test.mock; + +import org.apache.james.mpt.onami.test.annotation.MockObjType; + +/** + * Interface to specify mock framework class engine. + * + * @see org.apache.onami.test.mock.framework.EasyMockFramework + * @see org.apache.onami.test.mock.framework.MockitoFramework + */ +public interface MockEngine +{ + + /** + * Reset the mock objects + * + * @param objects to reset. + */ + void resetMock( Object... objects ); + + /** + * Create a typed mock + * + * @param <T> Class to mock + * @param cls Class to mock + * @param type the {@link MockObjType} + * @return the mock object + */ + <T> T createMock( Class<T> cls, MockObjType type ); + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/framework/EasyMockFramework.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/framework/EasyMockFramework.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/framework/EasyMockFramework.java new file mode 100644 index 0000000..c371dcc --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/framework/EasyMockFramework.java @@ -0,0 +1,65 @@ +/**************************************************************** + * 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.james.mpt.onami.test.mock.framework; + +import org.apache.james.mpt.onami.test.annotation.MockObjType; +import org.apache.james.mpt.onami.test.mock.MockEngine; +import org.easymock.EasyMock; + +/** + * Specifies the Easy-Mock Framework. + * + * @see MockEngine + */ +public class EasyMockFramework + implements MockEngine +{ + + /** + * {@inheritDoc} + */ + public void resetMock( Object... objects ) + { + EasyMock.reset( objects ); + } + + /** + * {@inheritDoc} + */ + public <T> T createMock( Class<T> cls, MockObjType type ) + { + switch ( type ) + { + case EASY_MOCK_NICE: + return EasyMock.createNiceMock( cls ); + + case EASY_MOCK_STRICT: + return EasyMock.createStrictMock( cls ); + + case EASY_MOCK_NORMAL: + case DEFAULT: + return EasyMock.createMock( cls ); + + default: + throw new IllegalArgumentException( "Unsupported mock type '" + type + "' for Easy-Mock Framework." ); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/framework/MockitoFramework.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/framework/MockitoFramework.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/framework/MockitoFramework.java new file mode 100644 index 0000000..1f45448 --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/mock/framework/MockitoFramework.java @@ -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.james.mpt.onami.test.mock.framework; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.james.mpt.onami.test.annotation.MockObjType.DEFAULT; + +import org.apache.james.mpt.onami.test.annotation.MockObjType; +import org.apache.james.mpt.onami.test.mock.MockEngine; +import org.mockito.Mockito; + +/** + * Specifies the Mockito Framework. + * + * @see MockEngine + */ +public class MockitoFramework + implements MockEngine +{ + + /** + * {@inheritDoc} + */ + public void resetMock( Object... objects ) + { + Mockito.reset( objects ); + } + + /** + * {@inheritDoc} + */ + public <T> T createMock( Class<T> cls, MockObjType type ) + { + checkArgument( DEFAULT == type, "Unsupported mock type '%s' for Mockito Framework.", type ); + return Mockito.mock( cls ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/AnnotationHandler.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/AnnotationHandler.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/AnnotationHandler.java new file mode 100644 index 0000000..5decb70 --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/AnnotationHandler.java @@ -0,0 +1,45 @@ +/**************************************************************** + * 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.james.mpt.onami.test.reflection; + +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; + +/** + * Interface to specify a generic annotation handler. + * + * @param <A> whatever annotation type + * @param <E> the element annotated with an annotation type + */ +public interface AnnotationHandler<A extends Annotation, E extends AnnotatedElement> +{ + + /** + * Invoked when {@link ClassVisitor} found an annotation into a class. + * + * @param annotation handled annotation + * @param element the element annotated with input annotation + * @throws HandleException if an error occurs while processing the annotated element + * and the related annotation + */ + void handle( A annotation, E element ) + throws HandleException; + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/ClassHandler.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/ClassHandler.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/ClassHandler.java new file mode 100644 index 0000000..abf3211 --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/ClassHandler.java @@ -0,0 +1,33 @@ +/**************************************************************** + * 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.james.mpt.onami.test.reflection; + +import java.lang.annotation.Annotation; + +/** + * Interface to specify a generic class handler. + * + * @param <A> whatever annotation type + */ +public interface ClassHandler<A extends Annotation> + extends AnnotationHandler<A, Class<?>> +{ + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/ClassVisitor.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/ClassVisitor.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/ClassVisitor.java new file mode 100644 index 0000000..ba2251b --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/ClassVisitor.java @@ -0,0 +1,110 @@ +/**************************************************************** + * 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.james.mpt.onami.test.reflection; + +import static com.google.common.base.Preconditions.checkArgument; + +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; + +/** + * <p> + * Class visitor engine. + * </p> + * <p> + * Visit the input class and all super classes and invokes handler to register annotations. + * </p> + */ +public final class ClassVisitor +{ + + private static final String JAVA_PACKAGE = "java"; + + private static final Logger LOGGER = Logger.getLogger( ClassVisitor.class.getName() ); + + private final Multimap<Class<? extends Annotation>, AnnotationHandler<? extends Annotation, ? extends AnnotatedElement>> handlers = + ArrayListMultimap.create(); + + /** + * Registers an annotation handler. + * + * @param <A> whatever annotation type + * @param annotationType the annotation class to handle + * @param handler the related annotation handler + * @return the current {@code ClassVisitor} instance + */ + public <A extends Annotation> ClassVisitor registerHandler( Class<A> annotationType, + AnnotationHandler<A, ? extends AnnotatedElement> handler ) + { + handlers.put( annotationType, handler ); + return this; + } + + /** + * Visits all fields, methods and super classes of the input class. + * + * @param <T> any type + * @param type The type + * @throws HandleException when an error occurs. + */ + public <T> void visit( final Class<? super T> type ) + throws HandleException + { + checkArgument( type != null, "Type to be visited cannot be null" ); + + if ( LOGGER.isLoggable( Level.FINER ) ) + { + LOGGER.finer( " Visit class: " + type ); + } + + if ( type.getPackage() != null && type.getPackage().getName().startsWith( JAVA_PACKAGE ) ) + { + return; + } + + handle( type ); + handle( type.getDeclaredFields() ); + handle( type.getDeclaredMethods() ); + + visit( (Class<? super T>) type.getSuperclass() ); + } + + @SuppressWarnings( "unchecked" ) + private void handle( AnnotatedElement... elements ) + throws HandleException + { + for ( AnnotatedElement element : elements ) + { + for ( Annotation annotation : element.getAnnotations() ) + { + for ( AnnotationHandler<? extends Annotation, ? extends AnnotatedElement> handler : handlers.get( annotation.annotationType() ) ) + { + ( (AnnotationHandler<Annotation, AnnotatedElement>) handler ).handle( annotation, element ); + } + } + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/FieldHandler.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/FieldHandler.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/FieldHandler.java new file mode 100644 index 0000000..b1906b0 --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/FieldHandler.java @@ -0,0 +1,34 @@ +/**************************************************************** + * 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.james.mpt.onami.test.reflection; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; + +/** + * Interface to specify a generic field handler. + * + * @param <A> whatever annotation type + */ +public interface FieldHandler<A extends Annotation> + extends AnnotationHandler<A, Field> +{ + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/HandleException.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/HandleException.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/HandleException.java new file mode 100644 index 0000000..50fb695 --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/HandleException.java @@ -0,0 +1,66 @@ +/**************************************************************** + * 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.james.mpt.onami.test.reflection; + +import static java.lang.String.format; + +/** + * Exception thrown by a {@link ClassVisitor} when a error occurs. + */ +public final class HandleException + extends Exception +{ + + private static final long serialVersionUID = 1L; + + /** + * Constructs a new HandleException with the specified detail message and cause. + * + * @param message detail message + * @param cause the cause + */ + public HandleException( String message, Throwable cause ) + { + super( message, cause ); + } + + /** + * Constructs a new HandleException with the specified detail message. + * + * @param message a format string + * @param args arguments referenced by the format specifiers in the format string + * @see String#format(String, Object...) + */ + public HandleException( String message, Object...args ) + { + super( format( message, args ) ); + } + + /** + * Constructs a new HandleException with the specified cause. + * + * @param cause the cause + */ + public HandleException( Throwable cause ) + { + super( cause ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/MethodHandler.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/MethodHandler.java b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/MethodHandler.java new file mode 100644 index 0000000..cf4e8e2 --- /dev/null +++ b/mpt/onami-test/src/main/java/org/apache/james/mpt/onami/test/reflection/MethodHandler.java @@ -0,0 +1,34 @@ +/**************************************************************** + * 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.james.mpt.onami.test.reflection; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; + +/** + * Interface to specify a generic method handler. + * + * @param <A> whatever annotation type + */ +public interface MethodHandler<A extends Annotation> + extends AnnotationHandler<A, Method> +{ + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractEmptyTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractEmptyTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractEmptyTestCase.java new file mode 100644 index 0000000..af71eca --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractEmptyTestCase.java @@ -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.james.mpt.onami.test; + +import java.io.File; +import java.io.FileInputStream; +import java.util.logging.LogManager; + +import org.junit.BeforeClass; + +/** + * Utility class. Just for logging initialization. + */ +abstract public class AbstractEmptyTestCase +{ + + @BeforeClass + public static void initLogging() + throws Exception + { + LogManager.getLogManager().readConfiguration( new FileInputStream( + new File( + "src/test/resources/log4j.properties" ) ) ); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractMockTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractMockTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractMockTestCase.java new file mode 100644 index 0000000..8013c2f --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractMockTestCase.java @@ -0,0 +1,40 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + +import org.apache.james.mpt.onami.test.annotation.Mock; +import org.apache.james.mpt.onami.test.data.Service; +import org.easymock.EasyMock; + +abstract public class AbstractMockTestCase + extends AbstractEmptyTestCase +{ + + // Create and inject a Provided EasyMock + @Mock( providedBy = "getMock" ) + protected Service providedMock; + + public static Service getMock() + { + // Create the mock object and inject the dependency via Google-guice into HelloWorld + return EasyMock.createNiceMock( Service.class ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractMockitoTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractMockitoTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractMockitoTestCase.java new file mode 100644 index 0000000..55de444 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractMockitoTestCase.java @@ -0,0 +1,45 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + + +import org.apache.james.mpt.onami.test.annotation.Mock; +import org.apache.james.mpt.onami.test.annotation.MockFramework; +import org.apache.james.mpt.onami.test.annotation.MockType; +import org.apache.james.mpt.onami.test.data.Service; +import org.mockito.Mockito; + +@MockFramework( MockType.MOCKITO ) +abstract public class AbstractMockitoTestCase + extends AbstractEmptyTestCase +{ + + // Create and inject a Provided EasyMock + @Mock( providedBy = "getMock" ) + protected Service providedMock; + + // @MockProvider + public static Service getMock() + { + // Create the mock object and inject the dependency via Google-guice into HelloWorld + return Mockito.mock( Service.class ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractTestCase.java new file mode 100644 index 0000000..b95dc05 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/AbstractTestCase.java @@ -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.james.mpt.onami.test; + +import java.util.ArrayList; + +import org.apache.james.mpt.onami.test.annotation.GuiceModules; +import org.apache.james.mpt.onami.test.annotation.GuiceProvidedModules; +import org.apache.james.mpt.onami.test.data.SimpleModule; +import org.junit.runner.RunWith; + +import com.google.inject.AbstractModule; +import com.google.inject.Module; +import com.google.inject.name.Names; + +@RunWith( OnamiRunner.class ) +@GuiceModules( SimpleModule.class ) +abstract public class AbstractTestCase + extends AbstractEmptyTestCase +{ + + @GuiceProvidedModules + public static Module genericModule() + { + return new AbstractModule() + { + @Override + protected void configure() + { + bind( String.class ).annotatedWith( Names.named( "test.info.inject" ) ).toInstance( "JUnice = JUnit + Guice" ); + } + }; + } + + @GuiceProvidedModules + public static Iterable<Module> genericModule2() + { + AbstractModule a = new AbstractModule() + { + @Override + protected void configure() + { + bind( String.class ).annotatedWith( Names.named( "test.info.inject2" ) ).toInstance( "JUnice = JUnit + Guice Iterable" ); + } + }; + + ArrayList<Module> al = new ArrayList<Module>(); + al.add( a ); + return al; + } + + @GuiceProvidedModules + public static Module[] genericModule3() + { + AbstractModule a = new AbstractModule() + { + @Override + protected void configure() + { + bind( String.class ).annotatedWith( Names.named( "test.info.inject3" ) ).toInstance( "JUnice = JUnit + Guice Array" ); + } + }; + return new Module[] { a }; + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectDependingMockObjectTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectDependingMockObjectTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectDependingMockObjectTestCase.java new file mode 100644 index 0000000..7701d12 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectDependingMockObjectTestCase.java @@ -0,0 +1,87 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.james.mpt.onami.test.annotation.Mock; +import org.apache.james.mpt.onami.test.data.HelloWorld; +import org.apache.james.mpt.onami.test.data.Service; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.google.inject.AbstractModule; +import javax.inject.Inject; +import com.google.inject.Injector; +import com.google.inject.TypeLiteral; + +@RunWith( OnamiRunner.class ) +public class InjectDependingMockObjectTestCase +{ + + @Mock + static private Service service; + + @Inject + Injector injector; + + private HelloWorld helloWorld; + + @Before + public void setUp() + { + final List<Service> list = new ArrayList<Service>(); + list.add( service ); + + AbstractModule listAbstractModule = new AbstractModule() + { + @Override + protected void configure() + { + bind( new TypeLiteral<List<Service>>() + { + } ).toInstance( list ); + } + }; + + Injector cInjector = injector.createChildInjector( listAbstractModule ); + helloWorld = cInjector.getInstance( HelloWorld.class ); + // required for optional dependencies + cInjector.injectMembers( helloWorld ); + } + + @Test + public void testMock() + { + Assert.assertNotNull( helloWorld ); + Assert.assertNotNull( service ); + EasyMock.expect( service.go() ).andReturn( "Ciao" ); + EasyMock.expectLastCall().once(); + + EasyMock.replay( service ); + helloWorld.sayHalloByServiceLists(); + EasyMock.verify( service ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectFromSuperClassTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectFromSuperClassTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectFromSuperClassTestCase.java new file mode 100644 index 0000000..e3ac6a4 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectFromSuperClassTestCase.java @@ -0,0 +1,57 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + +import org.junit.Assert; +import org.junit.Test; + +import javax.inject.Inject; +import com.google.inject.name.Named; + +public class InjectFromSuperClassTestCase + extends AbstractTestCase +{ + + @Inject + @Named( "test.info.inject" ) + private String info; + + @Inject + @Named( "test.info.inject2" ) + private String infoFromIterable; + + @Inject + @Named( "test.info.inject3" ) + private String infoFromArray; + + @Test + public void testInjectFromSuperClass() + { + Assert.assertNotNull( info ); + Assert.assertEquals( "JUnice = JUnit + Guice", info ); + + Assert.assertNotNull( infoFromIterable ); + Assert.assertEquals( "JUnice = JUnit + Guice Iterable", infoFromIterable ); + + Assert.assertNotNull( infoFromArray ); + Assert.assertEquals( "JUnice = JUnit + Guice Array", infoFromArray ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectJSR330ModuleClassTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectJSR330ModuleClassTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectJSR330ModuleClassTestCase.java new file mode 100644 index 0000000..7ea72c5 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectJSR330ModuleClassTestCase.java @@ -0,0 +1,54 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.google.inject.AbstractModule; +import com.google.inject.name.Names; + +@RunWith( OnamiRunner.class ) +public class InjectJSR330ModuleClassTestCase + extends AbstractModule +{ + + @Override + public void configure() + { + bind( Integer.class ).annotatedWith( Names.named( "numeber.version" ) ).toInstance( 10 ); + } + + @Inject + @Named( "numeber.version" ) + private Integer version; + + @Test + public void testInjectModuleClass() + { + Assert.assertNotNull( version ); + Assert.assertEquals( 10, version.intValue() ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectMockObjectTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectMockObjectTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectMockObjectTestCase.java new file mode 100644 index 0000000..66eefe6 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectMockObjectTestCase.java @@ -0,0 +1,97 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + +import org.apache.james.mpt.onami.test.annotation.GuiceModules; +import org.apache.james.mpt.onami.test.annotation.Mock; +import org.apache.james.mpt.onami.test.data.HelloWorld; +import org.apache.james.mpt.onami.test.data.SimpleModule; +import org.apache.james.mpt.onami.test.data.TelephonService; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import com.google.inject.Injector; + +@RunWith( OnamiRunner.class ) +@GuiceModules( SimpleModule.class ) +public class InjectMockObjectTestCase + extends AbstractMockTestCase +{ + + // Create and inject a simple EasyMock Strict mock + @Mock + private TelephonService telephonServiceMock; + + @Inject + Injector injector; + + @Inject + private HelloWorld helloWorld; + + @Test + public void testMock() + { + EasyMock.expect( providedMock.go() ).andReturn( "Ciao" ); + EasyMock.replay( providedMock ); + + Assert.assertNotNull( this.providedMock ); + Assert.assertEquals( "Ciao", helloWorld.sayHalloByService() ); + EasyMock.verify( providedMock ); + } + + @Test + public void testMock2() + { + EasyMock.expect( providedMock.go() ).andReturn( "Ciao" ); + EasyMock.replay( providedMock ); + + Assert.assertNotNull( this.providedMock ); + Assert.assertEquals( "Ciao", helloWorld.sayHalloByService() ); + EasyMock.verify( providedMock ); + } + + @Test + public void testStrickMock() + { + EasyMock.expect( telephonServiceMock.getTelephonNumber() ).andReturn( "1234567890" ); + providedMock.call( "1234567890" ); + EasyMock.expectLastCall().once(); + EasyMock.replay( telephonServiceMock ); + EasyMock.replay( providedMock ); + + helloWorld.callHelloWorldTelephon(); + + EasyMock.verify( telephonServiceMock ); + EasyMock.verify( providedMock ); + + // reset manually the mock object. Flag resettable is false!!! + EasyMock.reset( telephonServiceMock ); + } + + @Test + public void testStrickMock2() + { + Assert.assertNotNull( telephonServiceMock ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectModuleClassTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectModuleClassTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectModuleClassTestCase.java new file mode 100644 index 0000000..1c6557b --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectModuleClassTestCase.java @@ -0,0 +1,53 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.google.inject.AbstractModule; +import javax.inject.Inject; +import com.google.inject.name.Named; +import com.google.inject.name.Names; + +@RunWith( OnamiRunner.class ) +public class InjectModuleClassTestCase + extends AbstractModule +{ + + @Override + public void configure() + { + bind( Integer.class ).annotatedWith( Names.named( "numeber.version" ) ).toInstance( 10 ); + } + + @Inject + @Named( "numeber.version" ) + private Integer version; + + @Test + public void testInjectModuleClass() + { + Assert.assertNotNull( version ); + Assert.assertEquals( 10, version.intValue() ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectStaticSimpleTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectStaticSimpleTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectStaticSimpleTestCase.java new file mode 100644 index 0000000..5d318a5 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/InjectStaticSimpleTestCase.java @@ -0,0 +1,70 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + + +import org.apache.james.mpt.onami.test.annotation.GuiceModules; +import org.apache.james.mpt.onami.test.annotation.GuiceProvidedModules; +import org.apache.james.mpt.onami.test.data.ComplexModule; +import org.apache.james.mpt.onami.test.data.HelloWorld; +import org.apache.james.mpt.onami.test.data.SimpleModule; +import org.apache.james.mpt.onami.test.data.WhoIm; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import com.google.inject.Module; + +@RunWith( OnamiRunner.class ) +@GuiceModules( SimpleModule.class ) +public class InjectStaticSimpleTestCase +{ + + /* + * Any static filed will be injecteded once before creation of SimpleTest Class + */ + @Inject + public static HelloWorld helloWorld; + + @Inject + public static WhoIm whoIm; + + @GuiceProvidedModules + public static Module createComplexModule() + { + return new ComplexModule( "Marco Speranza" ); + } + + @Test + public void testHelloWorld() + { + Assert.assertNotNull( helloWorld ); + Assert.assertEquals( "Hello World!!!!", helloWorld.sayHallo() ); + } + + @Test + public void testWhoIm() + { + Assert.assertNotNull( whoIm ); + Assert.assertEquals( "Marco Speranza", whoIm.sayWhoIm() ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/MockTypeTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/MockTypeTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/MockTypeTestCase.java new file mode 100644 index 0000000..2d366db --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/MockTypeTestCase.java @@ -0,0 +1,43 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + +import org.apache.james.mpt.onami.test.annotation.Mock; +import org.apache.james.mpt.onami.test.annotation.MockObjType; +import org.apache.james.mpt.onami.test.data.Service; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + + +@RunWith( OnamiRunner.class ) +public class MockTypeTestCase +{ + + @Mock( type = MockObjType.EASY_MOCK_STRICT ) + Service service; + + @Test + public void test() + { + Assert.assertNotNull( service ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/MockitoFrameworkTestCase.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/MockitoFrameworkTestCase.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/MockitoFrameworkTestCase.java new file mode 100644 index 0000000..b12bf10 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/MockitoFrameworkTestCase.java @@ -0,0 +1,60 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + +import org.apache.james.mpt.onami.test.annotation.Mock; +import org.apache.james.mpt.onami.test.data.HelloWorld; +import org.apache.james.mpt.onami.test.data.TelephonService; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; + +@RunWith( OnamiRunner.class ) +public class MockitoFrameworkTestCase + extends AbstractMockitoTestCase +{ + + /* + * Any NON-static filed will be injected before run each tests. + */ + @Inject + private HelloWorld helloWorldNotStatic; + + @Mock + private TelephonService service; + + @BeforeClass + public static void setUpClass() + { + } + + @Test + public void testInjectNotStatic() + { + Assert.assertNotNull( helloWorldNotStatic ); + Assert.assertEquals( "Hello World!!!!", helloWorldNotStatic.sayHallo() ); + Assert.assertNotNull( service ); + Assert.assertNotNull( providedMock ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/OnamiSuiteTest.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/OnamiSuiteTest.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/OnamiSuiteTest.java new file mode 100644 index 0000000..56f5985 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/OnamiSuiteTest.java @@ -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.james.mpt.onami.test; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(OnamiSuite.class) +@SuiteClasses({ InjectDependingMockObjectTestCase.class, InjectFromSuperClassTestCase.class }) +public class OnamiSuiteTest { + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/ServiceMockProviderTest.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/ServiceMockProviderTest.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/ServiceMockProviderTest.java new file mode 100644 index 0000000..8efcbfd --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/ServiceMockProviderTest.java @@ -0,0 +1,43 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + +import org.apache.james.mpt.onami.test.annotation.Mock; +import org.apache.james.mpt.onami.test.data.Service; +import org.apache.james.mpt.onami.test.data.ServiceMockProvider; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + + +@RunWith( OnamiRunner.class ) +public class ServiceMockProviderTest +{ + + @Mock( providedBy = "providerMethod", providerClass = ServiceMockProvider.class ) + private Service service; + + @Test + public void test() + { + Assert.assertNotNull( service ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/SimpleTest.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/SimpleTest.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/SimpleTest.java new file mode 100644 index 0000000..37ad2e2 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/SimpleTest.java @@ -0,0 +1,56 @@ +/**************************************************************** + * 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.james.mpt.onami.test; + + +import org.apache.james.mpt.onami.test.annotation.GuiceModules; +import org.apache.james.mpt.onami.test.data.HelloWorld; +import org.apache.james.mpt.onami.test.data.SimpleModule; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; + +@RunWith( OnamiRunner.class ) +@GuiceModules( SimpleModule.class ) +public class SimpleTest +{ + + /* + * Any NON-static filed will be injecteded before run each tests. + */ + @Inject + private HelloWorld helloWorldNotStatic; + + @BeforeClass + public static void setUpClass() + { + } + + @Test + public void testInjectNotStatic() + { + Assert.assertNotNull( helloWorldNotStatic ); + Assert.assertEquals( "Hello World!!!!", helloWorldNotStatic.sayHallo() ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/ComplexModule.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/ComplexModule.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/ComplexModule.java new file mode 100644 index 0000000..b1f5963 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/ComplexModule.java @@ -0,0 +1,41 @@ +/**************************************************************** + * 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.james.mpt.onami.test.data; + +import com.google.inject.AbstractModule; + +public class ComplexModule + extends AbstractModule +{ + + private final String name; + + public ComplexModule( String name ) + { + this.name = name; + } + + @Override + protected void configure() + { + bind( WhoIm.class ).toInstance( new WhoIm( name ) ); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/HelloWordAnnotated.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/HelloWordAnnotated.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/HelloWordAnnotated.java new file mode 100644 index 0000000..a7d2d2c --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/HelloWordAnnotated.java @@ -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.james.mpt.onami.test.data; + +import javax.inject.Inject; +import com.google.inject.name.Named; + +public class HelloWordAnnotated +{ + + @Inject + @TestAnnotation + Service service; + + @Inject + @Named( "test.named" ) + Service named; + + public String go() + { + return service.go(); + } + + public String getNamed() + { + return named.go(); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/HelloWorld.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/HelloWorld.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/HelloWorld.java new file mode 100644 index 0000000..42bd451 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/HelloWorld.java @@ -0,0 +1,65 @@ +/**************************************************************** + * 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.james.mpt.onami.test.data; + +import java.util.List; + +import com.google.inject.Inject; + +import javax.inject.Singleton; + +@Singleton +public class HelloWorld +{ + + @Inject( optional = true ) + private Service service; + + @Inject( optional = true ) + private TelephonService telephon; + + @Inject( optional = true ) + private List<Service> services; + + public String sayHallo() + { + return "Hello World!!!!"; + } + + public String sayHalloByService() + { + return service.go(); + } + + public void callHelloWorldTelephon() + { + String number = telephon.getTelephonNumber(); + service.call( number ); + } + + public void sayHalloByServiceLists() + { + for ( Service service : services ) + { + service.go(); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/Service.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/Service.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/Service.java new file mode 100644 index 0000000..9dc151e --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/Service.java @@ -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.james.mpt.onami.test.data; + +public interface Service +{ + + String go(); + + void call( String value ); + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a37176fb/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/ServiceImpl.java ---------------------------------------------------------------------- diff --git a/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/ServiceImpl.java b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/ServiceImpl.java new file mode 100644 index 0000000..2bd76d1 --- /dev/null +++ b/mpt/onami-test/src/test/java/org/apache/james/mpt/onami/test/data/ServiceImpl.java @@ -0,0 +1,36 @@ +/**************************************************************** + * 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.james.mpt.onami.test.data; + +public class ServiceImpl + implements Service +{ + + public void call( String value ) + { + // do nothing + } + + public String go() + { + return "It's real class"; + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
