Tuesday, December 27, 2011

Functional Testing Tools

Modern software is extremely complex, and testing today's software is especially challenging because testers must be able to design and execute a set of tests that will provide project leaders with the appropriate information that highlights potential risks and qualifies important attributes and capabilities of the software. Testers must define a finite set of tests from all possible tests that provides the organization with high levels of confidence that testers have exposed critical issues and appropriately evaluated the important features of the product.

One way to increase the effectiveness of our tests and gather important information about certain aspects of the product is to employ functional testing techniques. Functional techniques are systematic procedures that can help us perform a comprehensive investigation of the software's functional attributes and capabilities. Functional techniques are generally applied from the user interface but can be used to design tests from both black box and white box test perspectives. When we use functional testing techniques correctly and in the appropriate context, they can help us achieve a more thorough analysis of the system under test and reduce the susceptibility to the pesticide paradox.{taken from book 'How We Test Software at Microsoft'}

Functional testing bases its test cases on the specifications of the software component under test. It verifies a program by checking it against design documents or specifications. Functional tests are supplemented with information that allow us to improve their ability to find and correct deficiencies in the system in a cost effective way. Additionally, understanding the physical principles behind the system under test is helpful in developing a test procedure that truly verifies the system.

Functional techniques provide testers with systematic approaches that can help achieve a more comprehensive investigation of individual features and components. There are various open source tools functional testing available. Most of those tools are based Behaviour-Driven Development(BDD). BDD is an evolution of test-driven development (TDD) and acceptance-test driven design, and is intended to make these practices more accessible and intuitive to newcomers and experts alike. It shifts the vocabulary from being test-based to behaviour-based, and positions itself as a design philosophy. The most impressive open source frameworks for functional testing are:

  • Cucumber: Cucumber is a tool that executes plain-text functional descriptions as automated tests. The language that Cucumber understands is called Gherkin.
    While Cucumber can be thought of as a “testing” tool, the intent of the tool is to support BDD. This means that the “tests” (plain text feature descriptions with scenarios) are typically written before anything else and verified by business analysts, domain experts, etc. non technical stakeholders. The production code is then written outside-in, to make the stories pass.
  • JBehave : JBehave is a framework for BDD. User write a story and maps the steps to Java. User stories are configured and run as unit tests. It also provide reports in HTML which can used to verify user stories.
  • Concordian: Specifications are written in simple HTML. Developers instrument the concrete examples in each specification with commands (e.g. "set", "execute", "assertEquals") that allow the examples to be checked against a real-life system.
    The instrumentation is invisible to a browser, but is processed by a Java fixture class that accompanies the specification and acts as a buffer between the specification and the system under test. The fixture is also a JUnit test case, so it's easy to run and integrate into an automated build. The results of running the examples are exported with green and red indicating successes and failures.

Sunday, November 27, 2011

What is an Executor?

An executor is an object that can start and manage threads. This can make thread programming much easier and more efficient. An executor can execute a Runnable task a Callable task. The java.util.concurrent.Executors class provides static factory methods for creating executor objects. You can create an ExecutorService object like this:

ExecutorService threadPool = Executors.newCachedThreadPool();

This creates an object that creates new threads as required. Each time you use the threadPool object to start another task, either an existing thread in the pool that is currently no longer running is used, or a new thread is created and added to the thread pool. If you want to limit the number of threads that might be created, you can create a thread pool with a fixed number of threads, like this:

int totalThreads = 5;
ExecutorService threadPool = Executors.newFixedThreadPool(totalThreads);


This fragment creates a thread pool that contains up to totalThreads threads. Starting a new task using the threadPool object results in the task being executed in a new thread as long as there are fewer than totalThreads threads already in operation. If all threads in the pool have been allocated, the new task is placed in a queue to wait until one of the existing threads becomes free. All the threads in the pool continue to exist until threadPool is shut down. The method throws an exception of type IllegalArgumentException if you specify the maximum number of threads to be less than 1.

Saturday, July 9, 2011

Secure Your Java Code Against the finalizer Vulnerability

Finalizers can cause a vulnerability in Java code when used to create objects. The exploit is a variation of the well-known technique of using a finalizer for resurrecting an object. When an object with a finalize() method becomes unreachable, it is put on a queue to be processed at some later time. This article explains how the exploit works and shows how you can protect your code from it.

Following is the best part of article:

