log4p

Peter Maas’s Weblog

Archive for October, 2007

Anterior Uveitis

Due to the fact that I was “optically challenged” for the past week it might have been a bit silent on my weblog. Since I’m still recovering I’m trying to keep time behind my computer as short as possible.

Normally I would have used this time to read about Leopard, and the fact that it hasn’t got Java6. Or playing around with some fancy new webframework. Today I spent some time reading about Anterior Uveitis; which is the name of the disease causing the pain in my right eye.

eyeSo, what is Uveitis? If we think of the eye as a hollow, fluid-filled, 3-layered ball, then the outer layer is the sclera, a tough coat, the innermost is the retina, the thin light-gathering layer, and the middle layer is the Uvea. The Uvea is made up of the iris, the ciliary body and the choroid (see diagram). When any part of the uvea becomes inflamed then it is called Uveitis.

And on the “anterior” part? Anterior means that it affects the front of the eye, normally the iris (iritis) or the ciliary body (iridocyclitis). Iritis, strictly speaking is an older term for Anterior Uveitis but is still used frequently. Iritis is by far the most common type of Uveitis and also the most readily treated. Having said that, iritis is something that needs quite close monitoring because complications such as raised eye pressure and cataracts can occur.

So even tough the initial inflammation is incredibly painful, and I’ll probably have blurred vision until the end of this week… I’m glad I now know exactly what it is and how it can be treated!

2 comments

Book: JBoss Seam – Simplicity and Power beyond Java EE

JBoss Seam - Cover Yesterday the mailman brought the copy of “JBoss Seam – Simplicity and Power beyond Java EE” by Michael Juntao Yuan and Thoma Heute which I asked Finalist to order for me.

I managed to read the first two chapters. My first impression is that it’s a bit ‘jumpy’. Chapter two contains so many references to upcoming chapters that it tends to loose its’ structure.

Funny to see that the book starts of with ‘cumbersome XML base J2ee frameworks’ but quickly resorts to the shorthand notation ’spring’. I understand that people find spring to be heavy on XML, but hey… weren’t the JBoss people the ones who introduced tons of Hibernate mapping files?

No comments

Grails – 1.0-RC1

Two days ago Grails reached the 1.0 milestone; the first release candidate is out. New Features inlude:

  • GORM features ORM DSL for advanced Mappings
  • Support for easy to use Filters
  • Tag Library Improvements – Namespaces, body variables, page scope, improved tag and more
  • Controllers Improvements – Multi-dimensional params object, data binding retains type conversion errors
  • URL Mapping Improvements – Mapping response codes, mapping directly onto views and more
  • GORM Improvements – Pessimistic locking support, session flushing control
  • Config Improvements – Support for JNDI data sources, property placeholders and flexible config locations

A descriptions of the various features can be found in the releasenotes.

Personally I must say that the 0.6 release brought more interesting new features; but hey… Grails going 1.0 is great news!

No comments

Starting with JBoss Seam (using seam-gen)

After two JSF-free years (I didn’t really enjoy myfaces at that time) I decided to get my head around Seam and JSF. My next project will consist of rebuilding a Delphi application to a multiuser Seam application; so let’s have a look at where we are!

Introduction

First of all, before I get flamed, Seam is not JSF. Seam supports JSF as a view technology. Seam defines a uniform component model for all business logic in your application. A Seam component may be stateful, with the state associated with any one of several well-defined contexts, including the long-running, persistent, business process context and the conversation context, which is preserved across multiple web requests in a user interaction.

There is no distinction between presentation tier components and business logic components in Seam. You can layer your application according to whatever architecture you devise, rather than being forced to shoehorn your application logic into an “unnatural layering” scheme forced upon you by whatever combination of frameworks you’re using today.

The position of Seam in the stack is therefor depicted as follows:

Seam Architecture

The Seam documentation states:

It turns out that the combination of Seam, JSF and EJB3 is the simplest way to write a complex web application in Java. You won’t believe how little code is required!

With my previous Grails experiments I highly doubt it; but we’ll see!
Read more

1 comment

Connecting JConsole to a Java process: the missing -D option

Once in a while I tend to use JConsole for basic profiling of a Java process.

