<?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>Postcards from Utopia</title>
	<atom:link href="http://blog.vergiss-blackjack.de/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.vergiss-blackjack.de</link>
	<description></description>
	<lastBuildDate>Wed, 08 Feb 2012 23:00:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>JMS basics</title>
		<link>http://blog.vergiss-blackjack.de/2012/01/jms-basics/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jms-basics</link>
		<comments>http://blog.vergiss-blackjack.de/2012/01/jms-basics/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 20:28:55 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[jms]]></category>
		<category><![CDATA[messaging]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=174</guid>
		<description><![CDATA[I'm currently integrating JMS (Java Messaging Service) into a project. Being a true enterprise component it has a very complex nomenclature and consists itself of many modules to cover all kinds of topologies. To aid other newcomers here is an overview of what I've learned so far.


Introduction
The Java Messaging Service is an official part ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2012%2F01%2Fjms-basics%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2012%2F01%2Fjms-basics%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;hashtags=j2ee,java,jms,messaging&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I&#8217;m currently integrating JMS (Java Messaging Service) into a project. Being a true enterprise component it has a very complex nomenclature and consists itself of many modules to cover all kinds of topologies. To aid other newcomers here is an overview of what I&#8217;ve learned so far.</p>
<p><span id="more-174"></span></p>
<h1>Introduction</h1>
<p>The <a title="Java Messaging Service" href="http://www.oracle.com/technetwork/java/index-jsp-142945.html" target="_blank">Java Messaging Service</a> is an official part of Java EE and defined in JSR 914. In its own words it provides &#8220;loosely coupled, reliable, and asynchronous&#8221; messaging services for Java applications. Essentially this means that the service is fault-tolerant towards network and application failures and messages will reach their intended target eventually. However, in contrast to other means such as RPC it is not designed for time-critical communication.</p>
<h1>Components</h1>
<p>The major building blocks of JMS are brokers, transports, queues / topics, producers and consumers.</p>
<h2>Consumers and producers</h2>
<p>Producers and consumers, commonly referred to as clients, are producing and consuming messages respectively, just like the Consumer-Producer pattern. They are the main point where your application interacts with the JMS infrastructure by sending and receiving messages.</p>
<h2>Brokers</h2>
<p>Brokers are responsible for storing and forwarding messages to their intended targets, that&#8217;s why one common JMS topology is referred to as as store-and-forward network. What this means is that brokers, of which there can and most likely will be several in a given JMS topology, have an inbuilt persistence layer (<em>store</em>). This ensures that messages are not lost due to the application crashing. Furthermore, brokers can send messages to other brokers (<em>forward</em>).</p>
<p>Depending on the JMS implementation used brokers can usually be run in stand-alone more or embedded in your application</p>
<h2>Transports</h2>
<p>JMS provides, depending on the implementation used, many different means of transportation for the messages. Transports can vary from TCP sockets to HTTP-based to intra-VM communiation (thus avoiding network latency). The VM transport makes it possible for components inside a single VM to communicate with each other, therefore making it unnecessary to access objects directly.</p>
<p>Transports are used both for communication between clients and brokers, as well as between brokers.</p>
<h2>Queues / Topics</h2>
<p>When a consumer sends a message in a JMS network it usually does not have to know exactly what its intended target is. While the brokers make sure that the eventual target can receive the message, queues and topics are used to discern the recipient. With <strong>Queues</strong> a producer can send a message to an arbitrarily named queue. The consumer can then fetch messages from this queue, after which the message is removed from the queue (although there are exceptions to this rule). This is mostly equivalent to point-to-point messaging. With <strong>Topics</strong> a publish-subscribe mechanism can be realized. The consumer sends a message to a topic (<em>publish</em>) and all interested producers can receive this message (<em>subscribe</em>) thus making it a broadcast.</p>
<p>By default communication is uni-directional although bi-directional communication is possible. Several different message types are supported such as simple text messages, (serialized) object messages and byte streams.</p>
<h1>Example</h1>
<p>For my example application I implemented a system where several servers receive and process certain queries. The queries shall be logged on a central server. While there are many JMS implementations I have used <a href="http://activemq.apache.org/" target="_blank">Apache ActiveMQ</a> due to the usual high quality and maturity of Apache projects and their open nature.</p>
<h2>Topology</h2>
<p>The topology is essentially comprised of one broker per query server (the producers), and one broker on the central server (the consumer). The brokers are embedded into the applications and the applications can run on different physical machines.</p>
<p>The clients use the VM transport to communicate with their local broker while the communication between the query brokers and log broker uses a TCP transport. This way no messages are lost in case of a network failure as the query brokers make sure to resume the connection to the log broker once the network connection is reestablished and resend any undelivered messages.</p>
<div id="attachment_183" class="wp-caption alignnone" style="width: 532px"><a href="http://blog.vergiss-blackjack.de/?attachment_id=183"><img class="size-full wp-image-183" title="JMS example topology" src="http://blog.vergiss-blackjack.de/wp-uploads/2012/01/jms.png" alt="JMS example topology" width="522" height="228" /></a><p class="wp-caption-text">JMS example topology</p></div>
<p>Once the consumer has received the message it can act on it depending on the business requirements, for example log it into a database for further analysis.</p>
<p>For the working example see <a href="http://blog.vergiss-blackjack.de/wp-uploads/2012/01/JmsTest.java_.txt">JmsTest.java</a>. To make things easier both brokers in the example reside in the same VM but still use TCP for communication. With this setup hey could just as easily run on different physical machines.</p>
<h1>Moving on</h1>
<p>As mentioned above JMS is a highly sophisticated API which should fulfill most requirements at the price of a steep learning curve. It integrates well with frameworks such as Spring and can be configured using XML files. Further topics include features fail-over / high availability setups, hub-and-spoke topologies, JMX support and discovery networks. Furthermore, many different implementations are available, each of which certainly has its own perks and focus.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=174&amp;md5=5191a4b4c1b5f2cfeb09a7d959e89101" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2012/01/jms-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to install CyanogenMod 7 on your Samsung Galaxy Ace</title>
		<link>http://blog.vergiss-blackjack.de/2011/11/install-cyanogenmod-samsung-galaxy-ace/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=install-cyanogenmod-samsung-galaxy-ace</link>
		<comments>http://blog.vergiss-blackjack.de/2011/11/install-cyanogenmod-samsung-galaxy-ace/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 20:20:39 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[cyanogenmod]]></category>
		<category><![CDATA[firmware]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=156</guid>
		<description><![CDATA[I just successfully installed CyanogenMod 7 on my Samsung Galaxy Ace. Finding all the right instructions was a bit tricky so here's my personal recollection on how to get you up and running with CM7.



If you follow any of these steps you do so at your own risk. You might void your warranty, your ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F11%2Finstall-cyanogenmod-samsung-galaxy-ace%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F11%2Finstall-cyanogenmod-samsung-galaxy-ace%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;hashtags=android,cyanogenmod,firmware&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I just successfully installed CyanogenMod 7 on my Samsung Galaxy Ace. Finding all the right instructions was a bit tricky so here&#8217;s my personal recollection on how to get you up and running with CM7.</p>
<p><span id="more-156"></span></p>
<p><strong>If you follow any of these steps you do so at your own risk. You might void your warranty, your phone could get irreversibly bricked.</strong></p>
<p>That being said, apparently it worked for me, so just be careful and you should be fine.</p>
<h2>Starting position</h2>
<p>My Ace came installed with 2.2 Froyo. After finding out about  a leaked 2.3 Gingerbread firmware I flashed that one (in the meantime it has officially been released by Samsung). You can find it at <a href="http://www.samfirmware.com/apps/blog/show/6791924-exclusive-gingerbread-galaxy-acehttp://" target="_blank">SamFirmware.com</a> and  flash it with the usual Samsung procedure of Odin (see <a href="http://www.sammobile.com/showthread.php?t=34" target="_blank">SamMobile.com</a> for more info). It worked fine for the most part but there is a well known problem with WLAN that causes random hang-ups. I could have tried the official 2.3 firmware which is supposed to partially fix the problem. Since I would have to flash anyway I decided to try out CyanogenMod 7 which had just been ported to the Ace and will become an official port. Part of the reason was of course that my brother had just installed it so I had to have it too <img src='http://blog.vergiss-blackjack.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>So, to start out you will be running some 2.3 firmware, otherwise your mileage may vary.</p>
<h1>Rooting your Ace</h1>
<p>The first step will be to root your Ace which is the prerequisite for all further parts.</p>
<p>I first tried <a href="http://shortfuse.org/?page_id=2" target="_blank">SuperOneClick</a> which is supposed to be an easy-to-use GUI program. I couldn&#8217;t get it to work in either Windows or Linux so I quickly dismissed that way. It turned out there were some root exploits just for Galaxy Aces running 2.3. All you need to do is upload a zip archive, reboot in recovery mode, apply that update and you&#8217;re done. It worked flawlessly for me. You can find all the info on <a href="http://forum.xda-developers.com/showthread.php?t=1034145" target="_blank">how to root your Galaxy Ace at the xda-developers forum</a>, and you just need to follow step D.</p>
<h1>Installing ClockWork Recovery</h1>
<p>Next up is installing ClockWork Recovery. ClockWorkMod (CWM) is an advanced recovery mode and ROM manager which allows you to easily pull backups of your current ROM and install new ones. Luckily it was just ported to the Ace. Why CyanogenMod cannot be installed with the stock recovery mode is beyond me but I&#8217;ll just go with it. It also seems like a good idea to get backup of your current ROM (do this!).</p>
<p>Same as the root exploit you just need to upload a zip archive to your sdcard, reboot, apply the update from the zip and you&#8217;re done. After this step you will have a new recovery mode so to check if CWM was successfully installed just reboot into the recovery mode and it should look different and have a lot more options available. You can <a href="http://androidflip.com/how-to-install-clockwork-mod-recovery-cwm-on-samsung-galaxy-ace-s5830/" target="_blank">get ClockWork Mod Recovery (CWM) at androidflip.com</a></p>
<h1>Installing CyanogenMod 7</h1>
<p>Finally, the moment you have been waiting for. The port for the Ace is currently in an unofficial state and deemed an RC1 &#8211; it&#8217;s working, the features are there, but there might still be some (smaller) bugs. It was originally posted to the xda-developers forum and the original poster keeps the thread in shape and collects all updates in his original post. So head on over to the <a href="http://forum.xda-developers.com/showthread.php?t=1263292" target="_blank">CyanogendMod 7 for Samsung Galaxy Ace</a> post for all the required files and instructions. Just be sure to grab the <em>ROM for flashing from stock or other custom ROMs</em>, not the one for updates. Again, upload the zip to your sdcard, reboot into recovery mode, apply the update and you&#8217;re done!</p>
<h1>Epilogue</h1>
<p>I haven been running CM7 for a few days now, and so far it has been rock solid &#8211; not a single crash and the uptime is approaching 2 days! I didn&#8217;t really notice much in the way of added features. My very subjective feeling is that the battery life is slightly shorter, and while the performance is mostly good sometimes it gets very sluggish, mostly after the phone has been idle for a while. Opening a menu might take a few seconds in that state but it will get better the longer you use it, all the way up to the menus opening instantly as is expected.</p>
<p>So, to summarize, theres&#8217;s no great advantage so far but no big disadvantage either. But seeing as the first release was less than two months ago, and Samsung is notoriously lazy about firmware updates, CyanogenMod for the Ace should hopefully get ahead of the stock firmware over time, fingers crossed. The more people use it, the better, so go ahead and flash the hell out of your Ace <img src='http://blog.vergiss-blackjack.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=156&amp;md5=0b50e2881fffd229aaa74a3ffcf39915" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2011/11/install-cyanogenmod-samsung-galaxy-ace/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Zendesk widget for Thunderbird</title>
		<link>http://blog.vergiss-blackjack.de/2011/09/zendesk-widget-for-thunderbird/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=zendesk-widget-for-thunderbird</link>
		<comments>http://blog.vergiss-blackjack.de/2011/09/zendesk-widget-for-thunderbird/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 19:41:33 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=153</guid>
		<description><![CDATA[I just released a small extension to integrate Zendesk with Thunderbird.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F09%2Fzendesk-widget-for-thunderbird%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F09%2Fzendesk-widget-for-thunderbird%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I just released a small extension to <a href="https://addons.mozilla.org/de/thunderbird/addon/zendesk-widget/" target="_blank">integrate Zendesk with Thunderbird</a>.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=153&amp;md5=88cc0e9abfbe1ef5954bdaa920f89eb0" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2011/09/zendesk-widget-for-thunderbird/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Run PostgreSQL in a ram disk</title>
		<link>http://blog.vergiss-blackjack.de/2011/02/run-postgresql-in-a-ram-disk/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=run-postgresql-in-a-ram-disk</link>
		<comments>http://blog.vergiss-blackjack.de/2011/02/run-postgresql-in-a-ram-disk/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 11:29:56 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[admin]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=141</guid>
		<description><![CDATA[At a a project I am heavily relying on PostgreSQL in the unit tests. This works fine but creating and dropping databases takes quite a while which goes against the purpose of unit tests. One simple solution to this is to run PostgreSQL in a RAM disk which will make IO-heavy operations  a ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F02%2Frun-postgresql-in-a-ram-disk%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F02%2Frun-postgresql-in-a-ram-disk%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;hashtags=administration,database,postgresql,tdd,test&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>At a a project I am heavily relying on PostgreSQL in the unit tests. This works fine but creating and dropping databases takes quite a while which goes against the purpose of unit tests. One simple solution to this is to run PostgreSQL in a RAM disk which will make IO-heavy operations  a snap.</p>
<p><span id="more-141"></span></p>
<h2>Setup</h2>
<p>The way I suggest works simply by</p>
<ol>
<li>Creating a RAM disk</li>
<li>Copying the contents of the PostgreSQL directory to the RAM disk</li>
<li>Mounting the RAM disk over the PostgreSQL directory</li>
</ol>
<p>This has the clear drawback that there is no write-back of any changed data. However, as I need the data only during testing this is acceptable.</p>
<h2>Alternatives</h2>
<p>Unfortunately PostgreSQL does not support RAM-based tables otherwise this would be a much cleaner solution. Another solution would be to create a new cluster directly in the RAM disk (using <code>pg_createcluster</code>). The drawback with this method is that the configuration and possibly already existing DB structures are not preserved.</p>
<h2>Mount script</h2>
<p>This was hacked together in a few minutes but &#8220;it works for me&#8221;™ using Ubuntu 10.10.</p>
<pre>sudo /etc/init.d/postgresql stop
TMPDIR=/tmp/tmpfs; # the actual mount point for the tmpfs
MOUNTPOINT=/var/lib/postgresql/;
[ -d $TMPDIR ] || mkdir $TMPDIR;
sudo mount -t tmpfs -o size=512M,nr_inodes=10k,mode=0777 tmpfs $TMPDIR;
sudo rsync --archive $MOUNTPOINT/ $TMPDIR/;
sudo mount -o bind $TMPDIR $MOUNTPOINT;
sudo /etc/init.d/postgresql start</pre>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=141&amp;md5=45cc34b33f18879031d8d205d8128d95" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2011/02/run-postgresql-in-a-ram-disk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing bundled resources in Java packages</title>
		<link>http://blog.vergiss-blackjack.de/2011/01/accessing-bundled-resources-in-java-packages/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=accessing-bundled-resources-in-java-packages</link>
		<comments>http://blog.vergiss-blackjack.de/2011/01/accessing-bundled-resources-in-java-packages/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 16:33:18 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[resource]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=109</guid>
		<description><![CDATA[This is sort of a continuation of a previous post about how to integrate your own Java classes into the Maven build lifecycle. This entry described how to generate files for inclusion in the final project artifact (ie. .jar, .war etc.). It took me some time to figure out how to access these files ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F01%2Faccessing-bundled-resources-in-java-packages%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F01%2Faccessing-bundled-resources-in-java-packages%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;hashtags=java,maven,resource&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This is sort of a continuation of a previous post about <a title="Calling Java classes at Maven lifecycle phases" href="http://blog.vergiss-blackjack.de/2011/01/calling-java-classes-at-maven-lifecycle-phases/">how to integrate your own Java classes into the Maven build lifecycle</a>. This entry described how to generate files for inclusion in the final project artifact (ie. <code>.jar</code>, <code>.war</code> etc.). It took me some time to figure out how to access these files using Java&#8217;s resource mechanism so this post will summarize my findings.</p>
<p><span id="more-109"></span></p>
<h1>Resource locations</h1>
<p>If you follow the Maven convention your source code is located in <code>src/main/java/</code> and the source code for your tests in <code>src/main/test/</code>. Resource  files will be in <code>src/main/resources/</code> and <code>src/test/resources/</code> respectively. In the final artifact the resouce files will be located in <code>target/classes/</code> or <code>target/test-classes/</code>. Sub-directories will be preserved.</p>
<p>If you want to be able to access a file during runtime drop them in  either of these resource folders depending on whether you want to access  them during tests or in your actual application.</p>
<h2>Packaging</h2>
<p>Copying resources to <code>target/</code> will be done automatically by Maven. After packaging to <code>.jar</code> etc. the contents of <code>target/test-classes/</code> will not be included while the contents of <code>target/classes/</code> will be found in the root directory of the artifact (for <code>jar</code> packaging) or in <code>WEB-INF/classes/</code> (for <code>war</code> packaging)</p>
<h1>Accessing resources</h1>
<p>During runtime of the artifact you can access resources with</p>
<pre>InputStream resource = SomeClass.class.getResourceAsStream("&lt;file or directory name"))
</pre>
<p>or</p>
<pre>URL resource = SomeClass.class.getResource("&lt;file or directory name"))</pre>
<p>where <code>SomeClass</code> is a class in the artifact. The first way will give you an <code>InputStream</code> that you can use to read the contents of the file, the second way will return a <code>URL</code> that you can use to find out the path of the file or directory or move along the directory tree.</p>
<p>Accessing a resource will be delegated to the classloader of the class you use to load the resource. So when <code>SomeClass</code> is a class in a dependency of your project the resource will be loaded from that dependency&#8217;s artifact.</p>
<h2>Resource paths</h2>
<p>Resources have a notion of absolute and relative paths. The resource <code>/filename</code> will be resolved to <code>target/classes/filename</code> whereas <code>filename</code> will be resolved using the package of the class ie. with <code>org.example.SomeClass</code> it will be resolved to <code>target/classes/org/example/filename</code>. Again, for test classes the base resource directory will be <code>target/test-classes/</code>.</p>
<p>Note that you do not have to give a file name at all. <code>/</code> for example will be resolved to just <code>target/classes/</code>.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=109&amp;md5=7bbf1383b17823609953f575f93661c8" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2011/01/accessing-bundled-resources-in-java-packages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calling Java classes from the Maven build lifecycle</title>
		<link>http://blog.vergiss-blackjack.de/2011/01/calling-java-classes-from-the-maven-build-lifecycle/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=calling-java-classes-from-the-maven-build-lifecycle</link>
		<comments>http://blog.vergiss-blackjack.de/2011/01/calling-java-classes-from-the-maven-build-lifecycle/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 19:56:27 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[lifecycle]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=105</guid>
		<description><![CDATA[I love Maven for its flexibility, but this flexibility at times makes it hard to figure out how to achieve certain tasks. In my case I wanted to execute a certain Java class that is included in the project at some point during the build lifecycle. While I can see many uses in my ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F01%2Fcalling-java-classes-from-the-maven-build-lifecycle%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2011%2F01%2Fcalling-java-classes-from-the-maven-build-lifecycle%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;hashtags=build,java,lifecycle,maven&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I love Maven for its flexibility, but this flexibility at times makes it hard to figure out how to achieve certain tasks. In my case I wanted to execute a certain Java class that is included in the project at some point during the build lifecycle. While I can see many uses in my case the class itself compiles a SQL dump out of several source files so it can be included in the final artifact.</p>
<p><span id="more-105"></span></p>
<h1>Integrating with Maven&#8217;s build lifecycle</h1>
<p>Long story short, here is what you need to add to your <code>pom.xml</code>:</p>
<pre>&lt;build&gt;
  &lt;plugins&gt;
    &lt;plugin&gt;
      &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
      &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
      &lt;version&gt;1.2&lt;/version&gt;
      &lt;executions&gt;
        &lt;execution&gt;
          &lt;id&gt;build-dump&lt;/id&gt;
          &lt;phase&gt;process-classes&lt;/phase&gt;
          &lt;goals&gt;
            &lt;goal&gt;java&lt;/goal&gt;
          &lt;/goals&gt;
        &lt;/execution&gt;
      &lt;/executions&gt;
      &lt;configuration&gt;
        &lt;mainClass&gt;com.example.BuildDump&lt;/mainClass&gt;
      &lt;/configuration&gt;
    &lt;/plugin&gt;
  &lt;/plugins&gt;
&lt;/build&gt;
 &lt;pluginManagement&gt;
</pre>
<p>The <code>org.codehaus.mojo.exec-maven-plugin</code> is used to execute the class <code>com.example.BuildDump</code>. The execution is done in the <code>process-classes</code> lifecycle using the java goal of the plugin. The <code>id</code> is arbitrary but has to be unique.</p>
<p>The whole idea of this snippet is to hook certain plugin&#8217;s goal into maven&#8217;s lifecycle at a certain phase. In that sense, the same effect can be achieved by calling (on the command line)</p>
<pre>mvn exec:java -DmainClass=com.example.BuildDump
</pre>
<p>As you can see the goal in the execution of the <code>pom.xml</code> corresponds to the part after the colon, the part before the colon references the plugin (<code>plugin:goal</code>). If I recall correctly <code>org.codehaus.mojo.*-maven-plugin</code>s have a shorthand name which is the part before <code>-maven-plugin</code>.</p>
<p>The execution doesn&#8217;t have to take place in the <code>process-classes</code> phase but it has to take place after the <code>compile</code> phase so the class has already been compiled. It also has to take place before the <code>test</code> phase if you need the files during testing. I do not know if the <code>process-classes</code> phase is correct (it is used to <em>post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.</em>) but it fulfills these two requirements.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=105&amp;md5=2941932130cc9f4bb2fab640dce131b8" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2011/01/calling-java-classes-from-the-maven-build-lifecycle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Security authentication with a Neo4j backend</title>
		<link>http://blog.vergiss-blackjack.de/2010/04/spring-security-authentication-with-a-neo4j-backend/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spring-security-authentication-with-a-neo4j-backend</link>
		<comments>http://blog.vergiss-blackjack.de/2010/04/spring-security-authentication-with-a-neo4j-backend/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 15:30:52 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[graph database]]></category>
		<category><![CDATA[graphdb]]></category>
		<category><![CDATA[neo4j]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring security]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=75</guid>
		<description><![CDATA[I am currently working on a Spring-based Neo4j (a graph database) application and needed to add user authentication. Since Spring Security allows integrating custom authentication providers it felt only natural to implement a provider based on Neo4j. A data structure  to support users and groups  is already provided in the Neo4j Wiki. ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2010%2F04%2Fspring-security-authentication-with-a-neo4j-backend%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2010%2F04%2Fspring-security-authentication-with-a-neo4j-backend%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;hashtags=graph+database,graphdb,java,neo4j,spring,spring+security&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I am currently working on a <a href="http://www.springframework.org">Spring</a>-based <a href="http://www.neo4j.org">Neo4j</a> (a graph database) application and needed to add user authentication. Since <a href="http://static.springsource.org/spring-security/site/index.html">Spring Security</a> allows integrating custom authentication providers it felt only natural to implement a provider based on Neo4j. A <a href="http://wiki.neo4j.org/content/Roles">data structure  to support users and groups</a> <a href="http://wiki.neo4j.org/content/Roles"> </a>is already provided in the Neo4j Wiki. I used it as the basis for my implementation with some wrapping so it could be integrated with Spring Security and provides administrative methods.</p>
<p><span id="more-75"></span>When you take a look at the data structure you will see how suitable a graph is to store user data. It allows you to easily create a hierarchical group structure and in order to aggregate all of the user&#8217;s groups just need to traverse (find one or more paths) from the user node to the start node.</p>
<h3>Implementation</h3>
<p>Only three pieces are really needed once you have the required components such as Neo4j and Spring Security included in your project:</p>
<ol>
<li>Define an interface that defines ways to manipulate the users and groups: <a href="http://blog.vergiss-blackjack.de/wp-uploads/2010/04/AuthenticationService.java_.txt">AuthenticationService.java</a> (actually I needed this only because of some JDK proxying issues, but it never hurts to have an interface)</li>
<li>An implementation of the user service interface for Neo4j: <a href="http://blog.vergiss-blackjack.de/wp-uploads/2010/04/NeoBasedUserService.java_.txt">NeoBasedUserService.java</a></li>
<li>Integration of the authentication provider in your context: <a href="http://blog.vergiss-blackjack.de/wp-uploads/2010/04/app-security.xml_.txt">app-security.xml</a></li>
</ol>
<p>NB: The &#8220;GrantedAuthority&#8221; objects in the Spring nomenclature are implemented here simply as groups.</p>
<p>The interface and implementation provide the bare minimum of what I needed for the application. Still, the interface is well defined and reasonably well tested, so don&#8217;t be afraid to give it a try.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=75&amp;md5=fae4a8232a86f4272015c69217803b2b" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2010/04/spring-security-authentication-with-a-neo4j-backend/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nagios Plugin to check memory consumption</title>
		<link>http://blog.vergiss-blackjack.de/2010/04/nagios-plugin-to-check-memory-consumption/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nagios-plugin-to-check-memory-consumption</link>
		<comments>http://blog.vergiss-blackjack.de/2010/04/nagios-plugin-to-check-memory-consumption/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 23:38:39 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[admin]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[nagios]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=63</guid>
		<description><![CDATA[For some reason the Nagios package in Debian / Ubuntu does not provide a plugin to check the memory consumption so I built a simple script to do that. It ignores swap space (since if your machine starts swapping you're lost anyway) and caches (it's nice to have memory available for caches but this ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2010%2F04%2Fnagios-plugin-to-check-memory-consumption%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2010%2F04%2Fnagios-plugin-to-check-memory-consumption%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;hashtags=administration,monitoring,nagios&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>For some reason the Nagios package in Debian / Ubuntu does not provide a plugin to check the memory consumption so I built a simple script to do that. It ignores swap space (since if your machine starts swapping you&#8217;re lost anyway) and caches (it&#8217;s nice to have memory available for caches but this isn&#8217;t really required). That way only the real memory consumption is checked and if that passes a certain threshold you can rest assured that you&#8217;re in for trouble.</p>
<p><span id="more-63"></span>Get the file here: <a href="http://blog.vergiss-blackjack.de/wp-uploads/2010/04/check_memory.txt">check_memory</a> (it&#8217;s got an .txt ending due to WordPress restrictions; rename to .sh or just remove the ending altogether).</p>
<p>The default warning and critical thresholds are triggered when more than 90% / 95% of the memory are in use. You can change these thresholds by passing in the well known -w and -c arguments.</p>
<p>The plugin was developed in cooperation with <a href="http://findologic.com" target="_blank">FINDOLOGIC GmbH</a>.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=63&amp;md5=5ef8be77de0a619da20837213f264cea" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2010/04/nagios-plugin-to-check-memory-consumption/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Iterating over nested objects in a Spring form</title>
		<link>http://blog.vergiss-blackjack.de/2010/04/iterating-over-nested-objects-in-a-spring-form/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=iterating-over-nested-objects-in-a-spring-form</link>
		<comments>http://blog.vergiss-blackjack.de/2010/04/iterating-over-nested-objects-in-a-spring-form/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 12:59:19 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[binding]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=40</guid>
		<description><![CDATA[Spring 3 allows for wonderfully simply forms and automatic binding of form values back to the command object. However if your command object has further nested objects that you want to make editable from the form as well things get a little tricky. In that case you can't just use a standard JSTL forEach ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2010%2F04%2Fiterating-over-nested-objects-in-a-spring-form%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2010%2F04%2Fiterating-over-nested-objects-in-a-spring-form%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;hashtags=binding,form,java,spring&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Spring 3 allows for wonderfully simply forms and automatic binding of form values back to the command object. However if your command object has further nested objects that you want to make editable from the form as well things get a little tricky. In that case you can&#8217;t just use a standard JSTL forEach loop as this will not allow you to build the path to the nested object correctly. The trick is to use the varStatus attribute to keep track of the index.</p>
<p><span id="more-40"></span></p>
<h3>Sets and Lists</h3>
<p>Assuming that your command object is called target the following will do just fine:</p>
<pre style="padding-left: 30px;">&lt;ul&gt;
  &lt;c:forEach items="${target.nestedObjects}" varStatus="index"&gt;
    &lt;li&gt;
      &lt;form:input path="nestedObjects[${index.count - 1}].property" /&gt;
      &lt;form:errors path="nestedObjects[${index.count - 1}].property" cssClass="errors" /&gt;
    &lt;/li&gt;
  &lt;/c:forEach&gt;
&lt;/ul&gt;</pre>
<p>Keep in mind that the <span style="color: #ff0000;">varStatus counter is one-based</span> while most collections&#8217; indices are zero-based; hence the &#8220;itemIndex.count &#8211; 1&#8243; expression.</p>
<p>Another way is to use the &lt;spring:bind&gt; tag. This will allow you to use standard HTML-Tags at the expense of some Spring form tricks such as checkboxes that are always submitted.</p>
<pre style="padding-left: 30px;">&lt;ul&gt;
  &lt;c:forEach items="${target.nestedObjects}" varStatus="index"&gt;
    &lt;li&gt;
      &lt;spring:bind path="nestedObjects[${index.count - 1}].property" /&gt;
      &lt;input type="text" name="${status.expression}" value="${status.value}" /&gt;
      &lt;p class="errors"&gt;${status.errorMessages}&lt;/p&gt;
    &lt;/li&gt;
  &lt;/c:forEach&gt;
&lt;/ul&gt;</pre>
<p>Of course you can also mix and match both approaches as you see fit.</p>
<h3>Maps</h3>
<p>To iterate over a map the approach is slightly different:</p>
<pre style="padding-left: 30px;">
<pre>&lt;ul&gt;
  &lt;c:forEach items="${target.nestedMap}" varStatus="index"&gt;
    &lt;li&gt;
      &lt;form:input path="nestedObjects[${index.current.key}].property" /&gt;
      &lt;form:errors path="nestedObjects[${index.current.key}].property" cssClass="errors" /&gt;
    &lt;/li&gt;
  &lt;/c:forEach&gt;
&lt;/ul&gt;</pre>
</pre>
<p>As you can see the varStatus variable will be set to each respective map entry as they are iterated. Therefore &#8220;index.current.value&#8221; is also available if needed.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=40&amp;md5=090de4d94c35f44e693fff872951962a" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2010/04/iterating-over-nested-objects-in-a-spring-form/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit Testing a Spring @Controller</title>
		<link>http://blog.vergiss-blackjack.de/2010/04/unit-testing-a-spring-controller/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=unit-testing-a-spring-controller</link>
		<comments>http://blog.vergiss-blackjack.de/2010/04/unit-testing-a-spring-controller/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 20:35:24 +0000</pubDate>
		<dc:creator>georg</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://blog.vergiss-blackjack.de/?p=10</guid>
		<description><![CDATA[Thanks to Spring's new MVC Annotations (@Controller, @RequestMapping, @RequestParam etc.) unit testing a Spring controller is now easier than ever before. Still, I had a hard time figuring out how to do this since most of your controller methods will not take a ServletRequest as input. Instead they use a form-backing / command object, ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2010%2F04%2Funit-testing-a-spring-controller%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.vergiss-blackjack.de%2F2010%2F04%2Funit-testing-a-spring-controller%2F&amp;source=piefke_schorsch&amp;style=normal&amp;service=bit.ly&amp;hashtags=java,spring,tdd,test&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Thanks to Spring&#8217;s new MVC Annotations (@Controller, @RequestMapping, @RequestParam etc.) unit testing a Spring controller is now easier than ever before. Still, I had a hard time figuring out how to do this since most of your controller methods will not take a ServletRequest as input. Instead they use a form-backing / command object, a BindingResult and maybe a ModelMap. In a web environment Spring takes care of filling the command object and validation results however in a unit test you will have to do this yourself.</p>
<p>Following is the basic outline for writing such a test.</p>
<p><span id="more-10"></span></p>
<p><a href="http://stackoverflow.com/questions/2319845/how-to-test-binders-property-editors-used-on-spring-2-5-controllers" target="_blank">This StackOverflow post</a> got me started originally.</p>
<h3>Configuration</h3>
<p>Make sure there is a validator configured somewhere in your <strong>test context</strong>, the corresponding  bean could look like this:</p>
<pre style="padding-left: 30px;">&lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /&gt;
</pre>
<p>In order for your controller to be picked up and injected into your test you will also need this in your config:</p>
<pre style="padding-left: 30px;">&lt;context:component-scan base-package="the.package.your.controllers.are.in" /&gt;</pre>
<p>I am using the Hibernate validators which provide a reference implementation of <a href="http://jcp.org/aboutJava/communityprocess/edr/jsr303/index.html" target="_blank">JSR-303 (Bean Validation)</a> so you will need these dependencies in your <strong>pom.xml</strong> (if you use Maven):</p>
<pre style="padding-left: 30px;">&lt;dependency&gt;
  &lt;groupId&gt;javax.validation&lt;/groupId&gt;
  &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
  &lt;version&gt;1.0.0.GA&lt;/version&gt;
  &lt;type&gt;jar&lt;/type&gt;
  &lt;scope&gt;compile&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
  &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
  &lt;version&gt;4.0.2.GA&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
  &lt;version&gt;3.0.0.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
</pre>
<h3>Test</h3>
<p>Let&#8217;s get down to business, here is what your <strong>unit test</strong> could look like:</p>
<pre style="padding-left: 30px;">public class ControllerTest {

  @Autowired
  private org.springframework.validation.Validator validator;
  @Autowired
  private YourController yourController;

  @Test
  public void testController() {

    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/edit");

    // populate the request, use the same names as you would for the form elements in the corresponding .jsp
    request.setParameter("text", "Some text");

    // create a new instance of your form object
    YourFormObject formObject = new YourFormObject();

    WebDataBinder binder = new WebDataBinder(formObject);
    binder.setValidator(validator); // use the validator from the context
    binder.bind(new MutablePropertyValues(request.getParameterMap()));

    // validation must be triggered manually
   Â binder.getValidator().validate(binder.getTarget(), binder.getBindingResult());

    // if you want to test if the validation works as expected you can check the binding result
    // eg. if no errors are expected:
    assertEquals(0, binder.getBindingResult().getErrorCount());

    // now call your controller method and store the return type if it matters
    // f. ex. a different view might be returned depending on whether there are validation errros
    String view = yourController.edit(binder.getTarget(), binder.getBindingResult());

    assertEquals("error", view);

    // now you can test the target object, f. ex.:
    assertEquals("Some text", binder.getTarget().getText());
  }
}</pre>
<h3>Conclusion</h3>
<p>Spring rocks <img src='http://blog.vergiss-blackjack.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>As you can see there is a lot of boilerplate code (creating the binder, binding, validation) that could easily be moved to a private method. More importantly there are two things which I have not really figured out yet:</p>
<ol>
<li>I need to explicitly call a method on the controller instead of having it figure out by itself which method to use depending on the request method and URL. Of course this would make building and passing in the command object impossible so I guess a viable option is to find which  Spring component is responsible for mapping URLs to controller methods and test these mappings explicitly.</li>
<li>An @ExceptionHandler has to be called manually. This stems from the fact that if the controller method throws an exception it is passed on to the test which will then fail if it does not catch the exception. Therefore the exception never reaches the exception handler. If you think about it that makes sense &#8211; the automatic calling of the @ExceptionHandler would be useful in an integration test. However in a unit test catching the (expected) exception from the controller method is sufficient as you only want to test that unit.</li>
</ol>
<p class="wp-flattr-button"></p> <p><a href="http://blog.vergiss-blackjack.de/?flattrss_redirect&amp;id=10&amp;md5=b3cfa4801210e99af8d1c2e7f0a894de" title="Flattr" target="_blank"><img src="http://blog.vergiss-blackjack.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.vergiss-blackjack.de/2010/04/unit-testing-a-spring-controller/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

