From 06dbcfb7804005b7261fdad0c9067d97271fd5dc Mon Sep 17 00:00:00 2001 From: Radim Hanus Date: Sat, 27 Jun 2015 22:32:11 +0200 Subject: [PATCH 1/3] fixed project's naming conventions --- cdi/README.md | 3 +++ cdi/events-conditional-reception/pom.xml | 2 +- cdi/instance-qualifiers/pom.xml | 2 +- cdi/instance/pom.xml | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cdi/README.md b/cdi/README.md index 24f431b75..b80caf6c2 100644 --- a/cdi/README.md +++ b/cdi/README.md @@ -24,6 +24,9 @@ The [JSR 346](https://jcp.org/en/jsr/detail?id=346) updates and clarifications t - nobeans-el-injection - nobeans-el-injection-flowscoped - events + - events-conditional-reception + - instance + - instance-qualifiers ## How to run diff --git a/cdi/events-conditional-reception/pom.xml b/cdi/events-conditional-reception/pom.xml index b5e94b7cd..7205781b3 100644 --- a/cdi/events-conditional-reception/pom.xml +++ b/cdi/events-conditional-reception/pom.xml @@ -9,5 +9,5 @@ ../pom.xml cdi-events-conditional-reception - Java EE 7 Sample: conditional reception of cdi events + Java EE 7 Sample: cdi - events-conditional-reception diff --git a/cdi/instance-qualifiers/pom.xml b/cdi/instance-qualifiers/pom.xml index 46d71cf4b..d47968f8b 100644 --- a/cdi/instance-qualifiers/pom.xml +++ b/cdi/instance-qualifiers/pom.xml @@ -12,6 +12,6 @@ cdi-instance-qualifiers - Java EE 7 Sample: cdi/instance-qualifiers + Java EE 7 Sample: cdi - instance-qualifiers \ No newline at end of file diff --git a/cdi/instance/pom.xml b/cdi/instance/pom.xml index 309e9ff8b..d745d9961 100644 --- a/cdi/instance/pom.xml +++ b/cdi/instance/pom.xml @@ -11,6 +11,6 @@ cdi-instance - Java EE 7 Sample: cdi/instance + Java EE 7 Sample: cdi - instance \ No newline at end of file From bf4482d3d627445184b2c40dbfad2ab3a7c72997 Mon Sep 17 00:00:00 2001 From: Radim Hanus Date: Tue, 7 Jul 2015 18:27:23 +0200 Subject: [PATCH 2/3] refactored cdi-beanmanager sample to use Arquillian exclusively instead of WebServlet --- cdi/beanmanager/pom.xml | 27 ++--- .../beanmanager/Greeting.java | 2 +- .../beanmanager/SimpleGreeting.java | 2 +- .../beanmanager/SmileyGreeting.java | 2 +- .../beanmanager/TestServletCurrent.java | 95 ---------------- .../beanmanager/TestServletInject.java | 96 ---------------- .../beanmanager/TestServletJNDI.java | 103 ------------------ .../src/main/webapp/WEB-INF/beans.xml | 0 cdi/beanmanager/src/main/webapp/index.jsp | 62 ----------- .../javaee7/cdi/beanmanager/GreetingTest.java | 71 ++++++++++++ cdi/beanmanager/src/test/resources/beans.xml | 8 ++ 11 files changed, 93 insertions(+), 375 deletions(-) rename cdi/beanmanager/src/main/java/org/javaee7/{cdisamples => cdi}/beanmanager/Greeting.java (69%) rename cdi/beanmanager/src/main/java/org/javaee7/{cdisamples => cdi}/beanmanager/SimpleGreeting.java (80%) rename cdi/beanmanager/src/main/java/org/javaee7/{cdisamples => cdi}/beanmanager/SmileyGreeting.java (81%) delete mode 100644 cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletCurrent.java delete mode 100644 cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletInject.java delete mode 100644 cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletJNDI.java delete mode 100644 cdi/beanmanager/src/main/webapp/WEB-INF/beans.xml delete mode 100644 cdi/beanmanager/src/main/webapp/index.jsp create mode 100644 cdi/beanmanager/src/test/java/org/javaee7/cdi/beanmanager/GreetingTest.java create mode 100644 cdi/beanmanager/src/test/resources/beans.xml diff --git a/cdi/beanmanager/pom.xml b/cdi/beanmanager/pom.xml index 227646024..3bc43f077 100644 --- a/cdi/beanmanager/pom.xml +++ b/cdi/beanmanager/pom.xml @@ -1,20 +1,15 @@ - - 4.0.0 + + 4.0.0 - - org.javaee7 - cdi - 1.0-SNAPSHOT - ../pom.xml - - org.javaee7 - cdi-beanmanager - 1.0-SNAPSHOT - war - Java EE 7 Sample: cdi - beanmanager + + org.javaee7 + cdi + 1.0-SNAPSHOT + + cdi-beanmanager + Java EE 7 Sample: cdi - beanmanager - - gfv3ee6 - diff --git a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/Greeting.java b/cdi/beanmanager/src/main/java/org/javaee7/cdi/beanmanager/Greeting.java similarity index 69% rename from cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/Greeting.java rename to cdi/beanmanager/src/main/java/org/javaee7/cdi/beanmanager/Greeting.java index 6c4aa4cc9..5e4ab2641 100644 --- a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/Greeting.java +++ b/cdi/beanmanager/src/main/java/org/javaee7/cdi/beanmanager/Greeting.java @@ -1,4 +1,4 @@ -package org.javaee7.cdisamples.beanmanager; +package org.javaee7.cdi.beanmanager; /** * @author Arun Gupta diff --git a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/SimpleGreeting.java b/cdi/beanmanager/src/main/java/org/javaee7/cdi/beanmanager/SimpleGreeting.java similarity index 80% rename from cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/SimpleGreeting.java rename to cdi/beanmanager/src/main/java/org/javaee7/cdi/beanmanager/SimpleGreeting.java index 1eea9fba6..8e34ccf7d 100644 --- a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/SimpleGreeting.java +++ b/cdi/beanmanager/src/main/java/org/javaee7/cdi/beanmanager/SimpleGreeting.java @@ -1,4 +1,4 @@ -package org.javaee7.cdisamples.beanmanager; +package org.javaee7.cdi.beanmanager; /** * @author Arun Gupta diff --git a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/SmileyGreeting.java b/cdi/beanmanager/src/main/java/org/javaee7/cdi/beanmanager/SmileyGreeting.java similarity index 81% rename from cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/SmileyGreeting.java rename to cdi/beanmanager/src/main/java/org/javaee7/cdi/beanmanager/SmileyGreeting.java index d0de2d9c1..f06f3c754 100644 --- a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/SmileyGreeting.java +++ b/cdi/beanmanager/src/main/java/org/javaee7/cdi/beanmanager/SmileyGreeting.java @@ -1,4 +1,4 @@ -package org.javaee7.cdisamples.beanmanager; +package org.javaee7.cdi.beanmanager; /** * @author Arun Gupta diff --git a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletCurrent.java b/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletCurrent.java deleted file mode 100644 index ca7343964..000000000 --- a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletCurrent.java +++ /dev/null @@ -1,95 +0,0 @@ -package org.javaee7.cdisamples.beanmanager; - -import java.io.IOException; -import java.io.PrintWriter; -import java.util.Set; -import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.CDI; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * @author Arun Gupta - */ -@WebServlet(urlPatterns = { "/TestServletCurrent" }) -public class TestServletCurrent extends HttpServlet { - - /** - * Processes requests for both HTTP - * GET and - * POST methods. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - protected void processRequest(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - response.setContentType("text/html;charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - out.println(""); - out.println(""); - out.println(""); - out.println("BeanManager using CDI.current"); - out.println(""); - out.println(""); - out.println("

