<?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>Jworks.nl - Agile Software Development using Groovy and Grails</title>
	<atom:link href="http://www.jworks.nl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jworks.nl</link>
	<description>Effective Software Development using Effective Tools, People and Process</description>
	<lastBuildDate>Mon, 03 Jun 2013 21:41:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Jacoco code coverage with Gradle</title>
		<link>http://www.jworks.nl/2013/06/03/jacoco-code-coverage-with-gradle/</link>
		<comments>http://www.jworks.nl/2013/06/03/jacoco-code-coverage-with-gradle/#comments</comments>
		<pubDate>Mon, 03 Jun 2013 21:40:54 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Gradle]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[gradle]]></category>
		<category><![CDATA[jacoco]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=583</guid>
		<description><![CDATA[One of the new features of Gradle 1.6, the Groovy based build tool, is Jacoco support. Jacoco is one of the few (only?) Open Source code coverage tools still in active development, and starting from Gradle 1.6, it&#8217;s now a supported plugin. Enabling the task is easy, just add apply plugin: 'jacoco' to your build.gradle, [...]]]></description>
				<content:encoded><![CDATA[<p>One of the new features of Gradle 1.6, the Groovy based build tool, is Jacoco support. Jacoco is one of the few (only?) Open Source code coverage tools still in active development, and starting from Gradle 1.6, it&#8217;s now a supported plugin. Enabling the task is easy, just add </p>
<pre class="brush: groovy">apply plugin: 'jacoco'</pre>
<p> to your build.gradle, and run <code>./gradlew jacocoTestReport</code> and it.. does nothing. For those running into the same issues, this might save you a bit of time:</p>
<p>After a bit of searching, it turns out the jacoco plugin is a bit broken. When using it with a Java project, as I&#8217;m doing, you need to do a couple of things to enable it, but also then, it unfortunately doesn&#8217;t work as advertised, though the fix seems not to complex.</p>
<p>First, you need to enable the following:</p>
<pre class="brush: groovy">
jacocoTestReport {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
}
</pre>
<p>And then, instead of running <code>./gradlew jacocoTestReport</code> as mentioned in the docs <a href="http://www.gradle.org/docs/current/userguide/jacoco_plugin.html">here</a>, you need to run <code>./gradlew test jacocoTestReport</code>. Unfortunately, no location is printed where the documentation is created, so no clickable links in your terminal, but you can find it under <code>build/reports/jacoco/test/html/index.html</code>. Well, that wasn&#8217;t so bad now, was it? Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2013/06/03/jacoco-code-coverage-with-gradle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating projects with Gradle</title>
		<link>http://www.jworks.nl/2013/04/16/creating-projects-with-gradle/</link>
		<comments>http://www.jworks.nl/2013/04/16/creating-projects-with-gradle/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 19:56:56 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Gradle]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[gradle]]></category>
		<category><![CDATA[task]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=568</guid>
		<description><![CDATA[Coming from a Maven background (Maven 1, 2, 3), I find it a bit strange that Gradle doesn&#8217;t provide archetypes like Maven does. There&#8217;s a template plugin, which helps a little, but besides that, you&#8217;re out of luck. Or are you? I found a pretty neat Gradle script in their Jira system, which can create [...]]]></description>
				<content:encoded><![CDATA[<p>Coming from a Maven background (Maven 1, 2, 3), I find it a bit strange that Gradle doesn&#8217;t provide archetypes like Maven does. There&#8217;s a template plugin, which helps a little, but besides that, you&#8217;re out of luck. Or are you?</p>
<p>I found a pretty neat Gradle script in their <a href="http://issues.gradle.org/browse/GRADLE-1289" target="_blank">Jira</a> system, which can create a directory structure for you even when working on a Groovy or Scala project. The easiest way to do so is to add the following &#8216;initProject&#8217; task to the Gradle build.gradle:</p>
<pre class="brush: groovy; highlight: [14]">
apply plugin: 'java'
apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.0.5'

    testCompile group: 'junit', name: 'junit', version: '4.+'
}

task initProject () << {
    if (hasProperty(initPlugins)) {
        initPlugins.split(',').each { plug -> 
            project.apply { 
                plugin(plug.trim()) 
            } 
        }
    }

    project.sourceSets*.allSource.srcDirTrees.flatten().dir.each { dir ->
        dir.mkdirs()
    }
}
</pre>
<p>You can call it by executing the following command:</p>
<pre>gradle initProject -PinitPlugins=groovy,java</pre>
<p>This works great until Hans or Luke decide to create the
<pre>gradle create-project myproject -PapplyPlugin=java,groovy</pre>
<p> command, which would make our lives even easier!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2013/04/16/creating-projects-with-gradle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Reviving Groovy in Action</title>
		<link>http://www.jworks.nl/2013/04/13/reviving-groovy-in-action/</link>
		<comments>http://www.jworks.nl/2013/04/13/reviving-groovy-in-action/#comments</comments>
		<pubDate>Sat, 13 Apr 2013 21:10:30 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Groovy]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=558</guid>
		<description><![CDATA[Some time ago, Dierk Koenig decided to write Groovy in Action, THE bible for learning the Groovy language and some libraries around it. The book was written for Groovy 1.0, and it was a great reference (I learned my first Groovy from that book), and though outdated, it still is a great book. Then, some [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://amzn.to/Zte3KJ"><img src="http://www.jworks.nl/wp-content/uploads/2013/04/9781932394849-150x150.jpg" alt="Groovy in Action" width="150" height="150" class="alignleft size-thumbnail wp-image-561" /></a><br />
Some time ago, Dierk Koenig decided to write <a href="http://amzn.to/Zte3KJ" title="Groovy in Action" target="_blank">Groovy in Action</a>, THE bible for learning the Groovy language and some libraries around it. The book was written for Groovy 1.0, and it was a great reference (I learned my first Groovy from that book), and though outdated, it still is a great book. Then, some years later, Groovy 1.5 came out. So it was time for an updated version of the Groovy in Action book, now the second edition. So, the musketeers got back together and investigated what the new features were, made some split who would write about what, and off they went. Only forgetting one small thing: this was not their only project on hand. So, sometimes the musketeers wrote some, sometimes they provided food for their family, sometimes they rested a bit. While time passed on, Groovy 1.6 came out. And even 1.7. And 1.8. And 1&#8230; well, 2.0 actually. </p>
<p>When Groovy 2.0 came out, not a lot of things had happend on the book. Sure, there were new some new chapters written, but there were also still 18 other chapters to write. So it was time for some Action. So, some time ago, just before a Grails Exchange far away, yours truly wrote Dierk, and with the famous words: &#8220;hee Dierk, wtf is going on with the book?&#8221;, was he invited in joining the musketeers. Ha, he thought, this is my chance on fame and fortune! So imagining the screaming girls, the fireworks, the rains of golden coins, I signed the contract with my blood, and joined the party. And what a party it was. It was more like a pack of brigands than the musketeers! Ohohoh, what had I signed up for? So, when the Grails Exchange really came, a chat with Guillaume was needed. So we did, after a great chat, some great ideas, and full of energy, we parted and&#8230; Nothing happened. All the things I had to do, for all the great ideas we had, I did nothing. Also I was to busy to get things going! But then, a voice from the sky came, which got us all by surprise. It was our great publisher, Manning, demanding progress. And he got it.</p>
<p><a href="http://www.manning.com/koenig2/"><img src="http://www.jworks.nl/wp-content/uploads/2013/04/koenig2_cover150-150x150.jpg" alt="Groovy in Action MEAP" width="150" height="150" class="alignright size-thumbnail wp-image-565" /></a><br />
So, after almost one year of inactivity, a great motivation, and a lot of talks, we published a new version of the <a href="http://www.manning.com/koenig2/" title="Groovy in Action, Second Edition" target="_blank">Groovy in Action MEAP</a>! There&#8217;s an updated chapter on the GDK, created by yours truly! So no, the project is not dead. Really, trust us when we say we&#8217;re working hard, really hard, on finishing this great book! We&#8217;re all heavily motivated in delivering THE best book on Groovy, so no, the project is not dead, it was merely in hibernation, and yes, we will deliver this book. It will still take a bit of work, but another chapter is just finished (another one by me, this time about Groovy and Testing), with features about Spock, Gradle and many other improvements, and other chapters will follow soon. So keep spamming the <a href="http://www.manning-sandbox.com/forum.jspa?forumID=571">forums</a>, keep spamming the authors, keep us sharp, and shout your interested. We will deliver!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2013/04/13/reviving-groovy-in-action/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding an alarm to Android Alarm clock</title>
		<link>http://www.jworks.nl/2013/04/03/adding-an-alarm-to-android-alarm-clock/</link>
		<comments>http://www.jworks.nl/2013/04/03/adding-an-alarm-to-android-alarm-clock/#comments</comments>
		<pubDate>Wed, 03 Apr 2013 14:28:33 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[alarm]]></category>
		<category><![CDATA[android]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=551</guid>
		<description><![CDATA[If you need to set an alarm from within your application, you might be guided to the android AlarmManager. While it&#8217;s possible to use the AlarmManager to fire an alarm, sometimes you want to use the alarm application the user has installed himserver. To do so, you can use AlarmClock. This can be done in [...]]]></description>
				<content:encoded><![CDATA[<p>If you need to set an alarm from within your application, you might be guided to the android <a href="http://developer.android.com/reference/android/app/AlarmManager.html" title="Android Alarm Manager" target="_blank">AlarmManager</a>. While it&#8217;s possible to use the AlarmManager to fire an alarm, sometimes you want to use the alarm application the user has installed himserver.</p>
<p>To do so, you can use <a href="http://developer.android.com/reference/android/provider/AlarmClock.html" title="Alarm Clock" target="_blank">AlarmClock</a>. </p>
<p>This can be done in the following way:</p>
<pre class="brush: java">
Intent openNewAlarm = new Intent(AlarmClock.ACTION_SET_ALARM);
openNewAlarm.putExtra(AlarmClock.EXTRA_HOUR, 0);
openNewAlarm.putExtra(AlarmClock.EXTRA_MINUTES, 20);
startActivity(openNewAlarm);
</pre>
<p>You&#8217;ll need to add the right security permission to your manifest though:</p>
<pre class="brush: xml">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
</pre>
<p>A small note: there&#8217;s no way to cancel the alarm after the intent has been sent. If you need more control over your alarm, you&#8217;ll need to to use something like the <a href="http://developer.android.com/reference/android/app/AlarmManager.html" title="Android Alarm Manager" target="_blank">AlarmManager</a> anyway, which provides more control over your set alarm, like cancelling.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2013/04/03/adding-an-alarm-to-android-alarm-clock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to Hamcrest Collection Matchers</title>
		<link>http://www.jworks.nl/2013/01/22/introduction-to-hamcrest-collection-matchers/</link>
		<comments>http://www.jworks.nl/2013/01/22/introduction-to-hamcrest-collection-matchers/#comments</comments>
		<pubDate>Tue, 22 Jan 2013 10:00:48 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[iterables]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=538</guid>
		<description><![CDATA[Another post in the series of Hamcrest Matchers, a framework for writing test matcher objects. This week we&#8217;ll dive into collections and iterables, and we&#8217;ll see how we can use Hamcrest for making those asserts even more readable. In many software projects I encounter code like the following: assertEquals("Collection size doesn't match", 4, reds.size()); Or, [...]]]></description>
				<content:encoded><![CDATA[<p>Another post in the series of Hamcrest Matchers, a framework for writing test matcher objects. This week we&#8217;ll dive into collections and iterables, and we&#8217;ll see how we can use Hamcrest for making those asserts even more readable.</p>
<p>In many software projects I encounter code like the following:</p>
<pre class="brush:java">
assertEquals("Collection size doesn't match", 4, reds.size());
</pre>
<p>Or, even worse:</p>
<pre class="brush:java">
assertTrue(greens.size() == 0);
</pre>
<p>The second loses context by not knowning the expected value and the actual value; it knows only that they are not equal.</p>
<p>So, while this above might be a valid asserts, their readability and error feedback can be improved by using a framework like Hamcrest. This can be done in the following way:</p>
<pre class="brush:java">
assertThat(reds, hasSize(4));
assertThat(greens, is(empty()));
</pre>
<p>In my opinion, this increases readability quite a bit, and makes it easier to maintain tests like this. Check the following piece of code for more examples:</p>
<pre class="brush:java">

    @Test
    public void testIterablesMatchers() {
        Catalogue catalogue = new Catalogue();

        catalogue.addMovie(new Movie("Lord of the Rings"));
        catalogue.addMovie(new Movie("The Ring"));
        catalogue.addMovie(new Movie("The Bling Ring"));

        List movies = catalogue.getMovies();

        // Check each item for a certain condition
        assertThat(movies, everyItem(Matchers.hasProperty("title", containsString("Ring"))));

        // Checks that the collection has at least one item which matches the specified matcher.
        assertThat(movies, hasItem(new Movie("The Ring")));
        // Checks that the collection has at least one or multiple items which match the specified matcher.
        assertThat(movies, hasItems(new Movie("The Ring"), new Movie("Lord of the Rings")));

        // Creates a matcher matching examined iterables that yield no items.
        assertThat(new ArrayList(), emptyIterable());

        // Checks that all of the items match the expected items, in the same order.
        assertThat(movies, contains(new Movie("Lord of the Rings"), new Movie("The Ring"), new Movie("The Bling Ring")));
        // Checks that all of the items match the expected items, in any order.
        assertThat(movies, containsInAnyOrder(new Movie("The Ring"), new Movie("The Bling Ring"), new Movie("Lord of the Rings")));

        // Beware here: iterableWithSize is a bit funky with Generics, so the bast (and only) way to solve this is to be explicit about the generics
        assertThat(Arrays.asList("foo", "bar"), IsIterableWithSize.iterableWithSize(2));

        // Matches when the size() method returns a value equal to the specified size.
        assertThat(movies, hasSize(3));

        // Checks that the examined collections whose isEmpty method returns true.
        assertThat(new ArrayList(), is(empty()));
        // Checks that the examined collections whose isEmpty method returns true.
        assertThat(new ArrayList(), is(emptyCollectionOf(Movie.class)));
    }
</pre>
<h5>Error messages</h5>
<p>Whenever a assertion fails, you get a pretty clear error message, which will help you quickly solve issues you run into. For example, if in the above code example I would have made a wrong assumption, and instead of:</p>
<pre class="brush:java">
        assertThat(movies, hasSize(3));
</pre>
<p>If would have typed:</p>
<pre class="brush:java">
        assertThat(movies, hasSize(2));
</pre>
<p>The following error message would have shown up:</p>
<pre class="brush:java">
java.lang.AssertionError:
Expected: a collection with size 
     but: collection size was 
</pre>
<p>Whereas the the JUnit equivalent:</p>
<pre class="brush:java">
assertEquals(2, movies.size());
</pre>
<p>Would have resulted in the following:</p>
<pre class="brush:java">
junit.framework.AssertionFailedError:
Expected :2
Actual   :3
</pre>
<p>Which would have helped a great deal also, but it&#8217;s just that the Hamcrest one is a bit more clear, and a bit more elaborate on the error message. </p>
<p>I hope this helps in picking up Hamcrest and that it helps in writing clear, consistent tests. See you next week!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2013/01/22/introduction-to-hamcrest-collection-matchers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introduction to Hamcrest String Matchers</title>
		<link>http://www.jworks.nl/2013/01/15/introduction-to-hamcrest-string-matchers/</link>
		<comments>http://www.jworks.nl/2013/01/15/introduction-to-hamcrest-string-matchers/#comments</comments>
		<pubDate>Tue, 15 Jan 2013 09:00:00 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[matchers]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=522</guid>
		<description><![CDATA[Another week, another blogpost. For those catching on just now, or finding this post using a search engine; I&#8217;m writing a series of posts about Hamcrest, a framework for writing test matcher objects, and showing you how to use Hamcrest. While the previous post was about the Core Matchers, the topic of this week is [...]]]></description>
				<content:encoded><![CDATA[<p>Another week, another blogpost. For those catching on just now, or finding this post using a search engine; I&#8217;m writing a series of posts about Hamcrest, a framework for writing test matcher objects, and showing you how to use Hamcrest. While the previous post was about the Core Matchers, the topic of this week is Hamcrest String Matchers.</p>
<p>The Hamcrest String Matchers are the following:<br />
<em>containsString, startsWith, endsWith, equalToIgnoringCase, equalToIgnoringWhitespace, isEmpty, isEmptyOrNull</em> and <em>stringContainsInOrder</em></p>
<p>Also in this post, I will give an example of all of them in the code block below:</p>
<pre class="brush:java">
    @Test
    public void testMovieStringMatchers() {
        // Setting up our class to test here.
        Movie movie = new Movie("Charlies Angels");
        movie.setDescription("");
        movie.setDirector(null);
        movie.setStoryLine("The captivating crime-fighting trio who are the masters of disguise, espionage and martial arts.");

        // Checks that the actual result contains the specified String
        assertThat(movie.getTitle(), containsString("Angels"));
        // Checks that the actual result starts with the specified String
        assertThat(movie.getTitle(), startsWith("Charlies"));
        // Checks that the actual result ends with the specified String
        assertThat(movie.getTitle(), endsWith("Angels"));

        // Checks that the actual result equals the specified String, ignoring casing
        assertThat(movie.getTitle(), equalToIgnoringCase("charlies angels"));
        // Beware: this method work different than you would expect, since it doesn't ignore whitespaces: it ignores most of them. Check the Javadoc.
        assertThat(movie.getTitle(), equalToIgnoringWhiteSpace(" charlies  angels   "));

        // Checks that the actual result is an empty String
        assertThat(movie.getDescription(), isEmptyString());
        // Checks that the actual result is null or an empty String
        assertThat(movie.getDirector(), isEmptyOrNullString());

        // Checks that the actual result contains the specified Strings in this sequence
        assertThat(movie.getStoryLine(), stringContainsInOrder(Arrays.asList("The", "trio", "arts")));
    }
</pre>
<p>Again, the Hamcrest assertions are very clear, and makes your testcode a easier to read an maintain. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2013/01/15/introduction-to-hamcrest-string-matchers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generate a toString using Guava Objects.toStringhelper with IntelliJ</title>
		<link>http://www.jworks.nl/2013/01/10/generate-a-tostring-using-guava-objects-tostringhelper-with-intellij/</link>
		<comments>http://www.jworks.nl/2013/01/10/generate-a-tostring-using-guava-objects-tostringhelper-with-intellij/#comments</comments>
		<pubDate>Thu, 10 Jan 2013 12:00:11 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[guava]]></category>
		<category><![CDATA[intellij]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=500</guid>
		<description><![CDATA[In IntelliJ it&#8217;s possible to generate a toString using Alt+Insert (Windows) or Ctrl+N (Mac). In this dialog, you can choose to use different implementations, like a StringBuffer, a StringBuilder, Apache Commons ToStringBuilder, and more. But if you&#8217;re using Google Guava, you can create your own template in IntelliJ. To do so, do the following: Open [...]]]></description>
				<content:encoded><![CDATA[<p>In <a href="http://www.jetbrains.com/idea">IntelliJ</a> it&#8217;s possible to generate a toString using Alt+Insert (Windows) or Ctrl+N (Mac). In this dialog, you can choose to use different implementations, like a StringBuffer, a StringBuilder, <a href="http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/builder/ToStringBuilder.html" target="_blank">Apache Commons ToStringBuilder</a>, and more. But if you&#8217;re using <a href="http://code.google.com/p/guava-libraries/" target="_blank">Google Guava</a>, you can create your own template in IntelliJ. To do so, do the following:</p>
<ul>
<li>Open a class file
<li>Press Alt + Insert or Ctrl + N to popup the &#8216;Generate&#8217; menu
<li>Choose toString()
<li>Click the button named &#8216;Settings&#8217;
<li>Go to the &#8216;Templates&#8217; tab
<li>Create a new template named
<li>Add the following in the template:
</ul>
<pre class="brush:java">
public String toString() {
#set ($autoImportPackages = "com.google.common.base.Objects")
   return Objects.toStringHelper(this)
#foreach ($member in $members)
   .add("$member.name", $member.accessor)
#end
   .toString();
}
</pre>
<div id="attachment_503" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.jworks.nl/?attachment_id=503" rel="attachment wp-att-503"><img src="http://www.jworks.nl/wp-content/uploads/2013/01/Screen-Shot-2013-01-06-at-21.30.43--300x209.png" alt="IntelliJ toString settings window" width="300" height="209" class="size-medium wp-image-503" /></a><p class="wp-caption-text">IntelliJ toString settings window</p></div>
<p>This template is based on the Apache Commons toStringBuilder, and will generate a consistent toString for you using Google Guava.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2013/01/10/generate-a-tostring-using-guava-objects-tostringhelper-with-intellij/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to Hamcrest Core Matchers</title>
		<link>http://www.jworks.nl/2013/01/08/introduction-to-hamcrest-core-matchers/</link>
		<comments>http://www.jworks.nl/2013/01/08/introduction-to-hamcrest-core-matchers/#comments</comments>
		<pubDate>Tue, 08 Jan 2013 09:00:30 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[core]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=508</guid>
		<description><![CDATA[In our previous post, we got a small introduction into Hamcrest, a framework for writing test matcher objects, and why you should use them. This post will dive deeper into Hamcrest Matchers. A Matcher is a &#8216;match&#8217; rule to be defined declaratively. The Hamcrest library comes with numerous matchers included, and it&#8217;s of course possible [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.jworks.nl/2013/01/15/introduction-to-hamcrest-core-matchers/photo-3/" rel="attachment wp-att-519"><img src="http://www.jworks.nl/wp-content/uploads/2013/01/photo-e1357509256680-150x150.jpg" title="I didn't write the post, but was involved a great deal" alt="I didn't write the post, but was involved a great deal" width="150" height="150" class="alignleft size-thumbnail wp-image-519" /></a></p>
<p>In our <a href="http://www.jworks.nl/2013/01/06/hamcrest-matchers-in-junit-tests/" title="Hamcrest Matchers in JUnit tests">previous post</a>, we got a small introduction into Hamcrest, a framework for writing test matcher objects, and why you should use them. This post will dive deeper into Hamcrest Matchers. A Matcher is a &#8216;match&#8217; rule to be defined declaratively. The Hamcrest library comes with numerous matchers included, and it&#8217;s of course possible to write your own Matcher. </p>
<p>The topic of this week: the Core Matchers.</p>
<p>The common Matchers of Hamcrest are the following:<br />
<em>is, equalTo, not, anything, instanceOf, any, isA, nullValue, notNullValue, sameInstance, theInstance, isIn, isOneOf</em> and <em>hasToString</em>.</p>
<p>I will give an example of all of them in the code block below:</p>
<pre class="brush:java">
    @Test
    public void testMovie() {
        // Creation of class under test.
        Movie movie = new Movie("Charlies Angels");
        movie.setInStock(false);
        movie.addGenre(ACTION);
        movie.addGenre(COMEDY);

        Movie otherMovie = movie;

        // This is method takes just a value, instead of a matcher.
        assertThat(movie.isRentable(), is(false));

        // This not method also just takes a value
        assertThat(movie.isRentable(), not(true));
        // This is a chained example for better readability, where the 'is' method takes the not matcher as an argument
        assertThat(movie.isRentable(), is(not(true)));

        // This is a simple String comparison
        assertThat(movie.getTitle(), equalTo("Charlies Angels"));
        // And can also be done this way:
        assertThat(movie.getTitle(), is("Charlies Angels"));

        // Anything always matches. Seriously, no idea what to use this for.
        assertThat(movie, is(anything()));

        // Checks the type of the object. Movie implements the Rentable interface
        assertThat(movie, is(instanceOf(Rentable.class)));

        // Matches when the examined object is an instance of the specified type, used eg. by JMock's with method.
        assertThat(movie, any(Movie.class));

        // Shortcut for is(instanceOf(SomeClass.class))
        assertThat(movie, isA(Rentable.class));

        // Checks if the object is null
        assertThat(new Movie(null).getTitle(), is(nullValue()));
        // Checks if the object is not null
        assertThat(movie.getTitle(), is(notNullValue()));

        // Check that the objects are the same instance
        assertThat(movie, is(sameInstance(otherMovie)));
        // Check that the objects are the same instance. Same as 'sameInstance'.
        assertThat(movie, is(theInstance(otherMovie)));

        // Checks if the examined object is found within the specified array.
        assertThat(movie.getMainGenre(), isIn(Arrays.asList(ACTION, DRAMA)));
        // Seems the same as 'isIn', but this is the varargs variant.
        assertThat(movie.getMainGenre(), isOneOf(ACTION, DRAMA));

        // Checks the toString of the tested object.
        assertThat(movie, hasToString("Movie{inStock=false, title=Charlies Angels, ratings=[]}"));
    }
</pre>
<p>As you can see, the Hamcrest assertions are very readable, and makes your testcode a bit easier to maintain.</p>
<p>PS: I always wanted to write some posts about Hamcrest, but the thing which really triggered me was the <a href="http://www.marcphilipp.de/downloads/posts/2013-01-02-hamcrest-quick-reference/Hamcrest-1.3.pdf">Hamcrest cheat sheet</a> created by Marc Philipp. Be sure to check it out, it&#8217;s a great piece of work!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2013/01/08/introduction-to-hamcrest-core-matchers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hamcrest Matchers in JUnit tests</title>
		<link>http://www.jworks.nl/2013/01/06/hamcrest-matchers-in-junit-tests/</link>
		<comments>http://www.jworks.nl/2013/01/06/hamcrest-matchers-in-junit-tests/#comments</comments>
		<pubDate>Sun, 06 Jan 2013 12:28:04 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=489</guid>
		<description><![CDATA[With JUnit being the dominant Test framework, chances are high that during your career as a software engineer, you&#8217;ve encountered the JUnit framework in one or more projects. Therefor, code like this: assertEquals("Result doesn't match hello", "hello", result.getValue()) Should not be unfamiliar to you. This blogpost is on improving the example above. There are good [...]]]></description>
				<content:encoded><![CDATA[<p>With JUnit being the dominant Test framework, chances are high that during your career as a software engineer, you&#8217;ve encountered the JUnit framework in one or more projects. Therefor, code like this:</p>
<pre class="brush:java">
assertEquals("Result doesn't match hello", "hello", result.getValue())
</pre>
<p>Should not be unfamiliar to you. This blogpost is on improving the example above. There are good alternatives in writing your tests, like <a href="http://www.spockframework.org">Spock</a>, but these take a little more effort to setup, since it&#8217;s mostly used in Groovy projects. The goal of this blogpost is to improve the above Java test with little effort, and for that, we&#8217;re going to use <a href="http://hamcrest.org/">Hamcrest</a>.</p>
<p>Hamcrest is (in our case) a set of Java matchers which improve the readability of the code and error messages shown when assertions do not match. So, let&#8217;s get back to our original example: what does it really say?</p>
<pre class="brush:java">
assertEquals("Result doesn't match hello", "hello", result.getValue())
</pre>
<p>Means something like: verify that the getValue method of result should return the String hello, and if not, print the message &#8216;result doesn&#8217;t match hello&#8217;. Wouldn&#8217;t it be more understandable if we could write something like this in a more natural language? Well, with Hamcrest we can:</p>
<pre class="brush:java">
assertThat(result.getValue(), is(equalsTo("hello"))
</pre>
<p>In this example, we&#8217;re using &#8216;assertThat&#8217;, which is a method coming from JUnit. JUnit itself supports a random amount of Matchers, but I would recommend on forgetting those, and just use only the Matchers from Hamcrest itself (which means, adding the Hamcrest library to the project, version 1.3 at this moment), since the Hamcrest library contains a much bigger set of Matchers. Also, note that the &#8216;is&#8217; method here is optional, but I added it to improve readability. </p>
<p>Hamcrest can be added to your Maven project by including the following dependency:</p>
<pre class="brush:xml">
<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-all</artifactId>
  <version>1.3</version>
</dependency>
</pre>
<p>And using this library, you&#8217;ll get many matchers, supporting String, Collections, Maps, Numbers, and even XML elements, but more on that in my next blog!</p>
<p>One last note: be aware to use the latests version of JUnit though (currently 4.11). When using an older version you might (read: will) run into dependency issues. Don&#8217;t say I didn&#8217;t warn you!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2013/01/06/hamcrest-matchers-in-junit-tests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Reloading Log4j Configuration in Tomcat</title>
		<link>http://www.jworks.nl/2012/10/22/reloading-log4j-configuration-in-tomcat/</link>
		<comments>http://www.jworks.nl/2012/10/22/reloading-log4j-configuration-in-tomcat/#comments</comments>
		<pubDate>Mon, 22 Oct 2012 10:24:41 +0000</pubDate>
		<dc:creator>Erik Pragt</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[log4j]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://www.jworks.nl/?p=475</guid>
		<description><![CDATA[When you&#8217;re in production environment and you want to diagnose some problems, it&#8217;s often handy to have some more logging available. So, when you&#8217;re using Log4j, and you want to have your log4j.xml reloaded, you can use Log4jWebConfigurer to make this happen. To use it, you need to add a listener your web.xml. Be careful [...]]]></description>
				<content:encoded><![CDATA[<p>When you&#8217;re in production environment and you want to diagnose some problems, it&#8217;s often handy to have some more logging available. So, when you&#8217;re using Log4j, and you want to have your log4j.xml reloaded, you can use <a href="http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/web/util/Log4jWebConfigurer.html">Log4jWebConfigurer</a> to make this happen.</p>
<p>To use it, you need to add a listener your web.xml. Be careful to place this before the ContextLoaderListener entry. Also important is to add a log4jRefreshInterval (in milliseconds), else Log4j won&#8217;t start it&#8217;s deamon thread.</p>
<p><b>web.xml</b></p>
<pre class="brush:xml">
&lt;context-param>
  &lt;param-name>log4jConfigLocation</param-name>
  &lt;param-value>classpath:log4j.xml</param-value> 
&lt;/context-param>
&lt;context-param>
  &lt;param-name>log4jRefreshInterval</param-name>
  &lt;param-value>1000</param-value>
&lt;/context-param>

&lt;context-param>
  &lt;param-name>contextConfigLocation</param-name>
  &lt;param-value>classpath:/application-context.xml</param-value>
&lt;/context-param>

&lt;listener>
  &lt;listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
&lt;/listener>

&lt;listener>
  &lt;listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
&lt;/listener>

</pre>
<p>This should reload your log4j.xml changes whenever they occur.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jworks.nl/2012/10/22/reloading-log4j-configuration-in-tomcat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
