<?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>Addictive Software &#187; Development</title>
	<atom:link href="http://blog.addictivesoftware.net/tag/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.addictivesoftware.net</link>
	<description>ramblings of a chaotic mind</description>
	<lastBuildDate>Tue, 06 Jul 2010 09:04:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Writing better unittests</title>
		<link>http://blog.addictivesoftware.net/2010/02/writing-better-unittests/</link>
		<comments>http://blog.addictivesoftware.net/2010/02/writing-better-unittests/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 12:37:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unittest]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=69</guid>
		<description><![CDATA[Unittests are an integral part of building quality software and the sole responsibility of the developer.
Here I explain some of the lessons i've learned after the last couple of years.

Unittests takes times to write
I'm even inclined to say that writing your tests should take at least the time as writing your functionality takes.
Start with the [...]]]></description>
			<content:encoded><![CDATA[<p>Unittests are an integral part of building quality software and the sole responsibility of the developer.<br />
Here I explain some of the lessons i've learned after the last couple of years.</p>
<ul>
<li><strong>Unittests takes times to write</strong><br />
I'm even inclined to say that writing your tests should take at least the time as writing your functionality takes.</li>
<li><strong>Start with the unit tests</strong><br />
Start writing your unittests first, setting up just enough of your classes to get the test compiling, this will force you write your test based on the functionality required not the buggy implementation you've just written.</li>
<li><strong>Independent<br />
</strong>as the name says, a unit test will test a single unit of code, use integration tests to see how parts are working together.</li>
<li><strong>Small<br />
</strong>A unit test should test a small bit of functionality, one class, or even one method. I usually write extra unittests that test entire use-cases, but those work next to the normal ones.</li>
<li><strong>Test negatives as wel as positives<br />
</strong>Your first test will probably check whether the functionality works when specifying the correct parameters , but also test whether the right results are returned or the correct exceptions are thrown when specifying the wrong ones.</li>
<li><strong>Use code coverage tools to measure effectiveness<br />
</strong>Coverage tools, like for instance Clover will tell you how much of your code is covered, and also which classes are the most risk (high complexy, low coverage)</li>
<li><strong>Be careful when using mock objects<br />
</strong>Using mock objects keeps your unittests indepent but it has the danger that you are testing your mock objects instead of your own functionality, write integration tests</li>
<li><strong>Don't change outputted results into asserts<br />
</strong>It can be very tempting to first see what your class outputs when running a test, and then writing asserts to test on those results, but you will just be validating that bug that is still in there.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2010/02/writing-better-unittests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rest easy</title>
		<link>http://blog.addictivesoftware.net/2009/12/rest-easy/</link>
		<comments>http://blog.addictivesoftware.net/2009/12/rest-easy/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 14:10:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=49</guid>
		<description><![CDATA[I’ve been playing around with REST (REpresentational State Transfer) for a bit and decided to write a small tutorial to show how easy it is to create a rest interface to your business objects
Advantages of rest

Works over HTTP, known protocol with known data formats (mime types), no weird ports open in your firewall
Every resource is [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been playing around with <a href="http://en.wikipedia.org/wiki/REST">REST</a> (REpresentational State Transfer) for a bit and decided to write a small tutorial to show how easy it is to create a rest interface to your business objects</p>
<p>Advantages of rest</p>
<ul>
<li>Works over HTTP, known protocol with known data formats (mime types), no weird ports open in your firewall</li>
<li>Every resource is uniquely identified by a URI</li>
<li>Can do CRUD on those resources (they are just called POST, GET, PUT, DELETE)</li>
<li>used by all the major players for cloud applications (<a href="http://kenai.com/projects/suncloudapis/pages/Home">SUN's cloudApi</a>, <a href="http://www.microsoft.com/windowsazure/">Microsoft's Azure services platform</a>)</li>
<li>we might finally get rid of webdav</li>
</ul>
<p>I'll be using the <a href="https://jersey.dev.java.net/">Jersey</a> Framework for this.<br />
As i’ve been working on a backend that will allow you to manage <a href="http://en.wikipedia.org/wiki/Agile_development">agile development</a> processes (Scrum, Kanban etc) I thought I use that.</p>
<p>In this application every object is represented by a Card for instance a Story, Task, Bug, work Item, etc.<br />
Cards have a hierarchical structure and can each have custom properties<br />
The class looks something like this (removed methods for clarity):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><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> Card <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Card<span style="color: #339933;">&gt;</span> children <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Card<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Entry<span style="color: #339933;">&gt;</span> properties <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Entry<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then for instance the Story class with some default values set in the constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Story <span style="color: #000000; font-weight: bold;">extends</span> Card <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Story<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		addProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Title&quot;</span>, <span style="color: #0000ff;">&quot;A new story&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		addProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Description&quot;</span>, <span style="color: #0000ff;">&quot;as a .. I want .. for the benefit of ..&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		addProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Storypoints&quot;</span>, <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To be able to return these objects through REST they need to be in a format that can be distributed over HTTP, in this case we will be using XML and JSON.<br />
The <a href="https://jaxb.dev.java.net/">Java XML Bindings</a> are perfectly suited to create xml representations of our classes</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@XmlRootElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;card&quot;</span><span style="color: #009900;">&#41;</span>
<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> Card <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
&nbsp;
	@XmlAttribute<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;type&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;card&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Card<span style="color: #339933;">&gt;</span> children <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Card<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;property&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Entry<span style="color: #339933;">&gt;</span> properties <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Entry<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlAttribute
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>* the Entry class is a simple class with a name and value property, I choose this approach as JAXB has an issue in representing HashMaps() in XML<br />
this will produce the following xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Task&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;nobody&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;assigned&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Slay dragon&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;his weak spot seems to be a small spot on his belly&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hours&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>for JSON we don’t need to do anything special then include the jersey-json.jar to the project.</p>
<p>Now to configure and hookup the rest servlet<br />
I added the jersey servlet to my web.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Rest Web Service<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.sun.jersey.spi.container.servlet.ServletContainer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Rest Web Service<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/services/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>And create the service provider class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@PerRequest
@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/agileassistant&quot;</span><span style="color: #009900;">&#41;</span>
@Consumes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> MediaType.<span style="color: #006633;">APPLICATION_JSON</span>, MediaType.<span style="color: #006633;">APPLICATION_XML</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
@Produces<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> MediaType.<span style="color: #006633;">APPLICATION_JSON</span>, MediaType.<span style="color: #006633;">APPLICATION_XML</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AgileAssistantService <span style="color: #009900;">&#123;</span>
 ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will tell Jersey that this is a provider that will respond to requests to /rest/agileassistant, accept JSON and XML and can output JSON or XML depending on the accept headers sent by the client</p>
<p>Now if we for instance want to return the first card (rootCard) and all its children we add this method</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Card rootCard <span style="color: #339933;">=</span> Card.<span style="color: #006633;">createCard</span><span style="color: #009900;">&#40;</span>Root.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@GET
	<span style="color: #000000; font-weight: bold;">public</span> Card getRoot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> rootCard<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>(in my data model the rootCard is always present and functions as a starting point to add the rest of the cards in a hierarchical structure)<br />
So requesting this card will effectively return the entire structure for my test data:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Root&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Story&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Convince King&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;to let the prince go on an adventure&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Storypoints&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Story&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Task&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;nobody&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;assigned&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Slay dragon&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;his weak spot seems to be a small spot on his belly&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hours&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Task&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;nobody&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;assigned&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Rescue Princess&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;She'll be the one screaming from the tower window&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hours&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Once upon a time&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;in a land far far away&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Storypoints&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Doing a parameterized query is almost as easy</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@GET
	@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/card/{id}&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Card getCard<span style="color: #009900;">&#40;</span>@PathParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> getCardById<span style="color: #009900;">&#40;</span>rootCard, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>here the id that you specify will be injected into the parameter<br />
giving the following output</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Task&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;nobody&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;assigned&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Slay dragon&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;his weak spot seems to be a small spot on his belly&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hours&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>To conclude enabling rest for your applications is pretty easy, writing this blog took longer than writing the code.<br />
In a next post I will add POST/PUT/DELETE requests to store data in your application</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2009/12/rest-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaFX data binding to Java</title>
		<link>http://blog.addictivesoftware.net/2009/11/data-binding-in-javafx/</link>
		<comments>http://blog.addictivesoftware.net/2009/11/data-binding-in-javafx/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 21:01:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=3</guid>
		<description><![CDATA[Data binding in JavaFX is pretty straightforward.
You should think binding to Java Objects should be just as simple, well it is a little complicater then just doing

var someObject : bind myJavaclass.getProperty&#40;&#41;;

JavaFX needs to be aware of any changes to the bound variables to be able to update the related objects. It does this automagically for [...]]]></description>
			<content:encoded><![CDATA[<p>Data binding in JavaFX is pretty straightforward.<br />
You should think binding to Java Objects should be just as simple, well it is a little complicater then just doing</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">var someObject <span style="color: #339933;">:</span> bind myJavaclass.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>JavaFX needs to be aware of any changes to the bound variables to be able to update the related objects. It does this automagically for all javafx objects.<br />
It seems JavaFX uses the Observer/Observable pattern for all its internal objects, well we can do the same to bind with JavaObjects.<br />
To get myself familiar with JavaFX i decided to write a photoviewer/editor. This application has a java backend with in particular a Photo class which contains all the information of a certain picture, filename, path, tags, exif information etcetera</p>
<p>To get changes in the Java class to appear in the gui I create a PhotoAdapter javafx class that extends Observer and implemented the properties I need in my GUI.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PhotoAdapter <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Observer</span> <span style="color: #009900;">&#123;</span>
    public<span style="color: #339933;">-</span>read var filename<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #339933;">;</span>
    public<span style="color: #339933;">-</span>read var tags<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    public<span style="color: #339933;">-</span>read var exif<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    public<span style="color: #339933;">-</span>init var photo<span style="color: #339933;">:</span> Photo
         on replace <span style="color: #009900;">&#123;</span>
            photo.<span style="color: #006633;">addObserver</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            filename<span style="color: #339933;">=</span>photo.<span style="color: #006633;">getFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tags <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getTags</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            exif <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getExif</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    override function update<span style="color: #009900;">&#40;</span>observable<span style="color: #339933;">:</span> <span style="color: #003399;">Observable</span>, arg<span style="color: #339933;">:</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        filename <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        tags <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getTags</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exif <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getExif</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>after that I had my existing Java Photo class extend Observable and in every method that made changes I added the methods setChanged() and NotifyObservers() to inform the Observers about the change</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Photo <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Observable</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> exifInformation<span style="color: #339933;">;</span> 
    ....
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setExifInformation<span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span> exifInfo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">exifInformation</span> <span style="color: #339933;">=</span> exifInfo<span style="color: #339933;">;</span>
        setChanged<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        notifyObservers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>in my main javafx class I can then use it as follows</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    var photoAdapter <span style="color: #339933;">=</span> PhotoAdapter <span style="color: #009900;">&#123;</span>
        photo <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">new</span> Photo<span style="color: #009900;">&#40;</span>startImage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and use to fill a ListView swing object with for instance the tags:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #003399;">ListView</span> <span style="color: #009900;">&#123;</span>
        items<span style="color: #339933;">:</span>     bind photoAdapter.<span style="color: #006633;">tags</span><span style="color: #339933;">;</span>
        width<span style="color: #339933;">:</span>     <span style="color: #cc66cc;">200</span><span style="color: #339933;">;</span>
        height<span style="color: #339933;">:</span>    <span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>et viola, thats all that there is to it</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2009/11/data-binding-in-javafx/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
