Joining Apache MyFaces Development Team

Last week on friday, the voting about my nomination is completed and I’ve joined the development team of Apache MyFaces.  Actually I was looking forward to it for some time after working on the patches for jira issues, client side validation and etc. I must say it feels great to be in the team and be an ASF committer. So what is next? I’m planning to work on the JSF1.2. implementation and finish this ongoing client side validation stuff. Also working on the the jira issues for sure. More sleepless nights seem to be ahead and I’ll continue doing what a night developer does.

Posted in Java. 6 Comments »

Testing JavaScript with JUnit

As a test-infected developer, I don’t know how I missed this cool project called JsTester. Matthias pointed me to the project’s homepage and after taking a quick look I’m really impressed. The usage seems easy, after loading the js file, testing can be done using the well known assertions like assertTrue, assertNull and etc. It uses rhino engine by the way.

All of these are the good sides, but I’ve also observed two main weak points, one is the lacking of documentations and other is the testing way. When writing the junit test, one must include an object of type JsTester to the test, also call jsTester’s setup and tear down methods explicitly. It could be much easier if there is a TestCase like AbstractJSTestCase which does this init stuff and more. Also give access to the jsTester for the test which implement it. Anyway the project is very new and I hope Andres Almiray will have time to improve it.

Posted in Java. 2 Comments »

PHP Error: Operation completed successfully

I don’t care much about php actually. My only encounter with php was in university, the assistant of a course assigned a homework about creating a simple dvd shop kinda web application with php. Anyway I found it to be too primitive and never used php again. There were many php fans at that time, still too.

Me and my main man Mert were trying to build an information base for our project we are currently working for. After setting up the wiki, we moved on with installing a forum and bind it to our exhange server’s mailing infrastructure. First thing we’ve done is to install apache server, than mysql and than the php stuff. The installation of forum went ok with phpmyadmin stuff and only thing remaining was to integrate mailing list to the forum. Phpbb forum does not ship with a built-in mailing list integration but there is a plugin for it called mail2forum(m2f). By the way I really liked the pluggable structure of phpbb, you can install new plugins(mods in php world) easily, there is also a third party mod installer for phpbb. But still I don’t care about php.

Long story short, this “error” was displayed after installing the mod installer called easymod. Check out this stupid error message from the screenshot below:)

I guess the meaning of success is not enough in php. An operation must be completed “perfect“, successfull or good is not enough in php world. I also think the guy who wrote this is a perfectionist, good thing in software development.

Posted in Java. 5 Comments »

Easy Select Items for JSF

In order to use selection components like combos or listboxes, SelectItem objects are needed in JSF. There are two tags for the job in the spec. f:selectItem and f:selectItems. The singular one has itemLabel and itemValue attributes which also accepts value bindings other than static texts. The main problem is with the f:selectItems because it only gets a value attribute and does not have the itemLabel-itemValue attributes. This causes two problems.

* To display a collection in a page, you have to create a SelectItem collection from your data. Suppose you have a collection of customer objects and want to display the name-surname attributes as key-value in a combobox. The bad thing is that you need to iterate over the collection and create a new collection of SelectItem objects and return it to the component.
* By doing so, the domain model is coupled with the faces api.

Because of these two disadvantages, me and my buddy Mert created an extended version: s:selectItems. It has three new attributes; var, itemLabel and itemValue. By using this, the component automatically creates the Select Items collection from the given data. I dont think many people care about ibm’s jsf but it has a custom resolver to create the selectitems. Anyway I’ve deployed the example webapplication to my homepage at;

www.cagataycivici.com/easysi

The component is also committed to Apache MyFaces Sandbox project by Martin and I believe it’s going to be promoted to tomahawk soon. Also an interesting thing occurred the day component joined sandbox, a user in myfaces list send a message saying the selectitems stuff is problematic in JSF, creating them takes time&effort and asked if an easier way exists in myfaces. Of course he is pointed to our new component in sandbox afterwards, seems the component joined at the right time. If you are not a myfaces user and want to use it anyway, check out the release at jsf-comp. I’ve also written a documentation there.

Posted in Java. 7 Comments »

Testing JSF without the Container

