Thursday, November 22, 2012

Difference between "==" operator and equals() method


Difference between "==" operator and equals() method

The “==” operator:

In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location. A very simple example will help clarify this:

By definintion, the objects are all created on the heap. When you create an object, say,

Object ob1 = new SomeObject();
Object ob2 = new SomeObject();

We have 2 objects with exactly the same contents, lets assume. We also have 2 references, lets say ob1 is at address 0x1234 and ob2 is at address 0x2345. Though the contents of the objects are the same, the references differ.

Using == compares the references. Though the objects, ob1 and ob2 are same internally, they differ on using this operation as we comare references. ob1 at address 0x1234 is compared with ob2 at address 0x2345. Hence, this comparison would fail.

object.equals() on the other hand compares the values. Hence, the comparison between ob1 and ob2 would pass. Note that the equals method should be explicitly overridden for this comparison to succeed.

The equals() method: 

The equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the '==' operator is expected to check the actual object instances are same or not.

The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory. This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal. Here is an example that will help clarify this:

String obj1 = new String("xyz");

String obj2 = new String("xyz");

if(obj1.equals(obj2))
   System.out.printlln("obj1==obj2 is TRUE");
else
  System.out.println("obj1==obj2 is FALSE");

Java’s String class overrides the equals() method to compare the characters in a string. This means that the comparison between the 2 String objects returns true since they both hold the string “xyz”.

Monday, November 12, 2012

Build Tools


Build tool allows developers and build managers to describe the build process also build tools help the programmer to build their project efficiently. 

Build tools are programs that automate the creation of executable applications from source code. Building incorporates compiling, linking and packaging the code into a usable or executable form.

Build process is repetitive process where programmer builds the project again and again to test the changes in the project code. While developing a project code changes needs to be test and the testing requires the build of the code and finally deploying on the server for testing. Build tools automate the repetitive process and allows the programmer to concentrate on the project development. It also reduces the overall build time. Build tools makes the build process just a single click work. 

In summary, a build tool provides a mechanism to describe the operations and structure of the build process. When the build tool is invoked it uses the description, examines the current state of affairs to determine the steps that need to be executed and in which order, and then manages the execution of those steps.

There are many build tools are available. Followings are few Java build tools available.
  • Rake
  • Ant
  • Maven

Thursday, September 27, 2012

waitForPageToLoad in Selenium 2.0/WebDriver

The following method can be used to wait until the expected page title appears.


public void waitForPageToLoad(String title){
     Integer i = 1;
     for(i=1;i<=100;i++) {
          if (driver.getTitle().equals(title)) {
               break;
          }
          Thread.sleep(10000);
     }
}
 

Wednesday, July 4, 2012

Encoding detector

http://code.google.com/p/juniversalchardet/

import org.mozilla.universalchardet.UniversalDetector;
 
public class TestDetector {
 
public static void main(String[] args) throws java.io.IOException {
   
byte[] buf = new byte[4096];
   
String fileName = args[0];
    java
.io.FileInputStream fis = new java.io.FileInputStream(fileName);

   
// (1)
   
UniversalDetector detector = new UniversalDetector(null);

   
// (2)
   
int nread;
   
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
      detector
.handleData(buf, 0, nread);
   
}
   
// (3)
    detector
.dataEnd();

   
// (4)
   
String encoding = detector.getDetectedCharset();
   
if (encoding != null) {
     
System.out.println("Detected encoding = " + encoding);
   
} else {
     
System.out.println("No encoding detected.");
   
}

   
// (5)
    detector
.reset();
 
} }

Friday, March 23, 2012

Handling Javascript Popups Using WebDriver

WebDriver has an inbuilt library for handling the JavaScript alerts, and capturing values from the dialogs. It has a class called Alert. The following actions can be captured by using a Alert class build in latest version of WebDriver



1. accept()
2. dismiss()
3. getText()
4. sendKeys()



1. accept()



This method can be used to click 'OK' button in a popup.
Ex: 


Clicking on OK for JavaScript Alert





Thursday, February 9, 2012

Maven Installation Instructions


1- Download Maven library from the following URL http://maven.apache.org/download.html. At the time of writing this tutorial the latest version of Maven is 2.0.9. Download apache-maven-2.0.9-bin.zip for Windows and apache-maven-2.0.9-bin.tar.gz from Linux platform.

2- Unzip the file at some proper location (in my case its C:\Program Files\Apache Software Foundation\apache-maven-2.0.9).

3- Make sure your JAVA_HOME environment variable is set to java home directory.

4- You need to setup two environment variables for Maven

a. M2_HOME

b. M2

For Windows:

Go to My Computer > Properties >Advance tab > Environment variables > Under User variables > New and provide both M2_HOME and M2 environment variables as shown in the following figures

5- You also need to update the PATH environment variable in order to access Maven commands from console. Go to My computer > properties > Advance tab > Click Environment Variable Button > under system variables find PATH variable > Click Edit and at the end provide the path up to the bin folder of Maven. In my case it is C:\Program Files\Apache Software Foundation\apache-maven-2.0.9\bin

6- To verify that every thing is setup according to the plan open console and type mvn –version. You should see something similar to the following figure.