Vishwanath Krishnamurthi's blog

A blog on Java EE, clean code, open source and TDD

Contributing to open source projects

with one comment

Recently I was voted as a committer at the lovely OpenEJB project !  Am I excited ? You bet !
For a long time I had wanted to contribute to Open Source projects that I love. Like everything takes a start somewhere, my start was at the OpenEJB project a few months back. Thanks to the friendly folks there who are great at mentoring, I’ve come to understand a few things  about “contributing to open source projects” in general, which I thought I’d share.

So which project to contribute to ?

The answer is pretty simple. Whichever project/projects that interest you.

But the project looks daunting.

Yes it might be. But there’s no rush to understand everything all at once. There’s always a small piece of work, to start from. Building an understanding is a gradual process…

What are the first things?

Get in touch with the folks at the project.

Almost all projects have a mailing list. Subscribe to the mailing list and observe what’s going on.. But without being shy or hidden for long, pop out a “Hi” to the folks there, just introducing yourself. You could ask them to suggest simple pieces that you could start with.

Mental Todos versus Saying a Hi ?

We make mental todos like “Wow.. This project is so good. I must get involved and contribute..” but never really start at it. I think saying a “Hi” and getting in touch,  works better than making mental todos.

Mailing lists are great and essential because members work from different timezones and there’s a need for asynchronous communication.

But that’s not the only way to get in touch. Most projects have an IRC chat,.. so you could chat up !

Contribute ? But I am not an expert… 

You don’t have to be !  Whether you are a beginner or an expert in that technology is not a factor for contributing to an open source project.

Just ,

1) Your interest

2) Time you could afford

would be the factors.

Don’t have hours to spend ? No big deal…

It’s of course great if you could have a few hours everyday to spend at the project. But even if you don’t, there are things you could to with the limited time. Like

1) Answer a user’s question

2) Document something

3) Blog a “how-to”

4) Read up some project docs and get better.

It’s not all about patches and code !

A must read…

I hope to add more to this post later on, but check out the awesome  “contribution tips” OpenEJB doc.   Excellent tips there !!

***End of post. WordPress Ads may follow***

Written by Vishwanath Krishnamurthi

December 11, 2011 at 5:09 pm

Posted in Thoughts

Tagged with , ,

Fun with unix commands -(Bash & imdb-api )

with 2 comments

I’ve been a Windows user for long. I had wanted to switch to Linux, learn a good bit of commands and shell scripting, but never really started until about a month back. So with a fresh install of Ubuntu,  as I was trying with Seds and awks, I just thought I could put these newly learned commands into fun use. And I remembered how I wished for the 1000GB of movies (my brother had that in an external harddisk) sorted based on imdb ratings.

Geekish work starts:

There’s a great RESTful API put up at http://www.imdbapi.com/

So all that was required was:

  • Scanning the filesystem for video files
  • Strip away unwanted words from movie’s filename
  • Encode the filenames (esp, spaces and apostrophes)
  • Send request to imdbapi
  • Parse the result 

So what I ended up with was some bash scripts run like this:

sh lsMovies.sh | ./encode.sh > mlist.txt

sh askImdb.sh  (that saves JSON response in a file mresponse.txt)

Not much need to parse it. Just replace every } with a newline character and save it as a CSV file.

Open the file with the seperator character set as : and voila ! Movie names, ratings, votes, decription etc go into different columns.

Ha, so can do all sorts of sorting 😉

Here’s the code:

Scanning the filesystem for video files, trimming away needless words from filenames

#lsMovies.sh -Scan directory given as argument. Else scan current directory for videos.
#!/bin/bash

if [ -z $1];
then
echo 'Directory parameter not provided. Searching in current dir'
dir='.'
else
echo 'Searching in' $1
dir=$1
fi

find $dir -iname *.avi |
sed "s/.*\///g" | #remove any path info
sed "s/\./ /g"| #replace . with space
sed "s/\[.*//g; s/(.*//g; s/DVD.*//i; s/xvid.*//i; s/rip.*//i; s/.avi//g"

