Author: rmannibucau Date: Tue Oct 30 15:59:23 2012 New Revision: 1403755 URL: http://svn.apache.org/viewvc?rev=1403755&view=rev Log: using openejb-mockito for app compser/mockito/rest example + adding a sample on how to test in embedded mode with providers
Added: openejb/trunk/openejb/examples/rest-applicationcomposer/ openejb/trunk/openejb/examples/rest-applicationcomposer/pom.xml openejb/trunk/openejb/examples/rest-applicationcomposer/src/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/GreetingService.java openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/IllegalArgumentExceptionMapper.java openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/org/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/org/superbiz/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/org/superbiz/composed/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/org/superbiz/composed/rest/ openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java Modified: openejb/trunk/openejb/examples/pom.xml openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/pom.xml openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java Modified: openejb/trunk/openejb/examples/pom.xml URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/pom.xml?rev=1403755&r1=1403754&r2=1403755&view=diff ============================================================================== --- openejb/trunk/openejb/examples/pom.xml (original) +++ openejb/trunk/openejb/examples/pom.xml Tue Oct 30 15:59:23 2012 @@ -99,6 +99,7 @@ <module>reload-persistence-unit-properties</module> <module>resources-declared-in-webapp</module> <module>rest-applicationcomposer-mockito</module> + <module>rest-applicationcomposer</module> <module>rest-on-ejb</module> <module>rest-example</module> <module>rest-example-with-application</module> Modified: openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/pom.xml URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/pom.xml?rev=1403755&r1=1403754&r2=1403755&view=diff ============================================================================== --- openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/pom.xml (original) +++ openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/pom.xml Tue Oct 30 15:59:23 2012 @@ -67,12 +67,6 @@ <version>4.10</version> <scope>test</scope> </dependency> - <dependency> - <groupId>org.mockito</groupId> - <artifactId>mockito-core</artifactId> - <version>1.9.0</version> - <scope>test</scope> - </dependency> <!-- The <scope>test</scope> guarantees that non of your runtime @@ -90,6 +84,12 @@ <version>4.5.1-SNAPSHOT</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.openejb</groupId> + <artifactId>openejb-mockito</artifactId> + <version>4.5.1-SNAPSHOT</version> + <scope>test</scope> + </dependency> </dependencies> <!-- Modified: openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java?rev=1403755&r1=1403754&r2=1403755&view=diff ============================================================================== --- openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java (original) +++ openejb/trunk/openejb/examples/rest-applicationcomposer-mockito/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java Tue Oct 30 15:59:23 2012 @@ -19,15 +19,14 @@ package org.superbiz.composed.rest; import org.apache.openejb.OpenEjbContainer; import org.apache.openejb.junit.ApplicationComposer; import org.apache.openejb.junit.Configuration; +import org.apache.openejb.junit.MockInjector; import org.apache.openejb.junit.Module; import org.apache.openejb.loader.IO; -import org.junit.Before; +import org.apache.openejb.mockito.MockitoInjector; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import javax.enterprise.inject.Produces; import java.io.IOException; import java.net.URL; import java.util.Properties; @@ -38,13 +37,7 @@ import static org.mockito.Mockito.when; @RunWith(ApplicationComposer.class) public class GreetingServiceTest { @Mock - @Produces - public static Messager messager; - - @Before - public void injectMocks() { - MockitoAnnotations.initMocks(this); - } + private Messager messager; @Configuration public Properties configuration() { @@ -53,6 +46,11 @@ public class GreetingServiceTest { }}; } + @MockInjector + public Class<?> mockitoInjector() { + return MockitoInjector.class; + } + @Module public Class<?>[] app() { return new Class<?>[] { GreetingService.class, Messager.class }; Added: openejb/trunk/openejb/examples/rest-applicationcomposer/pom.xml URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/rest-applicationcomposer/pom.xml?rev=1403755&view=auto ============================================================================== --- openejb/trunk/openejb/examples/rest-applicationcomposer/pom.xml (added) +++ openejb/trunk/openejb/examples/rest-applicationcomposer/pom.xml Tue Oct 30 15:59:23 2012 @@ -0,0 +1,104 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.superbiz</groupId> + <artifactId>rest-applicationcomposer</artifactId> + <packaging>jar</packaging> + <version>1.1-SNAPSHOT</version> + <name>OpenEJB :: Examples :: REST and Application Composer</name> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <defaultGoal>install</defaultGoal> + + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>2.4</version> + <configuration> + <source>1.6</source> + <target>1.6</target> + </configuration> + </plugin> + </plugins> + </build> + + <repositories> + <repository> + <id>apache-m2-snapshot</id> + <name>Apache Snapshot Repository</name> + <url>http://repository.apache.org/snapshots</url> + </repository> + </repositories> + + <dependencies> + <dependency> + <groupId>org.apache.openejb</groupId> + <artifactId>javaee-api</artifactId> + <version>6.0-4</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.10</version> + <scope>test</scope> + </dependency> + + <!-- + The <scope>test</scope> guarantees that non of your runtime + code is dependent on any OpenEJB classes. + --> + <dependency> + <groupId>org.apache.openejb</groupId> + <artifactId>openejb-core</artifactId> + <version>4.5.1-SNAPSHOT</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.openejb</groupId> + <artifactId>openejb-cxf-rs</artifactId> + <version>4.5.1-SNAPSHOT</version> + <scope>test</scope> + </dependency> + </dependencies> + + <!-- + This section allows you to configure where to publish libraries for sharing. + It is not required and may be deleted. For more information see: + http://maven.apache.org/plugins/maven-deploy-plugin/ + --> + <distributionManagement> + <repository> + <id>localhost</id> + <url>file://${basedir}/target/repo/</url> + </repository> + <snapshotRepository> + <id>localhost</id> + <url>file://${basedir}/target/snapshot-repo/</url> + </snapshotRepository> + </distributionManagement> +</project> Added: openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/GreetingService.java URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/GreetingService.java?rev=1403755&view=auto ============================================================================== --- openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/GreetingService.java (added) +++ openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/GreetingService.java Tue Oct 30 15:59:23 2012 @@ -0,0 +1,28 @@ +/** + * 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.superbiz.composed.rest; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +@Path("/greeting") +public class GreetingService { + @GET + public String message() { + throw new IllegalArgumentException("this exception is handled by an exception mapper"); + } +} Added: openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/IllegalArgumentExceptionMapper.java URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/IllegalArgumentExceptionMapper.java?rev=1403755&view=auto ============================================================================== --- openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/IllegalArgumentExceptionMapper.java (added) +++ openejb/trunk/openejb/examples/rest-applicationcomposer/src/main/java/org/superbiz/composed/rest/IllegalArgumentExceptionMapper.java Tue Oct 30 15:59:23 2012 @@ -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.superbiz.composed.rest; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +@Provider +public class IllegalArgumentExceptionMapper implements ExceptionMapper<IllegalArgumentException> { + @Override + public Response toResponse(final IllegalArgumentException throwable) { + return Response.ok(throwable.getMessage()).build(); + } +} Added: openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java?rev=1403755&view=auto ============================================================================== --- openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java (added) +++ openejb/trunk/openejb/examples/rest-applicationcomposer/src/test/java/org/superbiz/composed/rest/GreetingServiceTest.java Tue Oct 30 15:59:23 2012 @@ -0,0 +1,77 @@ +/** + * 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.superbiz.composed.rest; + +import org.apache.openejb.OpenEjbContainer; +import org.apache.openejb.config.EjbModule; +import org.apache.openejb.jee.EjbJar; +import org.apache.openejb.jee.SingletonBean; +import org.apache.openejb.jee.oejb3.EjbDeployment; +import org.apache.openejb.jee.oejb3.OpenejbJar; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.junit.Configuration; +import org.apache.openejb.junit.Module; +import org.apache.openejb.loader.IO; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.IOException; +import java.net.URL; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +@RunWith(ApplicationComposer.class) +public class GreetingServiceTest { + @Configuration + public Properties configuration() { + return new Properties() {{ + setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, Boolean.TRUE.toString()); + }}; + } + + @Module + public EjbModule app() { + // in embedded mode only ejb are deployed + final SingletonBean bean = (SingletonBean) new SingletonBean(GreetingService.class).localBean(); + bean.setRestService(true); + + // now create an ejbjar and an openejb-jar to hold the provider config + + final EjbJar ejbJar = new EjbJar(); + ejbJar.addEnterpriseBean(bean); + + final OpenejbJar openejbJar = new OpenejbJar(); + openejbJar.addEjbDeployment(new EjbDeployment(ejbJar.getEnterpriseBeans()[0])); + + final Properties properties = openejbJar.getEjbDeployment().iterator().next().getProperties(); + properties.setProperty("cxf.jaxrs.providers", IllegalArgumentExceptionMapper.class.getName()); + + // link all and return this module + + final EjbModule module = new EjbModule(ejbJar); + module.setOpenejbJar(openejbJar); + + return module; + } + + @Test + public void checkProviderIsUsed() throws IOException { + final String message = IO.slurp(new URL("http://localhost:4204/GreetingServiceTest/greeting/")); + assertEquals("this exception is handled by an exception mapper", message); + } +}