Until the third edition of the Java Language Specification (JLS) was implemented in Java SE 6, the only ways to avoid the attack — using an initialized flag, prohibiting subclassing, or creating a final finalizer — were unsatisfactory solutions.

The new way to prevent this kind of attack without introducing an extra code or restrictions, the Java designers modified the JLS to state that if an exception is thrown in a constructor before java.lang.Object is constructed, the finalize() method of that method will not be executed.

Read the article to see "how can an exception be thrown before java.lang.Object is constructed?"

Monday, July 4, 2011

Highly Scalable Java

Here is a collection of Concurrent and Highly Scalable Utilities. These are intended as direct replacements for the java.util.* or java.util.concurrent.* collections but with better performance when many CPUs are using the collection concurrently.

Dr. Heinz M. Kabutz measure the memory requirements of various types of hash maps available in Java in his post. There are some interesting statistics about the memory usages of Cliff Click's Highly Scalable Libraries.

How to compile a java source file from another Java source at runtime?

One of the cool features available in Java 6.0 (Mustang) is the ‘Java Compiler API’ which can be used compile a Java source file from another java file at run time.

Suppose we have the following class kept at C: drive of your disk:

public class FileToCompile {
public void sampleMethod() {
System.out.println("Method Called from FileToCompile");
}
}


Following code shows how can we compile this class at runtime:

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class RuntimeCompilation {

public static void main(String[] args) {
String lsFileToCompile = "C:/FileToCompile.java";
// ToolProvider provides methods for locating javac, javah etc.
JavaCompiler loCompiler = ToolProvider.getSystemJavaCompiler();
int liResult = loCompiler.run(null, null, null, lsFileToCompile);
if (liResult == 0) {
System.out.println("Compilation Successful");
} else {
System.out.println("Compilation Failed");
}
}
}


Read the javadoc for JavaCompiler class for more information.

Monday, June 13, 2011

An example where super() doesn't have to be the first statement in a constructor?

Java requires that if you call super() in a constructor, it must be the first statement. Do you know any place where we can call super() after some statement in a constructor?

Actually, When you construct a inner class within a non-static context, such as in a non-static method, a constructor or an initializer block, the class always has a pointer to the outer object. If you check the decompiled version of classes, the anonymous class sets the local field before calling super(). This is the only place where this is allowed in Java.

Please check this link for more details.

Friday, May 20, 2011

Java API for XML Processing (JAXP)

The Java API for XML Processing (JAXP), part of the Java SE platform, supports the processing of XML documents using Document Object Model (DOM), Simple API for XML (SAX), and Extensible Stylesheet Language Transformations (XSLT). JAXP enables applications to parse, transform, validate and query XML documents using an API that is independent of a particular XML processor implementation.

JAXP also provides namespace support, which lets you resolve naming conflicts. JAXP allows the user to use any XML-compliant parser or XSL processor from within the application. JAXP provides a pluggability layer to enable vendors to provide their own implementations without introducing dependencies in application code. Using this software, application and tool developers can build fully-functional XML-enabled Java applications for e-commerce, application integration, and web publishing.

JAXP Parsers

JAXP supports Object-based and Event-based parsing. In Object-based, W3C DOM parsing is supported. In Event-based, SAX and StAX parsing is supported.












JAXP API

JAXP API can be divided into two main parts: a parsing API and a transform API. Implementations that support the transform API are typically XSLT processors which require an XML parser to read input documents. Because of this, these implementations typically bundle an XML parser as part of their distribution.

The factory APIs let you plug in an XML implementation offered by another vendor without changing your source code. The implementation you get depends on the setting of the javax.xml.parsers. SAXParserFactory,javax.xml.parsers.DocumentBuilderFactory, and javax.xml.transform.TransformerFactory system properties, using System.setProperties() in the code, in an Ant build script, or -DpropertyName="..." on the command line. The default values (unless overridden at runtime on the command line or in the code) point to Sun's implementation.

DOM (Document Object Model)

DOM provides a familiar tree structure of objects. You can use the DOM API to manipulate the hierarchy of application objects it encapsulates. The DOM API is ideal for interactive applications because the entire object model is present in memory, where it can be accessed and manipulated by the user. Constructing the DOM requires reading the entire XML structure and holding the object tree in memory, so it is much more CPU and memory-intensive. For this reason, the SAX API tends to be preferred for server-side applications and data filters that do not require an in-memory representation of the data.

