Author Archives: admin

Google Web Toolkit

Sometime back I spent some time to figure our how to use GWT for one of my projects. Here are some notes from that-

The magic with GWT is that we are provided with a Java to JavaScript Compiler which takes your Java code and creates JavaScript out of it. Additionaly GWT provides us with various libraries to help us develop the web pages and behavior using Java.

The trick here is, that GWT works is two modes. Hosted mode and Web Mode

Hosted Mode: Here, your Java application is working. The GWT browser takes your Java code, interpret it and show it as it would appear in the web page. As we are just running the Java code, this is where we can debug our application just as a java app. And we already know debugging java is much easier than debugging JavaScript, hence life is simple.

Web Mode: When we are done with the Java application creation, we run it on Web Mode. GWT’s Java to JavaScript Compiler gives us JS and HTML pages which we can directly deploy and user need not know how they were created.

Let’s get started

Step 0: Prerequisite

Download GWT from http://code.google.com/webtoolkit/download.html and unzip.
(that’s all)

Step 1: Creation Project:
GWT comes with command line utilities which help us create a project and application

projectCreator -eclipse TestProject

applicationCreator -eclipse TestProject com.test.client.TestApplication

Eclipse keyword in above command shows that I am using eclipse IDE, which has special support with GWT. If you are using some other IDE, just remove this keyword.

Step 2: Copy all the newly created files to a folder. Open Eclipse and import this project

Step 3: Right Click TestApplication.java, RunAS, Run, Java Application, check the main class is shown as com.google.gwt.dev.GWTShell, click run
This will open up GWT development shell and will show you the demo web application. A button you click and get the Welcome Message.

This demo application is running from TestApplication java code. You can get into this file and play around. You will find classes like panels, dialoguebox, buttons etc. You can try and make changes to the web page that is appearing.

Time to convert the Java code to JS and HTML.

Simple-just click TestApplication-compile.cmd. this will create two more folders in project tomcat and www. Tomcat has server related info (web.xml) and www has all the web pages, css, JS etc.

But this example is totally JS. Now how to make our code communicate with Java Code. One simple method is RPC. Here is a simple example in continuation to the prev one. Let’s say the welcome message needs to show name as well for the user. Lets write a backend class which will send this name. Here I will hardcode the name, in real application it can be read from database.

1: Create two interfaces under existing com.test.client package
i. GetNameService

package com.test.client;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath(“gettingName”)
public interface GetNameService extends RemoteService {
}

GetNameServiceAsync

package com.test.client;
import com.google.gwt.user.client.rpc.AsyncCallback;

public interface GetNameServiceAsync {
void getName(AsyncCallback callback);
}
2: Goto TestApplication.gwt.xml under src->com.test and add this line

<servlet path=”/gettingName” class=”com.test.server.GetNameServiceImpl” />

3: Create a new Package com.test.server and add the server side class

package com.test.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.test.client.GetNameService;

public class GetNameServiceImpl extends RemoteServiceServlet implements
GetNameService {
public String getName()
{
File afile=new File(“C:\\file.txt”);
String name=””;
System.out.println(“name”);

try {
BufferedReader input = new BufferedReader(new FileReader(afile));
name=input.readLine();
System.out.println(“name now:”+ name);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“return name:”+ name);
return name;
}
}

4: Goto Test Application and Add this method

private void getNameNow() {
// Initialize the service proxy.
if (getNameService == null) {
getNameService = GWT.create(GetNameService.class);
}

// Set up the callback object.
AsyncCallback callback = new AsyncCallback () {
public void onFailure(Throwable caught) {
// TODO: Do something with errors.
}

public void onSuccess(String result) {
name=result;
}
};

// Make the call to the stock price service.
getNameService.getName(callback);
}

}

And change addClickListener to look like
button.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
getNameNow();
dialogBox.setText(“Welcome to GWT! “+name);
dialogBox.center();
dialogBox.show();
}

All set, now run the application again. Change the name in text file and you will see the changed name in your application.

Uploading images with wordpress

When I was trying to upload the dilber image while writing this post, I was surprised to find out that the image was not getting uploaded and worpress throwing back HTTP error.

After googling around, I figured out the similar problem here. Though in my case .htaccess file was blank (in root www directory of wordpress). I added following lines to the file and uploaded.

 <IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>

This solved the problem for now. Does anyone have detailed idea on this issue?

 

Selenium- A Web Application testing tool

Sometime back I analyzed selenium tool for testing the application from UI. Here are some notes which I created during the analysis

What is it: The Selenium is a web testing tool that promises to test any web application from UI. The tool has capabilities to record and play the test cases.

Downloadable: It provides two types of basic installations- IDE (for Firefox), RC- Remote Control (for all browsers).

How to use

Using Selenium with Firefox (FF): Using Selenium with FF is pure vanilla. The Selenium IDE can be simply installed with FF as a plug-in. This has capability to record and play the test cases. Go to any simple website (say google) with a small form (lets search for Kamalmeet)
Step1: Open the URL for webpage
Step2: Fill the form
Step3: Submit the form
Step4: Check for desired message (In this example with google, lets check if the desired website was listed. Selenium provides you various options; you can look for a text on the webpage to mark the test case as a success)

