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

CDI samples of @Specializes #373

Open
wants to merge 2 commits 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
2 changes: 2 additions & 0 deletions cdi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ The [JSR 346](https://jcp.org/en/jsr/detail?id=346) updates and clarifications t
- events-conditional-reception
- instance
- instance-qualifiers
- specializes
- specializes-qualifiers

## How to run

Expand Down
2 changes: 2 additions & 0 deletions cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<module>events-conditional-reception</module>
<module>instance</module>
<module>instance-qualifiers</module>
<module>specializes</module>
<module>specializes-qualifiers</module>
</modules>

<dependencies>
Expand Down
13 changes: 13 additions & 0 deletions cdi/specializes-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-specializes-qualifiers</artifactId>
<name>Java EE 7 Sample: CDI - specializes-qualifiers</name>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.javaee7.cdi.specializes;

import javax.inject.Named;


/**
* @author Radim Hanus
*/
@Personal
@Named("base")
public class Greeting {
public String greet(String name) {
return "Hello " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.javaee7.cdi.specializes;

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,15 @@
package org.javaee7.cdi.specializes;

import javax.enterprise.inject.Specializes;


/**
* @author Radim Hanus
*/
@Specializes
public class SpecializedGreeting extends Greeting {
@Override
public String greet(String name) {
return "Hello my friend " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.javaee7.cdi.specializes;

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.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.Instance;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Inject;
import javax.inject.Named;

import static org.hamcrest.CoreMatchers.instanceOf;
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(Personal.class, Greeting.class, SpecializedGreeting.class)
.addAsManifestResource("beans.xml");
}

@Inject
@Personal
private Greeting personalBean;

@Inject
@Named("base")
private Greeting simpleBean;

@Inject
@Any
private Instance<Greeting> instance;

@Test
public void beans_should_be_specialized() throws Exception {
// specialized implementation automatically inherits all qualifiers of the base implementation
assertThat(personalBean, instanceOf(SpecializedGreeting.class));
assertThat(simpleBean, instanceOf(SpecializedGreeting.class));
}

@Test
public void default_bean_should_not_be_available() throws Exception {
// specialized implementation inherited some qualifiers so that Default has not been set
Instance<Greeting> defaultInstance = instance.select(new AnnotationLiteral<Default>() {});
assertTrue(defaultInstance.isUnsatisfied());
}
}
9 changes: 9 additions & 0 deletions cdi/specializes-qualifiers/src/test/resources/beans.xml
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>
13 changes: 13 additions & 0 deletions cdi/specializes/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-specializes</artifactId>
<name>Java EE 7 Sample: CDI - specializes</name>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.javaee7.cdi.specializes;


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

import javax.enterprise.inject.Specializes;


/**
* @author Radim Hanus
*/
@Specializes
public class SpecializedGreeting extends Greeting {
@Override
public String greet(String name) {
return "Hello my friend " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.javaee7.cdi.specializes;

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 GreetingTest {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Greeting.class, SpecializedGreeting.class)
.addAsManifestResource("beans.xml");
}

@Inject
private Greeting bean;

@Test
public void should_bean_be_specialized() throws Exception {
assertThat(bean, instanceOf(SpecializedGreeting.class));
}
}
9 changes: 9 additions & 0 deletions cdi/specializes/src/test/resources/beans.xml
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>