BeanManager using CDI.current

"); - // Second way to get BeanManager - BeanManager bm = CDI.current().getBeanManager(); - - Set> beans = bm.getBeans(Greeting.class); - for (Bean b : beans) { - out.println(b.getBeanClass().getName() + "
"); - } - out.println(""); - out.println(""); - } - } - - // - /** - * Handles the HTTP - * GET method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Handles the HTTP - * POST method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Returns a short description of the servlet. - * - * @return a String containing servlet description - */ - @Override - public String getServletInfo() { - return "Short description"; - }// - -} diff --git a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletInject.java b/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletInject.java deleted file mode 100644 index 6c11b4c1d..000000000 --- a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletInject.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.javaee7.cdisamples.beanmanager; - -import java.io.IOException; -import java.io.PrintWriter; -import java.util.Set; -import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.BeanManager; -import javax.inject.Inject; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * @author Arun Gupta - */ -@WebServlet(urlPatterns = { "/TestServletInject" }) -public class TestServletInject extends HttpServlet { - - // First way to get BeanManager - @Inject - BeanManager bm; - - /** - * Processes requests for both HTTP - * GET and - * POST methods. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - protected void processRequest(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - response.setContentType("text/html;charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - out.println(""); - out.println(""); - out.println(""); - out.println("BeanManager using Injection"); - out.println(""); - out.println(""); - out.println("

BeanManager using Injection

"); - Set> beans = bm.getBeans(Greeting.class); - for (Bean b : beans) { - out.println(b.getBeanClass().getName() + "
"); - } - out.println(""); - out.println(""); - } - } - - // - /** - * Handles the HTTP - * GET method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Handles the HTTP - * POST method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Returns a short description of the servlet. - * - * @return a String containing servlet description - */ - @Override - public String getServletInfo() { - return "Short description"; - }// - -} diff --git a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletJNDI.java b/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletJNDI.java deleted file mode 100644 index 99d5bedc8..000000000 --- a/cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletJNDI.java +++ /dev/null @@ -1,103 +0,0 @@ -package org.javaee7.cdisamples.beanmanager; - -import java.io.IOException; -import java.io.PrintWriter; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.BeanManager; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * @author Arun Gupta - */ -@WebServlet(urlPatterns = { "/TestServletJNDI" }) -public class TestServletJNDI extends HttpServlet { - - /** - * Processes requests for both HTTP - * GET and - * POST methods. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - protected void processRequest(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - response.setContentType("text/html;charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - out.println(""); - out.println(""); - out.println(""); - out.println("BeanManager using JNDI"); - out.println(""); - out.println(""); - out.println("

BeanManager using JNDI

"); - // Third way to get BeanManager - BeanManager bm = null; - try { - InitialContext context = new InitialContext(); - bm = (BeanManager) context.lookup("java:comp/BeanManager"); - } catch (NamingException | NullPointerException ex) { - ex.printStackTrace(out); - } - Set> beans = bm.getBeans(Greeting.class); - for (Bean b : beans) { - out.println(b.getBeanClass().getName() + "
"); - } - out.println(""); - out.println(""); - } - } - - // - /** - * Handles the HTTP - * GET method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Handles the HTTP - * POST method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Returns a short description of the servlet. - * - * @return a String containing servlet description - */ - @Override - public String getServletInfo() { - return "Short description"; - }// - -} diff --git a/cdi/beanmanager/src/main/webapp/WEB-INF/beans.xml b/cdi/beanmanager/src/main/webapp/WEB-INF/beans.xml deleted file mode 100644 index e69de29bb..000000000 diff --git a/cdi/beanmanager/src/main/webapp/index.jsp b/cdi/beanmanager/src/main/webapp/index.jsp deleted file mode 100644 index f72da678c..000000000 --- a/cdi/beanmanager/src/main/webapp/index.jsp +++ /dev/null @@ -1,62 +0,0 @@ - -<%@page contentType="text/html" pageEncoding="UTF-8"%> - - - - - - CDI : BeanManager - - -

CDI : BeanManager

- - Show the list of beans using BeanManager by: -
    -
  1. injection -
  2. CDI.current -
  3. JNDI -
- - - diff --git a/cdi/beanmanager/src/test/java/org/javaee7/cdi/beanmanager/GreetingTest.java b/cdi/beanmanager/src/test/java/org/javaee7/cdi/beanmanager/GreetingTest.java new file mode 100644 index 000000000..a5bfe2bdd --- /dev/null +++ b/cdi/beanmanager/src/test/java/org/javaee7/cdi/beanmanager/GreetingTest.java @@ -0,0 +1,71 @@ +package org.javaee7.cdi.beanmanager; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.CDI; +import javax.inject.Inject; +import javax.naming.InitialContext; +import java.util.HashSet; +import java.util.Set; + +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +/** + * @author Radim Hanus + */ +@RunWith(Arquillian.class) +public class GreetingTest { + @Deployment + public static Archive deploy() { + return ShrinkWrap.create(JavaArchive.class) + .addClasses(Greeting.class, SimpleGreeting.class, SmileyGreeting.class) + .addAsManifestResource("beans.xml"); + } + + // First way to get BeanManager + @Inject + private BeanManager bm; + + @Test + public void testInject() throws Exception { + test(this.bm); + } + + @Test + public void testCurrent() throws Exception { + // Second way to get BeanManager: current CDI container + BeanManager bm = CDI.current().getBeanManager(); + + test(bm); + } + + @Test + public void test() throws Exception { + // Third way to get BeanManager: name service + BeanManager bm = InitialContext.doLookup("java:comp/BeanManager"); + + test(bm); + } + + private void test(BeanManager bm) throws Exception { + Set> beans = bm.getBeans(Greeting.class); + assertTrue(beans.size() == 2); + + Set beanClassNames = new HashSet<>(); + for (Bean bean : beans) { + beanClassNames.add(bean.getBeanClass().getName()); + } + + assertThat(beanClassNames, containsInAnyOrder(SimpleGreeting.class.getName(), SmileyGreeting.class.getName())); + } +} diff --git a/cdi/beanmanager/src/test/resources/beans.xml b/cdi/beanmanager/src/test/resources/beans.xml new file mode 100644 index 000000000..be95b1d6e --- /dev/null +++ b/cdi/beanmanager/src/test/resources/beans.xml @@ -0,0 +1,8 @@ + + + + + From 6b84efd4cf8098376706141ec36dda0babb6cbd1 Mon Sep 17 00:00:00 2001 From: Radim Hanus Date: Wed, 8 Jul 2015 10:44:20 +0200 Subject: [PATCH 3/3] refactored cdi-beanmanager sample to use Arquillian exclusively instead of WebServlet --- .../src/test/java/org/javaee7/cdi/beanmanager/GreetingTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdi/beanmanager/src/test/java/org/javaee7/cdi/beanmanager/GreetingTest.java b/cdi/beanmanager/src/test/java/org/javaee7/cdi/beanmanager/GreetingTest.java index a5bfe2bdd..222c596fd 100644 --- a/cdi/beanmanager/src/test/java/org/javaee7/cdi/beanmanager/GreetingTest.java +++ b/cdi/beanmanager/src/test/java/org/javaee7/cdi/beanmanager/GreetingTest.java @@ -50,7 +50,7 @@ public void testCurrent() throws Exception { } @Test - public void test() throws Exception { + public void testJNDI() throws Exception { // Third way to get BeanManager: name service BeanManager bm = InitialContext.doLookup("java:comp/BeanManager");