<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sword Systems &#187; Uncategorized</title>
	<atom:link href="http://swordsystems.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://swordsystems.com</link>
	<description>Cutting Commentary</description>
	<lastBuildDate>Tue, 15 Jun 2010 13:07:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mr. Haki tears it up</title>
		<link>http://swordsystems.com/2010/06/15/mr-haki-tears-it-up/</link>
		<comments>http://swordsystems.com/2010/06/15/mr-haki-tears-it-up/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 13:07:50 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=82</guid>
		<description><![CDATA[I always love the Groovy Goodness blog.  So many gems in such a compact space.  He just posted about a dozen tips in the past day alone, all on stuff that I could use in one project or another.  The only thing I don&#8217;t like about it is it makes me want [...]]]></description>
			<content:encoded><![CDATA[<p>I always love the <a href="http://mrhaki.blogspot.com">Groovy Goodness</a> blog.  So many gems in such a compact space.  He just posted about a dozen tips in the past day alone, all on stuff that I could use in one project or another.  The only thing I don&#8217;t like about it is it makes me want to go in and update all the places in my code where I could use the new calls <strong>right now</strong>.  Major time suck&#8230; must resist&#8230; ohhhh&#8230; may just one little tweak&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2010/06/15/mr-haki-tears-it-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails, Maven, packaging, and skipTests fun</title>
		<link>http://swordsystems.com/2010/04/22/grails-maven-packaging-and-skiptests-fun/</link>
		<comments>http://swordsystems.com/2010/04/22/grails-maven-packaging-and-skiptests-fun/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 16:25:55 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[frustration]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=62</guid>
		<description><![CDATA[The grails maven plugin honors the maven.test.skip option (which turns off compiling and executing tests), but not the skipTests option (which only turns off executing tests).  This means that you cannot easily create a package with test classes in it without running all of the tests.  I finally come up with a close [...]]]></description>
			<content:encoded><![CDATA[<p>The grails maven plugin honors the <code>maven.test.skip</code> option (which turns off compiling and executing tests), but not the <code>skipTests</code> option (which only turns off executing tests).  This means that you cannot easily create a package with test classes in it without running all of the tests.  I finally come up with a close work around:<br />
<code><br />
mvn clean grails:exec install -Dcommand=test-app -Dargs="-unit DoesNotExist" -Dmaven.test.skip=true<br />
</code><br />
By explicitly listing the <code>test-app</code> command but specifying a test that does not exist, I was able to get the unit tests to be compiled.  Since I only needed some common testing files in my package, this works for me.  If someone wanted to package both the unit and integration tests, I don&#8217;t think this would work.  I filed a new issue in the <a href="http://jira.codehaus.org/browse/GRAILS-6188">Grails Jira</a> to try to get a real resolution.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2010/04/22/grails-maven-packaging-and-skiptests-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails+Maven+GeoTools+PdfBox = PITA</title>
		<link>http://swordsystems.com/2010/03/20/grailsmavengeotoolspdfbox-pita/</link>
		<comments>http://swordsystems.com/2010/03/20/grailsmavengeotoolspdfbox-pita/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 20:35:27 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[GIS]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[frustration]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[geotools]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[pdfbox]]></category>
		<category><![CDATA[xml-api]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=54</guid>
		<description><![CDATA[Overcoming link errors related to XML libraries when using GeoTools and PdfBox]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a bit of maven-voodoo for you.  If you have a Grails (1.1.1) app that is built with Maven (2.1) and you try to link in GeoTools (specifically the gt-xsd-core module) and you try to bring in PdfBox as well, you will get really fascinating class loading and linking problems revolving around various XML pieces.  I finally found two different ways to get rid of the errors.  For those who like to cut to the chase, I&#8217;ll list the solutions first and then explain a bit more what errors I was seeing.</p>
<h3>Solution #1: Exclude, exclude, exclude</h3>
<p>If you don&#8217;t need commons-jxpath, you can add these three exclusions to the gt-xsd-core dependency declaration.</p>
<pre>    &lt;dependency&gt;
        &lt;groupId&gt;org.geotools.xsd&lt;/groupId&gt;
        &lt;artifactId&gt;gt-xsd-core&lt;/artifactId&gt;
        &lt;version&gt;${gt.version}&lt;/version&gt;
        &lt;exclusions&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;commons-jxpath&lt;/groupId&gt;
                &lt;artifactId&gt;commons-jxpath&lt;/artifactId&gt;
            &lt;/exclusion&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;xml-apis&lt;/groupId&gt;
                &lt;artifactId&gt;xml-apis&lt;/artifactId&gt;
            &lt;/exclusion&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;xml-apis&lt;/groupId&gt;
                &lt;artifactId&gt;xml-apis-xerces&lt;/artifactId&gt;
            &lt;/exclusion&gt;
        &lt;/exclusions&gt;
    &lt;/dependency&gt;</pre>
<p>This at least works for me given the subset of geotools functionality I am using (primarily encoding and parsing OGC Filter objects to and from XML so I can persist them with Hibernate).  It is still pulling in xercesImpl and that appears to be enough, at least to get my unit and integration tests working.</p>
<h3>Solution #2: Using Dependency Management</h3>
<p>If you need the commons-jxpath functionality in gt-xsd-core, add this to your dependencyManagement section of your POM:</p>
<pre>    &lt;dependencyManagement&gt;
        &lt;dependencies&gt;
            &lt;!-- The gt-xsd-core tries to bring commons-jxpath 1.2 in.  That messes up the maven junitreport plugin for
            some reason (like links to a version of xalan or xml-apis or something.  The 1.3 version doesn't have that
            issue. --&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;commons-jxpath&lt;/groupId&gt;
                &lt;artifactId&gt;commons-jxpath&lt;/artifactId&gt;
                &lt;version&gt;1.3&lt;/version&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    &lt;/dependencyManagement&gt;</pre>
<p>and then keep the exclusions for the xml-api artifacts as above:</p>
<pre>    &lt;dependency&gt;
        &lt;groupId&gt;org.geotools.xsd&lt;/groupId&gt;
        &lt;artifactId&gt;gt-xsd-core&lt;/artifactId&gt;
        &lt;version&gt;${gt.version}&lt;/version&gt;
        &lt;exclusions&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;xml-apis&lt;/groupId&gt;
                &lt;artifactId&gt;xml-apis&lt;/artifactId&gt;
            &lt;/exclusion&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;xml-apis&lt;/groupId&gt;
                &lt;artifactId&gt;xml-apis-xerces&lt;/artifactId&gt;
            &lt;/exclusion&gt;
        &lt;/exclusions&gt;
    &lt;/dependency&gt;</pre>
<p>This is what I have setup in my POM right now because I think I might need jxpath soon.  I also tried adding a node in dependency management to use the 1.3.03 version of xml-apis rather than 1.0b2 (which is what was pulled in by default), but that doesn&#8217;t appear to make any difference.  I still had to have the 2 exclusions under gt-xsd-core for things to work.</p>
<h2>So what was the problem?</h2>
<p>Now, a little more about the problems I had.  I had two types of errors:</p>
<p><strong>Maven junitreport conflict</strong> &#8211; When I added the dependency for gt-xsd-core and then ran my unit tests using the maven grails plugin (<code>mvn grails:exec -Dcommand=test-app -Dargs="-unit"</code>), my tests would run fine (except for one which I&#8217;ll detail below), but then I would get this error when the junitreport plugin tried to run its XSL transform:</p>
<p><code>Embedded error: java.lang.reflect.InvocationTargetException<br />
Provider org.apache.xalan.processor.TransformerFactoryImpl not found</code></p>
<p>Normally I would suspect that the geotools dependencies brought in some new version of the xalan library and it didn&#8217;t have an old class that the junitreport plugin required.  To check this, I compared the output from both maven dependency:classpath and dependency:tree between my trunk branch and the branch where I had added the geotools dependencies.  The comparision showed that only one one dependency had changed between the branches; a newer version of commons-pool was used.  All other classpath mods were new libraries that were not previously included at all.  In fact, xerces and xalan was not included by the trunk branch at all.  I am sure that someone with more recent experiences with xml-apis, xerces, and xalan knows exactly what was happening.  My guess is that by including the xml-apis-xerces dependency, some piece of the junitreport plugin thought that it could use xalan as opposed to some other XSL lib that it would use when xerces was not present.  This is supported by the fact that if I added xalan as a dependency, the error went away.  If I have time, I&#8217;ll look at the docs for junitreport one of these days and see if that sheds some light on the issue.</p>
<p><strong>PDFImageWriter/NodeList conflict</strong> &#8211; One of the unit tests exercised our use of PDFBox.  If I did not have the above exclusions and I include xalan, this error appeared during the PDFImageWriter constructor:</p>
<p><code>java.lang.LinkageError: loader constraint violation: loader (instance<br />
of &lt;bootloader&gt;) previously initiated loading for a different type with<br />
name "org/w3c/dom/NodeList"</code></p>
<p>I haven&#8217;t tracked down exactly why this linkage conflict occurred.  The bizare thing is that I inserted some link-loading debugging that I used in the past to try to see where NodeList was coming from in both my trunk and geotools-enabled branches:<br />
<code><br />
def clazz =  org.w3c.dom.NodeList.class<br />
def codeSource = clazz.getProtectionDomain().getCodeSource();<br />
println ("Source Location: " + codeSource);<br />
</code></p>
<p>In trunk (i.e. no geotools and working fine), it came back as null; NodeList wasn&#8217;t loaded at all.  In the geotools-enabled branch, it would load (from xerces, I think), but then had that conflict.</p>
<p>At this point, I have a solution (two, actually) and have spend pretty much a whole day researching various paths, so I need to wrap it up and move on.  If anyone has a better solution or thoughts as to why exactly there were problems, feel free to comment.</p>
<p>One last note in case there is any confusion from me mentioning xalan so much &#8211; I did not end up adding a dependency for xalan to my POM.  Just using the exclusion statements listed above solved the issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2010/03/20/grailsmavengeotoolspdfbox-pita/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSON conversion and request thread reuse</title>
		<link>http://swordsystems.com/2009/12/18/json-conversion-and-request-thread-reuse/</link>
		<comments>http://swordsystems.com/2009/12/18/json-conversion-and-request-thread-reuse/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 15:33:52 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Controller]]></category>
		<category><![CDATA[frustration]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=50</guid>
		<description><![CDATA[When reusable thread pools and thread local variables can bite you in the butt.]]></description>
			<content:encoded><![CDATA[<p>Wrapping up (I hope) my experience with custom JSON conversions in Grails&#8230; The day before we were supposed to hit a major release milestone last week, I finally caught sight of a random bug that had been driving us nuts.  For some AJAX requests, seemingly at random, the object that has the transient properties was not being rendered with them.  Instead, it had only the regular, persistent properties and full representations of its associated objects rather than just the normal stubs of related objects with just the type and id.  In other words, it was using deep JSON rendering, and this was overriding the custom JSON <code>ObjectMarshaller</code> that we had set for the class.  But why was it doing it seemingly at random?  This is where luck and experience combined to make the solution clear rather quickly and avoided some painful slogging through request handling on the server.</p>
<p>The luck part was that I remembered seeing a checkin from a teammate a couple of weeks ago where he got rid of direct use of a <code>grails.converters.deep.JSON</code> object (which is deprecated) and instead switched to a normal <code>grails.converters.JSON</code> object with a <code>use('deep')</code> statement:</p>
<p><code>def get = {<br />
	def o = OurClass.get(params.id);<br />
	JSON.use("deep")<br />
	render(contentType: "application/json", text: o as JSON)<br />
}<br />
</code><br />
This change was in a different controller for a different domain object though.  Why was it affecting our other domain class?  That&#8217;s where the experience part came in.  A few years ago I had to track down why hibernate transactions would fail seemingly at random in another web app when running under JBoss.  I could see why an initial transaction would fail (we&#8217;d get a timeout and not rollback properly), but there was no reason why later requests would cause failures.  That&#8217;s when I discovered the downside of request thread pooling.  Under most web contains, HTTP requests are handled by a pool of reusable threads.  That means changes to thread local variables stick around between requests unless explicitly reset.  In the case of the Hibernate bug, since the session was not getting closed properly, it wasn&#8217;t getting fully reset and cleared out, so subsequent requests that used that thread were getting the old session and failing because there was no way to get it back to a good state.</p>
<p>For the current JSON conversion bug, the <code>JSON.use</code> method was calling <code>ConvertersConfigurationHolder.setTheadLocalConverterConfiguration(...)</code>, thus making the deep converter stick around for that thread.  So when future requests came in to any controller on that thread, all JSON conversion calls were made with the deep configuration.</p>
<p>The solution is straightforward.  Use the <code>JSON.use(String, Closure)</code> call instead when you want to make a deep call:</p>
<p><code>def get = {<br />
    def o = OurClass.get(params.id);<br />
    JSON.use("deep") {<br />
        render(contentType: "application/json", text: o as JSON)<br />
    }<br />
}</p>
<p></code>This sets the configuration only for the closure, saving you from thread-based side effects later on.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/12/18/json-conversion-and-request-thread-reuse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails JSON converter and transient properties</title>
		<link>http://swordsystems.com/2009/11/23/grails-json-converter-and-transient-properties/</link>
		<comments>http://swordsystems.com/2009/11/23/grails-json-converter-and-transient-properties/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 16:04:55 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=42</guid>
		<description><![CDATA[I figured out why my domain object was rendering as JSON differently in unit tests vs. when running the application and integration tests.   Drum roll, please&#8230;No grailsApplication object is setup in unit tests.
I hear a great big, &#8220;Huh?&#8221;  
First, a little background on converters.  (You may want to get the Grails [...]]]></description>
			<content:encoded><![CDATA[<p>I figured out why my domain object was rendering as JSON differently in unit tests vs. when running the application and integration tests.   Drum roll, please&#8230;No grailsApplication object is setup in unit tests.</p>
<p>I hear a great big, &#8220;Huh?&#8221;  </p>
<p>First, a little background on converters.  (You may want to get the Grails source code and have directories open to the <code>org.codehaus.groovy.grails.web.converters</code> and <code>grails.converter</code> packages before diving into this.)  When a <code>JSON</code> converter is created, it obtains a <code>ConverterConfiguration</code> from the global <code>ConvertersConfigurationHolder</code>.  <code>ConverterConfiguration</code> has several properties which influence the conversion process, but the most interesting for this discussion is its prioritized set of <code>ObjectMarshallers</code>.  <code>ObjectMarshaller</code>s are what do the actual work of turning an object into a JSON representation.  Each <code>ObjectMarshaller</code> handles a certain set of classes, like arrays, maps, beans, grails domain objects, etc.  When the <code>JSON</code> object is ready to convert a data object, it calls <code>config.getMarshaller(data)</code>.  The config iterates through its list of marshallers, calling <code>marshaller.supports(data)</code> on each.  Since multiple marshallers may be able to handle a specific class (like <code>GroovyBeanMarshaller</code> and <code>DomainClassMarshaller</code> can both handle domain classes), whichever marshaller has highest priority (i.e. is called first) will be invoked.</p>
<p>When the standard <code>ConverterConfiguration</code> is initialized, the <code>DomainClassMarshaller</code> is given higher priority than the <code>GroovyBeanMarshaller</code>.  Thus, it handles the conversion of domain objects during normal Grails application execution even though domain objects are also groovy beans.  However, the <code>DomainClassMarshaller.supports()</code> method has a slight twist to it.  It depends on there being a <code>GrailsApplication</code> object registered in the <code>ConverterUtil</code> class in order to tell it that a particular object is a domain object.  Without the <code>GrailsApplication</code> object, <code>ConverterUtil.isDomainClass()</code> always returns false, causing <code>DomainClassMarshaller.supports()</code> to also return false.  The <code>ConverterConfiguration</code> next checks the <code>GroovyBeanMarshaller</code>.  Its <code>supports()</code> method returns true, so it handles the conversion in this case.  </p>
<p>So why is this a big deal?  For most domain object, it probably isn&#8217;t.  Both marshallers render the primary properties of the domain object.  However, the <code>DomainClassMarshaller</code> only renders properties return by <code>domainClass.getPersistentProperties()</code> while <code>GroovyBeanMarshaller</code> renders all properties, including transient ones.  Because I needed some transient properties in the JSON representation, my unit tests worked great (since there is no grailsApplication setup, so the <code>GroovyBeanMarshaller</code> did the full rendering), but then my code failed when I ran the integration tests and the real app (since the <code>DomainClassMarshaller</code> handled the rendering).</p>
<p>How do you fix this?  It&#8217;s pretty easy once you know where to look.  First, I wanted to make my unit tests behave like the real app, so I had to get them to fail.  Unfortunately, the conversion classes are written in java, not groovy, so you can&#8217;t just do a quick override of the <code>ConverterUtil</code> metaclass to get it to return what you want.  You actually need to setup a <code>GrailsApplication</code> object.  Rather than trying to create and initialize a whole one though, I determined that I could create a small stub class that overrode the two methods I needed:</p>
<p><code><br />
import org.codehaus.groovy.grails.commons.*<br />
class GrailsApplicationStub extends DefaultGrailsApplication {<br />
    def artefacts = [(DomainClassArtefactHandler.TYPE):[:]]</p>
<p>    boolean isArtefactOfType(String artefactType, String className) {<br />
        return getArtefact(artefactType, className) != null<br />
    }</p>
<p>    GrailsClass getArtefact(String artefactType, String name) {<br />
        def retVal = artefacts[artefactType] ? artefacts[artefactType][name] : null<br />
        return retVal<br />
    }<br />
}<br />
</code><br />
When initializing the unit test, I created the stub and set in the particular domain class that I wanted to be recognized.  Then register the application stub with the <code>ConverterUtil</code> class:</p>
<p><code><br />
def grailsApp = new GrailsApplicationStub();<br />
grailsApp.artefacts[DomainClassArtefactHandler.TYPE][YourClass.name] = new DefaultGrailsDomainClass(YourClass);<br />
ConverterUtil.setGrailsApplication(grailsApp);<br />
</code><br />
Instances of <code>YourClass</code> will now be handled by the <code>DomainClassMarshaller</code> in the unit test.  When the unit test checks for the existence of a transient property in a JSON representation, it should fail.</p>
<p>Next, I needed to change the marshaller prioritization so that the desired domain class was handled by a regular <code>GroovyBeanMarshaller</code>.  Since I always wanted the <code>GroovyBeanMarshaller</code> to handle the class, I inserted this code into my unit test setup (and later into <code>BootStrap</code> so the behavior would apply to the full app):</p>
<p><code><br />
JSON.registerObjectMarshaller(YourClass, {o, c -><br />
    new GroovyBeanMarshaller().marshalObject(o, c)<br />
    })<br />
</code></p>
<p>This creates a <code>ClosureObjectMarshaller</code> that handles <code>YourClass</code> objects and gives the new marshaller the default priority of 1 (which puts it at the top of the priority list).  When processing an object, this closure marshaller simply passes it through to a <code>GroovyBeanMarshaller</code>.  Since the new marshaller only handles <code>YourClass</code> objects, the rendering behavior for all other domain objects is not affected.</p>
<p>This resolved my original issue.  The JSON rendering of my object is now correct when I run my app. I did go down a side path while coming to this final solution.  If you only want to override how a <code>YourClass</code> object is rendered some of the time, you could instead register a named configuration for it like this:</p>
<p><code><br />
JSON.createNamedConfig("FullYourClassRender") {cfg -><br />
    cfg.registerObjectMarshaller(new ClosureOjectMarshaller<JSON>(YourClass, {o, c -><br />
        new GroovyBeanMarshaller().marshalObject(o, c)<br />
        }))<br />
    }<br />
</code><br />
Then when you want to use that particular configuration, you wrap the <code>JSON</code> object calls in a use statement:</p>
<p><code><br />
JSON.use("FullYourClassRender") {<br />
    render(contentType: "application/json", text: myObject as JSON)<br />
    }<br />
</code></p>
<p>Other configuration options are also available.  Take a look at the <code>JSON</code> class to see them.</p>
<p>I hope this helps document the conversion process a bit.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/11/23/grails-json-converter-and-transient-properties/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fun with Grails JSON conversion</title>
		<link>http://swordsystems.com/2009/11/20/fun-with-grails-json-conversion/</link>
		<comments>http://swordsystems.com/2009/11/20/fun-with-grails-json-conversion/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 06:15:15 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[Intellij]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=39</guid>
		<description><![CDATA[I discovered a problem this afternoon with how one of the domain objects in my Grails app was being converted to JSON in its controller.  The conversion functioned as expected in a unit test, but failed in integration tests and when running the real app.  &#8220;Time to see how good the debugger in Intellij 9 [...]]]></description>
			<content:encoded><![CDATA[<p>I discovered a problem this afternoon with how one of the domain objects in my Grails app was being converted to JSON in its controller.  The conversion functioned as expected in a unit test, but failed in integration tests and when running the real app.  &#8220;Time to see how good the debugger in Intellij 9 is,&#8221; I say to myself.  So I dive into the grails.converters.JSON class to try to find where the difference comes from.  Unfortunately, every time I step into the render() method, the rendering quickly fails with this exception:</p>
<p>org.codehaus.groovy.grails.web.json.JSONException: &#8220;Misplaced object&#8221;</p>
<p>It turns out that the JSON converter has a writer property that is a JSONWriter object.  The act of showing the writer in the variables window of the Intellij debugger causes its toString() method to be called by the IDE (though any breakpoints set in the class do not get triggered by the call).  This, in turn, causes the writer to actually do the writing of the object which, when complete, changes the writer&#8217;s state property to DONE.  When the real code execution tries to process the data, the JSONWriter throws the exception because it thinks it has already finished rendering.</p>
<p>The trick to step through the JSON converter is to define a custom Type Renderer for the JSONWriter class that does not invoke the &#8220;toString&#8221; method.  <a href="http://blogs.jetbrains.com/idea/2008/04/type-renderers/" target="_blank">A blog post</a> on the Intellij site defines how to setup a Type Renderer for a class.  I set mine up so that the node renders simply as &#8220;JSONWriter&#8221; since I don&#8217;t need to see the details for it.  One caveat though &#8211; I found that the new Type Renderer didn&#8217;t affect entries already in my variables window until I right-clicked one and selected &#8220;View As -&gt; JSONWriter&#8221;.  Then the view changed to use the new display settings.</p>
<p>Now back to debugging&#8230;maybe I&#8217;ll actually figure out the real problem sometime before the sun comes up.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/11/20/fun-with-grails-json-conversion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When dynamic programming backfires</title>
		<link>http://swordsystems.com/2009/11/20/when-dynamic-properties-backfire/</link>
		<comments>http://swordsystems.com/2009/11/20/when-dynamic-properties-backfire/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 05:53:22 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[frustration]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=35</guid>
		<description><![CDATA[I love Groovy&#8217;s meta-programming capabilities.  Duck typing, DSLs, ExpandoMetaClass&#8230; it&#8217;s all great until you blow your arm off with them and spend two hours picking the pieces back up.  Such was my experience today as I was trying to set up my first unit test for a Grails controller.  I needed to pass in some [...]]]></description>
			<content:encoded><![CDATA[<p>I love Groovy&#8217;s meta-programming capabilities.  Duck typing, DSLs, ExpandoMetaClass&#8230; it&#8217;s all great until you blow your arm off with them and spend two hours picking the pieces back up.  Such was my experience today as I was trying to set up my first unit test for a Grails controller.  I needed to pass in some parameters.  As I just wrote in my last post, the grails user guide has information on integration testing controllers, but nothing more than a brief mention of the <code>ControllerUnitTestCase</code>.  Unfortunately, searching for phrases like &#8220;grails unit test controller&#8221; on Google turn up posts from last Fall, before the <code>ControllerUnitTestCase </code>was released with Grails 1.1.  Don&#8217;t ask me why I didn&#8217;t just search for <code>ControllerUnitTestCase </code>from the start.  20/20 hindsight.</p>
<p>Working with what I could find, I came across a <a href="http://kousenit.wordpress.com/2008/03/24/integration-tests-of-controllers-in-grails/" target="_blank">blog post from March 2008</a> that covered integration testing with controllers, much like the grails User Guide.  It at least had some explicit examples of how to pass in parameters, so I hoped the approach would work with my <code>ControllerUnitTestCase </code>implementation.  Using the code snippets in the post as a guide, I setup my test something like this:</p>
<p><code>controller.request.params = [layoutType:'filmstrip',compKey:'testKey']<br />
controller.create()<br />
</code><br />
This looks good, right?  The test scaffolding connects the injected <code>params </code>property of the controller with the <code>params </code>property of the request.  Should work like a charm.</p>
<p>And so started my descent into WTF Land.</p>
<p>The tests compiled and ran fine, except that no params were getting passed through to the controller.  I assumed that the old documentation for integration testing no longer exactly applied for the new unit test setups.  Eventually I dug through enough of <code>ControllerUnitTestCase</code>, <code>MvcUnitTestCase</code>, and <code>MockUtils </code>that I figured out there was a way to get direct access to the <code>params </code>property of the controller.  &#8220;Makes sense,&#8221; I thought, &#8220;sort of a mini-mock.  Who needs the request for a unit test?&#8221;  Great, so let&#8217;s try this:</p>
<p><code>controller.params = [layoutType:'filmstrip',compKey:'testKey']<br />
controller.create()<br />
</code><br />
Run again and&#8230;</p>
<p><code>groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: params for class: YourController<br />
</code><br />
Erk.  Closer inspection of <code>grails.test.MockUtils.addCommonWebProperties()</code> reveals how the <code>params </code>property is added to the controller class with only a <code>get </code>method.  Okay, so we can still manipulate the map once we get it.  One more try:</p>
<p><code>controller.params.putAll([layoutType:"filmstrip",compKey:'testKey'])<br />
controller.create()<br />
</code><br />
Success!  Finally, information is going where it needs to be.  I later realized that section 9.2 of the user guide does actually show an example similar to this, except that it sets each item on params individually:</p>
<p><code>def controller = new AuthenticationController()<br />
controller.params.login = "marcpalmer"<br />
controller.params.password = "secret"<br />
controller.params.passwordConfirm = "secret"<br />
controller.signup()</p>
<p></code>&#8220;Oh well,&#8221; I say to myself, &#8220;at least I got it working.  I&#8217;d better post a comment on that old blog post so that others know the new way to set parameters.&#8221;  I returned to the blog page, moved down to the comments section&#8230; and saw that someone else had posted a comment just last month thanking the author for the good information.  Wait a second&#8230; you normally don&#8217;t thank someone if their examples don&#8217;t work.  So I scrolled up and took a closer look at the examples again:</p>
<p><code>controller.request.parameters = [sender:'me',msg:'test']<br />
controller.create()<br />
</code><br />
Anyone with a better eye for detail than me see the difference that sent me down the rat hole? It&#8217;s <code>request.parameters</code>, not <code>request.params</code> as I had entered.  For the 100th time today, Doh!</p>
<p>But wait a second (or an hour, as the case may be)&#8230; If I was calling <code>request.params</code> and there is no params property, how did my test run at all?  Dig, dig, dig&#8230; It turns out that the request is a <code>GrailsMockHttpServletRequest </code>object.  Scanning through that class, I see the ever so powerful and ever so dangerous <code>setProperty(String name, value)</code> override.  If you try to set a property that doesn&#8217;t exist, it adds the value as an entry in the request&#8217;s <code>attributes </code>map.  So the test scaffolding was perfectly happy with my <code>request.params</code> call.  It nicely tucked my params map away in the attributes and continued on its way.</p>
<p>Sigh&#8230;it almost makes me miss compile-time method checking&#8230; but not quite.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/11/20/when-dynamic-properties-backfire/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ControllerUnitTestCase: Documentation Has Arrived</title>
		<link>http://swordsystems.com/2009/11/19/controllerunittestcase-documentation-has-arrived/</link>
		<comments>http://swordsystems.com/2009/11/19/controllerunittestcase-documentation-has-arrived/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 04:53:35 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Controller]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=32</guid>
		<description><![CDATA[My team started on our first Grails app back in mid-August.  I set out to provide good examples of using the new testing features in Grails 1.1 to encourage the rest of the team.  Domain class testing went great, as did Service testing.  Unfortunately, there wasn&#8217;t any documentation on the ControllerUnitTestCase class in the Grails [...]]]></description>
			<content:encoded><![CDATA[<p>My team started on our first Grails app back in mid-August.  I set out to provide good examples of using the new testing features in Grails 1.1 to encourage the rest of the team.  Domain class testing went great, as did Service testing.  Unfortunately, there wasn&#8217;t any documentation on the ControllerUnitTestCase class in the Grails User Guide, and I couldn&#8217;t find any blog or forum posts to help out.  I tried creating an instance of it and tossing in a call to mockController(), but I didn&#8217;t get very far.  I had to shift focus to other tasks before I could dive into the source code itself and figure out what it did, so progress stopped for a while.</p>
<p>Fast forward to today when I finally had a strong need for a controller unit test setup.  I dove into the code and realized three things very quickly:</p>
<ol>
<li>You don&#8217;t need to call mockController().  It does that for you based on the name of the test class itself.</li>
<li>You don&#8217;t want to call the constructor of your controller class in the test cases.  Use the newInstance() method instead so that all the proper scaffolding is put in place.</li>
<li>You don&#8217;t even have to call newInstance() most of the time because the setUp() method inits a controller variable for you already.</li>
</ol>
<p>Doh!  I wish I had taken 15 minutes back in August to look at the source.  It would have saved quite a bit of time by encouraging us to get proper controller test cases in place faster.</p>
<p>To add insult to injury, after spending that time and more getting some test cases just right, I decided to try a search for more documentation online again.   This time there was an answer to my query for help.  A post to the <a href="http://www.pubbs.net/grails/200910/43539/" target="_blank">grails-user</a> mailing list less than a month ago generated several responses with useful info, including Scott Davis posting about the most recent addition to his <a href="http://www.ibm.com/developerworks/java/library/j-grails10209/index.html" target="_blank">Mastering Grails</a> series which was published just days before the post.</p>
<p>So nine months after the release of Grails 1.1 there is now good documentation on this great piece.  Many thanks to those who pulled it together.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/11/19/controllerunittestcase-documentation-has-arrived/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun at the ISWC</title>
		<link>http://swordsystems.com/2009/10/27/fun-at-the-iswc/</link>
		<comments>http://swordsystems.com/2009/10/27/fun-at-the-iswc/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 13:49:38 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[information extraction]]></category>
		<category><![CDATA[semantic web]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=29</guid>
		<description><![CDATA[I am sitting in the opening session of the International Semantic Web Conference (ISWC 2009) right now.   The conference chairs normally give stats about where submitted papers are from, primary topics, etc.   This time they decided to have some fun.  They did a full text extraction on all the papers, removing stop words [...]]]></description>
			<content:encoded><![CDATA[<p>I am sitting in the opening session of the International Semantic Web Conference (<a href="http://iswc2009.semanticweb.org/" target="_blank">ISWC 2009</a>) right now.   The conference chairs normally give stats about where submitted papers are from, primary topics, etc.   This time they decided to have some fun.  They did a full text extraction on all the papers, removing stop words and names and then running a Porter stemmer on the text.  They then came up with the list of words most used in accepted papers and those most used in rejected papers.  Finally, they graphed the words by the largest difference in occurrence between the accepted and rejected papers.   So if you want to get a paper accepted at a semantic web conference, don&#8217;t write about Europeans, users, or people in general.  Do write about services and arguments (or, at least, words with the stems &#8217;servic&#8217; and &#8216;argu&#8217;).  At least, that&#8217;s the rule this year.</p>
<p>Who says theoreticians don&#8217;t know how to have fun?</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/10/27/fun-at-the-iswc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://swordsystems.com/2009/07/30/hello-world/</link>
		<comments>http://swordsystems.com/2009/07/30/hello-world/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 21:06:47 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=1</guid>
		<description><![CDATA[Welcome to my new site.  I&#8217;ve had this domain for a few years and thought it was time to activate it.  Everything before this post was imported from my old blogger account, points-and-edges.blogspot.com.
]]></description>
			<content:encoded><![CDATA[<p>Welcome to my new site.  I&#8217;ve had this domain for a few years and thought it was time to activate it.  Everything before this post was imported from my old blogger account, points-and-edges.blogspot.com.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/07/30/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
