Sunday, March 30, 2008

JUNIT with Struts 2

lets c how to write Junit test cases for an application written in Struts2. The biggest problem i encountered is that theres hardlt any documentation abt this... by struggling over the net i discovered a way to circumvent the problem. The code goes as follows.

basically here we are see if the Entries made in Struts.xml (i.e. that we give using <> tag etc... ) is working fine or not.

Create a base Test case:
package com.company

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.mock.MockServletConfig;
import org.apache.struts.mock.MockServletContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.util.StrutsTestCaseHelper;

import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;

import junit.framework.TestCase;

public class BaseS2TestCase extends TestCase {
private Dispatcher dispatcher;
protected ActionProxy proxy;
protected static MockServletContext servletContext;
protected static MockServletConfig servletConfig;
protected HttpServletRequest request;
protected HashMap requestParameters = new HashMap();
protected HttpServletResponse response;

public BaseS2TestCase(String name) {
super(name);
}

/**
* Creates action class based on namespace and name
*
* @param clazz
* Class for which to create Action
* @param namespace
* Namespace of action
* @param name
* Action name
* @return Action class
* @throws Exception
* Catch-all exception
*/
@SuppressWarnings("unchecked")
protected T createAction(Class clazz, String namespace, String name)
throws Exception {
dispatcher = StrutsTestCaseHelper.initDispatcher(null);
// dispatcher = Dispatcher.getInstance();
proxy = dispatcher.getContainer().getInstance(ActionProxyFactory.class)
.createActionProxy(namespace, name, null, true, false);
proxy.getInvocation().getInvocationContext().setParameters(
requestParameters);

// do not execute the result after executing the action
proxy.setExecuteResult(true);

// set the actions context to the one which the proxy is using
ServletActionContext.setContext(proxy.getInvocation()
.getInvocationContext());
ServletActionContext.setRequest(request);
ServletActionContext.setResponse(response);
ServletActionContext.setServletContext(servletContext);
return (T) proxy.getAction();
}

/**
* The method(X) mentioned in < name="..." method="X" class="C"> tag is
* invoked on the class(C) and the string-result is returned.
*
* @return: action result, eg: SUCCESS, FAILURE etc as returned by the
* action method
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
protected String execActionMethod() throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
Object action = proxy.getAction();
Method method = action.getClass().getMethod(proxy.getMethod(), null);
Object result = method.invoke(action, null);
return (String) result;
}
}


and now extend this class and get the juint running :)

public class newTest extends EdsBaseTestCase {

public EdsWizardTest(String name) {
super(name);
}

protected void setUp() throws Exception {
super.setUp();
}

protected void tearDown() throws Exception {
super.tearDown();
}

public void testEventAttributes() throws Exception {
EdsWizard action = createAction(EdsWizard.class, "/eds",
"eventAttributes");
// String method = proxy.getMethod();
String exec = execActionMethod(); // action.eventAttributes();
assertEquals(exec, "success");
}
}

Thats it. You are done. Please let me know if u see any issues with this approach.

cheers,
coffee

Monday, March 10, 2008

Dirty IE.... IE7: All crap.

Open source products are far better. We buy Mocrosoft products and finally screw up our selves totally. Today i was totally working with div tags and now i understand that the bug is in IE7 and not my code. Aaaaah!!!! big pain !!!!

Sunday, March 2, 2008

Sun Tech days

I had been to suntech days held at Hyderabad International convocation center. It was good... though only 50% of the technologies presented were of use to me... i still like the visit. A fashion show was done to entertain the pple here... the bad thing is that the way the models were projected... i felt like they are being shown as an objects rather human beings... I was very angry with it..... wanted ot post this on the Sun forums....

How ever to the middle of the show the Sun guy came up and took the mike trying to anchor the event... his words tried to mitigate the effect... and i felt better... But what ever i wanted to ask the sun guys as to how far was it right to give such a bad projection about women! but had no chance to do it...

The biggest and immediate use i found by the tech days was the advantages of taking up the Java 6 ... its internals... Ruby and ruby on rails.... Here at my company we have CI running every night and i often used to wonder why its written in Ruby when it could be done on java too... (by java process) Netbeans IDE, was one the ohter things.... though i wouldnt go for it in the near future... the cool thing abt this ide was the profiling tool inbuilt into it. I remember the eclipse and its profiling plugin... which never works... wasted abt a weeks time of mine.... netbeans does it very nicely... with much improved features....

The worst of all the presentations was the Ajax framework: woodswork and dyna faces for JSF. It was a very important session for me... but the session was screwed up.... The next bad one was the last session on glass fish... he started by saying: "i know most of u are not interested... weekend ... and every ones planning for a picture.. this being the last session..... even i feel the same" OOPS! what a dirty introduction is it!!!!

in btw i attended one of the solaris sessions it was good.. shifting to linux itself.... i would nt enourage ... forget solaris...

finally, i felt like glassfish and solaris could be fruitful only if you can afford support for the sun guys... and if i remmeber it right it could cost u as much as 4500$ ... quite costly... :( and more over now i am using tomcat since 3 years and *known devil is better than unknown devil*. I already have a bad experience using ICEFACES -open source technology on JSF...