Created
October 16, 2012 09:18
-
-
Save maciej/3898245 to your computer and use it in GitHub Desktop.
@Alternative and producer method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Maciej Bilas | |
* @since 16/10/12 11:01 | |
*/ | |
public class A { | |
private final boolean produced; | |
public A() { | |
this.produced = false; | |
} | |
public A(String ignore) { | |
this.produced = true; | |
} | |
public boolean isProduced() { | |
return produced; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.enterprise.inject.Produces; | |
/** | |
* @author Maciej Bilas | |
* @since 16/10/12 11:01 | |
*/ | |
public class AProducer { | |
@Produces | |
@MyAlternative | |
public A produceA() { | |
System.out.println("Foo bar"); | |
return new A("foo"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.enterprise.inject.Alternative; | |
import javax.enterprise.inject.Stereotype; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.Target; | |
import static java.lang.annotation.ElementType.*; | |
import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
/** | |
* @author Maciej Bilas | |
* @see <a href="https://community.jboss.org/thread/179590">Forum thread on why we need my alternative</a> | |
* @since 16/10/12 11:13 | |
*/ | |
@Stereotype | |
@Alternative | |
@Target({TYPE, METHOD, FIELD}) | |
@Retention(RUNTIME) | |
public @interface MyAlternative { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jboss.arquillian.container.test.api.Deployment; | |
import org.jboss.arquillian.testng.Arquillian; | |
import org.jboss.shrinkwrap.api.ArchivePaths; | |
import org.jboss.shrinkwrap.api.ShrinkWrap; | |
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset; | |
import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
import org.testng.annotations.Test; | |
import javax.inject.Inject; | |
import static org.fest.assertions.Assertions.assertThat; | |
/** | |
* @author Maciej Bilas | |
* @since 16/10/12 11:03 | |
*/ | |
public class ProduceAlternativeTest extends Arquillian { | |
@Deployment | |
public static JavaArchive create() { | |
return ShrinkWrap.create(JavaArchive.class) | |
.addClass(A.class) | |
.addClass(AProducer.class) | |
.addAsManifestResource(new ByteArrayAsset(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
"<beans xmlns=\"http://java.sun.com/xml/ns/javaee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + | |
" xsi:schemaLocation=\"\n" + | |
" http://java.sun.com/xml/ns/javaee \n" + | |
" http://java.sun.com/xml/ns/javaee/beans_1_0.xsd\">\n" + | |
" <alternatives>\n" + | |
" <stereotype>" + MyAlternative.class.getCanonicalName() + "</stereotype>\n" + | |
" </alternatives>\n" + | |
"</beans>\n").getBytes()), ArchivePaths.create("beans.xml")); | |
} | |
@Inject | |
private A a; | |
@Test | |
public void shouldInjectTheProducerVersion() { | |
assertThat(a.isProduced()).isTrue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment