Thursday, November 17, 2011

Stubs vs Mocks


A stub is a piece of code that’s inserted at runtime in place of the real code, in order to isolate the caller from the real implementation. The intent is to replace a complex behavior with a simpler one that allows independent testing of some part of the real code.

Mocks replace real objects from the inside, without the calling classes being aware of it; They are objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
The most important point to consider when writing a mock is that it shouldn’t have any business logic. It must be a dumb object that does only what the test tells it to do. It’s driven purely by the tests. This characteristic is exactly the opposite of stubs, which contain all the logic.

http://martinfowler.com/articles/mocksArentStubs.html

Monday, November 14, 2011

Aspect Oriented Programming (AOP)

Enables modularization of cross-cutting concerns (to avoid tangling and to eliminate scattering)

Concepts:

1. Advice - The actual work AOP will perform on a given object (around, before, after)
2. Join Point - places within your code that are eligible for advice; method execution
3. Pointcut - description of the joinpoint you want to advise; you list the criteria of the joinpoint you are interested in advising here. Eg. execution(public * *(..))
4. Aspect - functionality you actually want to add to an object (e.g. logging, security)

Aspects use Pointcuts to find Joinpoints to give Advice to ...



How AOP works?

AOP works by separating common concerns across the layers in separate classes and names them Aspect.
Aspects have APIs which aim to solve a common problem, like logging, and exception handling.
These are then invoked while intercepting the actual method invocation. Such APIs are termed Advice.
Advices are interceptors, which intercept original method invocation and wrap it around with handling of cross cutting concerns.
Now, for applying Advices to methods, AOP provides artifacts named JoinPoint.
Using a JoinPoint we could defne, which API invocation would be wrapped around by what Advice.

Weaving is a mechanism by which the aspects are applied to the designated classes and methods.
Weaving is generally done by generating byte code for the interceptors, using various libraries for byte code generation libraries, such as JavaAssist, BCEL, and ASM.
There are three places, any of which could be used to generate the bytecode. These are while compiling the code (Compile Time Weaving), loading the application known as (Load Time Weaving) and (Runtime Time Weaving) while actual method invocation is running.


Ref:
Spring doc
Learning Google Guice

Tuesday, November 8, 2011

Clearing JSF Form

On page you have form with input fields and button that can clear that fields.
Button is set to immediate since there is no need to do PROCESS_VALIDATIONS and UPDATE_MODEL_VALUES phases,
instead method is invoked in APPLY_REQUEST_VALUES phase.
First method can be used in JSF 1.x or 2.x, but second is based on AJAX event and is usable only in JSF 2.x
Better approach is with AJAX since there is no need to refresh whole page.

<h:commandButton value="Clear" id="clearButton">
<f:ajax event="click" render="@form" listener="#{myController.clearForm}" immediate="true" />
</h:commandButton>

public void clearForm(ActionEvent ae)
{
System.out.println("clearForm - via ActionEvent");
// clear bean values ...

// clear component values
UIComponent form = getContainingForm(ae.getComponent());
clearEditableValueHolders(form);
}

public void clearForm(AjaxBehaviorEvent ae)
{
System.out.println("clearForm - via AjaxBehaviorEvent");
// clear bean values ...

// clear component values
UIComponent form = getContainingForm(ae.getComponent());
clearEditableValueHolders(form);
}

private UIComponent getContainingForm(UIComponent component)
{
if (!(component.getParent() instanceof UIForm))
{
return getContainingForm(component.getParent());
}
else
{
System.out.println("getContainingForm: UIForm found");
return component.getParent();
}
}

private void clearEditableValueHolders(UIComponent form)
{
Iterator<UIComponent> iterator = form.getFacetsAndChildren();
while (iterator.hasNext())
{
UIComponent c = iterator.next();
if (c instanceof EditableValueHolder)
{
// Convenience method to reset this component's value to the un-initialized state.
((EditableValueHolder) c).resetValue();
}
clearEditableValueHolders(c);
}
}