Tuesday, May 29, 2012

Log4j levels priority

Log4j levels priority:

Log4J Levels

TRACE LevelDEBUG LevelINFO LevelWARN LevelERROR LevelFATAL Level
TRACE LevelYYYYYY
DEBUG LevelNYYYYY
INFO LevelNNYYYY
WARN LevelNNNYYY
ERROR LevelNNNNYY
FATAL LevelNNNNNY
ALL LevelYYYYYY
OFF LevelNNNNNN

difference between bean id and name attribute in Spring

1. The id gets automatically and stricter validated by the XML parser since it has a special meaning in XML. So for example you can't add twice the same id into the XML, the parser would complain. This helps to edit the file since you can see an error in an XML editor. That's no possible for name. Spring can only complain at runtime about duplicate name.

2. Also, you cannot use certain characters in an XML id attribute: if you're using URLs as bean names, as is common in Spring MVC, then you'll need to use name="/path/somecontroller" instead of id, since the slashes are not legal characters to use for an XML id. That's actually the most common reason to use name instead of id. Others are if you need to specify several aliases using a comma seperated list in the name attribute: comma's are also not allowed inside an XML id.

Monday, May 28, 2012

Eclipse plugin for HTML and JavaScript editor with validations

Do below steps to add Eclipse plugin for HTML and JavaScript validations
1. Please click here to download Eclipse plugin for HTML and JavaScript validations.
2. Put the downloaded JAR file into ECLIPSE_HOME/plugins or ECLIPSE_HOME/dropins.
3.Then restart your Eclipse
Enjoy with new plugin!

Wednesday, May 23, 2012

Eclipse Tip: Static Imports

One of the great features of Java 1.5 is Static Imports. In order to configure Eclipse to search for static imports in a particular class, you have to perform the following steps:

  1. Navigate to Preferences by clicking on the Window -> Preferences Menu Item
  2. Navigate to Java -> Editor -> Content Assist -> Favorites using the menu tree (or search for Favorites using the search bar at the top)
  3. Click the New Type button
  4. Type in the name of the Class that has static methods that you would like to be used when using Eclipse's Content Assist / Code Completion (eg Assert)
  5. Click on the Browse button which will bring up the Open Type Dialog using what you entered previously as the search criteria
  6. Find the class that you would like to add, and then click Okay on the Open Type Dialog
  7. Then Click Okay on the New Type Favorite Dialog.
Now when you are editing Java code, instead of typing Assert.assertEquals, you only need to type assertEquals, with Ctrl-Space, and the Assert Type will be searched for in order to resolve the static import.

NoClassDefFoundError vs ClassNotFoundException


Difference between ClassNotFoundException vs NoClassDefFoundErrorBefore seeing the differences between ClassNotFoundException and NoClassDefFoundError let's see some similarities which are main reason of confusion between these two errors:

1) Both NoClassDefFoundError and ClassNotFoundException are related to unavailability of a class at run-time.
2) Both ClassNotFoundException and NoClassDefFoundError are related to java classpath.

Now let's see the difference between NoClassDefFoundError and ClassNotFoundException:

1) ClassNotFoundException comes in java if we try to load a class at run-time using with Class.forName() or ClassLoader.loadClass() or ClassLoader.findSystemClass() method and requested class is not available in Java. the most of the time it looks like that we have the class in classpath but eventually it turns out to be issue related to classpath and application may not be using classpath what we think it was using e.g. classpath defined in jar's manifest file will take precedence over CLASSPATH or -cp option, for more details see How classpath works in java. On the other hand NoClassDefFoundError is little different than ClassNotFoundException, in this case culprit class was present during compile time and let's application to compile successfully and linked successfully but not available during run-time due to various reason.

2) ClassNotFoundException is a checked Exception derived directly from java.lang.Exception class and you need to provide explicit handling for it while NoClassDefFoundError is an Error derived from LinkageError.

3) If you are using classloaders in Java and have two classloaders then if a classloader tries to access a class which is loaded by another classloader will result in ClassNoFoundException.

4) ClassNotFoundException comes up when there is an explicit loading of class is involved by providing name of class at runtime using ClassLoader.loadClass, Class.forName while NoClassDefFoundError is a result of implicit loading of class because of a method call from that class or any variable access.


Tuesday, May 22, 2012

How to find current directory in Java with Example

public class CurrentDirectoryExample {

    public static void main(String args[]) {
    
        String current = System.getProperty("user.dir");
        System.out.println("Current working directory in Java : " + current);
    
    }
}