Somehow I always forget which flag I need to set to enable remote connections to a process. And yes, I know the option is hidden somewhere on page 3 of the documentation…

You need to add the following in the VM arguments:

-Dcom.sun.management.jmxremote

After which you can just connect JConcole to the pid of the process you’re interested in.

No comments

Oracle buying BEA?

Various sources are reporting on Oracle putting in an offer to buy BEA for a whopping 6.7 billion dollar.

I did some projects using WebLogic, WebLogic portal and WebLogic workshop but was never really a fan of BEA. I do however like healthy appserver competition!

Somehow I feel that if Oracle actually acquires BEA, WebLogic will die a slow and silent death…. in contrary to what Oracle claims.

Still it seems the Race isn’t over; SAP is in it as well… which isn’t much of a different story…

Whatever the outcome is: I for one will just remove WebLogic (I was already thinking of doing that) from my resumé. WebLogic will be passé really soon.

– update –
The 6.7 billion dollar offer was turned down. Now let’s just wait to see if anyone puts in another offer!

2 comments

Behavior Driven Development

Some months ago I visited Aslak Hellesøys' RSpec presentation at RubyEnRails 2007. I was really intrigued by some of the concepts introduced by RSpec.

After checking out the way testing/TDD works in Grails I just had to see if my initial interest in RSpec would stand ground.

Just to recapitulate on what RSpec is:

RSpec is a framework which provides programmers with a Domain Specific Language to describe the behavior of Ruby code with readable, executable examples that guide you in the design process and serve well as both documentation and tests.

So RSpec gives a developer the means to write tests in a form that matches specification of functional requirements dictated by the customer. They call it behavior driven design.

Conceptually I think this is a good thing. Quite often customers don't actually know what is being tested in their project. Just look at one of the basic methods most often used in testing: assertNotNull. How does a customer know what this is supposed to test? The most obvious answer to this is: documentation/comments... which to be frankly isn't something most developers excel in.

Now, what does it look like... and how do you use it?

Installing rspec and its' Rails plugin is a breeze:

  1. bash> sudo gem install rspec
  2. bash> cd {my_rails_project}
  3. bash> ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec
  4. bash> ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails

After installing the plugins in your Rails project a couple of new rake tasks are available (prefixed with spec:) to create run tests for your models/views/controllers/helpers etc.

RSpec also install some generators: rspec, rspec_controller, rspec_model, rspec_scaffold. These generators are similar to the default Rails counterparts but generate corresponding RSpec tests as opposed to unittests.

Let's get started.

Say we have a really simple specification for a model: "if a new article is created its' description field should contain 'no description entered'".

So, let's start with creating a simple model:

  1. bash>./script/generate rspec_model Article title:string, description:string

This will give us, next to the expected model file and migration, a file called article_spec.rb in the spec directory:

  1. require File.dirname(__FILE__) + '/../spec_helper'
  2.  
  3. describe Article do
  4.   before(:each) do
  5.     @article = Article.new
  6.   end
  7.  
  8.   it "should be valid" do
  9.     @article.should be_valid
  10.   end
  11. end

Which we can run immediately:

  1. bash>rake spec
  2. (in /Users/peter/Development/rails/brs)
  3. .
  4.  
  5. Finished in 0.045879 seconds

And, we can also create a simple overview of the specifications which we test:

  1. 1 example, 0 failures
  2. bash>rake spec:doc
  3. (in /Users/peter/Development/rails/brs)
  4.  
  5. Article
  6. - should be valid

Cool, now let's add our specification:

  1. it "the description attribute should contain 'no description entered'" do
  2.   @article.should == "no description entered"
  3. end

Running the tests results in an error now:

  1. 'Article the description attribute should contain 'no description entered'' FAILED
  2. expected: "no description entered",
  3.      got: nil (using ==)

Setting the default value for the description attribute of Article like this:

  1. class Article <ActiveRecord::Base
  2.   def initialize(attributes = {})
  3.     super(attributes)
  4.     write_attribute(:description, "no description entered")
  5.   end
  6. end

