THE SOFTWARE EXPERTS
Apache DeltaSpike
... closes the gaps!
THE SOFTWARE EXPERTS
Agenda
• History
• DeltaSpike is…
• DeltaSpike by Example
 Core
 JSF Module
 Test-Control Module
• Portability
• Module Overview
• Bonus
THE SOFTWARE EXPERTS
History
Java EE
without CDI
Spring
Framework
Seam2
MyFaces
Orchestra
MyFaces CODI
Java EE
with CDI
Seam3
+ CDI implementation
DeltaSpike
Other
Extensions
THE SOFTWARE EXPERTS
DeltaSpike is...
• Portable CDI-Extension
(for Apache OpenWebBeans and JBoss Weld)
• Possible base for own CDI-Extensions
• Source of proper usages of CDI (API and SPI)
• Collection of the best concepts of
 Apache MyFaces CODI
 JBoss Seam3
 ...
THE SOFTWARE EXPERTS
Parts of DS
• Core (v0.1+)
• Container-Control (v0.2+)
• Modules
THE SOFTWARE EXPERTS
DS by Example
THE SOFTWARE EXPERTS
DS Core
THE SOFTWARE EXPERTS
Deactivate Beans
• Disable CDI-Bean unconditionally
@Exclude
public class MockMailService
implements MailService { /*...*/ }
• Enable CDI-Bean only for Unit-Tests
@Exclude(exceptIfProjectStage =
ProjectStage.UnitTest.class)
@Alternative
public class TestMailService
implements MailService { /*...*/ }
THE SOFTWARE EXPERTS
Window-Scope - Overview
• Based on the CDI Session-Scope
• A HTTP-Session is optional
 Allows logical windows (for batches,...)
 Simple/r to test
• Needs to get de-/activated explicitly
(autom. handling with the JSF-Module)
• Base for
 Grouped-Conversation-Scope
 View-Access-Scope
THE SOFTWARE EXPERTS
Window-Scope - Usage
• Bean-Definition
@WindowScoped
public class UserHolder
implements Serializable {
private User user;
public void setCurrentUser(User user) {
this.user = user;
}
//...
}
• Usage
@Inject private UserHolder holder;
//...
holder.setCurrentUser(authenticatedUser);
THE SOFTWARE EXPERTS
Highlights (DS Core)
• Scopes
 Window-Scope
 Grouped-Conversation-Scope
 View-Access-Scope
