Vanity Foul
Dedicated to the wanderings of an egotistical mind.


20090408
Wednesday April 08, 2009

Groovy Markup Builder

Rather than post this to the Groovy User's mailing list and look like a fool, I thought I'd restrict my foolishes to a smaller audience.

My employer generates a lot of XML, some of which contains an element that looks something like this:
<updatetime day='2' mon='8' date='11' yr='2008' hr='4' hr24='4' tz='GMT' mer='am' min='1' sec='0'>04:01 AM GMT Monday, Aug 11</updatetime>

The perl for this was straightforward (blat out the xml as a string), but when I rewrote it in Java it got complicated, using a SimpleDateFormat to generate a string that I parsed to constituent bits and built a DOM Element. The parsing to bits is irrelevant here, what was "nice" was that in Java for each attribute I just called (in a loop):
updateTimeEl.setAttribute(nameVal[0], nameVal[1]);

Now, for a bit of R&D, I'm rewriting it in Groovy (along with the rest of the class) using the MarkupBuilder and I built a map of attributes. I wanted to do
builder.updatetime( attributes.each{key, value -> key:value}, sdf.format(updateTime) )
but that failed (groovy insists that key/value must be members of my class) and I resorted to:

builder.updatetime(
	// first the attributes
	day:attributes['day'],
	mon:attributes['mon'],
	date:attributes['date'],
	yr:attributes['yr'],
	hr:attributes['hr'],
	hr24:attributes['hr24'],
	tz:attributes['tz'],
	mer:attributes['mer'],
	min:attributes['min'],
	sec:attributes['sec'],
	// now the text value
	sdf.format(updateTime)
)

This works, but is certainly less elegant than I like.
( Apr 08 2009, 02:15:44 PM ) Groovy n Grails Permalink Comments [4]
Trackback: http://www.brainopolis.com/roller/trackback/lance/Weblog/groovy_markup_builder



20090330
Monday March 30, 2009

Standalone GORM via Java

I spent some time re-jiggering the code, specifically getting Burt's GormHelper to load from the applicationContext.xml. I also pulled the use of domainclasses.txt and listed them directly (I'd rather make the code smarter and have it load anything with the @Entity annotation - but that's beyond the current exercise). Don't forget to change the base-package in the gorm:sessionFactory bean.

    <bean id="gormHelper" class="com.burtbeckwith.gorm.GormHelper">
	  <constructor-arg>
	  	<list>
	  		<value>com.brainopolis.GormAuthor</value>
	  		<value>com.brainopolis.GormBook</value>
	  	</list>
	  </constructor-arg>
	</bean>

I've got a simple GormProxy for a few dynamic GORM methods - save(), findBy() - but you'd probably want to build a proper Interface for the GORM classes to extend. I've built a small demonstration app, complete with new GormHelper, applicationContext.xml, and a couple Test Cases.
( Mar 30 2009, 01:07:25 PM ) Groovy n Grails Permalink
Trackback: http://www.brainopolis.com/roller/trackback/lance/Weblog/standalone_gorm_via_java



20090327
Friday March 27, 2009

Using GORM from Java

As the advocate of GORM, it fell to me to prove that it was possible to use GORM from within a Java application. I knew, theoritically, that is was possible, but it took me a couple days to prove it. The few related links I could find were all pre-Grails 1.1, and most were over a year old. Still, based on the shoulders of the giants who went before me, I was able to get it working.

Here is the list of Giants, and the pages the left behind to guide me:

  • The old Standalone GORM page told me it was possible, just not how.
  • Many links were found on this Swik page.
  • The Grails 1.1 Release Notes told me that something important had changed, and I couldn't just use what went before.
  • Anthony Caliendo was trying to do the opposite, but his struggles were still instructional.
  • Burt Beckwith's adventures were key, and without his GormHelper I wouldn't have succeeded. I've made changes based on Grails 1.1 GORM.
  • I'm going to buy Robert's GORM book, but I don't want a PDF and I don't have time to wait for it to come out in 'dead tree.'
  • Graeme answered my stupid and obvlivious question on the user-AT-grails.codehaus-DOT-org mailing list.
  • John Mclean provided many complicated posts that once I figure out will make my hack much more elegant.
  • Finally, the excellent API docs for Grails.

( Mar 27 2009, 01:27:54 PM ) Groovy n Grails Permalink
Trackback: http://www.brainopolis.com/roller/trackback/lance/Weblog/using_gorm_from_java



20090204
Wednesday February 04, 2009

Re: Re: Grails Testing Fixtures

I edited the Fixtures plugin:

class FixtureLoader implements ApplicationContextAware {
    
    def classLoader
    ApplicationContext applicationContext
    
    def createBuilder() {
        new FixtureBuilder(applicationContext, classLoader)
    }
    
    void load(String[] fixtures) {
        def bb = createBuilder()
        fixtures.each {
            bb.loadBeans("/fixtures/${it}.groovy")
        }
        applicationContext = bb.createApplicationContext()
    }
    
    void load(Closure beans) {
        def bb = createBuilder()
        bb.beans(beans)
        applicationContext = bb.createApplicationContext()
    }
    
