<?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>Jayway Team Blog &#187; xml</title>
	<atom:link href="http://blog.jayway.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Tue, 07 Feb 2012 10:49:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Testing Web.config Transformations, Part 1</title>
		<link>http://blog.jayway.com/2011/12/08/testing-web-config-transformations/</link>
		<comments>http://blog.jayway.com/2011/12/08/testing-web-config-transformations/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 20:44:45 +0000</pubDate>
		<dc:creator>Mads Troest</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[automated testing]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[transformation]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[Web.config]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=11089</guid>
		<description><![CDATA[Web.config transformations is a Microsoft-supported technology for adapting a base configuration to a particular deployment environment. In my previous post, I mentioned how AppHarbor provides an online tool for manually testing transformations. Also, the Visual Studio extension SlowCheetah provides support for manually testing and transformation and diffing them against the base configuration from inside Visual Studio. These [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/dd465326.aspx" target="_blank">Web.config transformations</a> is a Microsoft-supported technology for adapting a base configuration to a particular deployment environment.</p>
<p>In my <a href="http://blog.jayway.com/2011/11/14/web-config-transformations-and-xml-namespaces/" target="_blank">previous post</a>, I mentioned how <a href="http://appharbor.com" target="_blank">AppHarbor</a> provides an <a href="http://webconfigtransformationtester.apphb.com/" target="_blank">online tool</a> for manually testing transformations. Also, the Visual Studio extension <a href="http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5" target="_blank">SlowCheetah</a> provides support for manually testing and transformation and <a href="http://blog.jayway.com/wordpress/wp-content/uploads/2011/11/SlowCheetahPreviewTransform.png" target="_blank">diffing</a> them against the base configuration from inside Visual Studio. These tools are useful and may be fine for tinkering with getting a transform just right, but being software professionals, we find it highly desirable to reliably automate our transformation testing.</p>
<p>In this article we shall look at one way to exercise our Web.config transformations from our own code, so that we can TDD our transformations and regression test them as part of our continuous integration cycle, like everything else.</p>
<h2>Starting Point</h2>
<p>The actual Web.config transformation process is normally invoked by MSBuild, as described in detail by Elton Stoneman in <a href="http://geekswithblogs.net/EltonStoneman/archive/2010/08/20/using-msbuild-4.0-web.config-transformation-to-generate-any-config-file.aspx" target="_blank">this post</a>. (One approach to testing the transformations could, thus, be to fork off MSBuild processes on custom build targets, but we'd like to take a less cumbersome route.)</p>
<p>Being an MSBuild task means that the transformation is implemented as an <strong>ITask</strong> realization. The concrete implementation is called <strong>TransformXml</strong> and resides in the <strong>Microsoft.Web.Publishing.Tasks.dll</strong> assembly. Note that this assembly is not pre-installed with Windows/.NET but rather installed along with Visual Studio 2010, typically to <em>C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web</em>). Given that its location is not fixed, you'd probably want to add it to your dependencies folder (caveat emptor: check license terms before passing it around).</p>
<p>From Elton's post, we can also see 3 arguments being passed to the TransformXml task:</p>
<ul>
<li>Source (the XML file to transform)</li>
<li>Transform (the transformation XML file itself)</li>
<li>Destination (the target of the transformed XML file)</li>
</ul>
<p>Armed with that background knowledge, let's move on and try to get the transformation to run outside of the MSBuild environment in which it normally executes. For our experiment, we'll be transforming the following source (<em>app.config</em>):</p>
<pre class="brush: xml;">   &lt;configuration&gt;
     &lt;appSettings&gt;
	   &lt;add key="testKey" value="testValue"/&gt;
     &lt;/appSettings&gt;
   &lt;/configuration&gt;</pre>
<p>The transformation we'll be testing (<em>app.Debug.config</em>) should change the "testValue" of the "testKey" to something transformation specific ("debugValue" in this case):</p>
<pre class="brush: xml;">   &lt;configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"&gt;
     &lt;appSettings&gt;
       &lt;add key="testKey"
            value="debugValue"
            xdt:Locator="Match(key)" xdt:Transform="SetAttributes" /&gt;
     &lt;/appSettings&gt;
   &lt;/configuration&gt;</pre>
<h2>Test Approach</h2>
<p>Our initial stab at automatically testing the transformation will be to invoke the XmlTransform through its outermost interface; i.e. the ITask interface. First, we need to add a reference to <strong>Microsoft.Web.Publishing.Tasks.dll</strong> as mentioned above. Additionally, we need to reference <strong>Microsoft.Build.Framework</strong> and <strong>Microsoft.Build.Utilities.v4.0</strong> (these should be available from your usual <em>Project, Add Reference, .NET </em>tab).</p>
<p>Opening up the XmlTransform class in Visual Studio's Object Browser shows us a single method, <strong>Execute()</strong>, plus a handful of properties, most of which we recognize from Stoneman's previously mentioned post. A little experimentation reveals the following details:</p>
<ul>
<li><strong>SourceRootPath</strong></li>
<ul>
<li>The base path to which the XML file specified in <strong>Source</strong> is relative. If not specified, this will  be the current directory (which, when running from a test runner, would typically be your build target directory).</li>
</ul>
<li><strong>Source</strong></li>
<ul>
<li>The name of the XML source file to be transformed. If this is not an absolute path, it will be relative to the <strong>SourceRootPath</strong>.</li>
</ul>
<li><strong>TransformRootPath</strong></li>
<ul>
<li>The base path to which the XML file specified in <strong>Transform</strong> is relative. If not specified, this will be the same as <strong>SourceRootPath</strong>.</li>
</ul>
<li><strong>Transform</strong></li>
<ul>
<li>The name of the XML transformation to be applied to the source. If this is not an absolute path, it will be relative to the <strong>TransformRootPath</strong>.</li>
</ul>
<li><strong>Destination</strong></li>
<ul>
<li>The name of the resulting transformed XML file. If this is not an absolute path, it will be relative to the current directory (typically your build target directory, in a test context).</li>
</ul>
</ul>
<p>So all we need to do is something like the following, right?</p>
<pre class="brush: csharp;">   var transformer = new TransformXml
   {
      SourceRootPath = @"..\..\..\Jayway.WebTransformTesting\",
      Source = @"app.config",
      Transform = @"app.Debug.config",
      Destination = @"app.transformed.config"
   };
   var transformationSucceeded = transformer.Execute();</pre>
<p>Well, as the <strong>System.InvalidOperationException</strong> with the message <em>"Task attempted to log before it was initialized" </em>thrown in our face subtly tells us, this is not quite the case...</p>
<p>Investigating the task implementation with Reflector shows the problem to be due to the fact that a property, <strong>BuildEngine</strong>, of the Task base class has not been set. It is used as a dependency injection mechanism, giving the task access to some environment services (see <a href="http://msdn.microsoft.com/en-us/library/microsoft.build.framework.ibuildengine.aspx" target="_blank">IBuildEngine</a> on MSDN) normally provided by MSBuild.</p>
<p>Well, the whole purpose of this experiment was to run outside of MSBuild, so we'll roll our own "build engine" instead:</p>
<pre class="brush: csharp;">   public class BuildEngineForTest : IBuildEngine
   {
      public void LogErrorEvent(BuildErrorEventArgs e)
      {
         Console.WriteLine(LogFormat, "ERROR", e.Message);
      }

      public void LogWarningEvent(BuildWarningEventArgs e)
      {
         Console.WriteLine(LogFormat, "WARNING", e.Message);
      }

      public void LogMessageEvent(BuildMessageEventArgs e)
      {
         Console.WriteLine(LogFormat, e.Importance, e.Message);
      }

      public void LogCustomEvent(CustomBuildEventArgs e)
      {
         Console.WriteLine(LogFormat, "CUSTOM", e.Message);
      }

      public bool BuildProjectFile(
         string projectFileName, string[] targetNames,
         IDictionary globalProperties, IDictionary targetOutputs)
      {
         return true;
      }

      public bool ContinueOnError
      {
         get { return true; }
      }

      public int LineNumberOfTaskNode
      {
         get { return 0; }
      }

      public int ColumnNumberOfTaskNode
      {
         get { return 0; }
      }

      public string ProjectFileOfTaskNode
      {
         get { return string.Empty; }
      }

      private const string LogFormat = "{0} : {1}";
   }</pre>
<p>This was the piece we were missing to be able to run the transformation outside MSBuild. We are, thus, able to write our coveted test:</p>
<pre class="brush: csharp;">...
   [Test]
   public void ApplyingDebugTransformationToConfigurationSetsExpectedKey()
   {
      const string targetFile = @"app.transformed.config";

      var transformer = new TransformXml
      {
         BuildEngine = new BuildEngineForTest(),
         SourceRootPath = @"..\..\..\Jayway.WebTransformTesting\",
         Source = @"app.config",
         Transform = @"app.Debug.config",
         Destination = targetFile
      };

      // Left as exercise for the reader... ;]
      AssertKeyInTransformedFile(targetFile);
   }
...</pre>
<p>Our transformation runs successfully, yielding the following console output from our "build engine":</p>
<pre>Normal : Transforming Source File: ..\..\..\Jayway.WebTransformTesting\app.config
Normal : Applying Transform File: ..\..\..\Jayway.WebTransformTesting\app.Debug.config
Low : Executing SetAttributes (transform line 6, 35)
Low : on /configuration/appSettings/add[@key='testKey']
Low : Applying to 'add' element (source line 4, 6)
Low : Set 'key' attribute
Low : Set 'value' attribute
Low : Set 2 attributes
Low : Done executing SetAttributes
Normal : Output File: app.transformed.config
Normal : Transformation succeeded</pre>
<p>Yay? Yay.</p>
<h2>Conclusion</h2>
<p>We have shown how Web.config Transformations can be invoked programmatically, outside the MSBuild process by providing a simple custom IBuildEngine realization; a technique that might prove useful for other scenarios as well.</p>
<p>We have applied that knowledge to enable automated testing of our transformations.</p>
<p>In my next post in this series, we'll continue our investigation, aiming to get rid of the whole, nasty detour around the file system.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/12/08/testing-web-config-transformations/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Web.config Transformations and XML Namespaces</title>
		<link>http://blog.jayway.com/2011/11/14/web-config-transformations-and-xml-namespaces/</link>
		<comments>http://blog.jayway.com/2011/11/14/web-config-transformations-and-xml-namespaces/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 12:45:15 +0000</pubDate>
		<dc:creator>Mads Troest</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[namespace]]></category>
		<category><![CDATA[NLog]]></category>
		<category><![CDATA[transformation]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[Web.config]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=10849</guid>
		<description><![CDATA[In the project I'm currently working on, we use Web.config transformations to adapt a base configuration to a particular deployment environment. I was adding a transformation to adapt our NLog configuration for the various environments, when I ran into a problem. I'd added the following transformation, intended to set the log level to Warning on [...]]]></description>
			<content:encoded><![CDATA[<p>In the project I'm currently working on, we use <a href="http://msdn.microsoft.com/en-us/library/dd465326.aspx" target="_blank">Web.config transformations</a> to adapt a base configuration to a particular deployment environment.</p>
<p>I was adding a transformation to adapt our NLog configuration for the various environments, when I ran into a problem. I'd added the following transformation, intended to set the log level to Warning on the given deloyment:</p>
<pre class="brush: xml;">&lt;nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"&gt;
  &lt;rules&gt;
    &lt;logger name="*" minlevel="Warn"
      xdt:Transform="SetAttributes(minlevel)" xdt:Locator="Match(name)"
    /&gt;
  &lt;/rules&gt;
&lt;/nlog&gt;</pre>
<p>However, the original fragment kept being passed through without modification. I tried various modes of matching (incidentally, the online <a href="http://webconfigtransformationtester.apphb.com/" target="_blank">Web Config Transformation Tester</a> is a good tool for such tinkering with transformations) to no avail, until I decided to try and remove the NLog namespace from the transformation:</p>
<pre class="brush: xml;">&lt;nlog&gt;
  &lt;rules&gt;
    &lt;logger name="*" minlevel="Warn"
      xdt:Transform="SetAttributes(minlevel)" xdt:Locator="Match(name)"
    /&gt;
  &lt;/rules&gt;
&lt;/nlog&gt;</pre>
<p>And lo and behold; it worked.</p>
<p>That could have been the end of it, but I decided to dig a bit deeper, since we have the NLog schema in our solution and properly referencing it allows us to have IntelliSense within the web.config.</p>
<p>Eventually, as an experiment, I tried associating a namespace prefix with the NLog namespace at the top level, and then explicitly qualify the NLog elements with that prefix, rather than relying on specifying a default namespace on the NLog element:</p>
<pre class="brush: xml;">&lt;configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
  xmlns:nlog="http://www.nlog-project.org/schemas/NLog.xsd"
&gt;

  &lt;nlog:nlog&gt;
    &lt;nlog:rules&gt;
      &lt;nlog:logger name="*" minlevel="Warn"
        xdt:Transform="SetAttributes(minlevel)" xdt:Locator="Match(name)"
      /&gt;
    &lt;/nlog:rules&gt;
  &lt;/nlog:nlog&gt;

&lt;/configuration&gt;</pre>
<p>Success!</p>
<h2>Conclusion</h2>
<p>Explicitly prefixing elements from foreign XML namespaces in config transformations must be described as a workaround, since semantically the first and the third NLog fragments are equivalent.</p>
<p>In spite of this flaw in the web.config transformations implementation, this workaround allows you to get the benefits of IntelliSense and better validation when you have the schema for NLog or any other foreign fragment available.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/11/14/web-config-transformations-and-xml-namespaces/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>REST and XML using Spring MVC and Groovy</title>
		<link>http://blog.jayway.com/2010/04/09/rest-and-xml-using-spring-mvc-and-groovy/</link>
		<comments>http://blog.jayway.com/2010/04/09/rest-and-xml-using-spring-mvc-and-groovy/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 14:32:10 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[hateoas]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=5319</guid>
		<description><![CDATA[There's one particular thing Groovy is really good for and that is working with XML. When I started playing around with the REST support in the latest version of Spring MVC I wanted to try using Spring for the controller infrastructure and delegate to Groovy for producing XML responses. It turned out this wasn't as [...]]]></description>
			<content:encoded><![CDATA[<p>There's one particular thing Groovy is really good for and that is working with XML. When I started playing around with the REST support in the latest version of Spring MVC I wanted to try using Spring for the controller infrastructure and delegate to Groovy for producing XML responses. It turned out this wasn't as easy as expected.</p>
<p>The built-in support for rendering XML from RESTful web services in Spring is built on top of the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/oxm.html">Marshaller</a> abstraction originally from Spring Web Services (now moved to Spring core). This is a pretty nice abstraction if you want to use one of the existing Object-XML marshalling frameworks (JAXB, Castor, XMLBeans, etc.), but if you want to move out of that box you're pretty much stranded.</p>
<p>I really do want to move out of the box because I think the control and readability greatly improves if you do your XML marshalling in Groovy. It's quite common that the same domain object will need to be marshalled differently depending on the scenario (an object will e.g. quite probably need to be marshalled differently in a detail view than in a list). Also, it is not uncommon that more complicated application logic will be required when rendering the XML; a common example of this is URI generation in 'linked' REST applications (<a href="http://en.wikipedia.org/wiki/HATEOAS">HATEOAS</a>). Hence, Groovy is the perfect choice: Very little code is needed, you are able to include some logic if necessary, and the code written exactly reflects the XML that will be produced. </p>
<h3>The Building Blocks</h3>
<p>I started out by defining a simple interface for a completely generic Marshaller:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> SimpleMarshaller <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> marshal<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Writer</span></a> writer, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> o<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>I now needed a View implementation to render Model objects using the <code>SimpleMarshaller</code> interface (much of this code is actually borrowed from the Spring <code>MarshallingView</code> - Apache license rocks!):</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleMarshallingView <span style="color: #000000; font-weight: bold;">extends</span> AbstractView <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> DEFAULT_MODEL_KEY = <span style="color: #ff0000;">&quot;objectToMarshal&quot;</span>;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">boolean</span> AUTOMATIC_FLUSH = <span style="color: #000000; font-weight: bold;">true</span>;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> DEFAULT_CHARSET = <span style="color: #ff0000;">&quot;utf8&quot;</span>;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> SimpleMarshaller marshaller;
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> SimpleMarshallingView<span style="color: #66cc66;">&#40;</span>SimpleMarshaller marshaller<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">marshaller</span> = marshaller;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">void</span> renderMergedOutputModel<span style="color: #66cc66;">&#40;</span>Map&lt;String, Object&gt; model, HttpServletRequest request,
      HttpServletResponse response<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a> outputStream = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APrintWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">PrintWriter</span></a> writer = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APrintWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">PrintWriter</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AOutputStreamWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">OutputStreamWriter</span></a><span style="color: #66cc66;">&#40;</span>outputStream, DEFAULT_CHARSET<span style="color: #66cc66;">&#41;</span>, AUTOMATIC_FLUSH<span style="color: #66cc66;">&#41;</span>;
    marshaller.<span style="color: #006600;">marshal</span><span style="color: #66cc66;">&#40;</span>writer, objectToRender<span style="color: #66cc66;">&#40;</span>model<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    response.<span style="color: #006600;">setContentType</span><span style="color: #66cc66;">&#40;</span>getContentType<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    response.<span style="color: #006600;">setContentLength</span><span style="color: #66cc66;">&#40;</span>outputStream.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    FileCopyUtils.<span style="color: #006600;">copy</span><span style="color: #66cc66;">&#40;</span>outputStream.<span style="color: #006600;">toByteArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, response.<span style="color: #006600;">getOutputStream</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> objectToRender<span style="color: #66cc66;">&#40;</span>Map&lt;String, Object&gt; model<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> o = model.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>DEFAULT_MODEL_KEY<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>o == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AIllegalArgumentException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">IllegalArgumentException</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Model does not contain the expected key '&quot;</span> + DEFAULT_MODEL_KEY + <span style="color: #ff0000;">&quot;'&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> o;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Next I created an abstract <code>SimpleMarshaller</code> implementation for marshalling using a Groovy <code>MarkupBuilder</code>:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> AbstractMarkupBuilderSimpleMarshaller <span style="color: #000000; font-weight: bold;">implements</span> SimpleMarshaller <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">void</span> marshal<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Writer</span></a> writer, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> o<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    MarkupBuilder builder = <span style="color: #000000; font-weight: bold;">new</span> MarkupBuilder<span style="color: #66cc66;">&#40;</span>writer<span style="color: #66cc66;">&#41;</span>;
    marshalToBuilder<span style="color: #66cc66;">&#40;</span>builder, o<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #993333;">void</span> marshalToBuilder<span style="color: #66cc66;">&#40;</span>MarkupBuilder builder, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> o<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Now all that is left is to create a new Groovy View implementation for specific XML response you want, e.g.:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UserXmlMarshallingView <span style="color: #000000; font-weight: bold;">extends</span> SimpleMarshallingView <span style="color: #66cc66;">&#123;</span>
  @Autowired
  <span style="color: #000000; font-weight: bold;">public</span> UserXmlMarshallingView<span style="color: #66cc66;">&#40;</span>UriConstructionService uriConstructionService<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UserXmlMarshaller<span style="color: #66cc66;">&#40;</span>uriConstructionService<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    setContentType<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;application/xml&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> UserXmlMarshaller <span style="color: #000000; font-weight: bold;">extends</span> AbstractMarkupBuilderSimpleMarshaller <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> UriConstructionService uriConstructionService;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> UserXmlMarshaller<span style="color: #66cc66;">&#40;</span>UriConstructionService uriConstructionService<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">uriConstructionService</span> = uriConstructionService;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> marshalToBuilder<span style="color: #66cc66;">&#40;</span>MarkupBuilder builder, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> user<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      builder.<span style="color: #ff0000;">'user'</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span>: user.<span style="color: #006600;">name</span>, <span style="color: #ff0000;">'email'</span>: user.<span style="color: #006600;">email</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        user.<span style="color: #006600;">orders</span>.<span style="color: #006600;">each</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #ff0000;">'order-ref'</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dateCreated'</span>: it.<span style="color: #006600;">dateCreated</span>.<span style="color: #006600;">format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'yyyy-MM-dd hh:mm:ss'</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">'uri'</span>: uriConstructionService.<span style="color: #006600;">orderUri</span><span style="color: #66cc66;">&#40;</span>it<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Note above that we included some processing logic as well - we get a formatted date and we also make use of a service for producing URIs for referencing sub-entities.</p>
<p>Each individual View implementations now needs to be registered in the <code>DispatcherServlet</code>'s ApplicationContext:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;user&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.jayway.demo.xml.UserXmlMarshallingView&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
...
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.BeanNameViewResolver&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;</pre>
<p>Since we have defined a <code>BeanNameViewResolver</code> we can now refer to these Views using their bean names when pointing them out from a Controller:</p>
<pre class="java">&nbsp;
@Controller
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserController <span style="color: #66cc66;">&#123;</span>
  @Autowired
  <span style="color: #000000; font-weight: bold;">private</span> Repo repo;
&nbsp;
  @RequestMapping<span style="color: #66cc66;">&#40;</span>value = <span style="color: #ff0000;">&quot;/users/{id}&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">public</span> ModelAndView user<span style="color: #66cc66;">&#40;</span>@PathVariable <span style="color: #993333;">int</span> id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    User user = repo.<span style="color: #006600;">userById</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;user&quot;</span>, SimpleMarshallingView.<span style="color: #006600;">DEFAULT_MODEL_KEY</span>, user<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<h3>Infrastructure</h3>
<p>Obviously, for the above to work we need organize our code appropriately and make sure that our build system properly builds the groovy files. This turned out to be surprisingly easy (using maven). All groovy code should be placed under <code>src/main/groovy</code>. Then maven needs to be configured to build the groovy files using the <a href="http://docs.codehaus.org/display/GMAVEN/Building+Groovy+Projects">gmaven</a> plugin. Also, make sure you include the groovy libs in your dependency list:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.codehaus.groovy<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>groovy-all<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>1.7.1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<h3>Conclusion</h3>
<p>With the above setup it's easy to quickly implement (and test) new marshalling views for the different resources in a REST application. Even though this was used in a rather small proof-of-concept project my general feeling is that the approach will work pretty well. My next step with this will be to add some simplifications for working with HTTP status codes (the approach to HTTP response codes with the <code>@ResponseCode</code> annotation in Spring MVC feels insufficient for many cases).</p>
<p>Finally it should be noted that the approach with a completely generic marshaller makes it easy to expand this to working with other representations as well, e.g. JSON (although the JSON support in Groovy is still a little bit shaky).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/04/09/rest-and-xml-using-spring-mvc-and-groovy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JXPath simplifies Java queries</title>
		<link>http://blog.jayway.com/2006/08/04/jxpath-simplifies-java-queries/</link>
		<comments>http://blog.jayway.com/2006/08/04/jxpath-simplifies-java-queries/#comments</comments>
		<pubDate>Fri, 04 Aug 2006 10:30:52 +0000</pubDate>
		<dc:creator>Ulrik Sandberg</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[automated testing]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=259</guid>
		<description><![CDATA[Consider a company that contains many departments. Those departments contain employees, those employees have names, telephone numbers, and so on. To query a collection of companies and find, for example, all departments of companies in California with more than 10 employees, you could write something like this: &#160; for &#40;Iterator companies = database.getCompanies&#40;&#41;.iterator&#40;&#41;; companies.hasNext&#40;&#41;;&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>Consider a company that contains many departments. Those departments contain employees, those employees have names, telephone numbers, and so on. To query a collection of companies and find, for example, all departments of companies in California with more than 10 employees, you could write something like this:</p>
<pre class="java">&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AIterator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Iterator</span></a> companies =
      database.<span style="color: #006600;">getCompanies</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">iterator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      companies.<span style="color: #006600;">hasNext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
&nbsp;
   Company company = <span style="color: #66cc66;">&#40;</span>Company<span style="color: #66cc66;">&#41;</span>companies.<span style="color: #006600;">next</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>company.<span style="color: #006600;">getLocation</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;CA&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
&nbsp;
      <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AIterator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Iterator</span></a> departments =
            company.<span style="color: #006600;">getDepartments</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">iterator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            departments.<span style="color: #006600;">hasNext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
&nbsp;
         Department department = <span style="color: #66cc66;">&#40;</span>Department<span style="color: #66cc66;">&#41;</span>departments.<span style="color: #006600;">next</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
         <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>department.<span style="color: #006600;">getEmployees</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>department<span style="color: #66cc66;">&#41;</span>;
         <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Using <a href="http://commons.apache.org/jxpath/">JXPath</a> you can simplify this:</p>
<pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">// only needed once</span>
JXPathContext context = JXPathContext.<span style="color: #006600;">newContext</span><span style="color: #66cc66;">&#40;</span>database<span style="color: #66cc66;">&#41;</span>; 
&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AIterator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Iterator</span></a> departments = context.<span style="color: #006600;">iterate</span><span style="color: #66cc66;">&#40;</span>
      <span style="color: #ff0000;">&quot;/companies[location='CA']&quot;</span> +
      <span style="color: #ff0000;">&quot;/departments[count(employees) &gt; 10]&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>departments.<span style="color: #006600;">hasNext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>departments.<span style="color: #006600;">next</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Two points should be noted from this example:</p>
<ul>
<li>The code using JXPath is considerably shorter. The query is concisely expressed in one statement.</li>
<li>You can take that expression out of the code and parameterize it, or put it in a configuration file.</li>
</ul>
<h3>Perfect for Unit Testing</h3>
<p>Unit testing often requires checking object hierarchies for particular values. You can use JXPath to express the components to be tested. For example, you can write a helper method in a JUnit test:</p>
<pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * tests using a JXPath to express the required
 * component to test against. Note that nulls
 * aren't catered for!
 * @param path the path to find the object to
 *        test against
 * @param testable the base object to test
 * @param required the required result
 */</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> assertFromPath<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> path,
                            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> testable,
                            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> required<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
&nbsp;
   JXPathContext context = JXPathContext.<span style="color: #006600;">newContext</span><span style="color: #66cc66;">&#40;</span>testable<span style="color: #66cc66;">&#41;</span>;
   assertTrue<span style="color: #66cc66;">&#40;</span>required.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getValue</span><span style="color: #66cc66;">&#40;</span>path<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>and then you can use this in tests:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testBankAccount<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
&nbsp;
   <span style="color: #808080; font-style: italic;">// you get a bank account object from some operation...</span>
   Account account = .... 
&nbsp;
   <span style="color: #808080; font-style: italic;">// then test against it</span>
   assertFromPath<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/accountHolder/name&quot;</span>, account, <span style="color: #ff0000;">&quot;John Smith&quot;</span><span style="color: #66cc66;">&#41;</span>;
   assertFromPath<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/accountHolder/opened&quot;</span>, account, DATE_OPENED<span style="color: #66cc66;">&#41;</span>;
   assertFromPath<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/accountHolder&quot;</span> + <span style="color: #ff0000;">&quot;/transactions[1]/amount&quot;</span>,
         account, INITIAL_PAYMENT<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Read more in <a href="http://today.java.net/pub/a/today/2006/08/03/java-object-querying-using-jxpath.html">this java.net article</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2006/08/04/jxpath-simplifies-java-queries/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

