Archive for June, 2006


icalendar and Rails

Monday, June 26th, 2006

I’ve recently used icalendar to output an ical from a Rails app I’m running. It’s incredibly simple, here’s what I did:

  1. Create an ical_controller:
    script/generate controller ical
  2. Add a method to generates the events:
    require 'icalendar'
    
    class IcalController < ApplicationController
    
      caches_page :competitions
    
      def competitions
        @cal = Icalendar::Calendar.new
    
        Competition.find_all.each do |comp|
          event = Icalendar::Event.new
          event.start = comp.date
          event.end = comp.date
          event.summary = comp.name
    
          @cal.add event
        end
      end
    
    end
  3. Create a view (app/views/ical/competitions.rhtml):
    < %= @cal.to_ical %>
  4. Install icalendar into your vendor directory:
    cd vendor
    gem unpack icalendar
  5. Add the necessary dependency to config/environment.rb
    require 'icalendar-0.96.4/lib/icalendar'

And that’s it. Once deployed you can access your ical and import it anywhere. I’ve tried mine out with Google Calendar and it imported without a hitch. One deficiency in icalendar is the inability to give a name or description to the calendar itself, so you’ll have to rename it in your calendar application. The only caveat in the above code is the caching directive in the controller. I’ve cached the entire page because the events don’t change frequently. To expire the cache you’ll have to add the following line to the appropriate controller methods where your events changes:

expire_page :controller => "ical", :action => "competitions"
Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit icalendar and Rails digg.com digg it!  |  reddit reddit!

Who’s in charge of distributing mobile games?

Monday, June 26th, 2006

John Carmack is making mobile phone games these days. His current one is an RPG called Orcs and Elves. It looks pretty cool:

so I figured I’d give it a go. It’s a mobile phone game so I thought I should be able to get it pretty quickly, how wrong I was. I couldn’t buy it directly off ID, it’s being published by EA Mobile. No problem I go to their site, there it is for $2.99, I click Buy Now. I’m then told I need to select my mobile provider, but it only lists American ones. I’m on the American site, so I switch the to UK one. Orcs and Elves is nowhere to be found, at least on the front page. I use their game finder, which requires me to select my provider. I select Vodafone, it brings me to a page that tells me I need to go to the Vodafone website on the web or on my phone. They can’t even tell me which EA games are available through Vodafone.

I go to the Vodafone games site. I can’t see Orcs and Elves, and they force me to find games by browsing by phone model and type of game. I try the search, it comes up with nothing. I’m not sure if it searches the games, so I try searching for their current top game, FIFA 2006. There’s one result and it directs me to this page (a 404 for those too lazy to click). Clearly their search is broken so I end up browsing, thankfully there’s an EA category, because the Action/Adventure has 11 pages to browse through.

End result? They don’t have it. They do have Doom RPG, ID’s first attempt at a mobile game, which has good reviews, but I’m not that interested in a turn based version of Doom. Even if I was it costs £5.00, three times more than I could buy the newer game if I was in America! Why is getting a hold of this game so difficult? I don’t buy it in a shop, I don’t get a CD, I get it off the Internet, and with a phone connected to the Internet I should be able to get it virtually anywhere. But instead I’m forced to wait for my mobile provider to get it, then get charged a huge markup because I’m in the UK and can only get it through them.

I hope this isn’t a sign of how the mobile Internet is going to work for all media.

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit Who’s in charge of distributing mobile games? digg.com digg it!  |  reddit reddit!

Scala Might Have Legs

Saturday, June 17th, 2006

It looks like Scala is creeping into the mainstream. I definitely have to try it out on something ‘real’ soon. In the meantime here are some posts I’ve come across introducing Scala:

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit Scala Might Have Legs digg.com digg it!  |  reddit reddit!

Software project late again?

Saturday, June 17th, 2006

This post brought a smile to my face. Sound familiar? ;)

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit Software project late again? digg.com digg it!  |  reddit reddit!

Why do default implementations have such bad names?

Saturday, June 17th, 2006

Small rant about class names in Java, or more specifically classes who are the sole implementation of a particular interface. You go to the effort of coming up with a good name for your interface, e.g. IndexSearcher, but then the class name is enevitably one of:

  • SimpleIndexSearcher
  • IndexSearcherImpl

