Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

Sunday, June 12, 2011

Step One of Coding in the Cloud For Fun

One of my key desires is to create code that I quickly push out, use myself, learn from, and enhance. Deploying to the Cloud lets me do just that. In short, the Cloud gets administration related items out of my way so I can focus on writing code that I can use myself and get feedback on quickly.

I have investigated many different cloud-ish solutions such as google's appengine,  Heroku, and now finally the Cloud Foundry.

Since my goal is to get quick feedback, get away from distracting administration, and focus on coding, I decided to try using grails (groovy and Java), a cloud plugin, and follow the article titled One-step deployment with Grails and Cloud Foundry

The grails article points one to where he or she can register with the Cloud Foundry, how to install the command line interface (CLI), where to get the grails plugin, and more. Note: if you have not registered for your username / password with the Cloud Foundary, do so now. There can be a line of people wanting to try this out and thus a waiting period. (Tip: Remember the username must be an email address.)

Currently, I am at the point where I have registered and created a hello world application on the Cloud Foundry. Using the plugin, the grails command of cf-info worked great!

It helped that I already had Ruby installed. What does Ruby have to do with a VMware Cloud Foundry?

As the Deploying Applications With the VMware Cloud Foundry CLI article points out, one needs to gem install vmc to get the CLI. You will want the CLI so you can change the password they give you right away.

Has my time been well spent or am I yak shaving? The time has been well spent. Now that I see all the pieces and have gotten the upfront work done, I look forward to doing the rest of the One-step deployment with Grails and Cloud Foundry article.

I hope laying out what I have done so far in this blog entry has helped you as well!

Enjoy!

Monday, January 17, 2011

"as Whatever" is Essential When Doing Groovy Map Coercion

It's helpful to always use the "as" operator when using Map Coercion in Grails (which uses Groovy).
To understand Map Coercion, one should read an article such as http://docs.codehaus.org/display/GROOVY/Groovy+Mocks

While creating unit tests and mocking, one needs to be careful to always use the "as" operator. Example:
Here's a test (located at https://gist.github.com/783407 )

class SomethingTests extends GrailsUnitTestCase {

    void testSomething() {
        Something thing = new Something()
        // thing.someService = [ get : { a -> throw new Throwable() } ]  as SomeService // works
        thing.someService = [ get : { a -> throw new Throwable() } ] // need the "as" operator!

        assert thing.methodWeAreTesting()
    }
}

For completeness, here's the system under test, https://gist.github.com/783412, and the simple service, https://gist.github.com/783419.

If you do not use the "as" operator, you end up with a service that is a java.util.LinkedHashMap as opposed to a groovy proxy object (SomeService_groovyProxy). When the code calls 'get', it does not execute the mock "get" code that was set up! Instead you get back a value or null.

So remember to use the "as" operator such as " as SomeService "

Happy coding!