SAX (Simple API for XML)

SAX is an event-driven, serial-access mechanism that does element-by-element processing. The API for this level reads and writes XML to a data repository or the web. For server-side and high-performance applications, SAX API is mainly used.

XSLT API

The XSLT APIs defined in javax.xml.transform let you write XML data to a file or convert it into other forms. You can even use it in conjunction with the SAX APIs to convert legacy data to XML.

StAX

The Java Platform, Standard Edition version 6.0 includes JAXP 1.4. JAXP 1.4 is a maintenance release of JAXP 1.3 with support for the Streaming API for XML (StAX). JAXP defines a plugability mechanism to dynamically load compliant implementations of SAX and DOM parsers using the javax.xml.parsers and javax.xml.transform APIs. In an analagous manner, the StAX APIs define plugability mechanisms which allow applications to dynamcially load compliant implementations of StAX.

The StAX API exposes methods for iterative, event-based processing of XML documents. XML documents are treated as a filtered series of events, and infoset states can be stored in a procedural fashion. Moreover, unlike SAX, the StAX API is bidirectional, enabling both reading and writing of XML documents.

JAXP Implementations

Apache has several Java parsers: Crimson, Xerces 1, and Xerces 2. The reason is historical -- because Apache accepted two donations from two different companies. IBM donated XML4J which became Apache Xerces 1. Sun donated Project X which became Apache Crimson. Xerces 2 is a new third parser which is a rewrite. It has goals such as maintainability, modularity, and the implementation of certain features, which neither of the previous original parsers has achieved. Xerces 2 was designed to fill the long-term needs of Apache projects going forward. The current version of Xerces is 2.11.

The following implementations support the transform component of JAXP and also bundle a parser:

Name Parser Implementation XSLT Processor Implementation Comment

Apache Xalan-J

Xerces 2.7 Xalan XSLT None

JAXP Reference Implementation

Xerces 2.7 XSLT

J2SE 1.4

Crimson Xalan-J XSLT, cvs tag: xalan_2_2_d10 Uses JAXP RI version later than 1.1.2

J2SE 5.0

Xerxes 2.7 XSLT Uses JAXP RI version 1.3

J2SE 6.0

Xerces 2.7 Xalan 2.6 Uses JAXP RI version 1.4


Wednesday, May 18, 2011

Returning XML from SQL Server

SQL Server is a relational structure and XML is a hierarchy data structure. Hierarchal data is much more organized and Relational data is not organized in hierarchy way. With the release of SQL Server 2000, Microsoft introduced the capability of formatting return data streams from SQL Server as XML.

SQL Server supports this capability through a Transact-SQL (TSQL) clause called 'FOR XML'. This clause provides instructions to the data engine to output the results of the query in an XML format. Using various options, you have significant control over the format of that output, which gives you the ability to structure the data in a reasonable manner that the developer can then transform using Extensible Stylesheet Language (XSL) if desired.

Example:

SELECT Name, RollNo from Student.student
for XML RAW;

A RAW mode query returns each row of data in the result set as an element with the generic
label . If you look at the results, you will see that each row element represents a single row in the result set.

Following is an example of the output returned:

<row Name=”Ashwani” RollNo=”1700101”/>
<row Name=”Deepak” RollNo=”1700102”/>
<row Name=”Rajneesh” RollNo=”1700103”/>

We can add root element to make it well-formed.

Example:

SELECT Name, RollNo from Student.student
for XML RAW('Student'),
ROOT('Students')
ELEMENTS XSINIL;

This query returns a well-formed XML.

Following is an example of the output returned:

</Students>
<Student>
<Name>Ashwani</Name>
<RollNo>1700101</RollNo>
</Student>
<Student>
<Name>Deepak</Name>
<RollNo>1700102</RollNo>
</Student>
<Student>
<Name>Rajneesh</Name>
<RollNo>1700103</RollNo>
</Student>
</Students>

Friday, April 15, 2011

Lifecycle Callback Methods in JPA

JPA contains different lifecycle methods for monitoring changes in the the lifecycle of persistent objects. Every event in JPA has a corresponding callback method marker. These markers are shared between callback methods and their listeners. In JPA, these markers can be used for creating methods for callbacks by annoatating that method with callback annoatations. The lifecycle method annoations in JPA are: @PrePersist , @PostPersist, @PreUpdate, @PostUpdate, @PreRemove , @PostRemove and @PostLoad

