<?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; Testing</title>
	<atom:link href="http://swordsystems.com/tag/testing/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>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 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>
	</channel>
</rss>
