Friday, April 18, 2008

JQuery - Web 2.0 framework

Jquery opens up a new way of thinking in javascript programming. It is something really an abstraction in implementing old javascript ideas. Definitely, it is worth trying if you want to develop components for Web 2.0 based application.

When you try to implement some new rich user functionalities with rudimentary, it is time consuming and error-prone. Imagine that if you want to write a effects such as fading out a html element using ordinary javascript, it will take time and you should be little expert to do that. I mean you require some experience.Initially; you need to understand how dhtml is going to work Not only it is difficult for novice programmers; it is time consuming when we debug it. So this framework comes to rescue any experienced as well as novice javascript programmers to think in JQuery way. Initially it is bit intimidating to understand what this stuff is, but gradually it is really useful to useful in adapting this framwork.OK.Enough with the story

So anybody has thought that the Javascript is really stupid, and then it is right time to change that notion. Let us look into some of its concepts.

Jquery supports objected-oriented programming. And also, it supports Chaining. Chaining means, we can place successive actions or method invocations (for example) in a single call. So you do not have to make nested method calls to arrive the final result. But how do you make such a call. Let us say you need to add fade or hide a &l;div> element in an html document. So you can do it by

$("div").fadeOut() ;

Or

$("#idvalueofdivtag").fadeOut();

where as "div" is an HTML element type or #idvalueofdivtag is name of <div> tag's ID name present in the document. With the above execution contents between that <div> tags will be faded out. Of course, you need to decide what event will be fired to make the above call in order to achieve the result. It is pretty simple, right.

If you get a time, then please have a look at this web site, http://jquery.com/. If you see this web site at least once, it has got once nice demo of this framework on the opening page itself. Feel the experience. It is really superb.

Already there are two books written on this amazing framework. Two chapters are free to download from ‘Jquery in action’, published by Manning Publications. It has provided nice tutorial also on the site which you are required to go through it. It is really worth reading.

This framework never stops you using other frameworks. If you really need another javascript source to be used along with this, then Jquery has provided an alternative.
jQuery.noConflict(). This method call will create an alias to Jquery object itself and can prefix to the normal method routines.That way the conflicts can be eliminated.

Also, you can see numerous plug-INS have already been developed to enhance rich user experience.
Personally I felt this one more better performing than other web2.0 frameworks developed in JavaScript. Google also started using this framework!.

Sunday, March 09, 2008

Sahi - Open Source Web Testing Tool

Any body who has been working in testing applications know what is WinRunner and LoadRunner.

A commercial testing framework developed by Mercury Software.This tool provides testing script to an existing application and also a tester can master this scripts to test the applications.It happens this way.Tester starts testing each and every links or button in a web application and a in-built facility has been provided in the same to play back these scripts.Once the testing is over ,the tester play back the saved test script file.

Similarly , there is an open source testing framework developed in java called 'SAHI'.This frame works similar way that was explained above.Since it is developed 100% in java, customization to this framework is really easy depending upon the requirement.You can download this framework from http://sourceforge.net/projects/sahi/. There is a document included in this pack how to install and run it.

But this framework does not provide any in-built commands to enhance the testing, but merely a mediocre testing tool that can be used to test the web application.

Once you downloaded this framework, copy to a folder in your hard disk.Copy to C:\sahi. If you further explore this folder you can see a bin folder with a batch file to execute the sahi server.When you double click on this batch file, sahi server will start.This server will be listening to port 9999.

After this point you need to make few changes in your favourite browser which is self explanatory and enter any url.Alt + DBlClick will open script window where in you can start recording the events.It will record every click user making on website.Once you feel that the recording is enough then you can stop it and playback the scripts.

However there are certain discrepancies have been pointed out.Like, if you open more than one links, then this framework will register only one link.As far as security is concerned, there is one thing caught into my observation that it is decrypting password value automatically and was able to see the password entered!.

This is a very small framework which can be considered as a model testing framework.But a good deal of work is required to make this framework to use in day-to-day testing applications.
ThoughtWorks , US based company has extensively worked on this open source product.

Friday, August 31, 2007

Annotation and Dependency Injection in Struts1.2 application


Annotation is an attribute oriented programming. Means, a meta-data will be passed to the piece of code. There are two types of Annotation. Compile time and Run Time. Compile time annotation as you know, is taken care at the time of compilation time and the latter is happening at run time.