    Object getProperty(String name) {
    	if (applicationContext.getProperty(name)) {
    		return applicationContext.getProperty(name)
    	}
    	return super.getProperty(name)
    }
}

This doesn't completely fix my complaints, but now I've direct access to the object created in the fixture:

   fixtureLoader.load("testing")
   def fooInstance = fixtureLoader.foo1
   def barInstance = fixtureLoader.bar1

I still have the instantiation/relationship issues, but that's due to Spring's BeanBuilder, not the Fixture plugin itself. Perhaps I'll look into what it would take to fix that as well.
( Feb 04 2009, 08:09:21 PM ) Groovy n Grails Permalink
Trackback: http://www.brainopolis.com/roller/trackback/lance/Weblog/re_re_grails_testing_fixtures

Re: Grails Test Fixtures

I'm less happy with Grails Test Fixtures plugin today, as I discovered that it won't do both sides of a relationship properly. If I've got class Foo which hasMany Bar's, and Bar belongsTo Foo:

testing.groovy
beans {
   foo1(Foo) {
      name = "foobar"
      bars = [/* FAILS -> ref(bar1) */]
   }

   bar1(Bar) {
      name = "barbaz"
      foo = foo1
   }
}

I can't say bars = [bar1] because bar1 won't have been instantiated yet. Even trying to use the "deferred" object references fails. And I cannot declare bar1 first, because it won't be able to set it's foo property and will fail to save. So

   fixtureLoader.load("testing")
   def foo1 = Foo.findByName("foobar")
   // foo1.bars will be empty
   def bar1 = Bar.findByName("barbaz")
   foo1.bars = [bar1]

   // now I can start testing

Which brings me to another point: Ruby fixtures would expose foo1 and bar1 as instances for me to use, no lookup necessary (as I understand it). Maybe I'm not doing it right? Will they be there in Grails Fixtures if I use the Spring Application Context?
( Feb 04 2009, 02:07:35 PM ) Groovy n Grails Permalink Comments [1]
Trackback: http://www.brainopolis.com/roller/trackback/lance/Weblog/re_grails_test_fixtures



20090203
Tuesday February 03, 2009

Grails Test Fixtures

Yep, more Grails. Here's the background: in December I did some skunkworks on a Grails app, which has been thrown on the trash-heap. My next project is supposed to be in Rails, which I have not used yet. I started reading a co-workers book (the book is 3 years old), and it just wasn't feeling right - though I could definitely see where Grails was inspired by Rails. My coworker/team-lead took off for a week of vacation without checking in his work. I had already begun a project, but felt it wasn't going well. I didn't want to go too far off the rails (pardon the pun), so I restarted the project in Grails.

But Grails doesn't have test fixtures (yaml), not in the core, at least. So I started searching and found a plugin that does something similar. Now I'm converting my first couple test cases, and really liking it. And I'm wondering why Test Fixtures aren't a part of the new Testing plugin that is integrated into Grails 1.1....
( Feb 03 2009, 12:59:31 PM ) Groovy n Grails Permalink
Trackback: http://www.brainopolis.com/roller/trackback/lance/Weblog/grails_test_fixtures

Groovy/Grails GPath Goodness

I couldn't find any GPath references that really discussed using it on object-graphs other than those built from XML. In my case, I needed to use GPath to get a reference to an object so I could complete a Unit Test.

The controller I'm testing persisted new objects build from a JSON string passed in, but when accessed didn't have the child objects in the same order. Even if the child objects were in the correct sequence, the Hibernate PersistentSet won't allow you to access objects by index number (job.actions[0].subject will error):

    // this is after the controller call has been made and returned
    records = service.jobsByActionSubject(testSubject)
    def job = records[0]

    // this actually results in the second/wrong action (of type "status") 
    // of the submitted json being accessed
    assertEquals testSubject, job.actions.subject[0]
So I made use of
    records = service.jobsByActionSubject(testSubject)        
    def job = records[0]
    
    // an action has an actionType, which is named
    def testAction = job.actions.find{ it.actionType.name == "pop" }
    assertEquals testSubject, testAction.subject

( Feb 03 2009, 11:52:21 AM ) Groovy n Grails Permalink
Trackback: http://www.brainopolis.com/roller/trackback/lance/Weblog/groovy_grails_gpath_goodness



20090202
Monday February 02, 2009

Grails: Testing JSON Output

I couldn't find any resources that provided examples on how to test a Controller closure that "render[s] as JSON". Turns out it isn't terribly complicated, but I did have to root around a bit to find the right way to get ahold of the text that the controller generated.

def expectedObject = new ExpectedObject(foo: "bar")
expectedObject.save()
		    	
// Create expected JSON result
def expectedResult = expectedObject as JSON
    	
// okay, now we call the method we're testing
controller.params.foo = "bar"
controller.getObject()
def jsonResult = controller.response.contentAsString
    	
// did we get the job back as JSON?
assertEquals expectedResult.toString(), jsonResult

The key was discovering that Spring injected a MockHttpServletResponse, and that it has a contentAsString() method.

Thanks to the following resources:

( Feb 02 2009, 08:56:30 AM ) Groovy n Grails Permalink
Trackback: http://www.brainopolis.com/roller/trackback/lance/Weblog/grails_testing_json_output




archives
links