Results in a successful test. And running spec:doc now tells me I have a test for this specification:

  1. bash>rake spec:doc
  2. (in /Users/peter/Development/rails/brs)
  3.  
  4. Article
  5. - should be valid
  6. - the description attribute should contain 'no description entered'

Conclusion
Although this is a really basic example (and there is much more the RSpec) I think I got the point.
I really like the "behavior driven" way of thinking; it provides clear handles for writing tests and creates useful overviews of what is actually tested.

No comments

JBoss Masterclass 2007, Rome

Last couple of days I was attending the JBoss Masterclass in Rome. I learned a lot and met a let of interesting people. And I enjoyed being in Italy as well.

I'll be doing a full round-up (in Dutch) on the Masterclass for the company blog but first of all I'm going to have a good time with family now.

But, I just had to share the view I've been looking at while listening to some of the JBoss gurus:

180 degrees of Rome just from the conference room

The picture was compiled from about 10 separate pictures using the evaluation version of DoubleTake (hence the watermark)

--update--
My full report can be found here (in Dutch).

No comments

Grails and tests – Part II: Testing dynamic taglibs using XmlSlurper

A couple of days ago I wrote my first post on TDD using Grails. Now let's move on to something more interesting: testing dynamic taglibs.

In another post I showed how to create a simple tag. Which looked like this:

  1. import com.plink.plextile.*;
  2. class TextileTagLib {
  3.   TextParser textileParser
  4.   def textile = { attrs ->
  5.     out <<textileParser.parseTextile(attrs.text, true)
  6.   }
  7. }

When generating this test using the "grails create-tag-lib" command Grails also created a skeleton for the corresponding test:

  1. class TextileTagLibTests extends GroovyTestCase {
  2.   void testSomething() {
  3.   }
  4. }

Testing calling the taglib is simple, as opposed to JspTags they don't rely on a container. You can just call the closure with the necessary attributes:

  1. class TextileTagLibTests extends GroovyTestCase {   
  2.   void testRenderTextile() {
  3.     TextileTagLib tl = new TextileTagLib()
  4.     def res = tl.renderTextile(value: "# ab\n# cd\n# ef")
  5.     assert ol != res
  6.   }
  7. }

Since the textile taglib converts the given textile markup to HTML we'll need something to parse the resulting output. Enter XmlSlurper. XmlSluper is a powerful XML parser with lower overheads than XmlParser due to lazy evaluation. XmlSlurper only supports read operations. Using XmlSlurper we can actually test the output of the tag.

We'll enter something like this:

  1. # ab
  2. # cd
  3. # ef

Which should result in something like this:

  1.   <li>ab</li>
  2.   <li>cd</li>
  3.   <li>ef</li>
  4. </ol>

And here the actual implementation of the test:

  1. class TextileTagLibTests extends GroovyTestCase {   
  2.   void testRenderTextile() {
  3.     TextileTagLib tl = new TextileTagLib()
  4.     def res = tl.renderTextile(value: "# ab\n# cd\n# ef")
  5.  
  6.     def ol = new XmlSlurper().parseText(res)
  7.    
  8.     assert ol != null
  9.        
  10.     assertEquals(3, ol.li.size())
  11.     assertEqualsTr("ab", ol.li[0].text())
  12.     assertEqualsTr("cd", ol.li[1].text())
  13.     assertEqualsTr("ef", ol.li[2].text())
  14.   }
  15.    
  16.   def assertEqualsTr(expected, actual) {
  17.     assertEquals(expected, actual.trim())
  18.   }
  19. }

Surely I should have started out with the test in first place, but hey... I first had to see how Grails taglibs work in this case! Since testing is this simple I'll actually consider doing so next time.

2 comments

LazyList based on the Google Collections Library

In my previous post I mentioned implementing the Google Collections counterpart of the commons-collections LazyList. I'll refer to the Google Collections Library as GCL from now on.

The LazyList decorates another List to create objects in the list on demand. To code below is loosly based on the code in commond-collections' lazylist counterpart. Most modifications are generics related.

