Tuesday, July 31, 2012

Snippet to generate lines for TestSuite

I wanted to generate the TestSuite for all my classes and was not ready to click on all 200 test classes to generate from eclipse So wrote a small testcase which printed the lines for me to add into my TestSuite. Here is the code snippet.



    @Test
    public void testShouldPrintAllTheNameOfTestClasses() throws Exception {
        File dir = new File("/Project/DIF/dif_parent");
        LinkedList files = (LinkedList)FileUtils.listFiles(
                  dir,
                  new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$"),
                  DirectoryFileFilter.DIRECTORY
                );
        for (File file: files) {
          System.out.println("suite.addTestSuite(" + file.getName().replace("java","class" ) + ");");
        }
    }



Thanks
Manisha

Wednesday, July 25, 2012

Attaching Source code with Maven

Initially I use to download the source jars and then use to attach them to project. Then started adding source jar as dependencies to pom for dev profile. Recently I found a maven command which does i it seamlessly for us.

I ran below commands to download and attach the source code of all the projects in Eclipse for me.

mvn dependency:sources

mvn eclipse:eclipse -DdownloadSources=true 

Boom! refresh all project in Eclipse and the source code must be attached to your project.                    
Enjoy looking into source
Manisha

Tuesday, July 24, 2012

JMeter is not good tool for finding Request per second

I played around with performance tools a lot in my last project and this was one blog which I never got to finish. My goal was to come with a tool which is best for benchmarking an application on Tomcat. While working with JMeters all the numbers compare to AB tool were low, I suspected Tomcat and started reading the tomcat performance tuning book and found below lines.

(Page 136 - Chapter 4 - Tomcat Performance Tuning ) - JMeter's HTTP client is slower than that of AB or siege. You can use JMeter to  find out if a change to your webapp, your tomcat installation or your JVM, accelerated or slows the response times of the webpage however you cannot use JMeter to determine the server's maximum number of requests per second that it can successfully serve because of JMeter's HTTP client appears to be slower than Tomcat's server code.


http://books.google.com/books?id=vJttHyVF0SUC&pg=PA136&lpg=PA136&dq=%22maximum+number+of+requests+per+second%22++jmeter&source=bl&ots=i-5yv-tLh0&sig=0JnmfPtq2PwKWdEDkfSamUQMBEg&hl=en&sa=X&ei=6L0QT-3DGc6lsQKmio2LBA&ved=0CDIQ6AEwAg#v=onepage&q=%22maximum%20number%20of%20requests%20per%20second%22%20%20jmeter&f=false

I thought it was really valuable find :)
Manisha 

Get rid of ^M in VI editor

^M magically appears on scripts when we copy a file from windows to Linux OS after modification.

To remove the ^M characters at the end of all lines in VI use below command:

:%s/^V^M//g

The ^V is a CONTROL-V character and ^M is a CONTROL-M. When you type this, it will look like below command. In UNIX, you can escape a control character by proceeding it with a CONTROL-V.

:%s/^M//g

The :%s is a basic search and replace command in VI. Above command tells VI to replace all the ^M character globally with nothing and to escape ^M we need to type ^V.

Enjoy
Manisha

Thursday, July 19, 2012

Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given InputStream

I waited enough for Sandip to write this blog since it was his find :).

Here is the problem we had, we have an application where user can upload the excels and our application process them. Our users were uploading 2007 formatted excel file in .xls format or vices-verse. Microsoft excel 2007 is backward compatible but our POI libraries sometime gets confuse. DOM parser is smart enough to handle but when it comes to SAX we need to know the exact format of the file loaded. i.e. just checking the file extension might not be enough. So we have to look into input stream to find the exact loaded excel format of the file.

As usual I looked over the internet and could not find anything post how to do it. Since Sandip is lot more savvy with Apache POI he started looking into the Apace POI source code and found WorkbookFactory class (Factory for creating the appropriate kind of Workbook) has a help method which takes the inputStream and based on that creates an appropriate object. Look at the code below.

============================
/**
 * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
 *  the given InputStream.
 * Your input stream MUST either support mark/reset, or
 *  be wrapped as a {@link PushbackInputStream}!
 */
