Thursday, December 23, 2010

A Sample Web Application - 2 (Junit & Emma)

This is in continuation with my previous post


Now i have added following
  • Junit
Junit is a unit testing framework for Java. To use Junit with maven all i had to include it as a dependency in pom.xml

Then i wrote a test class (MatchingUsernamePasswordValidatorTest) for testing the isValidLogin(IUserContext) method of class (MatchingUsernamePasswordValidator)

As you can notice the test classes name just appends Test to the class name being tested.

This test class contains 2 tests. One for the happy scenario in which user name and password matches and another for negative scenario in which username and password don't match.

Now run
$mvn test

At the end of the output you can expect to see something like this

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running mayank.anup.ecom.login.MatchingUsernamePasswordValidatorTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.125 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
  • Emma
Emma is a code coverage tool. I have been recently introduced to it. With this you can know that how much of your code was executed during the time of your test execution. Again you have to include it in your pom.xml as a dependency.

So your pom.xml dependency list may have something like this














Now you can run emma reporting using following

$mvn emma:emma
You
can expect to see an output like following

[INFO] [emma:instrument {execu
tion: default-instrument}]
[INFO] Instrumenting classes with EMMA
processing instrumentation path ...
instrumentation path processed in 266 ms
[3 class(es) instrumented, 7 resource(s) copied]
metadata merged into [E:\myWorkspace\amj-ecom\target\coverage.em] {in 31 ms}
[INFO] [resources:testResources {e
xecutio
n: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platfo
rm dependent!
[INFO] skip non existing resourceDirectory E:\myWorkspace\amj-ecom\src\test\resources
[INFO] [compiler:testCompile {exe
cution: default-testCompile}]
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dep
endent!
[INFO] Compiling 1 source file to E:\myWorkspace\amj-ecom\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: E:\myWorkspace\amj-ecom\target\surefire-reports

-------------------------------------------------------
T E S T S
---------------------------------------
----------------
Running mayank.anup.ecom.login.MatchingUsernamePasswordValidatorTest
EMMA: collecting runtime coverage data ...
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.172 sec

Results :

Tests run: 2, Failures: 0, Errors:
0, Skipped: 0

EMMA: runtime coverage dat
a merged into [E:\myWorkspace\amj-ecom\coverage.ec] {in 31 ms}
[WARNING] While downloading hibernate:hibernate:3.0.5
This artifact has been relocat
ed to or
g.hibernate:hibernate:3.0.5.


[INFO] [emma:emma {execution: default-cli}]
processing input files ...
2 file(s) read and merged in 47 ms
writing [xml] report to [E:\myWorkspace\amj-ecom\target\site\emma\coverage.xml] ...
writing [html] report to [E:\myWorkspace\amj-ecom\target\site\emma\index.html] ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24 seconds

The key thing to note is that first emma does some byte code level instrumentation with your generated main classes, then it
runs the Junit Test and finally prints the result output in html format.


Monday, December 20, 2010

Reading: Wise & Otherwise (Sudha Murty)

Here are some of the good lines from this book.

Conversation
I have a theory about conversation. You can call it an empirical formula. Quantitatively speaking, 'conversation' is inversely proportional to economic standing. If you are travelling by bus, your fellow passengers will get into conversation with you very quickly and without any reservation. If you are travelling by first class on a train, people will be more reserved. If you are travelling by air, then the likelihood of entering into conversation is quite small. If you are on first class in an international flight, then you may travel twenty four hours without exchanging a single word with the person sitting next to you.

Friday, December 10, 2010

Reading: Unaccustomed Earth (Jhumpa Lahiri)

Recently we bought 2 books from the makeshift street side bookstore in Parihaar Chowk. One is "Unaccustomed Earth" by Jhumpa Lahiri and other is "Intelligent Investor" by Benjamin Graham.
Before this i have not got a chance to read any other work of Jhumpa Lahiri. She has written famous books ("The Interpreter of Maladies" and "The Namesake").

This books is a collection of short stories, divided in 2 parts. Part one contains 4 unrelated stories, whereas part two contains 3 stories which are linked to one another.
All of the stories revolve around the Indians (first or second generation) living in US. It is quite a fascinating read. Author has captured the psyche of characters very vividly. The relationships between father and daughter, between mother and daughter, between brother and sister and so on is explored in the stories in their given context. I was able to relate to many of the points of story because of my stay in US.
Although all stories are brilliant, my favorite ones are "Unaccustomed Earth", "Hell-Heaven", "Only Goodness" and "Once in a Lifetime". I have read fiction after 8-9 months and it was very refreshing to do so. Looking forward to read other above mentioned books in near future.