Since GCL supplies a 'Forwarding' wrappers it's really easy to write a decorator for the List type. Extend from ForwardingList and override the needed methods:

  1. package com.finalist.collections.lazy;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.finalist.collections.factories.Factory;
  7. import com.google.common.collect.ForwardingList;
  8.  
  9. /**
  10. * Decorates another <pre><code>List</code></pre> to create objects in the list on
  11. * demand. Code loosly based on the code in commond-collections' lazylist
  12. * counterpart. Using the forwardinglist provided by Googles' Collection library
  13. *
  14. * When the {@link #get(int)} method is called with an index greater than the
  15. * size of the list, the list will automatically grow in size and return a new
  16. * object from the specified factory. The gaps will be filled by null. If a get
  17. * method call encounters a null, it will be replaced with a new object from the
  18. * factory. Thus this list is unsuitable for storing null objects.
  19. *
  20. * @author Peter Maas
  21. *
  22. * @param <T> Type of the objects contained in the list
  23. */
  24. public class LazyList<T> extends ForwardingList<T> {
  25.     private static final long serialVersionUID = 1;
  26.     private Factory<T> factory;
  27.  
  28.     protected LazyList(Factory<T> factory) {
  29.         super(new ArrayList<T>());
  30.         this.factory = factory;
  31.     }
  32.  
  33.     protected LazyList(List<T> delegate, Factory<T> factory) {
  34.         super(delegate);
  35.         this.factory = factory;
  36.     }
  37.  
  38.     public static <D> LazyList<D> decorate(List<D> delegate, Factory<D> factory) {
  39.         return new LazyList<D>(delegate, factory);
  40.     }
  41.  
  42.     /*
  43.      * (non-Javadoc)
  44.      *
  45.      * @see com.google.common.collect.ForwardingList#get(int)
  46.      */
  47.     public T get(int index) {
  48.         int size = delegate().size();
  49.         if (index <size) {
  50.             // within bounds, get the object
  51.             T object = delegate().get(index);
  52.             if (object == null) {
  53.                 // item is a place holder, create new one, set and return
  54.                 object = factory.create();
  55.                 delegate().set(index, object);
  56.                 return object;
  57.             } else {
  58.                 // good and ready to go
  59.                 return object;
  60.             }
  61.         } else {
  62.             // we have to grow the list
  63.             for (int i = size; i <index; i++) {
  64.                 delegate().add(null);
  65.             }
  66.             // create our last object, set and return
  67.             T object = factory.create();
  68.             delegate().add(object);
  69.             return object;
  70.         }
  71.     }
  72.  
  73.     /*
  74.      * (non-Javadoc)
  75.      *
  76.      * @see com.google.common.collect.ForwardingList#subList(int, int)
  77.      */
  78.     public List<T> subList(int fromIndex, int toIndex) {
  79.         List<T> sub = delegate().subList(fromIndex, toIndex);
  80.         return new LazyList<T>(sub, factory);
  81.     }
  82. }

As you can see I modified code from the commons-collections counterpart to use generics. I reimplemented parts of commons-collections' factory structure to use generics as well.

The next test concludes in success:

  1. package com.finalist.collections.lazy;
  2.  
  3. import java.util.List;
  4. import com.finalist.collections.factories.FactoryUtils;
  5. import junit.framework.TestCase;
  6.  
  7. public class LazyListTest extends TestCase {
  8.  
  9.     /**
  10.      * Test for the get(int) method of the LazyList
  11.      */
  12.     public void testGetInt() {
  13.         List<Dummy> dummyList = new LazyList<Dummy>(FactoryUtils.instantiateFactory(Dummy.class));
  14.        
  15.         // initially the list should be empty
  16.         assertEquals(0, dummyList.size());
  17.        
  18.         // accessing indexes directly should not result in index-out-of-bounds exceptions
  19.         assertNotNull(dummyList.get(0));
  20.         assertNotNull(dummyList.get(10));
  21.        
  22.         // size should now be 11:
  23.         assertEquals(11, dummyList.size());
  24.        
  25.         // the spaces between the to requested indexes should be populated with
  26.         // null values:
  27.         assertTrue(dummyList.contains(null));
  28.         assertEquals(1, dummyList.indexOf(null));
  29.     }
  30.  
  31. }

Please note that the above isn't a showcase for the features of GCL, its' a simple extension to add a LazyList implementation. Feel free to use or extend it!

full sources, test and buildfile

4 comments