Once all the steps are done. Just stop the recording. A test case is ready. Now I can play this test case and all the above mentioned steps would execute automatically.

Using Record and Play technique, we can create much more complex test cases (say 10 forms or screens are involved). These test cases can saved and imported to any machine with FF. Additionally, the cases can be exported to languages like Java, Ruby, C#, Perl, PHP and Python.

Advantages:
1. The test cases can be created just by browsing through the application. Not even single line of coding required.
2. Test cases can be exported to multiple languages. For Java it means it gets imported as JUNIT test case. Now they can be run as standalone JUNIT test cases on any machine.
3. JUNIT test cases can be further modified by developers as per need.

Limitations:
1. The IDE is limited to Firefox
2. Did not get satisfactory results for dynamic contents. If text is changing dynamically at run time, the Selenium was not able to recognize. This might require more analysis.
3. Pop-ups: If the window has a pop up, say ok cancel button, the tool was not able to handle it smoothly. More analysis required here.

Using Selenium with Internet Explorer (IE): Using Selenium with IE is not as straight forward as it is with FF, still possible. We need to create test cases manually in this case and then execute.

One way to create test cases is as mentioned above, run the application in FF with Selenium IDE and export the test cases in Java.

Secondly we can actually create the test cases manually. This is as not very tough, but certainly more work than using tool with FF. We would need to understand the tool in more depth and figure out Java commands which it uses internally and then create appropriate test cases.

Who’s Shoe is that?

Imagine a scene that a journalist throws a shoe at a Politician.. not very hard to imagine.. right?

Politician: “which asshole has thrown this shoe on me”
Secretary (whispers): “Sir! Someone is just protesting about something.”
Politician:”Oh! Then it’s OK. Let me speak for few more minutes, we might get the other one from pair as well!”

I am sure that some shoe company has already got the idea of launching the shoes specifically for this purpose. I mean, we might read a hoarding in near future “Throwaway shoes at Throwaway prices!”

EMMA- because you need to check the code coverage

EMMA is a free open-source Java code coverage tool. The keyword here is free, as I am not aware of many free code coverage tool. Moreover, I am not sure if people would like to spend money on a code coverage tool, especially if we are working on small applications. It is relatively easier to check the code coverage in small applications, for example, if you run the application in debug mode on eclipse, you can very well see line by line which code is getting executed. But if your application is a multi thousand line, this approach fails. In that case you would need a proper code coverage tool, and if you are getting one for free, nothing like it.

One question we forgot to ask is, why do I need to check the code coverage at all. Say, we have an application where we need to get user information and this is done using three different forms which are handled by 3 servlets at the back-end. Now someone came up with an idea that instead of three forms we can have 2 or 1. The idea was good and it was implemented, design got changed, test suite got changed, code got changed as per the new design. But there is a major possibility that some code which was part of earlier design did not get remove. It was old code, so nobody bothered as it it will not be used any more. At then, we end up shipping code which was not required. More changes to the application and more such code. So it is better to check what all is required or not.

Another thing might be, that the code shown by the tool as unused is actually useful code, but that did not get executed while running the test suite. That implies, your test suite is not complete enough.

Now if you are convinced, here is your quick start link – Quick Start with EMMA

Artificially Intelligent

Artificial Intelligence or AI as it is commonly termed was one of my favorite subject while in college. Inspired by movies like ‘I,Robot’ I used to think that it might be possible to create machines which can think on its own.

No I am not thinking about hardcore AI right now, I am thinking about usage of AI in day today web applications. This is nothing new, one example is, when you go for shopping on some website, after you choose or check a product, it shows you a list of related products. That is some sort of intelligence.

First things first, what do we think intelligence is. Telling 2+2=4? That’s mathematics. Reasoning, analysis, common sense. These can be categorized as intelligence. So can a machine or software or an application reason. Not on its own for sure. It needs someone to code how to reason. A very simple example is lets say I show a sequence of numbers to a human, 1,2,3,4 and then I show 1,3,5,7 and then 1,4,7,10. he would be easily able to understand the pattern and will be able to tell the next in series. But can a machine do the same. Not unless someone code it that way. Does that mean the concept of ‘thinking machines’ would be possible only in fiction?

Coming back to my original point of adding some intelligence to the web applications, what I mean here is an application, smart enough to guess what a user wants from it. Or in simple words, helping user. Shopping cart is a good example as already mentioned. You search for a book on AI and it will show related books. Let’s take the same example in real world. You go to the book shop, ask for a book on AI, the shopkeeper will show you books on AI. And if he is a good salesman, will also suggest the best buy based on his past experience of sales of various AI books. You go next time again ask for the AI books. Third time you go, probability is that even before you ask, he will show you the latest arrivals in AI section. That’s intelligence. This can easily be replicated in web applications.

So think that next time you go to a web site and it just dishes up whatever you need without even asking.