Monthly Archives: May 2013

Array in Java

As in life we experience and come across new technologies, bits of information and etc , we usually sometime dont concern or overlook about little or small units in every context of life.

Working with array sound simple but i was myself not aware of so many different ways to initialize Arrays in Java, MUST read and understand the third way of declaring Java Arrays!

Method #01

  1. int[] myArray1 = new int[3];
  2. int[] myArray2 = {1,2,3};
  3. int[] myArray3 = new int[]{1,2,3};

Method #02

  1. int myArray4[] = new int[3];
  2. int myArray5[] = {1,2,3};
  3. int myArray3[] = new int[]{1,2,3};

Although Method #01  and Method #02 looks different but when it comes to compiler they both are similar. The difference is just the positioning of brackets.

Here comes the mystery!

Method #03

  1. int []myIntArray5 , ab = {1,2,3};     // Although it seems to us we have one myIntArray of Array type and ab of Int type , but its not the case ab here is also a One Dimensional Array with Values Initialized to 1,2,3
  2. float invt[][];                                        // invt of Type Float  and a 2-dimenionsal Array
  3. float []prct, grts[];                              // prct one dimensional array and  grts is TWO dimensional array
  4. float [][] sms ,  hms[];                       //  sms is two dimensional array where hms is THREE dimensional array
  5. float[]  []sms[] ,  hms[];                    // sms and hsm are both THREE dimensional array
  6. float[]  []sms[] , [] hms[];                //  sms is THREE dimensional array but hms will Produce a compile time error! i.e identifier expected after ,  (Comma)

Print Array Elements

In Java 5 Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that Object[] version calls .toString() of each object in array. If my memory serves me correct, the output is even decorated in the exact way you’re asking.

EAR , JAR , WAR Archives in Java

J2EE defines three types of archives:

1. Java Archives (JAR) A JAR file encapsulates one or more Java classes, a manifest, and a descriptor. JAR files are the lowest level of archive. JAR files are used in J2EE for packaging EJBs and client-side Java Applications.

  • This archive file format is platform-independent format which has been fully written in Java.
  • The popular usage is to bundle all the .class files, and other required component files of a typical subsystem into a JAR file and include that JAR file into the CLASSPATH of another Java application which requires the services of that subsystem.
  • The maintenance and deployment becomes very easier in this case.
  • A JAR file has an optional manifest file located in the path META-INF/MANIFEST.MF. The entries in the manifest file determine how one can use the JAR file

 

2. Web Archives (WAR) WAR files are similar to JAR files, except that they are specifically for web applications made from Servlets, JSPs, and supporting classes.

  • It is used to package all the components of a Web Application.
  • It may contain JARs, JSPs, Servlets, HTMLs, GIFs, etc.
  • The purpose of this archive format is same as that of the JAR – to make the deployment, shipping, and in turn the maintenance process easier.
  • This will have an XML file fileName.xml as the Deployment Descriptor. This Deployment Descriptor is used by the Web Container to deploy the web application correctly.

3. Enterprise Archives (EAR) An EAR file contains all of the components that make up a particular J2EE application.

  • It is composed of several Web Applications and other independent JARs
  • The purpose is same – making deployment, shipping, and hence the maintenance easier.
  • Since Enterprise Application may have several web applications as its components, Hence EAR may contain WARs and JARs.
  • An EAR also contains an XML-based Deployement Descriptor, which is used by the Application Server to deploy the enterprise application correctly.

Difference Between Hibernate Flush and Commit

Flushing the Session simply makes the data that is currently in the session synchronized with what is in the database.
However, just because you have flushed, doesn’t mean the data can’t be rolled back.

Commit does flush the session, but it also ends the unit of work.

To summarize commit does two things,

1. Commit internally does flush
2. Ends the unit of work (makes the changes permanent).

FLUSH:
Flushing the Session simply gets the data that is currently in the session synchronized with what is in the database. However, just because you have flushed, doesn’t mean the data can’t be rolled back.
Hibernate will flush changes automatically for you:
before query executions
when a transaction is committed
Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.

COMMIT:
Commit does flush the session, but it also ends the unit of work

How to Attach Hibernate Docs to Eclipse

Javadoc is a convenient way to learn and get help from documents which are provided by the programmer. These javadoc can be added to Eclipse IDE to view appropriate javadoc for a particular class or method or interface etc. These javadocs views on eclipse when auto complete (or content assist) appear. You can view the content assist by pressing ‘Ctrl+Space’.

You can see the javadoc window on right hand side of this figure.

By default the javadoc for hibernate distribution does not exists. To add the javadoc to eclipse follow these steps.

Step 1
Download javadoc jar file from following link hibernate-3.2.2.ga-javadoc.jar

Any how you should download the hibernate-3.2.2.ga-javadoc.jar file.

Step2 
Open eclipse and add above jar file to hibernate3.jar as a javadoc for it. To do that follow the steps explained below.

Step 3
Right click on ‘hibernate3.jar’ by expanding user library which contain all your hibernate reference jar files. And select ‘Properties’.

Step 4
Select ‘Javadoc Location’ from side pane and click on ‘Javadoc in archive’ to select it.

Step 5
Browse for downloaded jar file and add that jar file location as javadoc location for ‘hibernate3.jar’.

Step 6
Click ‘Apply’ and ‘OK’ to save the changes.

Now it is done you can view javadoc from eclipse using intellisence (Ctrl +Space ) on hibernate Keywords.

