You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2018/12/31 17:02:35 UTC

[1/3] tomee git commit: feat(Examples): added new exames for CDI Features

Repository: tomee
Updated Branches:
  refs/heads/master 3f17abc3d -> 6e8238b70


feat(Examples): added new exames for CDI Features

- added example with CDI Qualifier


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/dd25de02
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/dd25de02
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/dd25de02

Branch: refs/heads/master
Commit: dd25de024f3b430bdd2d7fc1041a254d1f91bf9f
Parents: 14b62ef
Author: Mattheus CAssundé <ma...@gmail.com>
Authored: Mon Dec 31 12:36:09 2018 -0300
Committer: Mattheus CAssundé <ma...@gmail.com>
Committed: Mon Dec 31 12:36:09 2018 -0300

----------------------------------------------------------------------
 examples/cdi-qualifier/README.adoc              | 19 +++++
 examples/cdi-qualifier/pom.xml                  | 90 ++++++++++++++++++++
 .../java/org/superbiz/cdi/qualifier/Cash.java   | 18 ++++
 .../org/superbiz/cdi/qualifier/CreditCard.java  | 18 ++++
 .../org/superbiz/cdi/qualifier/Payment.java     | 11 +++
 .../cdi/qualifier/PaymentQualifier.java         | 28 ++++++
 .../org/superbiz/cdi/qualifier/PaymentType.java | 12 +++
 .../src/main/resources/META-INF/beans.xml       | 19 +++++
 .../org/superbiz/cdi/qualifier/PaymentTest.java | 67 +++++++++++++++
 9 files changed, 282 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/dd25de02/examples/cdi-qualifier/README.adoc
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/README.adoc b/examples/cdi-qualifier/README.adoc
new file mode 100644
index 0000000..ee14b1b
--- /dev/null
+++ b/examples/cdi-qualifier/README.adoc
@@ -0,0 +1,19 @@
+= CDI @Qualifier
+:index-group: CDI
+:jbake-type: page
+:jbake-status: published
+
+== Introduction
+
+Sometimes we need write several implementation for a interface of rule business, to increase uncoupling let's inject only interface and CDI choose the right implementation, to help CDI with this choice was created the **Qualifiers**.
+
+
+== Example
+
+In this example, we have an interface `Payment` and theirs implementations: 
+* Cash
+* CreditCard 
+
+In our test (Payment Test) we inject only the interface Payment, without the Qualifier feature the CDI would not known which implementation to inject in test.
+
+We created a **Qualifier** called `PaymentQualifier` a single qualifier with only one difference, the annotation `@Qualifier`:
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tomee/blob/dd25de02/examples/cdi-qualifier/pom.xml
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/pom.xml b/examples/cdi-qualifier/pom.xml
new file mode 100644
index 0000000..6df7b90
--- /dev/null
+++ b/examples/cdi-qualifier/pom.xml
@@ -0,0 +1,90 @@
+<?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>cdi-qualifier</artifactId>
+  <packaging>jar</packaging>
+  <version>8.0.0-SNAPSHOT</version>
+  <name>OpenEJB :: Examples :: Basic Qualifier</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>3.5.1</version>
+        <configuration>
+          <source>1.8</source>
+          <target>1.8</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <repositories>
+    <repository>
+      <id>apache-m2-snapshot</id>
+      <name>Apache Snapshot Repository</name>
+      <url>https://repository.apache.org/content/groups/snapshots</url>
+    </repository>
+  </repositories>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.tomee</groupId>
+            <artifactId>javaee-api</artifactId>
+       <version>8.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.12</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- The <scope>test</scope> guarantees that none of your runtime code 
+      is dependent on any OpenEJB classes. -->
+    <dependency>
+      <groupId>org.apache.tomee</groupId>
+      <artifactId>openejb-core</artifactId>
+      <version>8.0.0-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>
+

