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
Java EE
without CDI
Spring
Framework
Seam2
MyFaces
Orchestra
MyFaces CODI
Java EE
with CDI
Seam3
+ CDI implementation
DeltaSpike
Other
Extensions
4. 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
...
8. 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 { /*...*/ }
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);
13. 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)
14. 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
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-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
17. 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 { }
}
}
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
21. 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)
22. 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
23. 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());
}
}
24. 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)
25. 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());
//...
}
26. THE SOFTWARE EXPERTS
Tests with Context-Control
• No additional Setup
• Inject ContextControl
• Control scopes specified by the CDI specification
27. 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());
//...
}
28. THE SOFTWARE EXPERTS
Tests with DeltaSpike Scopes
• No additional Setup
• Context-implementations provide Management-API
• Examples
@WindowScoped
WindowContext
@GroupedConversationScoped
GroupedConversationManager
29. 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();
//...
}
30. 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)
31. 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());
}
32. 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)
33. 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)
40. THE SOFTWARE EXPERTS
Customize CDI-Beans
• CDI-Bootstrapping-Process provides
several useful events
• Creating custom Bean-Metadata manually
can be tricky
41. 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());