Wednesday, October 31, 2012

Running Jstestdriver with PhantomJs in Windows

Using JSTestDriver with PhantomJS Locally
PahntomJS is command line web browser
JSTestDriver can be integrated to Jenkins; it can also be used for unit testing JavaScritpt Code; Only that it needs a browser running to execute the test cases.Using PahntomJs as the browser will  remove the need for a GUI browser to be launched from the script driven CI evnironment.
Steps
First start JStestdriver
D:\Coding\lib>java -jar JsTestDriver-1.3.5.jar  --port 9876 --captureConsole --runnerMode DEBUG
Starting Phantomjs browser and using it to capture the JStestdriver
Download Phantomjs
Download and use the javascript that is used to capture the pahntom browser
d:\Program_Files\phantomjs-1.7.0-windows>phantomjs.exe phantomjs-jstd.js
Wed Oct 31 2012 12:07:26 GMT+0530 (India Standard Time): Attempting (1) to load:
Run your JStestdiver tests
 D:\Coding\portlet\....\src\main\webapp\js>java -jar d:\Coding\lib\JsTestDriver-1.3.5.jar --tests all  --runnerMode DEBUG
Thats all
Note – Phantomjs can be used to change user agents (default is Safari)

Better Still, Running your JavaScript tests from Eclipse
Download and istall JStestDriver Eclipse Plugin from http://code.google.com/p/js-test-driver/wiki/UsingTheEclipsePlugin
Screen shot below for configuraiton 


Friday, October 12, 2012

JAX-RS with GSON and Jersy



