Tuesday, February 11, 2014

Using JMeter for load testing - configuring CSV File for POST using query parameters

The Apache JMeter™ desktop application is open source software, a 100% pure Java application designed to load test functional behavior and measure performance. ( http://jmeter.apache.org/index.html )

Apache JMeter may be used to test performance both on static and dynamic resources (Files, Web dynamic languages - PHP, Java, ASP.NET, etc. -, Java Objects, Data Bases and Queries, FTP Servers and more). It can be used to simulate a heavy load on a server, group of servers, network or object to test its strength or to analyze overall performance under different load types. 

In this article I will be explaining how to load test a URL by hitting it with multiple requests configured as different query parameters in the URL. The query parameters are in a csv file. JMeter will pick the query parameters and prepare the URL and hit the server and record the results.

We will write a test plan to hit URL http://testapp.testco.com/service?param1=<var1>&param2=<var2> multiple times using multiple threads using var1 and var2 values configured in csv file and stress test it.

So lets starts configuring JMeter for our test case.

Download JMeter

You can download JMeter from http://jmeter.apache.org. And install it on you local. It is very simple. Just follow the wizard for installation.

Launch JMeter

Incase you are on Windows, Go to <jmeter home>/bin and double click on jmeter.bat. This will launch JMeter standalone console.

Create new Test Plan

Once JMeter console is open, you will see in the left navigation tree, a Test Plan.
Right click on the Test Plan go to Add -> Thread Group and click on it. This will add a Thread Group element for you. For doing anything on JMeter you need to start with adding a Thread Group element.




Once you have added the Thread Group now we need to add some more elements to generate HTTP request and add CSV config elements.

1. Add HTTP Request 

Right click on Thread Group element and Add->Sampler->HTTP Request. Click on it to add it to your thread group.

2. CSV Data Set Config

Right click on Thread Group element and Add->Config Element->CSV Data Set Config. Click on it to add it to your thread group.

3. View Results Tree

Right click on Thread Group element and Add->Listener->View Results Tree. Click on it to add it to your thread group.


Now your Thread Group navigation on left should look like this.




Once we have added all the elements that we require for testing our Test Plan now we will go and configure them.

1. Configure HTTP Request element

Now suppose you want to test a URL http://testapp.testco.com/service?param1=<var1>&param2=<var2>, where the URL can have multiple var1 & var2 values. We will configure the test plan to read the var1 and var2 values from a csv file and hit http://testapp.testco.com/service with these values using multiple threads.

  • Add you Server Name or IP without http or https.
  • In Path add service
  • In the Parameters section add param1 & param2 and values as $(param1} and ${param2}. These will be picked up at runtime from csv file.
These configurations will create a URL like http://testapp.testco.com/service?param1=<var1>&param2=<var2>





2. Configure CSV Data Set Config

Now open notepad and create a csv file new.csv with param1 and param2 values seperated by tab, like below

param1a   param2a
param1b   param2b
param1c   param2c

Using this file we will configure CSV data set config element.

Put the file name in the Filename section as c:\temp\new.csv ( where csv file is located)
Add Variable Names param1, param2
Add Delimiter \t

The section looks as follows:



Once all this is setup, you can run the Test Plan using Start ( green play button on top) or go to Run->Start.

The results will be captured in View Results Tree element that we had added.




That is all. You can test your REST web services or any configurable URL using this.
Pls leave your comments incase you need further assistance.

Tuesday, December 10, 2013

Broken Image handling in jsp/html

Instead of showing a blank image, the best thing is to hide the img element itself from html page. 
This can be done using following script:-

<script>
// Hide img tag
$("img").error(function(){ $(this).hide(); });
</script>

In case you want to show a blank image jpg, this can be achieved using following script:

<script>
// Replace missing image $('img').error(function(){ $(this).attr('src', 'missing.jpg'); });
</script>






Wednesday, December 4, 2013

Chrome's Developer Tool - Handy when debugging your web application for CSS issues.

Wanted to share the benefits of using Chrome's developer tool. I know most of you would be using it for lot of debugging and profiling. We can also use Chrome to see all our incorrectly defined CSS properties on our website. Just go to the following icon on your Chrome and select Developers Tools under Tools.


Once you open your developers tool console, it appears at the bottom of your browser.

Now you can view all your invalid CSS on a web page when you open that webpage on the browser and view "Resource" section of the developers Tool. This will list all invalid CSS properties defined on the page and you can go to your code and remove / Change them.


Monday, November 25, 2013

Struts 2 and Apache Tiles Integration

This is an extension of a series of blogs I have written on Struts 2. Feel free to go through them on this blog site to a better understanding of how to create a web application and use Struts 2.

In this blog, I will be introducing Apache Tiles integration with Struts 2.

Tiles is a templating system. It can be used to create a common look and feel for a web application. Tiles can also be used to create reusable view components.


Benefits of Tiles are:-

  • Screen definitions - Create a screen by assembling Tiles : header, footer, menu, body, etc.
  • Layouts - Define common page layouts and reuse them across your website.
  • Dynamic page building - Tiles can be gathered dynamically during page reload. It is possible to change any attribute: layout, list of Tiles in portal, list of menu items, etc.
  • Reuse of Tiles / Components - If well defined, a Tile can be reused across multiple applications.
  • Multi-channels - It is possible to load different Tiles according to a key.


Configuring Tiles:-

Configuring pom.xml

Following tiles artifacts need to be added.
I am using ${tiles.version} = 2.2.2


<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-compat</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>${tiles.version}</version>

</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>${tiles.version}</version>

</dependency>

      <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-tiles-plugin</artifactId>
<version>2.2.1</version>

</dependency>


Configuring web.xml to use Tiles:-

Once we have added all the Tiles dependencies in pom.xml, we need to add Tiles in our web.xml

Add following entries with you strtus2 entry. Your web.xml should look like this:-



<context-param>
   <param-name>
      org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
   </param-name>
   <param-value>
      /WEB-INF/tiles.xml
   </param-value>
   </context-param>

   <listener>
   <listener-class>
      org.apache.struts2.tiles.StrutsTilesListener
   </listener-class>
   </listener>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>

  </filter-mapping>




Creating Tiles template and different jsps that will be included as tiles.

My tiles template would look like the image below. It will have a header jsp that contains the corporation name and logo. Body jsp that contains the dynamic content and a footer jsp that contains copyright information.
baseLayout.jsp

This is the tiles jsp that will hold the other jsps and act like a template page.

You see how I am using divs and css to create a table. It is always advisable to use div tables rather than normal html tables.


<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> Struts2 Tiles sample</title>
 <style type="text/css">
    .divInner1{

        text-align:center;
        border: .1px solid;
        float:left;

    }
div.page {
width: 11in;
height: 8.5in;
padding: .5cm auto;
margin: .1cm auto;
border: 1px #D3D3D3 solid;
border-radius: 5px;
background: white;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
    </style>
 </head>

 <body>
 <div class="page">
<div style="display: table; ">
<div style="display: table-row; ">
<div class='divInner1' style="width: 11in; height: 1.5in;" >
<tiles:insertAttribute name="header" />
</div>
</div>
</div>
<div style="display: table;  float:left;">
<div style="display: table-row;">
<div class='divInner1' style="width: 11in; height: 4.5in;" >
<tiles:insertAttribute name="body" />
</div>
</div>
</div>
<div style="display: table; width: 7in; height: 6.5in; float:left;">
<div style="display: table-row;">
<div class='divInner1' style="width: 11in; height: .5in;">
<tiles:insertAttribute name="footer" />
</div>   
</div>
</div>
 </body>

</html>



header.jsp

Following is the code for header.jsp marked as "header" in tiles tag in baseLayout.jsp page above.

<div class="header">

<p>This is the header</p>

</div>

<style>

div.header

{

padding:2px 3px; 

background:white;

}

p {font-size:30px;font-family:Arial;font-weight:bolder;color:blue;}

</style>

footer.jsp

Following is the html code for footer.

<div class="footer">

<p>This is the footer</p>
</div>

<style>

div.footer

{

padding:2px 3px; 

background:white;

}

p {font-size:30px;font-family:Arial;font-weight:bolder;color:blue;}

</style>


body.jsp

This is the html code for body page.

<div class="body">
<p>This is the body area</p>
</div>
<style>
div.body
{
padding:2px 3px; 
background:white;
         }
p {font-size:30px;font-family:Arial;font-weight:bolder;color:blue;}
</style>


Configuring Tiles configuration file - tiles.xml

Now a tiles.xml file has to be created and kept along with web.xml in WEB-INF. This file tells about the configuration of tiles and corresponding jsp pages.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>

   <definition name="baseLayout" template="/myapp/baseLayout.jsp">
      <put-attribute name="title"  value="Tiles Page"/>
      <put-attribute name="header" value="/myapp/header.jsp"/>
      <put-attribute name="body" value="/myapp/body.jsp"/>
      <put-attribute name="footer"   value="/myapp/footer.jsp"/>
   </definition>

   <definition name="success" extends="baseLayout">
   </definition>
  
</tiles-definitions>


Configuring struts.xml

Once you have created the baseLayout page, other tiles pages and configured the tiles.xml, now you will proceed to configuring the struts.xml so that everything could work.



<package name="sampleapp" namespace="/" extends="struts-default,json-default">
<result-types>
                    <result-type name="tiles"  class="org.apache.struts2.views.tiles.TilesResult" />

              </result-types>
                  ....
                  ....
                  <action name="/sampleapp/*" method="execute"  class="com.myorg.action.SampleAppAction">
<result name="success" type="tiles">success</result>
                   ....
                   ....
                   </action>
           </package>
                 


As you see in the code above, we define tiles with class TilesResult and then in our action, we direct result that are "success" to type="tiles".

This completes the sample implementation of tiles. 

Tiles framework is much more flexible and provided with much more feature, which you can try out after this sample implementation works.






Friday, November 22, 2013

Struts 2 -Repopulating a page with updated data after form submit

This post is an extension of my previous post( Creating sample Struts 2 Web Application ) that talks about creating Struts 2 web application. In this post I will talk about a much smaller and simpler trick to reload the submitted page with some updated content. I have seen on lots of forums, this question being asked that how do we show the same submitted page with new content.

 For that you need following entry as your struts.xml entry:-
<action name="loginaction" method="execute"  class="com.myorg.LoginAction">
<result name="input" >/sampleapp/Login.jsp</result>
<result name="success">/sampleapp/Login.jsp</result>
</action>


Now the entry in your Login.jsp page is, 


    <body>
              <s:form action="loginaction" method="post" > 
              <label for="name"><p>Enter your Name</p></label>
              <input type="text" size="50" name="id"/>
             <s:submit value="Submit" />
            </s:form>
      <s:if test="%{id != null}">
         <br/><p>Hello 
         <s:property value="id"/></h3>
     </s:if>

</body>


Your action class looks like this:-


public String execute() throws Exception {

if(null != id)
this.id = "Dear "+id;

        return SUCCESS;

    }


Now when you submit your page, and you have entered a name, it will be go to the action, and then repopulated on the refreshed Login page with text "Hello Dear <yourname>".


Monday, September 23, 2013

Using Arquillian with JUnit and Glassfish for Integration Testing

We are all familiar with Junit as a unit and integration testing framework. This article talks about configuration of JUnit with Arquillian for Integration testing on Glassfish 4.
Arquillian (http://arquillian.org/) provides a component model for integration tests, which includes dependency injection and container life cycle management. Arquillian lets you ditch the mocks and write real tests. That's because Arquillian brings your test to the runtime, giving you access to container resources, meaningful feedback and insight about how the code really works.

Following article will present a test project, where we have a service endpoint resource class (RESTResource.java) which is a service endpoint for a RESTful webservice. The RESTful request comes to this endpoint class and it calls the business service (ServiceImpl.java). We using Java EE 7, CDI (context & dependency injection - JSR 299 specification) to inject ServiceImpl in RESTResource. Now we use also have a OraServiceDAO that does a resource lookup (using Java EE @Resource) of Oracle Data source. This OraServiceDAO is also injected into ServiceImpl using CDI. This kind of 
The problem most developers will have while writing integration tests would be, how to simulate CDI and JNDI resource look up as done in OraServiceDAO, while doing integration testing without an actual Glassfish 4 environment running. Arquillian come to our rescue and along with JUnit it provides an embedded Glassfish environment for running integration test cases.

First I would like to introduce our project structure. 

RESTResource is the RESTful web service endpoint that receives the GET request.


ServiceImpl is injected into it using CDI. using Arquillian and JUnit we would write an integration test case that would test this business service namely testService API.

ServiceImpl class

If you see, the Oracle DAO OraServiceDAO is injected into this business service class.


OraServiceDAO  class



This class looks up JNDI resource to get the Data source.

Now if you see the complexity of this simple example, framework like JUnit would not be sufficient alone to test CDI and resource look ups done in this example without external help. Arquillian comes to rescue..!!  

Using Arquillian and JUnit we can simulate the Glassfish environment and run test cases.

First I would introduce with the Maven environment setting defined in pom.xml for running Arquillian, JUnit and embedded Glassfish using Maven.

Following versions of dependencies are being used in pom.xml
<properties>
        <arquillian.version>1.0.0.Final</arquillian.version>
        <jersey.version>2.0</jersey.version>
        <junit.version>4.11</junit.version>
        <glassfish.embedded.all.version>4.0</glassfish.embedded.all.version>
        <arquillian.glassfish.embedded.version>1.0.0.CR4</arquillian.glassfish.embedded.version>
   </properties>

I’m using the arquillian-bom (Bill Of Material) who contains all the versions for the arquillian dependencies.


The Arquillian & Junit dependencies:-


Build profile setting:-

Now the resource files defined above and required for Arquillian to correctly setup the resources

arquillian.xml

<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://jboss.org/schema/arquillian
        http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
    <container qualifier="glassfish-embedded" default="true">
        <configuration>
            <property name="resourcesXml">
              src/test/resources-glassfish-embedded/glassfish-resources.xml
             </property>
        </configuration>
    </container>

</arquillian>

glassfish-resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC
    "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN"
    "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>

    <jdbc-resource pool-name="ArquillianEmbeddedOraclePool" jndi-name="jdbc/testresource/ORAlookup"/>
    <jdbc-connection-pool name="ArquillianEmbeddedOraclePool" res-type="javax.sql.DataSource"
        datasource-classname="oracle.jdbc.pool.OracleDataSource" is-isolation-level-guaranteed="false">

  <property name="URL" value="jdbc:oracle:thin:@oraserver.com:1521:ora1"/>
    <property name="User" value="username"/>
    <property name="Password" value="password"/>

    </jdbc-connection-pool>
         

</resources>


Now let us look at the Test class which call its all:-



Line 25 - you have to use @RunWith annotation to specify 
Line 35 - ShrinkWrap api, that assembles it all. Shrinkwrap provides a simple mechanism to assemble archives like JARs, WARs, and EARs with a friendly, fluent API.
Line 47 - Injecting ServiceImpl

This is a JUnit test class that uses Arquillian to create archive that runs on embedded Glassfish env.



The structure of the complete project looks like:-






Finally, you can run maven install or maven test using Eclipse or from command prompt cmd> mvn clean install. You will see in the output logs, that Service and oracle DAO is called.




This example uses Arquillian to startup Glassfish env and run integration testing using JUnit.


Wednesday, September 11, 2013

JAXB - using XmlAdapter class and @XmlJavaTypeAdapter annotation to write custom binding from java bean to xml

We can use JAXB to convert data between a xml and java bean. Using JAXB annotations like @XmlRootElement, @XmlAccessorType & @XmlElement we can achieve marshaling/unmarshaling of java bean to xml and vice versa.

Now sometime we come across problems where we need some custom conversion of an xml attribute to a java object.  When we are binding our own classes to XML, we are hit with a situation where our class representation doesn't match what we'd like to see in the XML.
In those cases, @XmlJavaTypeAdapter comes to our rescue. We can implement a custom java type adapter in the following way.

Identify the conversion from/ to

For example sake, we will be converting a java.lang.String to a custom GUID class (com.myorg.GUID)

Write custom adapter

public class StringToGUIDAdapter extends XmlAdapter<java.lang.String, GUID> {
@Override
public GUID unmarshal(String strGUID) throws Exception {
return new GUID(strGUID);
}
@Override
public String marshal(GUID guid) throws Exception {
if(null == guid)
return null;
else 
return guid.toString();
}
}

As you see, we have to extend XmlAdapter class and implement two of its methods - namely unmarshal & marshal.

Define the annotation in your java bean

Now you need to define your annotation @XmlJavaTypeAdapter on the java field like:-
  
  @XmlJavaTypeAdapter(type=String.class,value =StringToGUIDAdapter.class)
    private GUID                    entity;

And that is it. Your custom binding is ready to be used. 

@XmlJavaTypeAdapter annotation can be used with the following program elements:
  • a JavaBean property
  • field
  • parameter
  • package

This kind of custom binding is very useful when using JAX-RS and JSON.