Following are the rules that apply to lifecycle callback methods in JPA:
  • Methods can have public, private, protected, or package-level access, but must not be static or final.
  • A method may be annotated with multiple life-cycle event annotations. However, only one life-cycle annotation of a given type may be present in an entity class (you can’t have two @PrePersist annotations in the same entity, for example).
  • A method can throw unchecked (runtime) exceptions but not checked exceptions. Throwing a runtime exception will roll back the transaction if one exists.
  • A method can invoke JNDI, JDBC, JMS, and EJBs but cannot invoke any EntityManager or Query operations.
  • With inheritance, if a method is specified on the superclass, it will get invoked before the method on the child class.
  • If event cascading is used in the relationships, the callback method will also get called in a cascaded way. For example, let’s say a Student has a collection of addresses, and a cascade remove is set on the relation. When you delete the student, the Address @PreRemove method would be invoked as well as the Customer @PreRemove method.

Thursday, April 14, 2011

JPA Performance Benchmark(JPAB)

JPAB is a comprehensive benchmark that compares the performance of different combinations of JPA providers and Database Management Systems (DBMS). It covers many JPA ORM providers (Hibernate, EclipseLink, OpenJPA and DataNucleus) and DBMS (MySQL, PostgreSQL, Derby, HSQLDB, H2, HSQLite) that are available in Java.

It is recommended to check the following benchmark site while deciding the JPA ORM Providers for your project:

Java Double Brace Initialization

This is one of the hidden feature of Java. First of all, lets see some sample code:
ArrayList<String> tags = new ArrayList<String>() {{
add("abc");
add("xyz");
}};
Notice that there are two concurrent curly braces in the code. First brace creates an anonymous inner class and second brace is an instance initializer block that is run while anonymous inner class is instantiated.

Actually, it is just a rewrite of:
ArrayList<String> tags = new ArrayList<String>() {
// Initialization Block
{
add("abc");
add("xyz");
}
};
The inner class created will be having a reference of its enclosing outer class and hence will use "this" pointer to add the string values in the tags arraylist.

So on running the following code:
System.out.println("First Tag: "+tags.get(0));
it will print:
First Tag: abc

Wednesday, April 13, 2011

"Configuration by Exception" Mechanism

Java EE 5 introduced the idea of configuration by exception (sometimes referred to as programming by exception or convention over configuration). This means, unless specified differently, the container or provider should apply the default rules. In other words, having to supply a configuration is the exception to the rule. This allows you to write the minimum amount of code to get your application running, relying on the container and provider defaults.

This concept is commonly used in JPA(Java Persistence API) where the mapping rules are specified as annotations. If you do not specify these rules, the classes will behave as simple POJOs.

Consider the following example:

@Entity

public class Student {

@Id

private Long id;

private String name;

private Float rollno;

public Student()

{

}

….. // setters and getters

}

If you do not provide @Entity and @Id annotations, still Student will behave like a POJO and not be persisted.

Tuesday, April 12, 2011

Oracle Connection Process

To connect to a database service, clients use a connect descriptor that provides the location of the database and the name of the database service. Connect descriptor is a specially formatted description of the destination for a network connection and is usually described in tnsnames.ora.

Assume you have the following connect descriptor in your tnsnames.ora:
orcl =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
Now, when you issue a connect, say like this:

$> sqlplus scott/tiger@orcl

tns will look in your tnsnames.ora for an entry called 'orcl'. Finding it, tns sends a request through the normal network stack to (PORT = 1521) on (HOST = myhost) using (PROTOCOL = TCP), asking for a connection to (SERVICE_NAME = orcl).

What happens behind the scene?

The database server receives an initial connection from a client application through the listener. The listener brokers client requests, handing off the requests to the Oracle database server. Every time a client requests a network session with a database server, a listener receives the initial request.

Each listener is configured with one or more protocol addresses that specify its listening endpoints. Clients configured with one of these protocol addresses can send connection requests to the listener.

Once a client request has reached the listener, the listener selects an appropriate service handler to service the client's request and forwards the client's request to it. The listener determines if a database service and its service handlers are available through service registration. During service registration, the PMON process—an instance background process—provides the listener with information about the following:
  • Names of the database services provided by the database

  • Name of the instance associated with the services and its current and maximum load

  • Service handlers (dispatchers and dedicated servers) available for the instance, including their type, protocol addresses, and current and maximum load
