Archive for category Grails

Ode to MockFor(March): Part 2 - mockDomain

I know March has long passed. My intentions to do this series during the month of March feel grossly short due to time constrains and a little bit of laziness. In [Part 1][] we started our discussion of the Testing Plugin and how it helps the Grails developer test the constraints on their domain classes. This time around we are going to look at how the Testing Plugin helps you write tests that use all those methods that are injected into your Domain Class at runtime.

Read the rest of this entry »

,

5 Comments

Grails Environment Checking: Better know a framework

Checking the environment that your Grails application is running.

This is nothing new to Grails 1.1, but I use it quite often and is handy to know.

Lets say you have some code that is dependent on the environment that it is running.

Here is one way to check it:

import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.GrailsUtil

//check if production 
if ( GrailsUtil.getEnvironment().equals(GrailsApplication.ENV_PRODUCTION)) ){...}

//check if development
if ( GrailsUtil.getEnvironment().equals(GrailsApplication.ENV_DEVELOPMENT)) ){...}

//check if testing
if ( GrailsUtil.getEnvironment().equals(GrailsApplication.ENV_TEST) ){...}

And here is how you can set the environment in your tests:

import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.GrailsUtil

//set envionment to production
System.setProperty(GrailsApplication.ENVIRONMENT, GrailsApplication.ENV_PRODUCTION)

//set envionment to development
System.setProperty(GrailsApplication.ENVIRONMENT, GrailsApplication.ENV_DEVELOPMENT)

//set envionment to test
System.setProperty(GrailsApplication.ENVIRONMENT, GrailsApplication.ENV_TEST)

No Comments

Grails Layouts: Better know a framework

A colleague of mine stated this quote the other day, with a sense of frustration:

Why is there a layout being applied to this view? I’m not assigning a layout to it!

He had recently upgraded a Grails application from 1.0.4 to 1.1, and was now getting a mystery style attached to his template.

So we scratched our heads for a couple minutes and saw that the layout that was being applied had the same name as the controller that was rendering the template.

So we dug through the Grails Docs and saw that Grails 1.1 has applied the convention over configuration paradigm to lay out assignment. Kind of cool if you know about, temporarily frustrating if you don’t.

So here is an example of how it works.

If you have a the following controller in the grails controller directory (grails-app/controllers/)

class BookController {
    def list = {...}
}

You can then have a layout called grails-app/views/layout/book.gsp

This layout will dynamically be associated to all views the the BookController delegates to.

You can take this a step further and also dynamically assign a layout to a specific action in a view. For this example if we wanted to assign a layout to the list action in the book controller we would just create a template in the following folder:

/grails-app/views/layout/book/list.gsp

And the list.gsp layout will be applied to the BookControlelr list action.

My initial reaction to this was one of skepticism:

This is taking convention over configuation a little too far? You are only saving one line of code in your view and it seems to > defiy the principle of least supprise.

If there is one thing I’ve learned it’s that if I have a knee-jerk reaction to something then I usually need to think about it some more.

What I realized is that this now allows us to create more generic and modular views and templates. We can create them with reuse in mind and then skin them for the context that they are being used.

This is cool stuff, you just have to know about it.

No Comments

Ode to MockFor(March): Part 1 - Testing Constraints

Last March, Glen Smith did a wonderfull series on how to write unit tests for your Grails application. This series addressed a major pain point in Grails development at the time: Testing.

A year later and testing your Grails application is much less painfull thanks to the Testing Plugin and its author Peter Ledbrook which has been now been integrated into Grails 1.1

This series is ment to give new users of Grails an overview of how to do Unit Testing using the Testing Plugin (pre Grails 1.1) or just Grails 1.1, and to highlight what the plugin gives you. This will not be an account of what you had to do prior to the Testing Plugin. Just be thankfull you don’t have to travel that road, I know I’m happy to be off it.

First off: Testing Constraints in your Domain Class

Read the rest of this entry »

,

No Comments

Updated GoogleChart Plugin ver 0.4

A couple weeks ago I released a GoolgeChartBuilder for the Google Charts API. At about the same time James Williams released the Google Chart Plugin for Grails.

Up until now the Google Chart Plugin provided the Grails developer with a fantastic way to generate charts in the view layer (GSP) via a tag library.

With the addition of the builder; the chart URLs can now be easily created in a domain, controller, or service object. The builder functionally provides the opportunity to save the URL to a database or pre-fetch the image file and cache it on the server.

The core functionally of the original builder remains intact and examples can be found here, and documentation can be found here.

The names of the charts were changed to more closely tie the builder to the tag library.

No Comments

Google Chart Builder

For Groovy and Grails

UPDATE: (2/4/2008) This builder has been added to the Google Charts Plugin for Grails version 0.4