public static Workbook create(InputStream inp) throws IOException, InvalidFormatException {
    // If clearly doesn't do mark/reset, wrap up
    if(! inp.markSupported()) {
        inp = new PushbackInputStream(inp, 8);
    }
   
    if(POIFSFileSystem.hasPOIFSHeader(inp)) {
        return new HSSFWorkbook(inp);
    }
    if(POIXMLDocument.hasOOXMLHeader(inp)) {
        return new XSSFWorkbook(OPCPackage.open(inp));
    }
    throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}

============================

Good find Sandip
Manisha

JSP/Tomcat 6 "org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException"

We noticed "org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException" on our JSP pages in the morning. I have copied the entire exception stack trace below for reference.

My first guess was the jsp pages have some issue where we are trying to access some index which has not been assigned. But we realized it's the server which ran our of space. so the first exception was "
java.io.IOException: There is not enough space in the file system.".

Why tomcat has to throw such an non intuitive error message? Why?  Not sure if it's tomcat's fault or the underneath framework is messing up.

==============================================================
org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException
 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 sun.reflect.GeneratedMethodAccessor209.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
 java.lang.reflect.Method.invoke(Method.java:600)
 org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:269)
 java.security.AccessController.doPrivileged(AccessController.java:284)
 javax.security.auth.Subject.doAsPrivileged(Subject.java:727)
 org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:301)
 org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)

root cause
java.lang.ArrayIndexOutOfBoundsException
 java.io.OutputStreamWriter.write(OutputStreamWriter.java:250)
 java.io.Writer.write(Writer.java:152)
 org.apache.log4j.helpers.CountingQuietWriter.write(CountingQuietWriter.java:44)
 org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:308)
 org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:236)
 org.apache.log4j.WriterAppender.append(WriterAppender.java:159)
 org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:230)
 org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:65)
 org.apache.log4j.Category.callAppenders(Category.java:203)
 org.apache.log4j.Category.forcedLog(Category.java:388)
 org.apache.log4j.Category.error(Category.java:319)
 com.jpmc.tao.common.logging.Logger.error(Logger.java:167)
 com.jpmc.tao.presentation.controller.FrontController.process(FrontController.java:61)
 org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 sun.reflect.GeneratedMethodAccessor225.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
 java.lang.reflect.Method.invoke(Method.java:600)
 org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:269)
 java.security.AccessController.doPrivileged(AccessController.java:284)
 javax.security.auth.Subject.doAsPrivileged(Subject.java:727)
 org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:301)
 org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
 java.security.AccessController.doPrivileged(AccessController.java:251)
 java.security.AccessController.doPrivileged(AccessController.java:251)
 org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:706)
 org.apache.jasper.runtime.PageContextImpl.access$1000(PageContextImpl.java:71)
 org.apache.jasper.runtime.PageContextImpl$11.run(PageContextImpl.java:664)
 java.security.AccessController.doPrivileged(AccessController.java:251)
 org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:662)
 org.apache.jsp.default_jsp._jspService(default_jsp.java:54)
 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 sun.reflect.GeneratedMethodAccessor209.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
 java.lang.reflect.Method.invoke(Method.java:600)
 org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:269)
 java.security.AccessController.doPrivileged(AccessController.java:284)
 javax.security.auth.Subject.doAsPrivileged(Subject.java:727)
 org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:301)
 org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
==============================================================

Any thoughts?
Manisha

Tuesday, July 10, 2012

How to be a Technical Leader

This is one question which is bothering me from a while. Honestly there are bunch of questions and this is the root of all.

1) What is leadership? Bossing over people?
2) How can I be leader and be technical at the same time?
3) Can I be a leader without becoming like those other people?

To me leadership should create an environment in which people feel empowered. An environment where one is free to talk, free to act and ask anything. Since each person is unique, can one style be application to entire team. Should leader choose different style of leadership for different teams? The biggest question to me is WHAT IS LEADERSHIP? Now a days I hear servant leadership term but what if your team is not capable to solve problems on their own. Will this style of leadership work in the mentioned scenario? I would assume no. I did not have very sweet experience.

To me a leader should be a good motivator. A leader should understand the problem and manage flow of ideas and manage quality too. Above all team should have FAITH in the leader and his leadership. There are lots of other factors which might come in play as a leader. May in next blog :)

Manisha