From ddb598396b0792a855b9b681b92a5a1067de3211 Mon Sep 17 00:00:00 2001 From: Rafael Benevides Date: Thu, 22 Nov 2012 05:41:16 -0800 Subject: [PATCH] JDF-148 showing how to create new beans using DeltaSpike utilities --- .gitignore | 1 - deltaspike-beanbuilder/README.md | 98 +++++++ deltaspike-beanbuilder/pom.xml | 276 ++++++++++++++++++ .../deltaspike/beanbuilder/ById.java | 57 ++++ .../deltaspike/beanbuilder/ByIdExtension.java | 141 +++++++++ .../deltaspike/beanbuilder/model/Person.java | 60 ++++ .../beanbuilder/util/Resources.java | 58 ++++ .../main/resources/META-INF/persistence.xml | 29 ++ .../javax.enterprise.inject.spi.Extension | 1 + .../src/main/resources/import.sql | 7 + .../WEB-INF/deltaspike-beanbuilder-ds.xml | 31 ++ .../src/main/webapp/WEB-INF/faces-config.xml | 9 + .../beanbuilder/test/ByIdExtensionTest.java | 78 +++++ .../resources/META-INF/test-persistence.xml | 30 ++ .../src/test/resources/arquillian.xml | 31 ++ .../src/test/resources/test-ds.xml | 31 ++ 16 files changed, 937 insertions(+), 1 deletion(-) create mode 100644 deltaspike-beanbuilder/README.md create mode 100644 deltaspike-beanbuilder/pom.xml create mode 100644 deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/ById.java create mode 100644 deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/ByIdExtension.java create mode 100644 deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/model/Person.java create mode 100644 deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/util/Resources.java create mode 100644 deltaspike-beanbuilder/src/main/resources/META-INF/persistence.xml create mode 100644 deltaspike-beanbuilder/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension create mode 100644 deltaspike-beanbuilder/src/main/resources/import.sql create mode 100644 deltaspike-beanbuilder/src/main/webapp/WEB-INF/deltaspike-beanbuilder-ds.xml create mode 100644 deltaspike-beanbuilder/src/main/webapp/WEB-INF/faces-config.xml create mode 100644 deltaspike-beanbuilder/src/test/java/org/jboss/as/quickstart/deltaspike/beanbuilder/test/ByIdExtensionTest.java create mode 100644 deltaspike-beanbuilder/src/test/resources/META-INF/test-persistence.xml create mode 100644 deltaspike-beanbuilder/src/test/resources/arquillian.xml create mode 100644 deltaspike-beanbuilder/src/test/resources/test-ds.xml diff --git a/.gitignore b/.gitignore index 095a09d471..2c1fdb4e1f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -<<<<<<< HEAD *~ target .DS_Store diff --git a/deltaspike-beanbuilder/README.md b/deltaspike-beanbuilder/README.md new file mode 100644 index 0000000000..7e634edf75 --- /dev/null +++ b/deltaspike-beanbuilder/README.md @@ -0,0 +1,98 @@ +DeltaSpike BeanBuilder: Shows how to create new beans using DeltaSpike utilities +====================================================== +Author: Rafael Benevides +Level: Advanced +Technologies: CDI, DeltaSpike +Summary: Shows how to create new beans using DeltaSpike utilities. +Target Product: WFK + +What is it? +----------- + +This project demonstrates a CDI Portable Extension that uses DeltaSpike utilities to create new Beans. + +This extension permits the injection of JPA Entities by its Id without the need of query it. To do that, the extension observes the `ProcessInjectionTarget` event and get all values that needs to be queried. Then in `AfterBeanDiscovery` event, the extension create `Bean` instances using the `BeanBuilder` utility. + +The project contains very simple JPA Entity class, an extension class, the service registration file for that extension and an Arquillian test to verify the extension is working correctly. + +It does not contain any user interface, the tests must be run to verify everything is working correctly. + +System requirements +------------------- + +All you need to build this project is Java 6.0 (Java SDK 1.6) or better, Maven 3.0 or better. + +The application this project produces is designed to be run on JBoss Enterprise Application Platform 6 or JBoss AS 7. + + +Configure Maven +--------------- + +If you have not yet done so, you must [Configure Maven](../README.md#mavenconfiguration) before testing the quickstarts. + +Start JBoss Enterprise Application Platform 6 or JBoss AS 7 with the Web Profile +------------------------- + +1. Open a command line and navigate to the root of the JBoss server directory. +2. The following shows the command line to start the server with the web profile: + + For Linux: JBOSS_HOME/bin/standalone.sh + For Windows: JBOSS_HOME\bin\standalone.bat + + +Run the Arquillian Tests +------------------------- + +This quickstart provides Arquillian tests. By default, these tests are configured to be skipped as Arquillian tests require the use of a container. + +_NOTE: The following commands assume you have configured your Maven user settings. If you have not, you must include Maven setting arguments on the command line. See [Run the Arquillian Tests](../README.md#arquilliantests) for complete instructions and additional options._ + +1. Make sure you have started the JBoss Server as described above. +2. Open a command line and navigate to the root directory of this quickstart. +3. Type the following command to run the test goal with the following profile activated: + + mvn clean test -Parq-jbossas-remote + + +Run tests from JBDS +----------------------- + +To be able to run the tests from JBDS, first set the active Maven profile in project properties to be either 'arq-jbossas-managed' for running on +managed server or 'arq-jbossas-remote' for running on remote server. + +To run the tests, right click on the project or individual classes and select Run As --> JUnit Test in the context menu. + + +Investigate the Console Output +---------------------------- + + +### Maven + +Maven prints summary of performed tests into the console: + + ------------------------------------------------------- + T E S T S + ------------------------------------------------------- + Running org.jboss.as.quickstart.deltaspike.beanbuilder.test.ByIdExtensionTest + log4j:WARN No appenders could be found for logger (org.jboss.logging). + log4j:WARN Please initialize the log4j system properly. + Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 8.641 sec + + Results : + + Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 + + +Run the Quickstart in JBoss Developer Studio or Eclipse +------------------------------------- +You can also start the server and deploy the quickstarts from Eclipse using JBoss tools. For more information, see [Use JBoss Developer Studio or Eclipse to Run the Quickstarts](../README.md#useeclipse) + + +Debug the Application +------------------------------------ + +If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following commands to pull them into your local repository. The IDE should then detect them. + + mvn dependency:sources + mvn dependency:resolve -Dclassifier=javadoc diff --git a/deltaspike-beanbuilder/pom.xml b/deltaspike-beanbuilder/pom.xml new file mode 100644 index 0000000000..fabbba89e8 --- /dev/null +++ b/deltaspike-beanbuilder/pom.xml @@ -0,0 +1,276 @@ + + + + 4.0.0 + + org.jboss.as.quickstarts + jboss-as-deltaspike-beanbuilder + 7.1.2-SNAPSHOT + war + JBoss AS Quickstarts: DeltaSpike BeanBuilder + DeltaSpike BeanBuilder: show how to create new beans using DeltaSpike utilities + + http://jboss.org/jbossas + + + Apache License, Version 2.0 + repo + http://www.apache.org/licenses/LICENSE-2.0.html + + + + + + + UTF-8 + + + 7.3.Final + 1.0.3.Final + + + 2.3.1 + 2.1.1 + 2.4.3 + + + 1.6 + 1.6 + + + + + + + + + org.jboss.bom + jboss-javaee-6.0-with-deltaspike + ${version.org.jboss.bom} + pom + import + + + + org.jboss.bom + jboss-javaee-6.0-with-tools + ${version.org.jboss.bom} + pom + import + + + + + + + + + + javax.enterprise + cdi-api + provided + + + + + org.jboss.spec.javax.annotation + jboss-annotations-api_1.1_spec + provided + + + + + org.jboss.spec.javax.faces + jboss-jsf-api_2.1_spec + provided + + + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + provided + + + + + org.jboss.spec.javax.transaction + jboss-transaction-api_1.1_spec + provided + + + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.1_spec + provided + + + + + org.apache.deltaspike.core + deltaspike-core-api + compile + + + + + org.apache.deltaspike.core + deltaspike-core-impl + runtime + + + + + + org.jboss.spec.javax.ws.rs + jboss-jaxrs-api_1.1_spec + provided + + + + + junit + junit + test + + + org.jboss.arquillian.junit + arquillian-junit-container + test + + + org.jboss.arquillian.protocol + arquillian-protocol-servlet + + + org.jboss.shrinkwrap.resolver + shrinkwrap-resolver-impl-maven + test + + + + + + ${project.artifactId} + + + maven-war-plugin + ${version.war.plugin} + + + false + + + + + org.jboss.as.plugins + jboss-as-maven-plugin + ${version.org.jboss.as.plugins.maven.plugin} + + + + maven-compiler-plugin + ${version.compiler.plugin} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + + default + + true + + + + + maven-surefire-plugin + ${version.surefire.plugin} + + true + + + + + + org.jboss.as.plugins + jboss-as-maven-plugin + ${version.org.jboss.as.plugins.maven.plugin} + + + + + + + + + + + arq-jbossas-managed + + + org.jboss.as + jboss-as-arquillian-container-managed + test + + + + + + + + arq-jbossas-remote + + + org.jboss.as + jboss-as-arquillian-container-remote + test + + + + + + + diff --git a/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/ById.java b/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/ById.java new file mode 100644 index 0000000000..aa7c970d34 --- /dev/null +++ b/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/ById.java @@ -0,0 +1,57 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstart.deltaspike.beanbuilder; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +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; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.jboss.as.quickstart.deltaspike.beanbuilder.model.Person; + +/** + * This {@link Qualifier} uses its value to distinguish the id {@link Id} of the {@link Person} {@link Entity} that will be + * injected. + * + * + * @author Rafael Benevides + * + */ +@Target({ TYPE, METHOD, FIELD, PARAMETER }) +@Retention(RUNTIME) +@Documented +@Qualifier +public @interface ById { + + String value(); + +} diff --git a/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/ByIdExtension.java b/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/ByIdExtension.java new file mode 100644 index 0000000000..638369f52e --- /dev/null +++ b/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/ByIdExtension.java @@ -0,0 +1,141 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstart.deltaspike.beanbuilder; + +import java.util.LinkedList; +import java.util.List; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.spi.AfterBeanDiscovery; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.Extension; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.enterprise.inject.spi.ProcessInjectionTarget; +import javax.enterprise.util.AnnotationLiteral; +import javax.inject.Qualifier; +import javax.persistence.EntityManager; + +import org.apache.deltaspike.core.api.provider.BeanProvider; +import org.apache.deltaspike.core.util.bean.BeanBuilder; +import org.apache.deltaspike.core.util.metadata.builder.ContextualLifecycle; +import org.jboss.as.quickstart.deltaspike.beanbuilder.model.Person; + +/** + * @author Rafael Benevides + * + */ +public class ByIdExtension implements Extension { + + // All nicks that needs to be found + private List ids = new LinkedList(); + + /** + * This method is fired for every component class supporting injection that may be instantiated by the container at runtime. + * + * It will look for all injection target and collect all nicks that were used and needs to be found on {@link EntityManager} + * + * @param pit + */ + public void processInjectionTarget(@Observes ProcessInjectionTarget pit) { + for (InjectionPoint ip : pit.getInjectionTarget().getInjectionPoints()) { + ById idValue = ip.getAnnotated().getAnnotation(ById.class); + if (idValue != null) { + // Store the nick value + ids.add(idValue.value()); + } + } + } + + /** + * This method is fired when CDI has fully completed the bean discovery process + * + */ + public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) { + for (final String idValue : ids) { + // Create a Bean using the Nick Qualifier with the right id value and a new contextualLifecycle + BeanBuilder beanBuilder = new BeanBuilder(bm) + .beanClass(Person.class) + .types(Person.class, Object.class) + // The qualifier with its value + .qualifiers(new ByIdLiteral(idValue)) + // Create a ContextualLifecyle for each id found + .beanLifecycle(new PersonContextualLifecycle(idValue)); + // Create and add the Bean + abd.addBean(beanBuilder.create()); + } + } + + + + /** + * This class represents the {@link ById} annotation with its value. It is used by the {@link BeanBuilder} to set the + * {@link Bean} {@link Qualifier} + * + */ + public static class ByIdLiteral extends AnnotationLiteral implements ById { + + private static final long serialVersionUID = 1L; + + private String value; + + public ByIdLiteral(String v) { + this.value = v; + } + + @Override + public String value() { + return value; + } + + } + + /** + * This is the {@link ContextualLifecycle} that is used to by the {@link Bean} to create instances of {@link Person}. + * + * It uses the {@link EntityManager#find(Class, Object)} to query the {@link Person} from Database + * + */ + public static class PersonContextualLifecycle implements ContextualLifecycle { + + private String idValue; + + public PersonContextualLifecycle(String idValue) { + this.idValue = idValue; + } + + @Override + public void destroy(Bean bean, Person instance, CreationalContext creationalContext) { + creationalContext.release(); + } + + @Override + public Person create(Bean bean, CreationalContext creationalContext) { + // Here we use the entityManager to get the Person Instance + EntityManager em = BeanProvider.getContextualReference(EntityManager.class); + return em.find(Person.class, idValue); + } + } + +} diff --git a/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/model/Person.java b/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/model/Person.java new file mode 100644 index 0000000000..b2c60b5c48 --- /dev/null +++ b/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/model/Person.java @@ -0,0 +1,60 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstart.deltaspike.beanbuilder.model; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * This is a tipical {@link Entity} with the field nick as its {@link Id} + * + * @author Rafael Benevides + * + */ +@Entity +@Table(name = "person") +public class Person { + + @Id + private String nick; + + private String name; + + public String getNick() { + return nick; + } + + public void setNick(String nick) { + this.nick = nick; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/util/Resources.java b/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/util/Resources.java new file mode 100644 index 0000000000..3ffb4c44c5 --- /dev/null +++ b/deltaspike-beanbuilder/src/main/java/org/jboss/as/quickstart/deltaspike/beanbuilder/util/Resources.java @@ -0,0 +1,58 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat, Inc. and/or its affiliates, and individual + * contributors by the @authors tag. See the copyright.txt in the + * distribution for a full listing of individual contributors. + * + * Licensed 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.jboss.as.quickstart.deltaspike.beanbuilder.util; + +import java.util.logging.Logger; + +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.faces.context.FacesContext; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +/** + * This class uses CDI to alias Java EE resources, such as the persistence context, to CDI beans + * + *

+ * Example injection on a managed bean field: + *

+ * + *
+ * @Inject
+ * private EntityManager em;
+ * 
+ */ +public class Resources { + + @Produces + @PersistenceContext + private EntityManager em; + + @Produces + public Logger produceLog(InjectionPoint injectionPoint) { + return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); + } + + @Produces + @RequestScoped + public FacesContext produceFacesContext() { + return FacesContext.getCurrentInstance(); + } + + +} diff --git a/deltaspike-beanbuilder/src/main/resources/META-INF/persistence.xml b/deltaspike-beanbuilder/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..a28103eb4c --- /dev/null +++ b/deltaspike-beanbuilder/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,29 @@ + + + + + + + java:jboss/datasources/DetaSpikeBeanBuilderDS + + + + + + + diff --git a/deltaspike-beanbuilder/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/deltaspike-beanbuilder/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension new file mode 100644 index 0000000000..2046ef11ac --- /dev/null +++ b/deltaspike-beanbuilder/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension @@ -0,0 +1 @@ +org.jboss.as.quickstart.deltaspike.beanbuilder.ByIdExtension \ No newline at end of file diff --git a/deltaspike-beanbuilder/src/main/resources/import.sql b/deltaspike-beanbuilder/src/main/resources/import.sql new file mode 100644 index 0000000000..1a0a1cc509 --- /dev/null +++ b/deltaspike-beanbuilder/src/main/resources/import.sql @@ -0,0 +1,7 @@ +-- You can use this file to load seed data into the database using SQL statements +insert into person (nick, name) values ('pmuir', 'Pete Muir') +insert into person (nick, name) values ('rbenevides', 'Rafael Benevides') +insert into person (nick, name) values ('lightguard_jp', 'Jason Porter') +insert into person (nick, name) values ('mbg', 'Marius Bogoevici') +insert into person (nick, name) values ('sgilda', 'Sande Gilda') + diff --git a/deltaspike-beanbuilder/src/main/webapp/WEB-INF/deltaspike-beanbuilder-ds.xml b/deltaspike-beanbuilder/src/main/webapp/WEB-INF/deltaspike-beanbuilder-ds.xml new file mode 100644 index 0000000000..0d84d58ecd --- /dev/null +++ b/deltaspike-beanbuilder/src/main/webapp/WEB-INF/deltaspike-beanbuilder-ds.xml @@ -0,0 +1,31 @@ + + + + + + + jdbc:h2:mem:deltaspike-beanbuilder;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1 + h2 + + sa + sa + + + + diff --git a/deltaspike-beanbuilder/src/main/webapp/WEB-INF/faces-config.xml b/deltaspike-beanbuilder/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000000..b3d2ce3408 --- /dev/null +++ b/deltaspike-beanbuilder/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/deltaspike-beanbuilder/src/test/java/org/jboss/as/quickstart/deltaspike/beanbuilder/test/ByIdExtensionTest.java b/deltaspike-beanbuilder/src/test/java/org/jboss/as/quickstart/deltaspike/beanbuilder/test/ByIdExtensionTest.java new file mode 100644 index 0000000000..8471f77b35 --- /dev/null +++ b/deltaspike-beanbuilder/src/test/java/org/jboss/as/quickstart/deltaspike/beanbuilder/test/ByIdExtensionTest.java @@ -0,0 +1,78 @@ +package org.jboss.as.quickstart.deltaspike.beanbuilder.test; + +/* + * JBoss, Home of Professional Open Source + * Copyright 2012, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed 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. + */ + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import javax.enterprise.inject.spi.Extension; +import javax.inject.Inject; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.as.quickstart.deltaspike.beanbuilder.ById; +import org.jboss.as.quickstart.deltaspike.beanbuilder.ByIdExtension; +import org.jboss.as.quickstart.deltaspike.beanbuilder.model.Person; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.resolver.api.DependencyResolvers; +import org.jboss.shrinkwrap.resolver.api.maven.MavenDependencyResolver; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Verification test. + */ +@RunWith(Arquillian.class) +public class ByIdExtensionTest { + + @Deployment + public static Archive getDeployment() { + + MavenDependencyResolver resolver = DependencyResolvers + .use(MavenDependencyResolver.class) + .loadMetadataFromPom("pom.xml"); + + Archive archive = ShrinkWrap + .create(WebArchive.class, "byid-extension.war") + .addPackages(true, ByIdExtension.class.getPackage()) + .addAsServiceProvider(Extension.class, ByIdExtension.class) + .addAsLibraries( + resolver.artifacts("org.apache.deltaspike.core:deltaspike-core-api", + "org.apache.deltaspike.core:deltaspike-core-impl").resolveAsFiles()) + .addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml").addAsResource("import.sql") + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") + // Deploy our test datasource + .addAsWebInfResource("test-ds.xml"); + return archive; + } + + // This will inject a qualified Person with @ById('rbenevides') qualifier + @Inject @ById("rbenevides") Person rafael; + + // This will inject a qualified Person with @ById('pmuir') qualifier + @Inject @ById("pmuir") Person pete; + + @Test + public void assertPersonInjected() { + assertThat(rafael.getName(), is("Rafael Benevides")); + assertThat(pete.getName(), is("Pete Muir")); + } +} diff --git a/deltaspike-beanbuilder/src/test/resources/META-INF/test-persistence.xml b/deltaspike-beanbuilder/src/test/resources/META-INF/test-persistence.xml new file mode 100644 index 0000000000..0f583a8b69 --- /dev/null +++ b/deltaspike-beanbuilder/src/test/resources/META-INF/test-persistence.xml @@ -0,0 +1,30 @@ + + + + + + + java:jboss/datasources/DeltaSpikeBeanBuilderTestDS + + + + + + + diff --git a/deltaspike-beanbuilder/src/test/resources/arquillian.xml b/deltaspike-beanbuilder/src/test/resources/arquillian.xml new file mode 100644 index 0000000000..ab93aa6214 --- /dev/null +++ b/deltaspike-beanbuilder/src/test/resources/arquillian.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/deltaspike-beanbuilder/src/test/resources/test-ds.xml b/deltaspike-beanbuilder/src/test/resources/test-ds.xml new file mode 100644 index 0000000000..4fb6335946 --- /dev/null +++ b/deltaspike-beanbuilder/src/test/resources/test-ds.xml @@ -0,0 +1,31 @@ + + + + + + + jdbc:h2:mem:deltaspike-beanbuilder-test;DB_CLOSE_DELAY=-1 + h2 + + sa + sa + + + +