UPDATE: Due to a conflict in Grails with the set closure, it has been renamed to dataSet. The examples have been updated as well as the source code and tests.

A little while ago Google released a their Chart API into the wild. It’s basically allows a client to call a RESTful url and get a PNG image back of the chart. Very cool stuff. After playing with it for a little bit I quickly realized that this RESTful approach was both very powerful and very annoying. I primarily do web development and if I’m creating a chart I want it to be on the fly and dynamic. Injecting data and labels into a potentially long url was not my idea of a good time.

Enter the GoogleChartBuilder (Download Source). Written in Groovy utilizing it’s fantastic BuilderSupport class.

Here is an example on how to create a 3D Pie Chart:

def chart = new GoogleChartBuilder()def textList = (1..5).toList()def result = chart.pie3DChart{size(w:350, h:200)data(encoding:'text'){ dataSet(textList)}labels{ textList.each{ label(it) }}}
The result will yield this url string:

http://chart.apis.google.com/chart?cht=p3&chs=350×200&chd=t:1,2,3,4,5&chl=1|2|3|4|5

Submit this URL and you will get the following chart:

Here is an example on how to create a Horizontal Grouped Bar Chart:

def chart = new GoogleChartBuilder()result = chart.barChart(['horizontal', 'grouped']){  barSize(witdth:10, space:2)  size(w:350, h:200)  title(color:808080, size:16){      row('Chart 1')      row('Sampel bar chart')  }  data(encoding:'simple'){      dataSet((1..5).toList())      dataSet((5..1).toList())  }  colors{      color('66CC00')      color('3399ff')  }  legend{      label('Joy')      label('Pain')  }  axis(left:(1..5).toList(), bottom:[])  backgrounds{      background{          solid(color:'999999')      }      area{          gradient(angle:45, start:'CCCCCC', end:'999999')      }  }}}
The result will yield this url string:

http://chart.apis.google.com/chart?cht=bhg&chbh=10,2&chs=350×200&chts=808080,16& chtt=Chart+1|Sampel+bar+chart&chd=s:BCDEF,FEDCB&chco=66CC00,3399ff& chdl=Joy|Pain&chxt=y,x&chxl=0:|1|2|3|4|5&chf=bg,s,999999|c,lg,45,CCCCCC,0,999999,1

Submit this URL and you will get the following chart:

Here is an example on how to create a Line Chart:

def chart = new GoogleChartBuilder()
result = chart.lineChart{
size(w:300, h:200)
title{
row('Joy vs. Pain')
}
data(encoding:'extended'){
dataSet([1,18,200,87,1090,44,3999])
dataSet([88,900,77,1,2998,4])
}
colors{
color('66CC00')
color('3399ff')
}
lineStyle(line1:[1,6,3])
legend{
label('Joy')
label('Pain')
}
axis(left:(1..5).toList(), bottom:[])
backgrounds{
background{
solid(color:'999999')
}
area{
gradient(angle:45, start:'CCCCCC', end:'999999')
}
}
markers{
rangeMarker(type:'horizontal', color:'FF0000', start:0.75, end:0.25)
rangeMarker(type:'vertical', color:'0000cc', start:0.7, end:0.71)
}
}

The result will yield this url string:

http://chart.apis.google.com/chart?cht=lc&chs=300×200&chtt=Joy+vs.+Pain& chd=e:ABASDIBXRCAs-f,BYOEBNABu2AE&chco=66CC00,3399ff&chls=1,6,3& chdl=Joy|Pain&chxt=y,x&chxl=0:|1|2|3|4|5&chf=bg,s,999999|c,lg,45,CCCCCC,0,999999,1& chm=r,FF0000,0,0.75,0.25|R,0000cc,0,0.7,0.71

Submit this URL and you will get the following chart:

The Google Charts API also allows for the creation of:

  • Scatter Point charts.
  • Venn Diagrams
  • 2D pie charts
  • Vertical Stacked Bar Charts
  • Vertical Grouped Bar Charts
  • Horizontal Stacked Bar Chars
  • Line XY Plot Chart
To see examples of these charts (Download the source code) and run the test suite under the ‘test’ folder. The following tests will have examples on how to use the Google Chart Builder and will also generate HTML files (in a folder named GoogleCharts under root)that contain the URL strings.
  • PieChartTest.groovy
  • BarChartTest.groovy
  • ScatterPlotTest.groovy
  • VennDiagramTest.groovy
  • LineChartTest.groovy
These

Documentation on the keywords that are used in the Google Chart Builder can be found in the ‘docs’ folder or here.

As you can see the Google Charts API allows you do generate some pretty complicated charts. However because of the RESTful nature of these charts the URLs can get out of hand very fast. My intention is to ease the pain in using this useful API.

4 Comments