Author: rmannibucau
Date: Sun Oct 30 20:07:00 2011
New Revision: 1195236
URL: http://svn.apache.org/viewvc?rev=1195236&view=rev
Log:
adding cdi-alternative-and-stereotypes example
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/README.md
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/pom.xml
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/resources/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/resources/META-INF/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/resources/META-INF/beans.xml
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/resources/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/resources/META-INF/
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/resources/META-INF/beans.xml
Modified:
openejb/trunk/openejb/examples/pom.xml
Added: openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/README.md
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/README.md?rev=1195236&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/README.md
(added)
+++ openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/README.md
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,125 @@
+# Introduction
+
+CDI is a revolution for JEE world. This specification is the best one to avoid
coupling between classes.
+
+This example simply aims to override bindings at runtime to simplify mocking
work.
+
+It uses two kind of mocks:
+1) a mock with no implementation in the classloader
+2) a mock with an implementation in the classloader
+
+The mock answer from CDI is called *alternative*.
+
+Annotating `@Alternative` a class will mean it will replace any implementation
if there is no other implementation
+or if it is forced (through `META-INF/beans.xml`).
+
+# Code explanation
+## main code
+
+We use an EJB `Jouney` to modelize a journey where the vehicle and the society
can change. Here an EJB is used simply
+because it simplifies the test. A jouney wraps the vehicle and society
information.
+
+We define then two interfaces to inject it in the `Journey` EJB: `Vehicle` and
`Society`.
+
+Finally we add an implementation for `Scociety` interface: `LowCostCompanie`.
+
+If we don't go further `Journey` object will not be able to be created because
no `Vehicle` implementation is available.
+
+Note: if we suppose we have a `Vehicle` implementation, the injected Society
should be `LowCostCompanie`.
+
+## test code
+
+The goal here is to test our `Journey` EJB. So we have to provide a `Vehicle`
implementation: `SuperCar`.
+
+We want to force the `Society` implementation (for any reason) by our test
implementation: `AirOpenEJB`.
+
+One solution could simply be to add `@Alternative` annotation on `AirOpenEJB`
and activate it through
+the `META-INF/beans.xml` file.
+
+Here we want to write more explicit code. So we want to replace the
`@Alternative` annotation by `@Mock` one.
+
+So we simply define an `@Mock` annotation for classes, resolvable at runtime
which is a stereotype (`@Stereotype`)
+which replace `@Alternative`.
+
+Here is the annotation:
+
+ @Stereotype // we define a stereotype
+ @Retention(RUNTIME) // resolvable at runtime
+ @Target(TYPE) // this annotation is a class level one
+ @Alternative // it replace @Alternative
+ public @interface Mock {}
+
+Note: you can add more CDI annotations after `@Alternative` and it will get
the behavior expected (the scope for instance).
+
+So now we have our `@Mock` annotation which is a stereotype able to replace
`@Alternative` annotation
+we simply add this annotation to our mocks.
+
+If you run it now you'll have this exception:
+ javax.enterprise.inject.UnsatisfiedResolutionException: Api type
[org.superbiz.cdi.stereotype.Vehicle] is not found with the qualifiers
+ Qualifiers: [@javax.enterprise.inject.Default()]
+ for injection into Field Injection Point, field name : vehicle, Bean
Owner : [Journey, Name:null, WebBeans Type:ENTERPRISE, API
Types:[java.lang.Object,org.superbiz.cdi.stereotype.Journey],
Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]]
+
+It means the stereotype is not activated. To do it simply add it to your
`META-INF/beans.xml`:
+
+ <alternatives>
+ <stereotype>org.superbiz.cdi.stereotype.Mock</stereotype>
+ </alternatives>
+
+Note: if you don't specify `AirOpenEJB` as `@Alternative` (done through our
mock annotation) you'll get this exception:
+
+ Caused by: javax.enterprise.inject.AmbiguousResolutionException: There is
more than one api type with : org.superbiz.cdi.stereotype.Society with
qualifiers : Qualifiers: [@javax.enterprise.inject.Default()]
+ for injection into Field Injection Point, field name : society, Bean
Owner : [Journey, Name:null, WebBeans Type:ENTERPRISE, API
Types:[org.superbiz.cdi.stereotype.Journey,java.lang.Object],
Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]]
+ found beans:
+ AirOpenEJB, Name:null, WebBeans Type:MANAGED, API
Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.AirOpenEJB,java.lang.Object],
Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]
+ LowCostCompanie, Name:null, WebBeans Type:MANAGED, API
Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.LowCostCompanie,java.lang.Object],
Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]
+
+which simply means two implementations are available for the same injection
point (`Journey.society`).
+
+# Conclusion
+
+With CDI it is really easy to define annotations with a strong meaning. You
can define business annotations
+or simply technical annotations to simplify your code (as we did with the mock
annotation).
+
+Note: if for instance you used qualifiers to inject societies you could have
put all these qualifiers on
+the mock class or defined a `@SocietyMock` annotation to be able to inject the
same implementation for
+all qualifiers in your tests.
+
+# Output
+
+ Running org.superbiz.cdi.stereotype.StereotypeTest
+ Apache OpenEJB 4.0.0-beta-2-SNAPSHOT build: 20111030-07:54
+ http://openejb.apache.org/
+ INFO - openejb.home =
/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes
+ INFO - openejb.base =
/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes
+ INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
+ INFO - Configuring Service(id=Default Security Service,
type=SecurityService, provider-id=Default Security Service)
+ INFO - Configuring Service(id=Default Transaction Manager,
type=TransactionManager, provider-id=Default Transaction Manager)
+ INFO - Found EjbModule in classpath:
/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/test-classes
+ INFO - Found EjbModule in classpath:
/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/classes
+ INFO - Beginning load:
/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/test-classes
+ INFO - Beginning load:
/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/classes
+ INFO - Configuring enterprise application:
/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes
+ INFO - Configuring Service(id=Default Managed Container, type=Container,
provider-id=Default Managed Container)
+ INFO - Auto-creating a container for bean
cdi-alternative-and-stereotypes_test.Comp: Container(type=MANAGED, id=Default
Managed Container)
+ INFO - Configuring Service(id=Default Singleton Container, type=Container,
provider-id=Default Singleton Container)
+ INFO - Auto-creating a container for bean Journey:
Container(type=SINGLETON, id=Default Singleton Container)
+ INFO - Enterprise application
"/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes"
loaded.
+ INFO - Assembling app:
/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes
+ INFO -
Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes_test.Comp!org.apache.openejb.BeanContext$Comp")
+ INFO -
Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes_test.Comp")
+ INFO -
Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes.Comp!org.apache.openejb.BeanContext$Comp")
+ INFO -
Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes.Comp")
+ INFO -
Jndi(name="java:global/cdi-alternative-and-stereotypes/Journey!org.superbiz.cdi.stereotype.Journey")
+ INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/Journey")
+ INFO -
Jndi(name="java:global/EjbModule162291475/org.superbiz.cdi.stereotype.StereotypeTest!org.superbiz.cdi.stereotype.StereotypeTest")
+ INFO -
Jndi(name="java:global/EjbModule162291475/org.superbiz.cdi.stereotype.StereotypeTest")
+ INFO - Created
Ejb(deployment-id=cdi-alternative-and-stereotypes_test.Comp,
ejb-name=cdi-alternative-and-stereotypes_test.Comp, container=Default Managed
Container)
+ INFO - Created Ejb(deployment-id=cdi-alternative-and-stereotypes.Comp,
ejb-name=cdi-alternative-and-stereotypes.Comp, container=Default Managed
Container)
+ INFO - Created
Ejb(deployment-id=org.superbiz.cdi.stereotype.StereotypeTest,
ejb-name=org.superbiz.cdi.stereotype.StereotypeTest, container=Default Managed
Container)
+ INFO - Created Ejb(deployment-id=Journey, ejb-name=Journey,
container=Default Singleton Container)
+ INFO - Started
Ejb(deployment-id=cdi-alternative-and-stereotypes_test.Comp,
ejb-name=cdi-alternative-and-stereotypes_test.Comp, container=Default Managed
Container)
+ INFO - Started Ejb(deployment-id=cdi-alternative-and-stereotypes.Comp,
ejb-name=cdi-alternative-and-stereotypes.Comp, container=Default Managed
Container)
+ INFO - Started
Ejb(deployment-id=org.superbiz.cdi.stereotype.StereotypeTest,
ejb-name=org.superbiz.cdi.stereotype.StereotypeTest, container=Default Managed
Container)
+ INFO - Started Ejb(deployment-id=Journey, ejb-name=Journey,
container=Default Singleton Container)
+ INFO - Deployed
Application(path=/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes)
+ INFO - Undeploying app:
/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes
Added: openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/pom.xml?rev=1195236&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/pom.xml
(added)
+++ openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/pom.xml Sun
Oct 30 20:07:00 2011
@@ -0,0 +1,92 @@
+<?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.
+-->
+
+<!-- $Rev: 1178411 $ $Date: 2011-10-03 15:35:26 +0200 (lun. 03 oct. 2011) $ -->
+
+<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>cdi-alternative-and-stereotypes</artifactId>
+ <packaging>jar</packaging>
+ <version>1.1-SNAPSHOT</version>
+ <name>OpenEJB :: Examples :: CDI Stereotypes</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.3.2</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-3-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</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.0.0-beta-2-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>
\ No newline at end of file
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,21 @@
+package org.superbiz.cdi.stereotype;
+
+import javax.ejb.Singleton;
+import javax.inject.Inject;
+
+/**
+ * @author rmannibucau
+ */
+@Singleton
+public class Journey {
+ @Inject private Vehicle vehicle;
+ @Inject private Society society;
+
+ public String vehicle() {
+ return vehicle.name();
+ }
+
+ public String category() {
+ return society.category();
+ }
+}
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,11 @@
+package org.superbiz.cdi.stereotype;
+
+/**
+ * @author rmannibucau
+ */
+public class LowCostCompanie implements Society {
+ @Override
+ public String category() {
+ return "maybe you'll leave...";
+ }
+}
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,8 @@
+package org.superbiz.cdi.stereotype;
+
+/**
+ * @author rmannibucau
+ */
+public interface Society {
+ String category();
+}
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,8 @@
+package org.superbiz.cdi.stereotype;
+
+/**
+ * @author rmannibucau
+ */
+public interface Vehicle {
+ String name();
+}
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/resources/META-INF/beans.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/resources/META-INF/beans.xml?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/resources/META-INF/beans.xml
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/main/resources/META-INF/beans.xml
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+ http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" />
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,23 @@
+package org.superbiz.cdi.stereotype;
+
+/**
+ * @author rmannibucau
+ *
+ * without @Mock annotation which specifies this class as an alternative
+ * you'll have this exception:
+ *
+ * Caused by: javax.enterprise.inject.AmbiguousResolutionException: There is
more than one api type with : org.superbiz.cdi.stereotype.Society with
qualifiers : Qualifiers: [@javax.enterprise.inject.Default()]
+ * for injection into Field Injection Point, field name : society, Bean Owner
: [Journey, Name:null, WebBeans Type:ENTERPRISE, API
Types:[org.superbiz.cdi.stereotype.Journey,java.lang.Object],
Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]]
+ * found beans:
+ * AirOpenEJB, Name:null, WebBeans Type:MANAGED, API
Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.AirOpenEJB,java.lang.Object],
Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]
+ * LowCostCompanie, Name:null, WebBeans Type:MANAGED, API
Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.LowCostCompanie,java.lang.Object],
Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]
+ *
+ * because 2 implementations match the same injection point (Journey.society).
+ */
+@Mock
+public class AirOpenEJB implements Society {
+ @Override
+ public String category() {
+ return "simply the best";
+ }
+}
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,23 @@
+package org.superbiz.cdi.stereotype;
+
+import javax.enterprise.inject.Alternative;
+import javax.enterprise.inject.Stereotype;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * @author rmannibucau
+ */
+// defining a stereotype for class level
+@Stereotype
+@Retention(RUNTIME)
+@Target(TYPE)
+
+// here define all annotations you want to replace by this one.
+// this stereotype define an alternative
+@Alternative
+public @interface Mock {
+}
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,41 @@
+package org.superbiz.cdi.stereotype;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.naming.NamingException;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author rmannibucau
+ */
+public class StereotypeTest {
+ private static EJBContainer container;
+ private static Journey journey;
+
+ @BeforeClass
+ public static void start() throws NamingException {
+ container = EJBContainer.createEJBContainer();
+ journey = (Journey)
container.getContext().lookup("java:global/cdi-alternative-and-stereotypes/Journey");
+ }
+
+ @AfterClass
+ public static void shutdown() {
+ if (container != null) {
+ container.close();
+ }
+ }
+
+ @Test
+ public void assertVehicleInjected() {
+ assertEquals("the fatest", journey.vehicle());
+ }
+
+ @Test
+ public void assertMockOverrideWorks() {
+ assertEquals("simply the best", journey.category());
+ }
+}
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,12 @@
+package org.superbiz.cdi.stereotype;
+
+/**
+ * @author rmannibucau
+ */
+@Mock
+public class SuperCar implements Vehicle {
+ @Override
+ public String name() {
+ return "the fatest";
+ }
+}
Added:
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/resources/META-INF/beans.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/resources/META-INF/beans.xml?rev=1195236&view=auto
==============================================================================
---
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/resources/META-INF/beans.xml
(added)
+++
openejb/trunk/openejb/examples/cdi-alternative-and-stereotypes/src/test/resources/META-INF/beans.xml
Sun Oct 30 20:07:00 2011
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+ http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
+ <alternatives>
+ <stereotype>org.superbiz.cdi.stereotype.Mock</stereotype>
+ </alternatives>
+</beans>
Modified: openejb/trunk/openejb/examples/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/pom.xml?rev=1195236&r1=1195235&r2=1195236&view=diff
==============================================================================
--- openejb/trunk/openejb/examples/pom.xml (original)
+++ openejb/trunk/openejb/examples/pom.xml Sun Oct 30 20:07:00 2011
@@ -89,6 +89,7 @@
<module>dynamic-dao-implementation</module>
<module>cdi-produces-disposes</module>
<module>dynamic-implementation</module>
+ <module>cdi-alternative-and-stereotypes</module>
</modules>
<profiles>
<profile>