Grails: Mocking localization in Controllers

programming No Comments »

If you are wondering how to mock the localizaton provider in a Grails Controller, these are two example on how to do it.

The first one  (FooController) is used when you want to have an instance of MessageSource injected as a member of your controller.

class FooController {
  def messageSource

  def index = {
    def someMessage = messageSource.getMessage("some.key")
    log.debug("Localized message: " + someMessage)
    render(someMessage)
  }
}

And its test class:

class FooControllerTests extends ControllerUnitTestCase {
  void testIndex() {
    //controller.messageSource = [getMessage: { def key, def locale -> return key }]
    controller.messageSource = [getMessage: { def key -> return key }] //returns the input key
    controller.index()
    assertEquals "Should return a localized key", "some.key", controller.response.contentAsString
  }
}

The second one (BarController) is when you prefer to use the “message” closure:

class BarController {
  def index = {
    def someMessage = message(code: "some.key", default: "Some default message")
    log.debug("Localized message: " + someMessage)
    render(someMessage)
  }
}

Finally, how to mock it:

class FooControllerTests extends ControllerUnitTestCase {
  void testIndex() {
    //controller.messageSource = [getMessage: { def key, def locale -> return key }]
    controller.messageSource = [getMessage: { def key -> return key }] //returns the input key
    controller.index()
    assertEquals "Should return a localized key", "some.key", controller.response.contentAsString
  }
}

Quick and simple. I hope you enjoy these simple and useful examples.

Exception Handling vs. Reflection calls. Wich is more expensive?

programming 2 Comments »

In terms of performance, it is well known that Reflection Calls and Exception Handling are some of the most expensive operations in a programming language.
However, which of them is more expensive?

It only makes sense to compare the performance of these two mechanisms, if you have a situation on where one can replace the other. I have to admit that if a problem can be solved with Reflection or Exceptions, then the solution for this problem smells very bad.
That is exactly the situation which brought up this question, a stink code. My team is working with the NVelocity library, a version of the popular Java Template Engine, ported to MS .Net. This lib makes extensively use of the Code by Exception anti-pattern and refactoring its code is not an option due to the lack of time.
It is weird, but in that code there is a comment suggesting that a reflexive call could avoid a Exception Raising/Handling.

I don’t know about you, but I was surprised by the result. A reflexive call is 5 times faster than an Exception raising and handling.

The following code (in CSharp) runs a Reflexive call and an Exception raising/handling 10.000 times.

Read the rest of this entry »

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in