Tags

Jan 22, 2012

Java SimpleDateFormat class to convert a Java Date to a formatted String.

The Java SimpleDateFormat class provides lets you easily convert (a) between a Java String to a Date or (b) perform the opposite conversion, from a Java Date to a String.

In following example, we first retrieve current date   and then we'll create a Java SimpleDateFormat object to define the custom format  Here's the source code for a complete Java SimpleDateFormat example class 
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
 * SimpleDateFormat example: Convert from a Date to a formatted String
*/
public class JavaSimpleDateFormatTest
{
  public static void main(String[] args)
  {
    // (1) get today's date
    Date today = Calendar.getInstance().getTime();
    // (2) create our date "formatter"
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm");
    // (3) create a new String using the date format we want
    String formatedDate = formatter.format(today);
   
    System.out.println("Folder Name = " + formatedDate );
  }
}
In this example, we specify custom date format looked like this:
yyyy-MM-dd-hh.mm
Formatted date output looks like this:
2012-01-06-08.23
Other custom date format
yyyy-MM-dd                 Output    2012-01-06    yyyyMMdd                   Output    20120106    EEE MMM dd hh:mm:ss yyyy   Output    Fri Jan 06 08:32:51 20   yyyy-MM-dd-hh.mm.ss                        output            2012-01-06-08.23.32

Java for loop syntax(Generics)

 I have created sample program to demonstrate Java  for-each loop syntax, using both a List with pre-Java5 syntax, and a second example using Java Generics

    import java.util.ArrayList;
    import java.util.List;

    public class JavaForLoop
    {
    public static void main(String[] args) {
    withoutGenerics();
    withGenerics();
    }
    private static void withoutGenerics() {
    List integers = new ArrayList();
    integers.add(new Integer(10));
    integers.add(new Integer(20));
    integers.add(new Integer(30));
    // you get an Object back from a List
    for (Object integer : integers)
    {
    // to convert from an Object to an Integer
    System.out.println(integer.toString());
    }
    }
    /*In this example method when you get the items out of the list you get them out as Objects, which you must then cast to the correct type.*/

    private static void withGenerics() {
    List integers = new ArrayList();
    integers.add(new Integer(40));
    integers.add(new Integer(50));
    integers.add(new Integer(60));

    // you get an Object back from a List
    for (Integer integer : integers)
    {
    // here you know for sure that you're dealing with
    // an Integer reference
    System.out.println(integer);
    }
    }
    }
    /*In this method, I have specify the type of the collection when you created it. In this cast the extra casting step is not required*/


Add new Jar file to your project


Here are all the steps you need to take to add a new jar file to your Eclipse build path:
  • Outside of Eclipse, copy the jar file you need to your project's jar file folder . Most projects have a lib folder for their jar files, so I'll assume that's where we're going to add it.
  • In the Eclipse Package Explorer, select your project, and then press the F5 key to refresh your project. (Alternatively you can just select the lib folder, and refresh it.)
  • Now when you look at your lib folder you'll see your new jar file in there.
  • Next, right-click that jar file, then select Build, and then Add To Build Path.

 
A few other notes about adding a new jar file to your Eclipse build path:
  • If you prefer using the menu system, you can add your jar file to the Eclipse Build Path like this: select Project, then Properties, then Java Build Path, then Libraries, and finally Add Jar.
  • To refresh your project view in the Eclipse Package Explorer, you can either press the F5 key, or right-click the project and select the Refresh menu option.
  • As mentioned above, it can be faster to just select your lib directory and refresh it.


That's all you have to do to add your new jar file to your current Eclipse project build path.

Jan 21, 2012

What It Means to Mock: Isolating Units for Testing

Mockito is a fantastic mock library for Java. I’m fascinated by how easy it is to use, compared to other things out there in the Java. To someone who is new to unit testing, the idea of mock objects can be confusing to say the least. However, in the real world, software has dependencies. We have action classes that depend on services and services that depend on data access objects (DAOs) and the list goes on. The idea of unit testing is that we want to test our code without testing the dependencies. This test allows you to verify that the code being tested works, regardless of it's dependencies. The theory is that if the code I write works as designed and my dependencies work as designed, then they should work together as designed.

          If you want to create unit tests to test your code, having an Interface that defines the signature of your Dao also lets you create a "mock"  Dao object that you can use in your tests. 

As a template, it’s a good practice to view tests like a BDD style test.

// Given
// When
// Then




The code below would be an example of this: 
  • Add the Mockito dependency to your pom.xml: 
org.mockito
mockito-all
1.8.4
test 
  • Mockito in JUnit test:
    import static org.mockito.Mockito.*;
    //...
    @Test
    public void testMock() {
         // Given
        
    Map mapMock = mock(Map.class);

    // When
    when(mapMock.get("1")).thenReturn("1");
    when(mapMock.get("2")).thenReturn("2");

    Object o = new Object();
    mapMock.put("anyObject", o);

    //Then
     assertEquals("firstValue", mapMock.get("1"));
    assertEquals("secondValue", mapMock.get("2"));

    verify(mapMock).get("1");
    verify(mapMock).get("1");

    verify(mapMock).put("anyObject", o);
    }
That’s it – all you need to know to get started. Mock objects are a very valuable tool in testing. They provide you with the ability to test what you write without having to address dependency concerns.

Dec 26, 2011

Creating new project using maven acrhetype from commnad line

Link:
http://maven.apache.org/archetype/maven-archetype-plugin/usage.html

Steps :
  • Define environmental varible
M2_HOME    Software Foundation\apache-maven-2.2.1
                          path       %M2_HOME%\bin;   (%PATH%;%ANT_HOME%\bin;%JBOSS_HOME%    \bin;%JAVA_HOME%\bin;%JAVA_HOME%\lib;%M2_HOME%\bin;)

  • download maven bin from
                 http://maven.apache.org/download.html
  •       mvn archetype:generate
           It will listdown  all archetype
               Enter archetype ID
               Enter group id
               Enter archetype Id
               Enter version
               Enter package