Then the encoding part

encode.sh


#!/bin/bash
sed "s/ /%20/g" |
sed "s/'/%27/g"

Sending request: askImdb.sh


echo "" > mresponse.txt #clear contents initially
count=1
while IFS= read -r line
do
echo "Movie #"$count "requesting imdb for info about"$line
curl http://www.imdbapi.com/?t="$line" >>mresponse.txt
count=$(($count+1))
echo "info on" $count "movies retrieved"
done

So to cheat without parsing, the insertion of newlines


 cat mresponse.txt |
sed "s/}/\n/g"

plus saving this as csv and opening with : as separator is all that’s left !

I’m walking down the list watching the best rated movies ! Fun 🙂

[Update: Did a Java Swing application later, that scans the filesystem for .avi files, queries imdbapi.com for imdb info for each movie, parses the result using Jackson and creates  a CSV report of it using OpenCsv. Try out https://github.com/stratwine/movie-insight.  Download section has the executable JAR ]

***End of post. WordPress Ads may follow***

Written by Vishwanath Krishnamurthi

October 26, 2011 at 6:36 pm

Posted in Uncategorized

Tagged with , , ,

Apache TomEE Quickstart – Details on Configurations

leave a comment »

Apache TomEE is now released (beta-1).

What makes it so exciting is that, “develop – deploy -test ” cycle is extremely fast, allowing the developer to experience the “lightweighted-ness” Well, no more talk..

Here’s a quick list on configurations.

So, download TomEE and get on with ‘EE’ the light-way 😉

Written by Vishwanath Krishnamurthi

October 7, 2011 at 11:38 am

Understanding Persistence Context

with 3 comments

Here’s something on Persistence Context !

This understanding below, is mainly based on what I had read ( and re-read ) in this great article by Sahoo

But before you read further, these are the short-forms used in this post.

PC – Persistence Context

BMT – Bean managed transactions

CMT – Container Managed Transactions

SFSB – Stateful session beans

OO – Object Oriented

What is a Persistence Context ? 

Imagine a circle that depicts the OO world. We will call this the PC circle (Persistence Context circle) Imagine another circle that depicts the DB world.

The DB circle depicts only one data-store. There can be multiple entity instances within this PC. For each entity within the PC there is definitely a corresponding row in the DB world. All entities in this PC are managed i.e. any updates (CRUD) done here will be reflected in the DB world.

Well, its a collection of entity instances. And changes to those instances gets reflected in the db rows. Is it anything more ?

Yes. You can think of it as a cache too, since this is an in-memory collection. Once an object is “attached” it is a part of this in-memory-collection.

The term, ‘Persistence Context’ looks pretty abstract. Something more concrete ?

Any operation on the collection of entity instances (PC) is managed by an EntityManager. One entity manager would be associated with a persistence context.. Something like this

And anything in the PC is modified (CRUD) only using an EM.

Fine ! So when is a PC created ?

  • A persistence context is created as soon as the entity manager is created, in the case of Application managed persistence.
  • In the case of container managed persistence the PC is created when the EM is invoked.

 I want to access this collection of entity instances in another component (bean / servlet.. ) How can I do that ?

There are two options.

Option A:  Pass around the EM instance associated with the PC

Option B:  Let the container propagate the PC. ( And associate it with the entity manager of the component that was propagated to )

Option B being much cleaner.

This is called as Persistence Context Propagation. But there is a catch here. It doesn’t do it every time. When using BMT it thinks that you know what you’re doing and doesn’t propagate the PC. PC propagation comes up only with CMT. We know that a transaction can be propagated. REQUIRED, SUPPORTS allow transaction propagation. A PC is propagated along with such a transaction propagation.  But note that there’s no propagation to a bean that already has a PC associated with it !

Written by Vishwanath Krishnamurthi

August 21, 2011 at 7:21 pm

EJB 3.1 Cookbook by Richard Reese – Review

with one comment

Recently, I received a free copy of EJB 3.1 Cookbook, from PacktPub for review.

But I must say that it did not meet my expectations in a lot of ways.

Some had to do with

  • Bad practices given as recipes
  • Lengthy sections on concepts not related to EJB
  • EJB 3.1 Specific features not being highlighted enough.

To explain,

EJB 3.1 Specific features not being highlighted enough:


If you have been following some popular blogs on JavaEE, you’d have already been exposed to some EJB 3.1 features. I fall into that category. And my natural expectation was that, this book would explain those concepts in much more detail. But much to my disappointment, it was the other way around.

 In ‘Packaging the EJB’  Chapter :

I expected to learn about how packaging EJBs was made simpler in EJB 3.1 compared to EJB 3. The most important one being that, it’s no longer required in EJB 3.1 to make an .ear file, and EJBs could now be packaged within WAR.

There’s only a two line description of this, something that a reader could easily miss.The consolidated table in page 348 misses to specify this too.

Instead there was a lengthy explanation on how to archive using the ‘jar’ command in Java, something that I’ve never seen developers doing.

 The Embedded Container:

EJB 3.1 makes testing EJBs easy with the embedded container.  And a team could benefit a lot writing unit tests with this newly introduced API. But there was only a small section on ‘Accessing EJB 3.1 embedded container’.

Wish more examples / more focus on embedded container were present.

Bad practices given as recipes:

EJBs were accessed directly from JSP using scriptlets! Using scriptlets in JSP is considered as a bad practice, something that was done a decade back.

Hundreds of threads in Javaranch explain users of it, and you’d find JSP moderators (like Bear Bibeault ) explaining that often.

Lengthy sections on concepts not related to EJB:

Sections ‘Exception Handling with EJB ‘ and ‘Using Logging with EJB ‘ had nothing to do with EJB after all and merely talked about good practices on exception handling and logging rather than talking about EJB specific exceptions.

I don’t feel the need for a general lecture on “exception handling / logging ” in a EJB 3.1 specific book and if at all, present, I don’t see why it should be in a chapter rather than appendix.

The same about the sections ‘Efficient mainpulation of Strings‘ and ‘How to support Currency

 The good part:

The good thing you’d find about the book is that it is pretty easy to read through, with the sections of the code being discussed, presented in bold.

The language was kept simple, with screenshots and tables when required.

To summarize,

This is a book, that is easy to read. Had it only concentrated only on ‘EJB 3.1’ specific features, in 100 odd pages, it would’ve been a pretty good.

If you wanted a book to get a quick rough picture, in a week’s time, then I’d recommend this. If you had more time, and wanted a book to help you develop a firm understanding of the concepts, I wound not recommend this one.

2/5 Stars

Written by Vishwanath Krishnamurthi

August 8, 2011 at 6:35 pm

Posted in Java EE

Using mockito to unit test Spring Webflow (2)

with one comment

This is as a continuation to this post on unit testing flows in Spring Webflow.

I wish to start testing  from an intermediate state (other than start state )

Sure, No Problem.

@Test
public void  discountCheck()
{
this.setCurrentState("viewDiscountDetails");
resumeFlow(context);
//Now we should be in viewDiscountDetails state. Can fire any transition defined for this state.
context.setEventId("checkIfDiscountValid");
resumeFlow(context);
}

So the only thing to watch in the above snippet is that, if  you are starting your test from an intermediate state,  set the state to start with and then use resumeFlow()  instead of using startFlow() .

Also note that the <on-entry> …. </on-entry> expressions of  ‘viewDiscountsDetails’ state are not evaluated when the test is executed. I think that is because by setting the currentState, we have told SWF, that the state has already been entered. (So it wouldn’t bother with the on-entry expressions 😀 )

How about stubbing of some scoped attributes ?

We could simply add this line in the test.

this.getFlowScope().put("productSelected", new Car());

But note that this line is valid only after you’ve started a flow.. Hence the following would result in an error, since there’s no active flow.

this.getFlowScope().put("productSelected", new Car());
this.startFlow();

Asserting  for an action-state  doesn’t work

Once a flow is started, it goes through the decision-states or action-states and pauses its execution only in a view-state. Hence asserting a current state to be an action state or a decision state would never pass.

Instead, we can use  the verify method in mockito to test if a method in the action was indeed invoked.


   this.startFlow(context);
   context.setEventId("processPayment");
   resumeFlow(context);
   verify(mockedBazService.paymentProcess(accountInfo)); // verify an action method was executed
  assertCurrentStateEquals("paymentSuccessfulView"); //assert execution has paused in this view state

Switching to some general talk, the question we’d have while writing these unit tests is

What could SWF unit-testing expose ?

I haven’t tried with all the below points, but looks like it is all possible.

  • Ensure validators bound were actually called
  • Ensure exception handler catches exceptions as expected
  • Ensure a matching transition does exist 
  • Ensure the traversal path was as expected 
  • Ensure a model is bound to a view correctly : assertModelAttributeNotNull()
  • Help us in the use of right attributes in the right scope: getRequired<XXX>Attribute() asserts that an attribute is present in that scope. Ensure it is present / cleared etc. Probably the best way to understand the use of flashScope, viewScope etc that SWF provides. 
  • Ensure a view was resolved as expected
  • Simulate a refresh and run some asserts
I wish to trace the flow better / I would like to make some hacky workarounds:
Its pretty easy to attach a listener to the flow using
this.setFlowExecutionListener( new MyListener());
where MyListener is a class that extends FlowExecutionListenerAdapter and overrides the required methods.
Well then, I’ve typed enough.. Time I get back to my IDE. Enjoy your coding ! and do let me know of any helpful SWF testing tips. 😉

Written by Vishwanath Krishnamurthi

July 21, 2011 at 5:50 pm

Using Mockito to unit test Spring Webflow

with 6 comments

The flows in Spring Webflow act as powerful controllers. And it is very crucial in a webapp to have the controllers tested.

Unit testing Spring Webflow becomes all the way easier, when a mocking framework is used. And I find Mockito to be pretty good in that arena.

Without much talk, here goes some code:

Step1:
To extend AbstractXmlFlowExecutionTests.

Let’s locate the flow-under-test as such

@Override
 protected FlowDefinitionResource getResource(
 FlowDefinitionResourceFactory resourceFactory) {
 FlowDefinitionResource resource = resourceFactory.createResource("foo-flow.xml");
 Assert.notNull(resource);
 return resource;
 }

Step2:

Mock away the beans used in flow


BazService mockedBazService;

@Override
 protected void registerMockFlowBeans(ConfigurableBeanFactory flowBeanFactory){
 mockedBazService=mock(BazService.class);
flowBeanFactory.registerSingleton("bazService",mockedBazService);
 flowBeanFactory.registerSingleton("barService", mock(BarService.class));
 flowBeanFactory.registerSingleton("MyExceptionHandler", mock(MyExceptionHandler.class));
 }
}

Note that I’m holding the reference for BazBean so I can do some stubbing using Mockito later..
Step3:
Then lets code up a test.

 MockExternalContext context = new MockExternalContext();
 @Test
 public void testFlowShouldEnterStartState()
 {
  this.startFlow(context);
  assertCurrentStateEquals("myFirstState");

 }

How about triggering an event, and checking a transition ?

@Test
 public void testStateWhenFooBarProcessingEventTriggered()
 {

   this.startFlow(context);
   context.setEventId("startProcessingFooBar");
   resumeFlow(context);
   assertCurrentStateEquals("fooBarProcessor");
 }

Some stubbing..

 @Test
 public void testProcessingDetails()
 {
    this.startFlow(context);
    when(mockedBazService
     .isDetailsAvailable(anyString())
     ).thenReturn(true);
    context.setEventId("processDetails");
    resumeFlow(context);
    assertCurrentStateEquals("processDetails");

 }

Enough to get started with testing webflows, I guess. I’ll update more later 🙂

Update:

Some more observations on SWF unit testing.

Written by Vishwanath Krishnamurthi

July 12, 2011 at 10:59 am