javadoc

Comparable vs Comparator

  1. Comparator is an interface from java.util package.
  2. It includes two abstract methods – compare() and equals().
  3. A comparator object (a class that implements Comparator interface) can compare any two objects and these two objects are passed as parameters to compare().
  4. int compare(Object obj1, Object obj2).
  5. The Comparator object is used with Collections.sort(List, Comparator) andArrays.sort(Object[], Comparator) methods.
  6. Incase of Comparator, it is Collections.sort(studentList, esbm)
  1. Comparable is an interface from java.lang package.
  2. It includes only one abstract method compareTo()
  3. A comparable object (a class that implements Comparable interface) compares itself with another object.
  4. int compareTo(Object obj2)
  5. The Comparable object is used along with Collections.sort(List) andArrays.sort(Object[]) methods
  6. Incase of Comparable, it is Collections.sort(studentList)

Both methods return the same. Returns

  • A positive integer value if obj1 is greater than obj2.
  • 0(Zero) value if obj1 and obj2 are equal.
  • A negative integer value if obj1 is less than obj2.

 

Comparator Example

 import java.util.*;

// observe, Student does not implement Comparator 
class Student       
{
  private int marks;
  private String name;
 
  public Student(int marks, String name)
  {
    this.marks = marks;
    this.name = name;
  }
  public int getMarks()
  {
    return marks;
  }
  public String getName()
  {
    return name;
  }
}

// observe, StudentSortByMarks implements Comparator 
public class StudentSortByMarks implements Comparator<Student>
{                     
  public int compare(Student st1, Student st2) 
  {
    return st1.getMarks() - st2.getMarks();
  }
  public static void main(String args[])
  {
    List<Student> studentList = new ArrayList<Student>();
    studentList.add(new Student(50, "Rao"));
    studentList.add(new Student(40, "Seth"));
    studentList.add(new Student(60, "Kunal"));
    studentList.add(new Student(20, "Reddy"));
    studentList.add(new Student(30, "Nidhi"));
 
    StudentSortByMarks ssbm = new StudentSortByMarks();
    Collections.sort(studentList, ssbm);
    for(Student st : studentList)
    {
      System.out.println(st.getMarks() + " : " + st.getName());
    }
  }
}

Comparable Example

import java.util.*;
class Student implements Comparable<Student>
{
  private int marks;
  private String name;
 
  public Student(int marks, String name)
  {
    this.marks = marks;
    this.name = name;
  }
  public int getMarks()
  {
    return marks;
  }
  public String getName()
  {
    return name;
  }
  public int compareTo(Student st1)
  {
    return this.marks - st1.marks;
  }
}
 
public class StudentSortByMarks
{
  public static void main(String args[])
  {
    List<Student> studentList = new ArrayList<Student>();
    studentList.add(new Student(50, "Rao"));
    studentList.add(new Student(40, "Seth"));
    studentList.add(new Student(60, "Kunal"));
    studentList.add(new Student(20, "Reddy"));
    studentList.add(new Student(30, "Nidhi"));
 
    Collections.sort(studentList);
    for(Student st : studentList)
    {
      System.out.println(st.getMarks() + " : " + st.getName());
    }
  }
}

Sanity Vs Smoke Testing -www.guru99.com-

what is Smoke Testing?

Smoke Testing is performed after software build to ascertain that the critical functionalities of the program is working fine.It is executed “before” any detailed functional or regression tests are executed on the software build.The purpose is to reject a badly broken application, so that the QA team does not waste time installing and testing the software application.

In Smoke Testing, the test cases chosen cover the most important functionality or component of the system. The objective is not to perform exhaustive testing, but to verify that the critical functionalities of the system is working fine.
For Example a typical smoke test would be – Verify that the application launches successfully, Check that the GUI is responsive … etc.

what is Sanity Testing?

After receiving a software build, with minor changes in code, or functionality, Sanity testing is performed to ascertain that the bugs have been fixed and no further issues are introduced due to these changes.The goal is to determine that the proposed functionality works roughly as expected. If sanity test fails, the build is rejected to save the time and costs involved in a more rigorous testing.

The objective is “not” to verify thoroughly the new functionality, but to determine that the developer has applied some rationality (sanity) while producing the software. For instance, if your scientific calculator gives the result of 2 + 2 =5! Then, there is no point testing the advanced functionalities like sin 30 + cos 50.

Read more at http://www.guru99.com/smoke-sanity-testing.html#hCLU3KhZK0xrjzrv.99

Sanity Vs Smoke Testing

Sanity Vs Smoke Testing

Points to Remember

  • Both sanity tests and smoke tests are ways to avoid wasting time and effort by quickly determining whether an application is too flawed to merit any rigorous testing.
  • Sanity Testing is also called tester acceptance testing.
  • Smoke testing performed on a particular build is also known as a build verification test.
  • One of the best industry practice is to conduct a Daily build and smoke test in software projects.
  • Both smoke and sanity tests can be executed manually or using an automation tool. When automated tools are used, the tests are often initiated by the same process that generates the build itself.As per the needs of testing, you may have to execute both Sanity and Smoke Tests on the software build.
  • In such cases you will first execute Smoke tests and then go ahead with Sanity Testing. In industry, test cases for Sanity Testing are commonly combined with that for smoke tests, to speed up test execution. Hence it’s a common that the terms are often confused and used interchangeably.