Pages

Wednesday, March 13, 2013

A Simple JUnit example

JUnit is unit testing tool/framework for the Java programming language.

The methods that we want to test must be annotated with @Test annotation.

Look at the below sample code for quick reference.

Jar : Include junit-4.x.jar in your build path.

package com.j4b.junit;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

class HelloWorld {
 int x = 0;
 
 public String sayHello(){
  if(x == 0)
   throw new NullPointerException();
  return "Hello World";
 }

}

public class TestHelloWorld {
 
 @Test (expected= NullPointerException.class)
 public void testSayHello(){
  HelloWorld h = new HelloWorld();
  assertEquals("Hello World",h.sayHello());
 }
}
Now run the above code as JUnit Test and check.

Observe in the above code that - testSayHello() is a public method and returns void.

assertEquals() takes two params, 1st one is the Expected output and 2nd is the actual method we are calling.

@Test (expected= NullPointerException.class) - is a test method and it may throw a NullPointerException. So if even though it throws NullPointerException, JUnit Test will pass.

Monday, March 11, 2013

Does finally block executes when try block has a return statement in Java?

You know finally block is useful for preventing resource leaks.

Look at the below code, may be interesting for you if you didn't tried yet.

package com.j4b.samples;

public class FinallyReturn {
	
	@SuppressWarnings("finally")
	public static String display(String hi){
		try{
			System.out.println("Hello");
			return hi;
		}
		finally{
			System.out.println("in finally");
			return "Hello dude";
		}
	}
	public static void main(String[] args) {
		String str = display("Hi Praveen");
		System.out.println("str : "+str);
	}
}



output:
Hello
in finally
str : Hello dude

Now try to give System.exit(0); before return hi; statement at line 9.
now run the sample code and check the output.

Tuesday, July 24, 2012

hashCode() method in java

public int hashCode() : Returns a hash code value for the object.

- As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.
- If two object are equal a/c to the equals() method, then calling hashCode() on those object must produce the same hash value.
- It's not mandatory that if hash values of two object are equal, then the two objects should be equal.

It's generally said,

Adapter design pattern in Java with example


This is a structural design pattern. A Wrapper class, i.e., Adatper class is used to convert requests from it to Adaptee class.

The Adapter pattern converts the interface of a class into another interface the client expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Two forms of Adapter pattern:
1) class adapter (uses inheritace)
2) object adapter (uses composition)

object adapter 
---------------------
object adapter design pattern



 
A simple example

Thursday, July 19, 2012

Comparator and Comparable example in Java

java.lang.Comparable
--------------------------
int compareTo(Object o) : This method compares 'this' object with 'o' object

if the int value is
positive - 'this' object is greater than 'o' object
zero - 'this' object is equals with 'o' object
negative - 'this' object is less than 'o' object

java.util.Comparator
-----------------------------
int compare(Object o1,Object o2) : This method compares o1 and o2 objects.

if the int values is
positive - o1 is greater than o2
zero - o1 equals o2
negative - o1 is less than o2

A simple example

Tuesday, July 17, 2012

Sandbox model in Java

The Original Sandbox Model 
The original security model provided by the Java platform is known as the sandbox model, which existed in order to provide a very restricted environment in which to run untrusted code obtained from the open network. The essence of the sandbox model is that local code is trusted to have full access to vital system resources (such as the file system) while downloaded remote code (an applet) is not trusted and can access only the limited resources provided inside the sandbox.