Following from the excellent tutorial from Vogella (http://www.vogella.com/articles/REST/article.html ), extending with some extra details.
 Using GSON in this as my  class had  a custom class wrapped in Java collection, GSON makes it very easy to marshall JSON, same also with JAXB using Jersy; But with GSON unmarshalling to Java is also pretty easy.  Also multiple parmeter support is shown
So let us start with the interface first. Kindly read Roy Thomas Fielding's 2000 Dissertation, where he described his architecture style for the web This is a must read for , especially 'architects'. Enough Talk, Let us show some code Yes read about JAX-RS annotations, otherwise it is a simple Java Class (packaged as a servlet - WAR, see Vogellas article first. Okay here goes the interface (in rest everything is HTTP,GET,SET, POST etc

package com.test.prog;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;

@Path("/somerest")
public class RESTfulService
{
       //-- Using GSON way
 @GET 
 @Path("/get_some_list/{startId}/{count}")
 public String getSomelist( @PathParam("startId")  int startId, @PathParam("count") int count) {

  System.out.println("In getSomelistGenerate startId= "+startId+" count= "+count);
                DataList datalist= new DataList ();
  List mylist = new ArrayList();
  for (int i = startId; i < startId+count; i++) {
   // Parameter Constructor okay if using GSON, not okay for JAXB   //Data temp = new Data(i+1,"A long Text");
   DataList temp = new DataList ();
          temp.setId(i+1);
   temp.setText("A long Text");
   
   mylist.add(temp);

  }
  datalist.setList(mylist);
         return new Gson().toJson(datalist);

 }


        //-- Using JAXB way
 @GET 
 @Path("/get_some_list_jaxb/{startId}/{count}")
 public DataList getSomelist( @PathParam("startId")  int startId, @PathParam("count") int count) {

  System.out.println("In getSomelistGenerate startId= "+startId+" count= "+count);
                DataList datalist= new DataList ();
  List mylist = new ArrayList();
  for (int i = startId; i < startId+count; i++) {
   // Parameter Constructor okay if using GSON, not okay for JAXB   //Data temp = new Data(i+1,"A long Text");
   DataList temp = new DataList ();
          temp.setId(i+1);
   temp.setText("A long Text");
   
   mylist.add(temp);

  }
  datalist.setList(mylist);
         return datalist;

 }

Here goes the approximate Structure for Data and DataList class. Note the JAXB annotation is not needed if you are using Just GSON

package com.test.prog;

@XmlRootElement
public class DataList {
 protected List datalist;
       //--- Write getter and setter for this

@XmlRootElement
public class Data {
        private int id;
        private String text;
  
       //--- Write getter and setter for this
       // Data(int id, String text) No if you are using JAXB ctors with partmeters won't work, it expects 
      /Java Bean type class

-->Now create a WAR file and put this in Tomcat or some other Servlet container. Let us see how a client for this can be written using GSON

import java.io.ByteArrayInputStream;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
//Below only needed if you are using JAB to unmarhsall
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.google.gson.Gson;

import com.sun.jersey.api.client.*;

//To test the GSON getting and demarshalling 
 public static void main(String[] args) throws JAXBException{

  ClientConfig config = new DefaultClientConfig();
  Client client = Client.create(config);
  WebResource service = client.resource(getBaseURI());

String datalist= service.path("rest").path("somerest/get_some_list/2432/77").
     accept(MediaType.APPLICATION_JSON).get(String.class);
   DataList mydatalist= new Gson().fromJson(datalist, DataList.class);
// To test JAX demarshalling ? - Not succeeded from the String, and weary to try :)


//TODO JAXB Part not working proerly
  datalist= service.path("rest").path("somerest/get_some_list_jaxb/4234/42").
    accept(MediaType.APPLICATION_JSON).get(String.class);
  JAXBContext jaxbContext = JAXBContext.newInstance(DataList.class);
   
  Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  ByteArrayInputStream input = new ByteArrayInputStream (alarmlist.getBytes()); 
   mydatalist=  (AlarmDataList) jaxbUnmarshaller.unmarshal(input);

//Getting the URI, JAX-RS -JESRY automagically does create the URL up to your package, --Check the Web.xml configuration setting too
private static URI getBaseURI() {
  return UriBuilder.fromUri("http://localhost:8080/com.test.prog").build();
 
 }



Saturday, March 17, 2012

Ubuntu 11.10 on my Dell Inspirion M101Z (dual core)

From the time I had this  laptop  Dell Inspiron M101z (AMD Athlon II Neo K325 Processor 1.3GHz, 2GB RAM)  (http://asia.cnet.com/product/dell-inspiron-m101z-amd-athlon-ii-neo-k325-processor-1-3ghz-4gb-ram-45287123.htm), I had used the excellent wubi installer to dual boot Ubuntu  10.10 to my system. This was okay, however the Windows 7 starter OS I have in this is also pretty light and fast and most often I was using it. I guess with Windows Ubuntu half lost the battle.

Since Ubuntu has lot of releases, one day I tried upgrading Ubuntu in place. I guess upgrade is one area where Ubuntu is not that strong as earlier also upgrade has not been smooth.However this time, it managed to corrupt the Windows booting also. Since mine is a kind of high power Netbook, there is no CD tray. My hear skipped a beat when I saw this. However Windows 7 asked if it should go ahead and repair. Usually this sort of dialog from windows results in nothing much worthwhile, but this time in Windows 7 it actually repaired itself.  Amazing!

However Ubuntu was completely gone. After a few weeks I got courage again, fired up wubi and it installed 10.10 this time perfectly. I had thought that it would install the latest version 11.10. So I uninstalled wubi from my windows ,downloaded and installed a new wubi and fired it again. This time it installed 11.10 with the Unity 3D shell. Great I thought. Wubi is indeed pretty awesome.

However I immediately noticed the boot time for Ubuntu now was more than for Windows 7!. Also all the new effects of Unity and Unity 2D was making the system very very slow! and also erratic with complete freeze up. Ubuntu seems to have lost it now compared to Windows 7 Starter.

However the way the Ubuntu render my laptop screen looks to me better than for Windows 7, especially the white in windows 7 drivers looks pretty washed out. I can hardly see the thin borders of edit boxes and other such lines, example separators of mails in Windows Live Mail. But still this is just a minor annoyance than with Ubuntu.

However as others have suggested, I installed GNOME Shell and used the classic version, and with it the desktop performance is snazzy, though boot up times are pretty long.

Ubuntu 11.10 64 bit is relegated to the second love for now, Windows 7 Starter 32 bit providing all what I need.

I had come to love Ubuntu  (older version) for being light and fast on my old Pentium 3 PC which I had for quite some time. Windows XP would struggle in it and would not detect the  USB board that I plugged in, but Ubuntu was light and detected it. And also the web-cams etc that I attached.

However I have now discarded that old PC, and Ubuntu also seemed to have discarded its philosophy of being light enough in supporting low to medium  powered machines,but capable of providing users with a modern operating system. Sad, one tear drop  for Ubuntu,

Install/Upgrade NVIDI Driver in Ubuntu for CUDA SDK

Most linux distribution comes with the Nouveau https://nouveau.freedesktop.org/wiki/ display driver configured. If you need to use NVIDIA...