If you run above program from C:\Test it will print C:\Test as current working directory

C:\Test> java CurrentWorkingDirectoryExample
Current working directory in Java : C:\Test


If you run it from C:\ then it will print C:\ as current working directory as shown in below example

C:\> java -cp ./Test  CurrentWorkingDirectoryExample
Current working directory in Java : C:\


How to reverse number in Java - Example

import java.util.Scanner;

/**
 * Simple Java program to reverse a number in Java using loop and operator
 * This program also shows example of using division operator(/) and Remainder Operator(%)
 */

public class ReverseNumberExample {

    public static void main(String args[]) {
       //input number to reverse
        System.out.println("Please enter number to be reversed using Java program: ");
        int number = new Scanner(System.in).nextInt();
     
        int reverse = reverse(number);
        System.out.println("Reverse of number: " + number + " is " + reverse(number));  
   
    }
 
    /*
     * reverse a number in Java using iteration
     * @return reverse of number
     */

    public static int reverse(int number){
        int reverse = 0;
        int remainder = 0;
        do{
            remainder = number%10;
            reverse = reverse*10 + remainder;
            number = number/10;
         
        }while(number > 0);
     
        return reverse;
    }

}

Output:
Please enter number to be reversed using Java program:
1234
Reverse of number: 1234 is 4321

What's new with JUnit4

1. @Test

Mark your test cases with @Test annotations. You don’t need to prefix your test cases with “test”. In addition, your class does not need to extend from “TestCase” class.



@Test

public void addition() {

assertEquals(12, simpleMath.add(7, 5));

}



@Test

public void subtraction() {

assertEquals(9, simpleMath.substract(12, 3));

}

2. @Before and @After

Use @Before and @After annotations for “setup” and “tearDown” methods respectively. They run before and after every test case.



@Before

public void runBeforeEveryTest() {

simpleMath = new SimpleMath();

}



@After

public void runAfterEveryTest() {

simpleMath = null;

}

3. @BeforeClass and @AfterClass

Use @BeforeClass and @AfterClass annotations for class wide “setup” and “tearDown” respectively. Think them as one time setup and tearDown. They run for one time before and after all test cases.



@BeforeClass

public static void runBeforeClass() {

// run for one time before all test cases

}



@AfterClass

public static void runAfterClass() {

// run for one time after all test cases

}

4. Exception Handling

Use “expected” paramater with @Test annotation for test cases that expect exception. Write the class name of the exception that will be thrown.



@Test(expected = ArithmeticException.class)

public void divisionWithException() {

// divide by zero

simpleMath.divide(1, 0);

}

5. @Ignore

Put @Ignore annotation for test cases you want to ignore. You can add a string parameter that defines the reason of ignorance if you want.



@Ignore(“Not Ready to Run”)

@Test

public void multiplication() {

assertEquals(15, simpleMath.multiply(3, 5));

}

6. Timeout

Define a timeout period in miliseconds with “timeout” parameter. The test fails when the timeout period exceeds.



@Test(timeout = 1000)

public void infinity() {

while (true)

;

}

7.New Assertions

Compare arrays with new assertion methods. Two arrays are equal if they have the same length and each element is equal to the corresponding element in the other array; otherwise, they’re not.



public static void assertEquals(Object[] expected, Object[] actual);

public static void assertEquals(String message, Object[] expected, Object[] actual);



@Test

public void listEquality() {

List expected = new ArrayList();

expected.add(5);



List actual = new ArrayList();

actual.add(5);



assertEquals(expected, actual);

}

JUnit4Adapter

Run your Junit 4 tests in Junit 3 test runners with Junit4Adapter.



public static junit.framework.Test suite() {

return new JUnit4TestAdapter(SimpleMathTest.class);

}

Servlet and Struts Junit test case with Mock objects

Thursday, May 3, 2012

java script hide and show funcation


Here id is div or span id

function shoh(id) {
if (document.getElementById) { // DOM3 = IE5, NS6
if (document.getElementById(id).style.display == "none"){
document.getElementById(id).style.display = 'block';
} else {
document.getElementById(id).style.display = 'none';
}
} else {
if (document.layers) {
if (document.id.display == "none"){
document.id.display = 'block';
} else {
document.id.display = 'none';
}
} else {
if (document.all.id.style.visibility == "none"){
document.all.id.style.display = 'block';
} else {
document.all.id.style.display = 'none';
}
}
}
}