tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgallim...@apache.org
Subject [2/3] tomee git commit: feat(Examples): added explanation about qualifier
Date Mon, 31 Dec 2018 17:02:36 GMT
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é <mattheus.089@gmail.com>
Authored: Mon Dec 31 12:52:30 2018 -0300
Committer: GitHub <noreply@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();
+    }
+}
+
+....


Mime
View raw message