Now let us talk about ‘Dependency Injection’. This paradigm is to inject the dependencies into the code. Let us talk about one practical example.

Since the introduction of the EJB3.0, enterprise bean development became very easy. There are no hurdles like creating remote and local interfaces and the respective deployment descriptor files. These have been eliminated from the development life cycle.

What exactly happened here inside the enterprise bean, we can specify what type of bean it is? Is it a stateless or statefull and what type of interfaces that you are going to use.

Let us look at the following example:

@Stateless

Public class HelloBean implements Hello{

Public void hello(String str){

System.out.println (“ Hello “+str);

}

This is an attribute to the EJB3.0 container that this bean is a stateless session bean. Likewise, take a look at the following interface how an interface is marked as ‘Remote’.

@Remote

Public interface Hello{

public void hello(String str){};

}

So these informations can be available to the ejb3.0 container by the help of Dependency Injection. In earlier ejb specifications, it was the responsibility of the developer to develop home, remote interface components and giving entries in the ejb-jar xml file and this approach was taking a lot of development time.Since the attribute oriented programming can save a lot of development time.

Let us take a look at struts 1.2 application where in Dependency Injection can be implemented. But if you are already using Struts2 framework, then in many situations they are using this feature readily. Here we are worried about Struts 1.2 application where dependency injection can be achieved using custom annotation.

In order to achieve the above, we have written a custom annotation that will serve this purpose. The custom annotation in this scenario is like the following:

package test;

import static java.lang.annotation.ElementType.FIELD;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;

import java.lang.annotation.Target;

@Target({ FIELD})

@Retention(RUNTIME)

public @interface VarRef {

}

This custom annotation binds the strVal variable in the TestAction class and specifies Retention policy is RUNTIME.

We use a RequestProcessor to pre-process the request and a public variable in the action class will be pre-populated with some value before the action class is executed. What really happens is the CustomRequestProcessor will check whether the variable is binding to an annotation of type FIELD. If any annotation is present on the variable, then the CustomRequestProcessor will process the variable and will invoke the setter method of the action class. The variable will be set to its value inside checkAnnotation(Object obj) method when set method was invoked.

The variable is a public variable in the TestAction class.

package process;

import java.io.IOException;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.RequestProcessor;

import test.VarRef;

public class CustomRequestProcessor extends RequestProcessor {

protected ActionForward processActionPerform(HttpServletRequest

request,

HttpServletResponse response, Action action,ActionForm form,

ActionMapping mapping) throws IOException, ServletException{

ActionForward forward = null;

checkAnnotation(action);

forward = super.processActionPerform( request, response,action, form, mapping);

return forward;

}

public Object checkAnnotation(Object obj){

String args[] = new String[1];

try{

Field [] fArray = obj.getClass().getDeclaredFields();

for(int i=0; i

Field ff = fArray[i];

String strFieldName = ff.getName();

if(ff.isAnnotationPresent(VarRef.class)){

if (obj instanceof Action) {

Method m = obj.getClass().getDeclaredMethod("set"+strFieldName,String.class);

args[0]="hello";

m.invoke(obj,args[0]);

}

}

}

}catch(Exception e){

e.printStackTrace();

}

return null;

}

}

Take a closer look at the checkAnnotation method in the CustomRequestProcessor class. This method takes Object is an argument and determines what type of class was invoked. If the invoking class is of type ACTION type, then it will call the setstrVar method in the action class using reflection and the corresponding variable will be set to ‘hello’ value. The check annotation method is invoked from the processActionPerform method.

So this gives us an idea that dependency injection can be achieved in struts application. This will lead to many ideas of implementing dependency injection like getting the datasource name; the ejb references etc in the struts 1.2 based applications as and when required. Also this will minimize number of lines in the code. All can be achieved in 2-3 lines of code.

The action class in this scenario is,

package test;

import java.io.IOException;

import javax.naming.InitialContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

public class TestAction extends Action{

@VarRef public String strVar="";

public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

System.out.println(" the strVar "+strVar);

return mapping.findForward("success");

}

public void setstrVar(String strVar) {

this.strVar=strVar;

}

}

You can see output in the tomcat console when the action class was invoked.

The strVar variable will be set to hello at the time of TestAction was invoked.


So annotation opens up plethora of innovative ideas when it comes to attribute oriented programming. It can be applied almost anywhere without much difficulty. You have to define what the application will be doing when it sees such Meta data.