Tuesday, January 29, 2013

Fast shell utilities to report Maven's unit tests results

I would like to share couple shell utilities that I have collected. These are for fast Maven multi modules reporting from your unit tests results, without having you to run entire Maven site reports, which can take a LONG time to generate if you have a large project! They should work on Linux or Window's Cygwin shell.

# Show failed tests among all the surefire results.
function failedtests() {
    for DIR in $(find . -maxdepth 3 -type d -name "surefire-reports") ; do
        ruby -ne 'puts "#$FILENAME : #$&" if $_ =~ /(Failures: [1-9][0-9]*.*|Errors: [1-9][0-9]*.*)/' $DIR/*.txt

# Show the top tests that took the longest time to run from maven surefire reports
function slowtests() {
    for DIR in $(find . -maxdepth 3 -type d -name "surefire-reports") ; do
        FILES="$FILES $DIR/*.txt"
    head -q -n 4 $FILES \
        | ruby -ne 'gets; print $_.chomp + " "; gets; print gets' \
        | ruby -ane 'printf "%8.03f sec: ", $F[-2].to_f; puts $_' \
        | sort -r \
        | head -10

When developing with Maven, you often want to see a summary of failed tests, and you want those surefire TXT file content to see what's going on. The failedtests function will give you a list of all the failed tests filenames in all modules; and then you can cat each one to investigate.

With slowtests function you may quickly see the top 10 most time consuming tests from your project!


Updated (2013/01/30)

I found out that the head command on MacOSX doesn't have the "-q" option and it always prints the "==> filename <==" lines. How annoying! To work around this, you can use this ruby command replacement instead:
# Replace this
head -q -n 4 $FILES \

# With this
ruby -e 'ARGV.each{ |n| File.open(n) {|f| 4.times{ puts f.readline}} }' $FILES \

It's a tad longer, but it's PORTABLE!

Saturday, January 12, 2013

Apache Tapestry 5 makes Java web development easy

I went through the Apache Tapestry 5 tutorial today and found it surprisingly easy to use. The two most productive features I love are auto class reloading and rich built-in components! I can edit the model/controller/view in a java IDE that auto compiles upon file save, and then reload browser to see immediate changes. I think any Java web framework should have this feature in order to be count as productive. (Yes there is JRebel, but it's commercial, and nothing more simpler than a framework supports it directly. Well to be fair JRebel is much more advance and richer in support your Java classes reloading, however for purpose of web dev, Tapestry seems to provide a sweet spot and boot productivity tremendously.)

I was intrigued enough to continue reading the rest of their documentation for advance usage and architecture design. I love simple and yet productive libraries/framework design principles, and I think Tapestry fits into that category. Their documentation is rich and well organized. I browsed their forums and user communities and they seem to be active and plenty of usage in the field. I also found these jumpstart examples to be extremely helpful.

I am happy that I spent the time in learning this framework. I think it makes web development easy, fast and fun to work with. If you haven't tried it before, or have not tried their latest version 5 lately, I would highly recommend you to give it a try.

Tuesday, January 1, 2013

Java implementation of String#next() successor

I've found the Ruby's String#next() or #succ very useful and productive, specially when generating data for testing. Here is what the Ruby doc says:

succ -> new_str

next -> new_str

Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character > if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter > of the same case. Incrementing nonalphanumerics uses the underlying character set’s collating sequence.

If the increment generates a “carry,” the character to the left of it is incremented. This process repeats until there is no carry, adding an additional > character if necessary.

"abcd".succ        #=> "abce"
"THX1138".succ     #=> "THX1139"
"<<koala>>".succ   #=> "<<koalb>>"
"1999zzz".succ     #=> "2000aaa"
"ZZZ9999".succ     #=> "AAAA0000"
"***".succ         #=> "**+"

So when I saw Groovy actually has provided a String extension #next() method, I was happy to try it out. But then I was quickly disappointed when the behavior is very different. The Groovy version is very simple and actually not very productive since it simply loop through Character set range in incrementally (including non-printable characters blindly!). The Ruby's version, however, is much more productive since it produce visible characters. For examples:

bash> ruby -e 'puts "Z".next()'
bash> groovy -e 'println("Z".next())'

I wish Groovy version would improve in future as it's not very useful at the moment. Just for fun, I wrote a Java implementation version that mimics the Ruby's behavior:

And here is my unit test for sanity check: