Saturday, July 14, 2007

Robot Class

Robot class is one of the most common classes used by guys doing automated testing. Giving some cool features to automate process. But still I find some thing lacking in this library or maybe I don't know how to use them.

Here I try to write a simple code, which write some text on a text editor and then close the editor without saving the file. Many things are hard coded in this example, like

robot.mouseMove(1420,20); // this is my desktop close button place in full screen mode.
So, if you want to run this code, just change this line according to your desktop. After changing run this code, open any editor say notepad within 10 seconds and then this example will write some text on it and then it will close the window.

import java.awt.Robot;
import java.awt.event.*;

public class MainClass {

public static void main(String[] args) throws Exception{


Robot robot = new Robot();
// giving you 10 sec to open any text editor.
robot.delay(10000);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_T);


robot.mouseMove(1420,20);
robot.mousePress( InputEvent.BUTTON1_MASK );
robot.mouseRelease( InputEvent.BUTTON1_MASK );
Thread.sleep(50);
robot.mousePress( InputEvent.BUTTON1_MASK );
robot.mouseRelease( InputEvent.BUTTON1_MASK );
robot.delay(1000);
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ENTER);

}
}

This is some useless example, but one can thing of some good examples but using this class.

Saturday, July 07, 2007

jikes vs javac

In my last post Priyanshu made a comment, " have you tried jikes". So, decided to give a try.

jikes is something which converts .java file into .class file. Yes, it is javac but IBM product not a Sun product. And let me begin with jikes page on sourceforge.net :

"You may wonder why the world needs another Java compiler, considering that Sun provides javac free with its SDK. Jikes has five advantages that make it a valuable contribution to the Java community:".

Providing free things, softwares and codes is a big deal for IBM but never for Sun. So don't keep on singing this Open source contribution song all the time. Whole world know what IBM is giving free.

So, advantage(s)

No. 1 : jikes is a open source software.
And so javac is ! Didn't get the advantage.

No. 2: Strictly Java compatible - It is not providing any superset or subset variation support.

Good, but this little of strictness most of time increase the code line from thousands or so.

No. 3: High Performance: Compilation is faster than javac.

Who really bother about compilation, anyway you have to come again and run on java command. I guess world is ready to devote 2 more minute in compilation.

Anyway, I have compiled one of the .java file(one which i have used in the last post) on the most stable version of jikes:

jikes TestForJDB.java

Issued 5 system warnings:

*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.

Didn't get what this warning message is and really not why you show it to me 5 times.

Then I have tried to look into -version command.

javac -version

- javac 1.6.0

jikes -version

- Jikes Compiler - Version 1.22 - 3 October 2004
Copyright (C) IBM Corporation 1997-2003, 2004.
- Licensed Materials - Program Property of IBM - All Rights Reserved.
Originally written by Philippe Charles and David Shields of IBM Research,
Jikes is now maintained and refined by the Jikes Project at:

Please consult this URL for more information and for reporting problems.

Very informative :)). Originally written by ... what does that mean ?



No 4. jikes provides some cool feature like incremental build.

I didn't get anything out of it.

Look at this feature.

jikes ++ TestForJDB.java

// After the 5 warning message.

Incremental: Enter to continue or q + Enter to quit:

nothing changed...

Incremental: Enter to continue or q + Enter to quit: (this if you change the code)

ok...

Incremental: Enter to continue or q + Enter to quit:

nothing changed...


No 5. clear error message.

I have removed one semi-colon from the code at line no. 25. Look at the error message printed by two compilers:

javac TestForJDB.java

TestForJDB.java:25: ';' expected
stringJDB = value
^
1 error

jikes TestForJDB.java

Found 1 syntax error in "E:/Program Files/Java/jdk1.6.0/bin/TestForJDB.java":

25. stringJDB = value
^---------------^
*** Syntax Error: ";" inserted to complete BlockStatementsopt

Are you able to find out something extra ? By the way what was the meaning of BlockStatementsopt ?


I guess I made too many of negative statement :), Ok some good features are here:

- it is providing some additional options like

> jikes +Punused-package-imports TestForJDB.java - which at compilation time, will show you those import statement which are not in use.

> jikes +Pnaming-convention testForJDB.java - it will throw warning if you are not following the naming convention ( its really an idea of Sun, don't mind :) )


Again, I didn't like the verbose information(javac -verbose filename.java) . Where is the time information man... parsing time, total time to make class file... these are some of the cool use of verbose option. Showing only which all files you are reading is a useless information.

Anyway, nice to see one step ahead in the world of open source.

Sunday, July 01, 2007

xjc.exe ?

Today I was looking some options in jdk/bin and suddenly I came across a file name xjc.exe in JDK6(I guess Mustang is no more official name of JDK6). Quickly I checked the existence of this file in JDK 5.0 and JDK 1.4.2. File was not present. Goggling didn't provided too much of help and I tried with myself :)

I just copied an XSD file from somewhere:

FileName: unknowntest.xsd (some problem with blogger, not able to take xml code :D, so pasting the image)










and passed this XSD file into xjc option:
Usage: xjc [-options ...] ... [-b ] ...

> xjc unknowntest.xsd

parsing a schema...
compiling a schema...
generated\Country.java
generated\ObjectFactory.java

Ah I got 2 Java files yeppp - Country.java and ObjectFactory.java
Look at these 2 files ( I am removing the documentation part of it, to make it small)

package generated;

import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"pop"
})
@XmlRootElement(name = "country")
public class Country {

@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected BigDecimal pop;

/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}

/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}

/**
* Gets the value of the pop property.
*
* @return
* possible object is
* {@link BigDecimal }
*
*/
public BigDecimal getPop() {
return pop;
}

/**
* Sets the value of the pop property.
*
* @param value
* allowed object is
* {@link BigDecimal }
*
*/
public void setPop(BigDecimal value) {
this.pop = value;
}

}

and objectFactory don't have too much in it. Just creating a Country class instance. Oh so it is something cool, its making all the getter and setter method for me, what else I need :).

Remember, such type of methods are too much in demand when you are writing any jsp page. So, if you are clear with the data type, just make an xsd file and xjc will convert in into java code.

I still doubt what xjc refers for ... may be xml to java converter :)