<?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; frustration</title>
	<atom:link href="http://swordsystems.com/tag/frustration/feed/" rel="self" type="application/rss+xml" />
	<link>http://swordsystems.com</link>
	<description>Cutting Commentary</description>
	<lastBuildDate>Tue, 10 Jan 2012 02:02:29 +0000</lastBuildDate>
	<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[frustration]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Uncategorized]]></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 work around: [...]]]></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>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>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>
	</channel>
</rss>

