Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added samples using both cdi alternatives and qualifiers #374

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cdi/alternatives-qualifiers/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.javaee7</groupId>
<artifactId>cdi</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>cdi-alternatives-qualifiers</artifactId>
<name>Java EE 7 Sample: cdi - alternatives-qualifiers</name>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.javaee7.cdi.alternatives;

import javax.enterprise.inject.Alternative;


/**
* @author Radim Hanus
*/
@Alternative
public class AlternativeGreeting implements Greeting {
@Override
public String greet(String name) {
return "Nice to meet you, hello " + name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.javaee7.cdi.alternatives;

import javax.enterprise.inject.Alternative;


/**
* @author Radim Hanus
*/
@Alternative
@Personal
public class AlternativePersonalGreeting implements Greeting {
@Override
public String greet(String name) {
return "Fine to see you, hi " + name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.javaee7.cdi.alternatives;


/**
* @author Radim Hanus
*/
public class DefaultGreeting implements Greeting {
@Override
public String greet(String name) {
return "Hello " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.javaee7.cdi.alternatives;


/**
* @author Arun Gupta
*/
public interface Greeting {
String greet(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.javaee7.cdi.alternatives;

import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


/**
* @author Radim Hanus
*/
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface Personal {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.javaee7.cdi.alternatives;


/**
* @author Radim Hanus
*/
@Personal
public class PersonalGreeting implements Greeting {
@Override
public String greet(String name) {
return "Hi " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.javaee7.cdi.alternatives;

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.inject.Inject;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;


/**
* @author Radim Hanus
*/
@RunWith(Arquillian.class)
public class AlternativeGreetingTest {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Greeting.class, DefaultGreeting.class, AlternativeGreeting.class,
Personal.class, PersonalGreeting.class, AlternativePersonalGreeting.class)
.addAsManifestResource("beans-default-alternative.xml", "beans.xml");
}

@Inject
private Greeting greeting;

@Inject
@Personal
private Greeting personalGreeting;

@Test
public void should_greeting_be_alternative() throws Exception {
// alternative implementation is defined in beans.xml
assertThat(greeting, instanceOf(AlternativeGreeting.class));
}

@Test
public void should_personal_greeting_be_default() throws Exception {
// qualified implementation because only default alternative is defined in beans.xml
assertThat(personalGreeting, instanceOf(PersonalGreeting.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.javaee7.cdi.alternatives;

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.inject.Inject;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;


/**
* @author Radim Hanus
*/
@RunWith(Arquillian.class)
public class AlternativePersonalGreetingTest {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Greeting.class, DefaultGreeting.class, AlternativeGreeting.class,
Personal.class, PersonalGreeting.class, AlternativePersonalGreeting.class)
.addAsManifestResource("beans-personal-alternative.xml", "beans.xml");
}

@Inject
private Greeting greeting;

@Inject
@Personal
private Greeting personalGreeting;

@Test
public void should_greeting_be_default() throws Exception {
// default implementation because qualified alternative is defined in beans.xml
assertThat(greeting, instanceOf(DefaultGreeting.class));
}

@Test
public void should_personal_greeting_be_alternative() throws Exception {
// qualified alternative implementation is defined in beans.xml
assertThat(personalGreeting, instanceOf(AlternativePersonalGreeting.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.javaee7.cdi.alternatives;

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.inject.Inject;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;


/**
* @author Radim Hanus
*/
@RunWith(Arquillian.class)
public class DefaultGreetingTest {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Greeting.class, DefaultGreeting.class, AlternativeGreeting.class,
Personal.class, PersonalGreeting.class, AlternativePersonalGreeting.class)
.addAsManifestResource("beans-empty.xml", "beans.xml");
}

@Inject
private Greeting greeting;

@Inject
@Personal
private Greeting personalGreeting;

@Test
public void should_greeting_be_default() throws Exception {
// default implementation because of empty beans.xml
assertThat(greeting, instanceOf(DefaultGreeting.class));
}

@Test
public void should_personal_greeting_be_default() throws Exception {
// qualified implementation because of empty beans.xml
assertThat(personalGreeting, instanceOf(PersonalGreeting.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all"
version="1.1">

<alternatives>
<class>org.javaee7.cdi.alternatives.AlternativeGreeting</class>
</alternatives>

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all"
version="1.1">

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all"
version="1.1">

<alternatives>
<class>org.javaee7.cdi.alternatives.AlternativePersonalGreeting</class>
</alternatives>

</beans>
1 change: 1 addition & 0 deletions cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>scopes</module>
<module>alternatives</module>
<module>alternatives-priority</module>
<module>alternatives-qualifiers</module>
<module>nobeans-el-injection</module>
<module>nobeans-el-injection-flowscoped</module>
<module>events</module>
Expand Down