Thanks to the AbstractJsfTestCase, Test Driven JSF Development has become my favorite hobby nowadays. At past before Shale, we’ve created our own static mock library containing 4-5 mock implementations of classes like FacesContext, Application in our project. But Shale Test Framework is almost an actual mock implementation of the JSF spec. I’ve first used it when I was writing the tests of the extended UISelectItems of MyFaces(EasySI) and absolutely fascinated by the features. It basically gives you everything you need. In order to demonstrate features I’ve written a custom JSF component as an example and test it with the test framework.

The component’s name is InputDate and it’s a simple input text that can be used for binding dates. It only accepts the “dd.MM.yyyy” type of pattern and if the entered date is after today’s date, mark’s itself at invalid. This is just a simple example, of course there are fancy date components like InputCalendar, InputDate(I’ve fixed the forceId issue by the way) in MyFaces for actual usage.

  
    
    

package inputdate;

import java.io.IOException;
import java.util.Date;

import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

public class InputDate extends UIInput {

  public InputDate() {
    super();
    setRendererType(null);
  }

  public void decode(FacesContext context) {
    String clientId = getClientId(context);
    String submittedValue = (Stringcontext.getExternalContext().getRequestParameterMap().get(clientId);
    setSubmittedValue(submittedValue);
    setValid(true);
  }

  public void validate(FacesContext context) {
    super.validate(context);
    if (getLocalValue() != null) {
      Date date = (DategetLocalValue();
      Date now = new Date();
      if (now.compareTo(date0)
        setValid(false);
    }
  }

  public void encodeEnd(FacesContext contextthrows IOException {
    ResponseWriter writer = context.getResponseWriter();
    writer.startElement(“input”this);
    writer.writeAttribute(“id”, getClientId(context)“id”);
    writer.writeAttribute(“name”, getClientId(context)null);
    if (getValue() != null)
      writer.writeAttribute(“value”, getConverter().getAsString(          this, getValue())“value”);
    else
      writer.writeAttribute(“value”“”“value”);

    writer.endElement(“input”);
  }

}      

As you see it’s quite simple, no renderer, just renders an input text and only used for dates. In a page it can be used like;

 <barca:inputDate id=”inputDate1″ value=”#{SomeBean.birthDate}”>
                    <f:convertDateTime pattern=”dd.MM.yyyy”/>
   </barca:inputDate>

Here comes the fun part, the tester;

  
    
    

package inputdate.test;

import javax.faces.component.UIForm;
import javax.faces.component.html.HtmlForm;
import javax.faces.convert.DateTimeConverter;
import javax.faces.el.ValueBinding;

import inputdate.InputDate;
import inputdate.SomeBean;
import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.shale.test.base.AbstractJsfTestCase;

public class InputDateTester extends AbstractJsfTestCase {

  private InputDate inputDate;
  
  public InputDateTester(String testname) {
    super(testname);
  }
  
  public void setUp() {
    super.setUp();
    
    //create the view
    UIForm parent = new HtmlForm();
    parent.setId(“form1″);
    parent.setParent(facesContext.getViewRoot());
    inputDate = new InputDate();
    inputDate.setId(“inputDate1″);
    inputDate.setParent(parent);
    
    //set the converter
    DateTimeConverter converter = new DateTimeConverter();
    converter.setPattern(“dd.MM.yyyy”);
    inputDate.setConverter(converter);
    
    //set the value binding
    ValueBinding valueBinding = facesContext.getApplication().createValueBinding(“#{SomeBean.birthDate}”);
    inputDate.setValueBinding(“value”, valueBinding);
    facesContext.getExternalContext().getRequestMap().put(“SomeBean”new SomeBean());
  }
  
  public static Test suite() {
      return new TestSuite(InputDateTester.class);
  }
  
  public void tearDown() {
    inputDate = null;
      super.tearDown();
  }
  
  public void testSubmittedValueMustBeEqualToThePostedData() {
    facesContext.getExternalContext().getRequestParameterMap().put(“form1:inputDate1″“15.07.2000″);
    inputDate.processDecodes(facesContext);
    assertEquals(inputDate.getSubmittedValue()“15.07.2000″);
    assertNull(inputDate.getLocalValue());      //no conversion-validation yet
  }
  
  public void testConversionErrorMustOccurWhenTheInputIsNotInStatedFormat() {
    facesContext.getExternalContext().getRequestParameterMap().put(“form1:inputDate1″“ineedavacation”);
    inputDate.processDecodes(facesContext);
    inputDate.processValidators(facesContext);
    assertFalse(inputDate.isValid());
  }
  
  public void testWhenImmediateIsTrueConversionValidationTakesPlaceAtDecode() {
    facesContext.getExternalContext().getRequestParameterMap().put(“form1:inputDate1″“15.07.2000″);
    inputDate.setImmediate(true);
    inputDate.processDecodes(facesContext);      //conversion-validation
    assertNotNull(inputDate.getLocalValue());
  }
  
  public void testWhenTheEnteredDateIsLaterThanTodayValidationErrorMustOccur() {
    facesContext.getExternalContext().getRequestParameterMap().put(“form1:inputDate1″“15.07.2010″);
    inputDate.processDecodes(facesContext);
    inputDate.processValidators(facesContext);
    assertFalse(inputDate.isValid());
  }
  
  public void testSomeBeansBirthDateAttributeIsUpdatedAtUpdateModel() {
    facesContext.getExternalContext().getRequestParameterMap().put(“form1:inputDate1″“15.07.2000″);
    inputDate.processDecodes(facesContext);
    inputDate.processValidators(facesContext);
    inputDate.processUpdates(facesContext);
  &
nbsp; 
SomeBean someBean = (SomeBean)facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, “SomeBean”);
    assertNotNull(someBean.getBirthDate());
  }
}      

Extending from the AbstractJsfTestCase, everything I need(FacesContext, Application, External Context, RequestMap etc.) is prepared before beginning the testing. On setup I setup a component tree is created and set a converter to the inputDate. Also a valuebinding for the SomeBean’s birth date variable. The tests are very useful to see how the component will act during request lifecycle and in a real container environment. In order to imitate it, I’ve tried to cover the component’s behaviours in different phases and cases. The phase I’ve not covered is the render response which means the output of the component. Well, first it does not suit the unit testing idea well and does’nt seem to be possible for now. The workaround is to use a library like htmlunit and run the component in a real container. Using htmlunit you can extract the output of the component from DOM and compare it with the expected output.

There is also another AbstractJsfTestCase in Shale-Test Framework, my main man Matthias has worked on it and added the JMock support to the library. My example uses the static mocks, to learn more about the jmock support in the framework, check out his cool example. Since converters and validators are interfaces they suit well to jmock. I’ve also used jmock to test the infrastructure(making managed beans aware of the jsf cycle) I’ve created and added to the project which I’m currently working for.

AbstractJsfTestCase reminds me the Abstract***SpringContextTests in Spring, the whole idea is speeding-up the web application development and gain time. I think any web framework should create it’s own TestCase in order to enable “Testing Without The Container”. Viva la AbstractJsfTestCase!

Posted in Java. 5 Comments »

JSF Datatable with Custom Paging for Large Datasets

When using a datatable, the general approach is to fetch all the data, bind it to the datatable and let the component do it’s job. This is generally ok when you are working with small sized datasets but what if there are hundereds, thousands or even more amount of data waiting to be displayed. This is likely to lead memory problems and not a good practice. The worse thing happened happened some time ago in our project; sometimes hibernate proxies throw lazy initialization exceptions when a paging occurs. The good news is that after working some time on the problem, I’ve figured out a solution with javax.faces.DataModel.

Actually a component extending from uidata like DataTable interacts with it’s data through an adapter class; DataModel. This class wraps the actual collection for example, if a list is bound to a datatable then a ListDataModel is created and the component use this ListDataModel to access it’s data. DataModel provides methods like getWrappedData, isRowAvailable, getRowData, getRowCount letting the component access the wrapped data the way datamodel determines. To give an example, In case the wrapped data is a list, getRowCount returns list.size().

The solution to the large data problem is to use a custom data model and show only the data page allowed by the page size of the datatable. Whenever a paging occurs, the data of the next page is fetched and displayed, by this way the whole data is never read, the idea is to load only the page size of data when needed. There are two key attributes of a datatable, “first” and “rows”, first refers to the starting element and rows refers to the page size to be displayed. So when the first is 10 and rows is 5, then data between 10 and 15 is displayed. A pager simply sets the first attribute and the rows elements of the datatable, and then datatable displays it’s data considering these attributes. Also when the pages defines the number of pages, it looks at the datatable’s getRowCount method. Following is the PagedDataModel class providing the solution based on these informations.

  
    
    

package forca.barca;

import java.util.List;
import javax.faces.model.DataModel;

public class PagedListDataModel extends DataModel{

  private int rowIndex = -1;
  
  private int totalNumRows;
  
  private int pageSize;
  
  private List list;
  
  public PagedListDataModel() {
    super();
  }
  
  public PagedListDataModel(List list, int totalNumRows, int pageSize) {
    super();
    setWrappedData(list);
    this.totalNumRows = totalNumRows;
    this.pageSize = pageSize;
  }
  
  public boolean isRowAvailable() {
    if(list == null)
      return false;
    
    int rowIndex = getRowIndex();
    if(rowIndex >=&& rowIndex < list.size())
      return true;
    else
      return false;
  }

  public int getRowCount() {
    return totalNumRows;
  }

  public Object getRowData() {
    if(list == null)
      return null;
    else if(!isRowAvailable())
      throw new IllegalArgumentException();
    else {
      int dataIndex = getRowIndex();
      return list.get(dataIndex);
    }
  }

  public int getRowIndex() {
    return (rowIndex % pageSize);
  }

  public void setRowIndex(int rowIndex) {
    this.rowIndex = rowIndex;
  }

  public Object getWrappedData() {
    return list;
  }
  
  public void setWrappedData(Object list) {
    this.list = (Listlist;
  }

}      

The key point is to fool the pager by returning the total list size as the row count. Pager components use this value when rendering themselves, for example a simple pager divides this number to the page size and use the result to render the page numbers. Other important thing is always return the mod(rowIndex) as the rowIndex. Let’s say we have a datatable with page sizes:10. The rendering algorithm of a datatable initially gets the first element and sets it as the rowIndex and then renders the data until the rowIndex <= pagesize. When the user wants to see the next page, since pager sets the first attribute of the datatable as 10, the rowIndex is initially set to 10. Problem occurs here, since we use custom paging and load only 10 blocks of data (0-9), the 10th element is null and isRowAvailable returns false. In order to hack it, whenever the rowIndex is needed, the (rowIndex mod(page)) is returned. This means when the 10th element is needed 10 mod(10) = 0 is returned. Similarly referring to 15th element returns 5th element in the list.

In order to the custom paging at the view layer, we need features to do the same thing at business layer. I’m going to present a way with Hibernate Criteria API, also Query API should do the job. If you are not using spring and hibernate there are other options to fetch paged data like oracle’s rownum. Anyway the method below is located at an Hibernate DAO class and accessed via Spring beans. Also there is another one to return only the size of the actual data.

 
    
    

public List getPagedData(SomeCriteriaObject someObject, int start, int page) {
  try {
    Criteria criteria = getSession().createCriteria(ClassToBeQueried.class);
    //Build Criteria object here
    criteria.setFirstResult(start);
    criteria.setMaxResults(page);
    return criteria.list();
  catch (HibernateException hibernateException) {
    //do something here with the exception
  }
}      
  
    
    

  public int getDataCount(SomeCriteriaObject someObject) {
    Criteria criteria = getSession().createCriteria(ClassToBeQueried.class);
    criteria.setProjection(Projections.rowCount());

    // Build Criteria object here
    Number nuofRecords = ((Numbercriteria.uniqueResult());
    return nuofRecords == null : nuofRecords.intValue();
  }      

Since all the stuff is ready to do custom paging, how to enable it? The first thing is to bind the custom data model to the datatable component as the value.

<h:datatable id=”table1″ value=”#{didYouSeeZidanesHeaderToMaterazzi.myPagedDataModel}“>



</h:datatable>

Finally the last job is to create a PagedDataModel and return it in the getter as;

    
    

public DataModel getMyPagedDataModel() {
  int totalListSize = getSomeBusinessService().getDataCount(getSomeCriteriaObject());
  List pagedList = getSomeBusinessService().getPagedData(getSomeCriteriaObject(), getTable1().getFirst(), getTable1().getRows());
  PagedDataModel dataModel = new PagedDataModel(pagedList, tatalListSize, getTable1().getRows());
  return dataModel;
}      

The SomeBusinessService is not important and just a business bean providing access to the hibernate daos, also the somecriteriaobject is a simple bean whose members are bound to the page components used when building the criteria object. As I mentioned these are the stuff I’ve used to do custom paging at business level, you can replace it with your own stuff. The important idea is to use a custom data model to enable custom paging at view layer.

Posted in Java. 25 Comments »

Unit Testing JSF with Mock Objects

I’ve seen some discussions on the unit testing business with JSF and questions are mainly focused on the testing of managed beans, converters, validators and other stuff. Most of the business logic is generally located in managed beans and since these beans are actually ordinary java POJOs, I dont think it is too hard to figure out the way for testing. The tricky parts may be the ones where JSF stuff like FacesContext is involved.

In one of my previous entries, I’ve shown a way to make the backing beans of the pages aware of the JSF lifecycle. Each backing bean implementing ILifeCycleAware can execute it’s code at several phases of the lifecycle like onPageLoad, onPreValidations and etc. In summary the idea is simple, from the view id, say index.jsp, the managed bean name, Index is extracted and casted to ILifeCycleAware, later phaselisteners execute the methods like onPageLoad and etc. Refer to my old post to learn more about the idea and going further with the testing of it.

In order to provide more features like these to the developers in my project, I’ve extended this solution to provide two extra methods; onPreValidations and onPreRender. After adding these two methods to the ILifeCycleAware interface, I’ve written the tests of this functionality. The testing scenario and the environment may seem tricky to create since there are phaselisteners, phaseevents, a request for a page, facescontext and other stuff involved but in the end it is so fun and easy to test these buddies with mocks. Here is the tester class;

  
    
    

public class LifeCycleListenerTester extends MockObjectTestCase{

    private PageLoadPhaseListener pageLoadPhaseListener;
    
    private PreValidationsPhaseListener preValidationsPhaseListener;
    
    private PreRenderPhaseListener preRenderPhaseListener;
    
    Mock mockManagedBean;      //dynamic mock
    
    MockFacesContext facesContext;  //static mock
    
    protected void setUp() {
      pageLoadPhaseListener = new PageLoadPhaseListener();
      preValidationsPhaseListener = new PreValidationsPhaseListener();
      preRenderPhaseListener = new PreRenderPhaseListener();
      
      mockManagedBean = new Mock(ILifeCycleAware.class);
      Object proxyManagedBean = mockManagedBean.proxy();
      
      pageLoadPhaseListener.setManagedBean(proxyManagedBean);
      preValidationsPhaseListener.setManagedBean(proxyManagedBean);
      preRenderPhaseListener.setManagedBean(proxyManagedBean);
      facesContext = new MockFacesContext();
      UIViewRoot viewRoot = new UIViewRoot();
      viewRoot.setViewId(“/darthmaul.jsp”);
      facesContext.setViewRoot(viewRoot);
    }
    
    public void testOnLoadEventIsCalledAfterRestoreView() {
      PhaseEvent phaseEvent = new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW, new LifecycleImpl());
      mockManagedBean.expects(once()).method(“onPageLoad”);
      pageLoadPhaseListener.afterPhase(phaseEvent);
    }
    
    public void testPreValidationsEventIsCalledBeforeValidations() {
      PhaseEvent phaseEvent = new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS, new LifecycleImpl());
      mockManagedBean.expects(once()).method(“onPreValidations”);
      preValidationsPhaseListener.beforePhase(phaseEvent);
    }
    
    public void testPreRenderEventIsCalledBeforeRenderResponse() {
      PhaseEvent phaseEvent = new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE, new LifecycleImpl());
      mockManagedBean.expects(once()).method(“onPreRender”);
      preRenderPhaseListener.beforePhase(phaseEvent);
    }
  }     

There are two mock objects here, the dynamic one is the managed bean implementing the ILifeCycleAware bean. I  set an expectation on it to test whether the life cycle methods are called or not. MockFacesContext is a static mock class and used whenever a faces context instance is needed during testing. In this case it is used setting a dummy viewroot with a view id and creating a phaseevent for a phaselistener. Shale has also filled this gap in JSF testing and provided a static mock library for classes like FacesContext, ViewHandler, ExternalContext and etc. Well, there was no Shale when the project I’m working for started so we’ve created our own static mock library. But now I’d to use the shale’s library containing the mock implementations for testing purposes instead of creating my own.

This was just an example to show how testing could be done with JSF, with the Mock cousins of the real JSF stuff you can easily create the testing scenarios where your phaselisteners, converters, validators, managed beans, resolvers, view  handlers, navigation handlers and anything else handlers face with.

Posted in Java. 1 Comment »

Aspect Oriented Audit Logging with Spring and Hibernate

Audit logging is one of the important concerns in huge applications and usually treated as some kind of a non-functional requirement. I mean a software may work with or without an audit logging infrastructure, the thing is when it plays an important role in a software, design problems may likely to occur because it does not seem to be easy to change the design of the software to enable audit logging at first glance.

In our project, there is a layered architecture where spring is located at business/service layer and hibernate at DAO layer. At first this architecture was not designed considering stuff like audit logging or security. But the good news is that, since these are cross-cutting concerns, they can be easily integrated in an Aspect Oriented Way. Audit Logging could also be solved for simple systems using database triggers in an old school fashion but  I can’t imagine how hard it could be to create and maintain it. Master Kenan created a high level base infrastructure to enable audit logging and I’ve worked on it for some time to make it ready for integration to our project modules.  The ingridients are the following;

* IAuditable: Marker interface which domain object must implement.
* AuditLogger: Spring bean to do the audit logging job.
* ServiceAuditLogInterceptor: Spring’s MethodInterceptor that intercepts the calls to service beans.
* EntityAuditLogInterceptor: Hibernate’s interceptor to intercept the transactions.
* Commons attributes: Attributes to define the audit log types for service methods. JAVA5 annotations should do the job too.
* AuditLogRecord: A persistent object to hold the audit logs. It has types like service_level_save, service_level_update, entity_level_save and etc.

An audit logging story is enabled using the commons attributes defined for the interfaces which are implemented by spring beans;

    public interface ISomeBean {  
    /**
     * @@org.springframework.transaction.interceptor.DefaultTransactionAttribute (propagationBehaviorName=”PROPAGATION_REQUIRED”)
     * @@ineeda.vacation.AuditLogAttribute (ineeda.vacation.now.domain.AuditLogType.SERVICE_LEVEL_SAVE) 
     */
    public void saveSomethingToDB(DomainObject forcaBarca);
  }
      

This is how it works;
1) ServiceAuditLogInterceptor intercepts the method call and checks for the audit log commons attributes, if it finds any, creates a service level audit log record to the AuditLogger bean.
2) When the call reaches the below layers which are hibernate dao objects, EntityAuditLogInterceptor takes the scene. By overriding methods like onSave, onFlushDirty, it adds entity level audit logs to the service level audit log record hold in the audit logger for this transaction.
3) When the flush method of EntityInterceptor is called, audit logger flushes itself by calling hibernate templates save method to insert the audit log records it holds.

Important note:
Since audit logger makes a db call using hibernate, it needs to use a seperate session factory configured in application context. If not, it will start intercepting it’s own calls and cause infinite loop. Also you need to configure the interceptors in Spring. ServiceAuditLogInterceptor can be configured to intercept the calls to the beans ending with *Service implicitly by adding it to the interceptor names property of your auto proxy creator. The other one, EntityAuditLogInterceptor is integrated by configuring the entityInterceptor property of a session factory bean.

One of the best sides of this architecture is, it is so easy to add new audit log types, like user logins, db functions, changing passwords and more you can think of. I’ve also build pages with JSF to search the audit log records in the system criterias like date, user, log types and etc. Using it we can figure out the finger prints of each user from the system tracking module. Anyway, if you want to learn more, Kenan Sevindik will post an entry at his blog and present the whole thing with codes of these ingridients to describe the audit logging infrastructure in detail.

Posted in Java. 5 Comments »