Depending on the type of handler selected, the communication protocol used, and the operating system of the database server, the listener performs one of the following actions:
  • Hands the connect request directly off to a dispatcher.
  • Sends a redirect message back to the client with the location of the dispatcher or dedicated server process. The client then connects directly to the dispatcher or dedicated server process.
  • Spawns a dedicated server process and passes the client connection to the dedicated server process.
Once the listener has completed the connection operation for the client, the client communicates with the Oracle database server without the listener's involvement. The listener resumes listening for incoming network sessions.

Friday, April 8, 2011

Sample Spring Configuration File in Spring 3.0

When building large enterprise applications, developer usually ends up with a lot of spring beans configured into Spring ApplicationContext. ApplicationContext allows us to interact with the configured beans.

Following is an example of basic 3.0 spring configuration file:


Spring provides extensive support for namespaces. In the above example, bean namespace is used for defining all basic definitions of the application and context namespace is provided for convenience for configuring core beans.

The property-placeholder will automatically locate and parse any property files you have in your classpath under the META-INF/spring directory.

The context:component-scan declaration will cause Spring to locate every @Component, @Repository, and @Service in your application. This allows you to write a POJO, and then simply annotate the new POJO as a @Service. Spring will then automatically detect, instantiate, and dependency inject your service at startup time.
The exclude-filter is declared to avoid instantiating our @Controller classes, as these should be instantiated by a web tier application context. 
Imports allow you to specify an external Spring configuration file to be integrated into your application context.

Patterns Used in Spring's Persistence Layer

The application tier that deals with persistence is often called the persistence tier. Spring helps to enforce a modular architecture in which the persistence tier is divided into several core layers that contain the following:

  • The Domain Model
  • The Data Access Object (DAO) Layer
  • The Service Layer (or Service Façade)

Spring goes a long way towards reducing code complexity and maintenance. Following are mostly used of the patterns in spring’s persistence layer:

  1. Template Pattern: The Template pattern allows a template to be defined in which a series of standard steps are followed, delegating to a subclass for those operations that are specific to the business logic. For example, when working with Hibernate, it is first necessary to create and initialize a new Hibernate session and optionally begin a transaction, before executing any Hibernate operations. When the operations are completed, it is necessary to close the session, and optionally commit or rollback the transaction. It would be rather redundant to repeat these same steps each time it was necessary to interface with Hibernate. Instead, we can leverage Spring's HibernateTemplate or JpaTemplate abstractions, which handle these steps for us.
  2. Active-Record Pattern: In this pattern, an instance of a particular domain class represents a single row within the respective database table. To save changes to the instance (and thereby the appropriate row within the database), a save instance method is called directly on the instance. To delete an instance, we can simply invoke delete() on the instance that needs to be deleted. Query operations are usually invoked as static methods on the domain class itself.

Folder Structure for a Standard Maven 3 Web App

Taken from Chapter 3 - Basic Application Setup

Spring Persistence with Hibernate


Though you can define a custom directory structure and modify your Maven settings or graft Maven onto a legacy project with a directory structure that's already defined, your best bet is to stick with the basic folder structure that Maven expects. That will ensure proper interoperability with all of the plugins and tools that are available.

To build a basic art gallery application, start by making a folder called gallery:

mkdir gallery

Within the gallery folder, create the folders that are customary for a Java application:

mkdir gallery/src mkdir gallery/src/main mkdir gallery/src/main/java mkdir gallery/src/main/resources mkdir gallery/src/main/resources/META-INF 

For web applications, we need a webapp and WEB-INF folder:

mkdir gallery/src/main/webapp mkdir gallery/src/main/webapp/WEB-INF 

We also need to create the folders that are required for unit testing our application:

mkdir gallery/src/test mkdir gallery/src/test/java mkdir gallery/src/test/resources 

And finally, create the two folders where Spring configuration files are ordinarily placed:

mkdir gallery/src/main/webapp/WEB-INF/spring

mkdir gallery/src/main/resources/META-INF/spring
Note

You can also have maven automatically generate your project's structure by using Maven's archetype feature. Maven archetypes are like project templates that can be used to streamline the creation of a maven project, including setting up your directory structure and a baseline pom.xml file.