Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BZ-1076196 - Enhance KieCDIExtension to allow optional injections
(cherry picked from commit bbebcb47759c2104972d356474a721a0829b90ca)
- Loading branch information
1 parent
3eb7e95
commit d07d53e
Showing
4 changed files
with
133 additions
and
35 deletions.
There are no files selected for viewing
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
37 changes: 37 additions & 0 deletions
37
...s-cdi/cdi-example/src/main/java/org/drools/example/cdi/cdiexample/CDIInstanceExample.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.drools.example.cdi.cdiexample; | ||
|
||
import java.io.PrintStream; | ||
import javax.enterprise.inject.Instance; | ||
import javax.inject.Inject; | ||
|
||
import org.jboss.weld.environment.se.Weld; | ||
import org.jboss.weld.environment.se.WeldContainer; | ||
import org.kie.api.cdi.KSession; | ||
import org.kie.api.runtime.KieSession; | ||
|
||
public class CDIInstanceExample { | ||
|
||
@Inject | ||
@KSession("ksession-optional") | ||
private Instance<KieSession> kSession; | ||
|
||
public void go(PrintStream out) { | ||
|
||
KieSession ksession = kSession.get(); | ||
|
||
ksession.setGlobal("out", out); | ||
ksession.insert(new Message("Dave", "Hello, HAL. Do you read me, HAL?")); | ||
ksession.fireAllRules(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Weld w = new Weld(); | ||
|
||
WeldContainer wc = w.initialize(); | ||
CDIInstanceExample bean = wc.instance().select(CDIInstanceExample.class).get(); | ||
bean.go(System.out); | ||
|
||
w.shutdown(); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
<kbase name="cdiexample"> | ||
<ksession name="ksession1"/> | ||
<ksession name="ksession-optional"/> | ||
</kbase> | ||
|
||
</kmodule> |
35 changes: 35 additions & 0 deletions
35
...i/cdi-example/src/test/java/org/drools/example/cdi/cdiexample/CDIInstanceExampleTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.drools.example.cdi.cdiexample; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
|
||
import org.jboss.weld.environment.se.Weld; | ||
import org.jboss.weld.environment.se.WeldContainer; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class CDIInstanceExampleTest { | ||
|
||
@Test | ||
public void testGo() { | ||
Weld w = new Weld(); | ||
WeldContainer wc = w.initialize(); | ||
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
PrintStream ps = new PrintStream(baos); | ||
|
||
CDIInstanceExample bean = wc.instance().select(CDIInstanceExample.class).get(); | ||
bean.go(ps); | ||
|
||
ps.close(); | ||
|
||
String actual = new String(baos.toByteArray()); | ||
String expected = "" + | ||
"Dave: Hello, HAL. Do you read me, HAL?\n" + | ||
"HAL: Dave. I read you.\n"; | ||
assertEquals(expected, actual); | ||
|
||
w.shutdown(); | ||
} | ||
} |