• @Exclude
• Type-safe Project-Stages
• Builders for CDI-Metadata
• Exception-Control
• JMX integration
• Utilities
THE SOFTWARE EXPERTS
DS JSF-Module
THE SOFTWARE EXPERTS
Window-Scope with JSF
• Like a session per browser window/tab
• Pluggable ClientWindow
(+ optional adapter for JSF 2.2+)
• Requires ds:windowId Tag in the page(-template)
(Namespace: http://deltaspike.apache.org/jsf)
THE SOFTWARE EXPERTS
Type-safe View-Config - Overview
• Type-safe configuration
 Re-usable for JSF-Navigation
 Parameters (static or dynamic)
• Highly customizable/extensible
 Custom naming conventions,…
 Custom Metadata,…
• Std. Java  Std. IDE Support
 Autocomplete
 Usages / Code-Navigation
 Refactoring
 Show inheritance hierarchy
THE SOFTWARE EXPERTS
View-Config - Minimal Example
• Definition
public class MyPage
implements ViewConfig {}
• Usage
public Class<? extends ViewConfig>
toNextPage() {
return MyPage.class;
}
/myPage.xhtml
THE SOFTWARE EXPERTS
View-Config with Metadata - 1
• Definition
@View(navigation = REDIRECT)
public interface Pages extends ViewConfig {
interface Admin extends Pages {
class Overview implements Admin {}
}
}
• Usage
public Class<? extends Pages> toNextPage() {
return Pages.Admin.Overview.class;
}
/pages/admin/overview.xhtml?faces-redirect=true
THE SOFTWARE EXPERTS
View-Config with Metadata - 2
• Definition
@Secured(MyAccessDecisionVoter.class)
public interface SecuredPages {}
@View(navigation = REDIRECT)
public interface Pages extends ViewConfig {
interface Admin extends Pages,SecuredPages {
@ViewControllerRef(AdminPage.class)
class Overview implements Admin { }
}
}
THE SOFTWARE EXPERTS
Custom Metadata
• Definition
@ViewMetaData
@interface InfoPage { /*...*/ }
• Usage
@Inject
private ViewConfigResolver vcr;
//...
ViewConfigDescriptor vcd =
vcr.getViewConfigDescriptor(
/*[Class|String]*/);
List<InfoPage> metaDataList =
vcd.getMetaData(InfoPage.class);
THE SOFTWARE EXPERTS
Highlights (JSF Module)
• Type-safe View-Config
• View-Controller concept
• Double-Submit prevention
• Injection for converters and validators
• JSF Scope- and Event-Bridge
• Special integration of other parts provided by DS
(e.g. Security-adapter, Scopes, I18n,...)
• Optional JSF 2.2+ integration/support
THE SOFTWARE EXPERTS
DS Test-Control
THE SOFTWARE EXPERTS
Testing CDI Applications with DS
• Available options
 Manual usage of DS-Core and Container-Control
 JUnit Runner of DS-Test-Control
• Mainly for CDI-Beans
 No full Java EE tests like with Arquillian
 Optional support of EJBs with
Apache TomEE (embedded)
 Optional integration with Apache MyFaces-Test
(e.g. to test JSF Page-Beans manually)
THE SOFTWARE EXPERTS
Inject CDI-Beans in Tests - Setup
• Add OpenWebBeans or Weld
• Add DeltaSpike (at least Core)
• Add the test-dependencies
 JUnit
 DeltaSpike Container-Control
 DeltaSpike Test-Control
THE SOFTWARE EXPERTS
Inject CDI-Beans in Tests - Usage
• Use the Test-Runner to write a test
@RunWith(CdiTestRunner.class)
public class SimpleTest {
@Inject
private QuestionOfLifeBean bean;
@Test
public void ultimateAnswer() {
assertEquals(42, bean.getAnswer());
}
}
THE SOFTWARE EXPERTS
Page-Bean Tests - Setup
• Add MyFaces-Core and MyFaces-Test
• Add one of the adapters to the configuration
(META-INFservicesorg.apache.deltaspike.testcontrol.spi.ExternalContainer)
THE SOFTWARE EXPERTS
Page-Bean Tests - Usage
• Use the JSF-API in a test
@Test
public void registerUser() {
FacesContext fc =
FacesContext.getCurrentInstance();
assertTrue(
fc.getMessageList().isEmpty());
//...
assertEquals(Pages.User.Login.class,
this.registrationPage.register());
assertFalse(
fc.getMessageList().isEmpty());
//...
}
THE SOFTWARE EXPERTS
Tests with Context-Control
• No additional Setup
• Inject ContextControl
• Control scopes specified by the CDI specification
THE SOFTWARE EXPERTS
Tests with Scope-Control - Usage
• Simulate multiple Requests/... per Test-Method
@Test
public void registerUser() {
//...
assertFalse(
fc.getMessageList().isEmpty());
this.contextControl.stopContexts();
this.contextControl.startContexts();
assertTrue(
fc.getMessageList().isEmpty());
//...
}
THE SOFTWARE EXPERTS
Tests with DeltaSpike Scopes
• No additional Setup
• Context-implementations provide Management-API
• Examples
 @WindowScoped
 WindowContext
 @GroupedConversationScoped
 GroupedConversationManager
THE SOFTWARE EXPERTS
Window Aware Tests - Usage
• Control the Window
@Test
public void registerUserAndLogin() {
this.windowContext.activateWindow("w1");
//...
this.registrationPage.register();
//...
this.contextControl.stopContexts();
this.contextControl.startContexts();
this.windowContext.activateWindow("w1");
//...
this.registrationPage.login();
//...
}
THE SOFTWARE EXPERTS
Tests with Mocks for CDI-Beans
• Only needed if a single Test-Bean isn’t possible/useful
(via @Specializes or @Alternative)
• Mocking framework is optional
( additional Setup depends on it)
• Register custom (mock-)instances
 Inject ApplicationMockManager
for application scoped mocks
 Inject DynamicMockManager
for mocks per request ( Test-Method per default)
THE SOFTWARE EXPERTS
Tests with Mocks - Usage
• Register mock-instance created via Mockito
@Test
public void registerUserWithMockedBean() {
UserRepository mocked =
mock(UserRepository.class);
when(mockedRepository.findUser("gp"))
.thenReturn(new User(/*...*/));
this.dynamicMockManager.addMock(mocked);
//...
this.registrationPage.register();
assertEquals("gp",
this.userRepository.findUser("gp")
.getUserName());
}
THE SOFTWARE EXPERTS
Advantages of DS Test-Control
• Easy to use and fast
(no additional container-/deployment-configurations)
• Testing the whole application
(no "Micro-Deployments" like with Arquillian)
• Integration with DS Project-Stages
(see the optional @TestControl Annotation)
• Allows to (re-)use mocks
(and mocking frameworks like Mockito)
THE SOFTWARE EXPERTS
Disadvantages of DS Test-Control
• No deployment to the Target-EE-Server
• Mainly for CDI
• EJB-Support just with TomEE embedded
(@Transactional of DeltaSpike
might be easier for tests)
THE SOFTWARE EXPERTS
Tested Portability
THE SOFTWARE EXPERTS
Tested Portability - 1
• CDI-Implementations
 Apache OpenWebBeans (>= 1.1.5)
 JBoss Weld (>= 1.1.9)
• EE6+ Servers
 Apache TomEE 1.6.0+
 JBoss AS7 und WildFly8
 Oracle WebLogic 12.1.3+
 IBM WebSphere 8+
THE SOFTWARE EXPERTS
Tested Portability - 2
THE SOFTWARE EXPERTS
DS Module Overview
THE SOFTWARE EXPERTS
DS Modules
• Security (since v0.2+)
• JPA (Transaction) (since v0.3+)
• JSF (since v0.4+)
• Partial-Bean (since v0.4+)
• Bean-Validation (since v0.5+)
• Data (Query) (since v0.5+)
• Servlet (since v0.5+)
• Scheduler (since v0.6+)
• Test-Control (since v0.6+)
THE SOFTWARE EXPERTS
Bonus - Advanced CDI
THE SOFTWARE EXPERTS
Customize CDI-Beans
• CDI-Bootstrapping-Process provides
several useful events
• Creating custom Bean-Metadata manually
can be tricky
THE SOFTWARE EXPERTS
DS helps with Builders for Metadata
• Dynamically change the existing metadata
AnnotatedType<T> annotatedType =
processAnnotatedType.getAnnotatedType();
processAnnotatedType.setAnnotatedType(
new AnnotatedTypeBuilder<T>()
.readFromType(annotatedType)
.addToClass(
new MyAnnotationLiteral())
.create());
THE SOFTWARE EXPERTS
Q&A!?!
THE SOFTWARE EXPERTS
Links
• Apache DeltaSpike
http://deltaspike.apache.org
• Add-ons, Project-Templates,…
https://github.com/os890
• CDI@Work
http://cdiatwork.irian.at
• Professional Support
http://www.irian.at

Apache DeltaSpike

  • 1.
    THE SOFTWARE EXPERTS ApacheDeltaSpike ... closes the gaps!
  • 2.
    THE SOFTWARE EXPERTS Agenda •History • DeltaSpike is… • DeltaSpike by Example  Core  JSF Module  Test-Control Module • Portability • Module Overview • Bonus
  • 3.
    THE SOFTWARE EXPERTS History JavaEE without CDI Spring Framework Seam2 MyFaces Orchestra MyFaces CODI Java EE with CDI Seam3 + CDI implementation DeltaSpike Other Extensions
  • 4.
    THE SOFTWARE EXPERTS DeltaSpikeis... • Portable CDI-Extension (for Apache OpenWebBeans and JBoss Weld) • Possible base for own CDI-Extensions • Source of proper usages of CDI (API and SPI) • Collection of the best concepts of  Apache MyFaces CODI  JBoss Seam3  ...
  • 5.
    THE SOFTWARE EXPERTS Partsof DS • Core (v0.1+) • Container-Control (v0.2+) • Modules
  • 6.
  • 7.
  • 8.
    THE SOFTWARE EXPERTS DeactivateBeans • Disable CDI-Bean unconditionally @Exclude public class MockMailService implements MailService { /*...*/ } • Enable CDI-Bean only for Unit-Tests @Exclude(exceptIfProjectStage = ProjectStage.UnitTest.class) @Alternative public class TestMailService implements MailService { /*...*/ }
  • 9.
    THE SOFTWARE EXPERTS Window-Scope- Overview • Based on the CDI Session-Scope • A HTTP-Session is optional  Allows logical windows (for batches,...)  Simple/r to test • Needs to get de-/activated explicitly (autom. handling with the JSF-Module) • Base for  Grouped-Conversation-Scope  View-Access-Scope
  • 10.
    THE SOFTWARE EXPERTS Window-Scope- Usage • Bean-Definition @WindowScoped public class UserHolder implements Serializable { private User user; public void setCurrentUser(User user) { this.user = user; } //... } • Usage @Inject private UserHolder holder; //... holder.setCurrentUser(authenticatedUser);
  • 11.
    THE SOFTWARE EXPERTS Highlights(DS Core) • Scopes  Window-Scope  Grouped-Conversation-Scope  View-Access-Scope • @Exclude • Type-safe Project-Stages • Builders for CDI-Metadata • Exception-Control • JMX integration • Utilities
  • 12.
  • 13.
    THE SOFTWARE EXPERTS Window-Scopewith JSF • Like a session per browser window/tab • Pluggable ClientWindow (+ optional adapter for JSF 2.2+) • Requires ds:windowId Tag in the page(-template) (Namespace: http://deltaspike.apache.org/jsf)
  • 14.
    THE SOFTWARE EXPERTS Type-safeView-Config - Overview • Type-safe configuration  Re-usable for JSF-Navigation  Parameters (static or dynamic) • Highly customizable/extensible  Custom naming conventions,…  Custom Metadata,… • Std. Java  Std. IDE Support  Autocomplete  Usages / Code-Navigation  Refactoring  Show inheritance hierarchy
  • 15.
    THE SOFTWARE EXPERTS View-Config- Minimal Example • Definition public class MyPage implements ViewConfig {} • Usage public Class<? extends ViewConfig> toNextPage() { return MyPage.class; } /myPage.xhtml
  • 16.
    THE SOFTWARE EXPERTS View-Configwith Metadata - 1 • Definition @View(navigation = REDIRECT) public interface Pages extends ViewConfig { interface Admin extends Pages { class Overview implements Admin {} } } • Usage public Class<? extends Pages> toNextPage() { return Pages.Admin.Overview.class; } /pages/admin/overview.xhtml?faces-redirect=true
  • 17.
    THE SOFTWARE EXPERTS View-Configwith Metadata - 2 • Definition @Secured(MyAccessDecisionVoter.class) public interface SecuredPages {} @View(navigation = REDIRECT) public interface Pages extends ViewConfig { interface Admin extends Pages,SecuredPages { @ViewControllerRef(AdminPage.class) class Overview implements Admin { } } }
  • 18.
    THE SOFTWARE EXPERTS CustomMetadata • Definition @ViewMetaData @interface InfoPage { /*...*/ } • Usage @Inject private ViewConfigResolver vcr; //... ViewConfigDescriptor vcd = vcr.getViewConfigDescriptor( /*[Class|String]*/); List<InfoPage> metaDataList = vcd.getMetaData(InfoPage.class);
  • 19.
    THE SOFTWARE EXPERTS Highlights(JSF Module) • Type-safe View-Config • View-Controller concept • Double-Submit prevention • Injection for converters and validators • JSF Scope- and Event-Bridge • Special integration of other parts provided by DS (e.g. Security-adapter, Scopes, I18n,...) • Optional JSF 2.2+ integration/support
  • 20.
  • 21.
    THE SOFTWARE EXPERTS TestingCDI Applications with DS • Available options  Manual usage of DS-Core and Container-Control  JUnit Runner of DS-Test-Control • Mainly for CDI-Beans  No full Java EE tests like with Arquillian  Optional support of EJBs with Apache TomEE (embedded)  Optional integration with Apache MyFaces-Test (e.g. to test JSF Page-Beans manually)
  • 22.
    THE SOFTWARE EXPERTS InjectCDI-Beans in Tests - Setup • Add OpenWebBeans or Weld • Add DeltaSpike (at least Core) • Add the test-dependencies  JUnit  DeltaSpike Container-Control  DeltaSpike Test-Control
  • 23.
    THE SOFTWARE EXPERTS InjectCDI-Beans in Tests - Usage • Use the Test-Runner to write a test @RunWith(CdiTestRunner.class) public class SimpleTest { @Inject private QuestionOfLifeBean bean; @Test public void ultimateAnswer() { assertEquals(42, bean.getAnswer()); } }
  • 24.
    THE SOFTWARE EXPERTS Page-BeanTests - Setup • Add MyFaces-Core and MyFaces-Test • Add one of the adapters to the configuration (META-INFservicesorg.apache.deltaspike.testcontrol.spi.ExternalContainer)
  • 25.
    THE SOFTWARE EXPERTS Page-BeanTests - Usage • Use the JSF-API in a test @Test public void registerUser() { FacesContext fc = FacesContext.getCurrentInstance(); assertTrue( fc.getMessageList().isEmpty()); //... assertEquals(Pages.User.Login.class, this.registrationPage.register()); assertFalse( fc.getMessageList().isEmpty()); //... }
  • 26.
    THE SOFTWARE EXPERTS Testswith Context-Control • No additional Setup • Inject ContextControl • Control scopes specified by the CDI specification
  • 27.
    THE SOFTWARE EXPERTS Testswith Scope-Control - Usage • Simulate multiple Requests/... per Test-Method @Test public void registerUser() { //... assertFalse( fc.getMessageList().isEmpty()); this.contextControl.stopContexts(); this.contextControl.startContexts(); assertTrue( fc.getMessageList().isEmpty()); //... }
  • 28.
    THE SOFTWARE EXPERTS Testswith DeltaSpike Scopes • No additional Setup • Context-implementations provide Management-API • Examples  @WindowScoped  WindowContext  @GroupedConversationScoped  GroupedConversationManager
  • 29.
    THE SOFTWARE EXPERTS WindowAware Tests - Usage • Control the Window @Test public void registerUserAndLogin() { this.windowContext.activateWindow("w1"); //... this.registrationPage.register(); //... this.contextControl.stopContexts(); this.contextControl.startContexts(); this.windowContext.activateWindow("w1"); //... this.registrationPage.login(); //... }
  • 30.
    THE SOFTWARE EXPERTS Testswith Mocks for CDI-Beans • Only needed if a single Test-Bean isn’t possible/useful (via @Specializes or @Alternative) • Mocking framework is optional ( additional Setup depends on it) • Register custom (mock-)instances  Inject ApplicationMockManager for application scoped mocks  Inject DynamicMockManager for mocks per request ( Test-Method per default)
  • 31.
    THE SOFTWARE EXPERTS Testswith Mocks - Usage • Register mock-instance created via Mockito @Test public void registerUserWithMockedBean() { UserRepository mocked = mock(UserRepository.class); when(mockedRepository.findUser("gp")) .thenReturn(new User(/*...*/)); this.dynamicMockManager.addMock(mocked); //... this.registrationPage.register(); assertEquals("gp", this.userRepository.findUser("gp") .getUserName()); }
  • 32.
    THE SOFTWARE EXPERTS Advantagesof DS Test-Control • Easy to use and fast (no additional container-/deployment-configurations) • Testing the whole application (no "Micro-Deployments" like with Arquillian) • Integration with DS Project-Stages (see the optional @TestControl Annotation) • Allows to (re-)use mocks (and mocking frameworks like Mockito)
  • 33.
    THE SOFTWARE EXPERTS Disadvantagesof DS Test-Control • No deployment to the Target-EE-Server • Mainly for CDI • EJB-Support just with TomEE embedded (@Transactional of DeltaSpike might be easier for tests)
  • 34.
  • 35.
    THE SOFTWARE EXPERTS TestedPortability - 1 • CDI-Implementations  Apache OpenWebBeans (>= 1.1.5)  JBoss Weld (>= 1.1.9) • EE6+ Servers  Apache TomEE 1.6.0+  JBoss AS7 und WildFly8  Oracle WebLogic 12.1.3+  IBM WebSphere 8+
  • 36.
  • 37.
    THE SOFTWARE EXPERTS DSModule Overview
  • 38.
    THE SOFTWARE EXPERTS DSModules • Security (since v0.2+) • JPA (Transaction) (since v0.3+) • JSF (since v0.4+) • Partial-Bean (since v0.4+) • Bean-Validation (since v0.5+) • Data (Query) (since v0.5+) • Servlet (since v0.5+) • Scheduler (since v0.6+) • Test-Control (since v0.6+)
  • 39.
  • 40.
    THE SOFTWARE EXPERTS CustomizeCDI-Beans • CDI-Bootstrapping-Process provides several useful events • Creating custom Bean-Metadata manually can be tricky
  • 41.
    THE SOFTWARE EXPERTS DShelps with Builders for Metadata • Dynamically change the existing metadata AnnotatedType<T> annotatedType = processAnnotatedType.getAnnotatedType(); processAnnotatedType.setAnnotatedType( new AnnotatedTypeBuilder<T>() .readFromType(annotatedType) .addToClass( new MyAnnotationLiteral()) .create());
  • 42.
  • 43.
    THE SOFTWARE EXPERTS Links •Apache DeltaSpike http://deltaspike.apache.org • Add-ons, Project-Templates,… https://github.com/os890 • CDI@Work http://cdiatwork.irian.at • Professional Support http://www.irian.at