I’m guilty of this. There must be a better way to name our classes!?!

Don’t forget the other pattern, if IndexSearcher is your class, then IIndexSearcher is your interface. I hate that one too. :)

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit Why do default implementations have such bad names? digg.com digg it!  |  reddit reddit!

Mock Objects and Testing Controllers

Saturday, June 17th, 2006

Up to now I assumed mock objects were just dummy objects you used in your unit tests when you didn’t want to use a real object, e.g. because the real object goes off and does something weird to a remote server. But apparently Mocks Aren’t Stubs, they are objects that allow you to do a different style of testing. Most units tests involve verifying the correct state of an object after an interaction, with mocks you can make sure the right methods are being called in the first place (see Martin Fowler’s article for more).

Most of the time I wouldn’t want to test the interaction, I just want to test an object, so mocks aren’t needed, but one area of my programming that always falls short of testing is controllers (in the MVC sense). They just require too many bits and pieces to get running that you eventually end up running your entire program for one test. Then I came across a couple of posts from Robert Chatley, who was in the same research group as me at university. He talks about using Spring’s mocks, as well as using JMock. That’s pretty much what I wanted to do and it all looked very easy.

Imagine we have a controller that does a Lucene search, we want to make sure it’s actually doing the search, here’s an example test case using JMock:

public SearchControllerTest extends MockObjectTestCase {
    private Mock searcher;
    private Mock logger;
    private SearchController controller;

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    public void setUp() {
        super.setUp();

        this.mockSearcher = mock(IndexSearcher.class);
        this.mockLogger = mock(QueryLogger.class);

        this.request = new MockHttpServletRequest();
        this.response = new MockHttpServletResponse();
        this.request.setMethod(“GET”);

        this.controller = new SearchController();
        this.controller.setSearcher((IndexSearcher) searcher.proxy());
        this.controller.setQueryLogger(logger.proxy());
    }

    public void testSearch() {
        // Set our search term
        this.request.setParameter(“q”, “apples”);

        // Set our expectations
        Mock mockHits = mock(Hits.class);
        mockHits.stubs().method(“length”).will(returnValue(10));

        this.mockSearcher.expects(once()).method(“search”).will(returnValue(hits));

        // Do the request
        ModelAndView modelAndView = this.controller.handleRequest(this.request,
                                                                  this.response);

        // Verify our hits are stored in the model
        Map model = modelAndView.getModel();
        assertEquals(model.get(“hits”, mockHits.proxy());
    }

    public void testSearchWithNoResults() {
        // Set our search term
        this.request.setParameter(“q”, “apples”);

        // Set our expectations
        Mock mockHits = mock(Hits.class);
        mockHits.stubs().method(“length”).will(returnValue(0)); // We have an empty hits object

        // We’re not checking this time, just providing a stub to return our
        // hits object
        this.mockSearcher.stubs().method(“search”).will(returnValue(hits));

        // Because we have no results, we expect this query to be logged.
        this.mockLogger.expects(once()).method(“noHits”);

        // Do the request
        ModelAndView modelAndView = this.controller.handleRequest(this.request,
                                                                  this.response);
    }

}

In the above example we have two test cases, the first checks a search is executed correctly, and the second checks a special case where we log the query if it returns no results. We setup several mock objects, and for each one you have to state what calls you expect to be made on it, and what (if any) return value should be given.

There are two ways to get a mock to respond to a method, either with expects() or stubs(). Use expects if you want to test the method call. You’ll have to specify none(), once() or atLeastOnce(). Use stubs if you just want it to return a value, e.g. the length of our Hits object.

One nice thing about JMock is that the tests are sentence like, it’s very easy to read and understand what a line is doing, e.g. the searcher expects the method ’search’ to be called once. One (major) problem with JMock is the completely barren Javadocs. There is no documentation for the MockObjectTestCase class. This makes it pretty hard to write a test unless you’re copying someone else’s example.

This above example is very simple, but it’s possible to test error conditions, page flow and various other niggly bits of controllers, which means we can be confident of their behaviour and no longer have to ‘run and pray’.

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit Mock Objects and Testing Controllers digg.com digg it!  |  reddit reddit!