http://git-wip-us.apache.org/repos/asf/tomee/blob/dd25de02/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/Cash.java
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/Cash.java b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/Cash.java
new file mode 100644
index 0000000..2fbdcb8
--- /dev/null
+++ b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/Cash.java
@@ -0,0 +1,18 @@
+package org.superbiz.cdi.qualifier;
+
+/**
+ * 
+ * Implementation for {@link Payment} interfaces
+ * <br>
+ * This implementation have a qualifier annotation to help CDI choose the implementation correct.
+ * 
+ * */
+@PaymentQualifier(type=PaymentType.CASH)
+public class Cash implements Payment {
+
+	@Override
+	public String pay() {
+		
+		return "cash";
+	}
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/dd25de02/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/CreditCard.java
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/CreditCard.java b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/CreditCard.java
new file mode 100644
index 0000000..5d8c337
--- /dev/null
+++ b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/CreditCard.java
@@ -0,0 +1,18 @@
+package org.superbiz.cdi.qualifier;
+
+/**
+ * 
+ * Implementation for {@link Payment} interfaces
+ * <br>
+ * This implementation have a qualifier annotation to help CDI choose the implementation correct.
+ * 
+ * */
+@PaymentQualifier(type=PaymentType.CREDITCARD)
+public class CreditCard implements Payment {
+
+	@Override
+	public String pay() {
+		
+		return "creditCard";
+	}
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/dd25de02/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/Payment.java
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/Payment.java b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/Payment.java
new file mode 100644
index 0000000..063036c
--- /dev/null
+++ b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/Payment.java
@@ -0,0 +1,11 @@
+package org.superbiz.cdi.qualifier;
+
+public interface Payment {
+
+	/**
+	 * 
+	 * Method return string with Payment Method description
+	 * 
+	 * */
+	public String pay();
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/dd25de02/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/PaymentQualifier.java
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/PaymentQualifier.java b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/PaymentQualifier.java
new file mode 100644
index 0000000..3a5369f
--- /dev/null
+++ b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/PaymentQualifier.java
@@ -0,0 +1,28 @@
+package org.superbiz.cdi.qualifier;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * 
+ * Qualifier that help CDI the choose the right implementation in the inject
+ * <br>
+ * This qualifier have a {@link PaymentType}, this <b>enum</b> contain all options to separate implementations.
+ * 
+ * */
+@Documented
+@Retention(RUNTIME)
+@Target({ TYPE, FIELD, METHOD })
+@Qualifier
+public @interface PaymentQualifier {
+
+	PaymentType type();	
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/dd25de02/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/PaymentType.java
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/PaymentType.java b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/PaymentType.java
new file mode 100644
index 0000000..f4f0a9d
--- /dev/null
+++ b/examples/cdi-qualifier/src/main/java/org/superbiz/cdi/qualifier/PaymentType.java
@@ -0,0 +1,12 @@
+package org.superbiz.cdi.qualifier;
+
+/**
+ * 
+ * Enum with all options to separate implementation in {@linkplain PaymentQualifier}
+ *  
+ * */
+public enum PaymentType {
+
+	CASH,
+	CREDITCARD
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/dd25de02/examples/cdi-qualifier/src/main/resources/META-INF/beans.xml
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/src/main/resources/META-INF/beans.xml b/examples/cdi-qualifier/src/main/resources/META-INF/beans.xml
new file mode 100644
index 0000000..6668db4
--- /dev/null
+++ b/examples/cdi-qualifier/src/main/resources/META-INF/beans.xml
@@ -0,0 +1,19 @@
+<!--
+
+    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.
+-->
+<beans>
+</beans>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tomee/blob/dd25de02/examples/cdi-qualifier/src/test/java/org/superbiz/cdi/qualifier/PaymentTest.java
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/src/test/java/org/superbiz/cdi/qualifier/PaymentTest.java b/examples/cdi-qualifier/src/test/java/org/superbiz/cdi/qualifier/PaymentTest.java
new file mode 100644
index 0000000..4ee7f02
--- /dev/null
+++ b/examples/cdi-qualifier/src/test/java/org/superbiz/cdi/qualifier/PaymentTest.java
@@ -0,0 +1,67 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.cdi.qualifier;
+
+import static org.junit.Assert.assertEquals;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.inject.Inject;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class PaymentTest {
+
+    private static EJBContainer container;
+
+    @Inject
+    @PaymentQualifier(type=PaymentType.CREDITCARD)
+    private Payment paymentCreditCard;
+    
+    @Inject
+    @PaymentQualifier(type=PaymentType.CASH)
+    private Payment paymentCash;
+
+    @BeforeClass
+    public static void start() {
+        container = EJBContainer.createEJBContainer();
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        container.getContext().bind("inject", this);
+    }
+
+    @Test
+    public void mustReturnCreditCard() {
+    	
+        assertEquals(paymentCreditCard.pay(), "creditCard");
+    }
+    
+    @Test
+    public void mustReturnCash() {
+    	
+        assertEquals(paymentCash.pay(), "cash");
+    }
+
+    @AfterClass
+    public static void stop() {
+        container.close();
+    }
+}


[2/3] tomee git commit: feat(Examples): added explanation about qualifier

Posted by jg...@apache.org.
feat(Examples): added explanation about qualifier

Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/a7c27083
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/a7c27083
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/a7c27083

Branch: refs/heads/master
Commit: a7c270833cdacb6addb964e60687a96c76f3b978
Parents: dd25de0
Author: Mattheus Cassundé <ma...@gmail.com>
Authored: Mon Dec 31 12:52:30 2018 -0300
Committer: GitHub <no...@github.com>
Committed: Mon Dec 31 12:52:30 2018 -0300

----------------------------------------------------------------------
 examples/cdi-qualifier/README.adoc | 83 ++++++++++++++++++++++++++++++++-
 1 file changed, 82 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/a7c27083/examples/cdi-qualifier/README.adoc
----------------------------------------------------------------------
diff --git a/examples/cdi-qualifier/README.adoc b/examples/cdi-qualifier/README.adoc
index ee14b1b..fe9b8a5 100644
--- a/examples/cdi-qualifier/README.adoc
+++ b/examples/cdi-qualifier/README.adoc
@@ -16,4 +16,85 @@ In this example, we have an interface `Payment` and theirs implementations:
 
 In our test (Payment Test) we inject only the interface Payment, without the Qualifier feature the CDI would not known which implementation to inject in test.
 
-We created a **Qualifier** called `PaymentQualifier` a single qualifier with only one difference, the annotation `@Qualifier`:
\ No newline at end of file
+We created a **Qualifier** called `PaymentQualifier` a single qualifier with only one difference, the annotation `@Qualifier`.
+
+....
+@Retention(RUNTIME)
+@Target({ TYPE, FIELD, METHOD })
+@Qualifier
+public @interface PaymentQualifier {
+
+	PaymentType type();	
+}
+....
+
+This qualifier has a method named `type()`, this method will help the CDI to inject correctly implementation. see this enum:
+
+....
+public enum PaymentType {
+
+	CASH,
+	CREDITCARD
+}
+....
+
+now see an implementation
+
+....
+@PaymentQualifier(type=PaymentType.CASH)
+public class Cash implements Payment {
+
+	@Override
+	public String pay() {
+		
+		return "cash";
+	}
+}
+....
+
+Each implementation should marked with this qualifier.
+
+How to inject? see simplicity
+
+....
+public class PaymentTest {
+
+    private static EJBContainer container;
+
+    @Inject
+    @PaymentQualifier(type=PaymentType.CREDITCARD) //qualifier informing the CDI about the correctly implementation
+    private Payment paymentCreditCard;
+    
+    @Inject
+    @PaymentQualifier(type=PaymentType.CASH) //qualifier informing the CDI about the correctly implementation
+    private Payment paymentCash;
+
+    @BeforeClass
+    public static void start() {
+        container = EJBContainer.createEJBContainer();
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        container.getContext().bind("inject", this);
+    }
+
+    @Test
+    public void mustReturnCreditCard() {
+    	
+        assertEquals(paymentCreditCard.pay(), "creditCard");
+    }
+    
+    @Test
+    public void mustReturnCash() {
+    	
+        assertEquals(paymentCash.pay(), "cash");
+    }
+
+    @AfterClass
+    public static void stop() {
+        container.close();
+    }
+}
+
+....


[3/3] tomee git commit: Merge branch 'master' of github.com:cassunde/tomee

Posted by jg...@apache.org.
Merge branch 'master' of github.com:cassunde/tomee


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/6e8238b7
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/6e8238b7
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/6e8238b7

Branch: refs/heads/master
Commit: 6e8238b70857a0ee550cc06adcde5ed491dfdbeb
Parents: 3f17abc a7c2708
Author: Jonathan Gallimore <jo...@jrg.me.uk>
Authored: Mon Dec 31 16:48:12 2018 +0000
Committer: Jonathan Gallimore <jo...@jrg.me.uk>
Committed: Mon Dec 31 16:48:12 2018 +0000

----------------------------------------------------------------------
 examples/cdi-qualifier/README.adoc              | 100 +++++++++++++++++++
 examples/cdi-qualifier/pom.xml                  |  90 +++++++++++++++++
 .../java/org/superbiz/cdi/qualifier/Cash.java   |  18 ++++
 .../org/superbiz/cdi/qualifier/CreditCard.java  |  18 ++++
 .../org/superbiz/cdi/qualifier/Payment.java     |  11 ++
 .../cdi/qualifier/PaymentQualifier.java         |  28 ++++++
 .../org/superbiz/cdi/qualifier/PaymentType.java |  12 +++
 .../src/main/resources/META-INF/beans.xml       |  19 ++++
 .../org/superbiz/cdi/qualifier/PaymentTest.java |  67 +++++++++++++
 9 files changed, 363 insertions(+)
----------------------------------------------------------------------