<?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>Schauderhaft &#187; Softwaredevelopment</title>
	<atom:link href="http://blog.schauderhaft.de/category/softwaredevelopment/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Sun, 25 Jul 2010 14:04:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Properties of a Good Unit Tests</title>
		<link>http://blog.schauderhaft.de/2010/07/18/properties-of-a-good-unit-tests/</link>
		<comments>http://blog.schauderhaft.de/2010/07/18/properties-of-a-good-unit-tests/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 04:03:07 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=556</guid>
		<description><![CDATA[Question: what are the properties which make a test a good unit test? If you have a good answer, check out my answer and add to it in the comments. If you don&#8217;t here is mine.
Long Descriptive Name: For example returnsNullWhenCalledOnEmptyList is a nice name for a test
Simple Clean Structure: A test performs necessary setup, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Question:</strong> what are the properties which make a test a good unit test? If you have a good answer, check out my answer and add to it in the comments. If you don&#8217;t here is mine.</p>
<p><strong>Long Descriptive Name:</strong> For example <tt>returnsNullWhenCalledOnEmptyList</tt> is a nice name for a test</p>
<p><strong>Simple Clean Structure:</strong> A test performs necessary setup, executes the method to be tested and performs its assertions. Not more not less </p>
<p><strong>Fast:</strong> one thousand tests should run in less than a minute. This allows an extensive test suite to run often during development. If your tests are slower, you might be doing integration tests.</p>
<p><strong>Test a Single Feature or behavior:</strong> This is sometimes phrased as &#8216;one assertion&#8217;, which I consider misleading. If you need multiple assertion in order to assert one behavior thats OK, although you might consider a new assert method for that. </p>
<p><strong>Reliable:</strong> For the same code base a test should produce the same result every time. A test that fails every 10th time will fail for a real bug and you won&#8217;t know it.</p>
<p><strong>Stable:</strong> The test only fails, when the code under test doesn&#8217;t behave as expected, not because anything else changed.</p>
<p><strong>Easy to Understand:</strong> Just as normal code, the intention of a test should be easy to understand. Names help with this. Extracting setup code in a properly named method helps as well.</p>
<p><strong>Should Not Use Production Classes, Except The Class Under Test:</strong> This helps with the &#8216;Stable&#8217; and the &#8216;Fast&#8217; property. It&#8217;s ok to use simple classes like String or Integer.</p>
<p><strong>Don&#8217;t Try to Squeeze All Tests for a Class in a Single Test Class:</strong> For many classes you will have more then one test class. The different test classes will probably differ in the setup needed for the contained tests.</p>
<p><strong>Independent:</strong> No Test should depend on any other Test or on the order of execution.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/07/18/properties-of-a-good-unit-tests/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>An Analysis of NonUniqueObjectException and LazyInitializationException in Hibernate</title>
		<link>http://blog.schauderhaft.de/2010/07/11/an-analysis-of-nonuniqueobjectexception-and-lazyinitializationexception-in-hibernate/</link>
		<comments>http://blog.schauderhaft.de/2010/07/11/an-analysis-of-nonuniqueobjectexception-and-lazyinitializationexception-in-hibernate/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 19:25:19 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[domain driven design]]></category>
		<category><![CDATA[hibernate]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=529</guid>
		<description><![CDATA[I consider Hibernate a great and useful tool. But it has some mean Exceptions lurking in the darker corners. Today I&#8217;d like to explore the ones from the title in a little more detail, including different approaches on how to avoid them.
Let&#8217;s start with the well known LazyInitializationException. When you search for this one in [...]]]></description>
			<content:encoded><![CDATA[<p>I consider Hibernate a great and useful tool. But it has some mean Exceptions lurking in the darker corners. Today I&#8217;d like to explore the ones from the title in a little more detail, including different approaches on how to avoid them.</p>
<p>Let&#8217;s start with the well known <tt>LazyInitializationException</tt>. When you search for this one in the Internet you end up with scenarios like this one:</p>
<p>You load <tt>A</tt>, close the session you  used and try to access a property of <tt>A</tt> which gets loaded lazily resulting in a <tt>LazyInitializationException</tt>. That one is well described, including strategies to avoid it, which basically consist of not closing the session to early.</p>
<p>But there are more hideous scenarios. Assume the classes Mom, Dad and Kid are mapped as one would expect, with references from Mom and Dad to Kid. Now consider the following piece of code:</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">Session session = openSession<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">session.<span class="me1">beginTransaction</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Mom mom = <span class="br0">&#40;</span>Mom<span class="br0">&#41;</span> session.<span class="me1">load</span><span class="br0">&#40;</span>Mom.<span class="kw2">class</span>, momId<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">Kid momsKid = mom.<span class="me1">getKid</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// dad comes by</span></div>
</li>
<li class="li1">
<div class="de1">Dad dad = <span class="br0">&#40;</span>Dad<span class="br0">&#41;</span> session.<span class="me1">load</span><span class="br0">&#40;</span>Dad.<span class="kw2">class</span>, dadId<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">Kid dadsKid = dad.<span class="me1">getKid</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// and leaves</span></div>
</li>
<li class="li1">
<div class="de1">session.<span class="me1">evict</span><span class="br0">&#40;</span>dad<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// alas he has taken kido with him</span></div>
</li>
<li class="li1">
<div class="de1">momsKid.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Depending on your exact Mapping you might get a <tt>LazyInitializationException</tt> in the last line, although the session is still open. The problem here is that when dad got evicted the kid got evicted as well. And since Mom has a reference to the exact same object, that object isn&#8217;t attached to any session anymore. In order to see this effect you&#8217;ll have to use mappings like this:</p>
<div class="geshi no java">
<div class="head">@ManyToOne(fetch = LAZY, cascade = CascadeType.ALL)</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @Cascade<span class="br0">&#40;</span>org.<span class="me1">hibernate</span>.<span class="me1">annotations</span>.<span class="me1">CascadeType</span>.<span class="me1">ALL</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> Kid kid<span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>The <tt>NonUniqueObjectException</tt> gets caused by what could be considered the reverse process. Examine the following piece of code:</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Dad left for another woman, I mean Session</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Dad dad = loadDad<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Session session = openSession<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; session.<span class="me1">beginTransaction</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Mom mom = <span class="br0">&#40;</span>Mom<span class="br0">&#41;</span> session.<span class="me1">load</span><span class="br0">&#40;</span>Mom.<span class="kw2">class</span>, momId<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>mom.<span class="me1">getKid</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Now dad wants to move back in</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; session.<span class="me1">saveOrUpdate</span><span class="br0">&#40;</span>dad<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>The first method call loads a <tt>Dad</tt> object including <tt>Kid</tt> from a different session. Then from the &#8216;main&#8217; session <tt>Mom</tt> gets loaded including kid. When we attach the <tt>Dad</tt> object to the session it tries to attach its version of the <tt>Kid</tt> as well, which collides with the <tt>Kid</tt> already present in the session.</p>
<p>So how can we avoid these kind of problem? I see several options:</p>
<ul>
<li>Don&#8217;t use Cascade. Of course this means much more effort on your side in many cases, since you have to carefully track what you need to attach or evict from a session.</li>
<li>Don&#8217;t use <tt>evict</tt> and don&#8217;t reattach existing instances to sessions. I guess this should work in many cases. But I have seen cases where this approach would have caused a large overhead, because reloading complex object graphs.</li>
<li>Carefully design your cascading boundaries. A look at the Aggregate Root from <a href="http://www.amazon.de/gp/product/0321125215?ie=UTF8&#038;tag=schauderhafte-21&#038;linkCode=as2&#038;camp=1638&#038;creative=19454&#038;creativeASIN=0321125215">DDD</a> might help here. An Aggregate Root is a single class which works as a gate keeper (did anybody say facade) to a cluster of classes related to the Aggregate Root. When all the Hibernate related actions go against that Aggregate Root, and cascade from there to the content and only the content of the Aggregate Root you should be safe. All entities outside an Aggregate Root have to use the Aggregate Root or Repositories to gain access. This needs some bookkeeping. But when you do it properly it might actually improve the over all design of your code. Please note that all this is a thought experiment on my side, and I can&#8217;t promise that it actually works as intended, but I think it should.</li>
</ul>
<p>If you want the complete source code for the example, you can download this <a href="/wp-content/uploads/2010/07/sources.zip">source.zip</a> file.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/07/11/an-analysis-of-nonuniqueobjectexception-and-lazyinitializationexception-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Tune a SQL Statement</title>
		<link>http://blog.schauderhaft.de/2010/06/27/how-to-tune-a-sql-statement/</link>
		<comments>http://blog.schauderhaft.de/2010/06/27/how-to-tune-a-sql-statement/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 04:07:03 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=531</guid>
		<description><![CDATA[One of my standard questions I ask when interviewing a candidate with some database or SQL know how is:
Assume you have a fairly complex select statement operating on some serious tables (tens of millions of rows). The customer complains the SQL statement is to slow. What options would you consider for tuning such a select [...]]]></description>
			<content:encoded><![CDATA[<p>One of my standard questions I ask when interviewing a candidate with some database or SQL know how is:</p>
<blockquote><p>Assume you have a fairly complex select statement operating on some serious tables (tens of millions of rows). The customer complains the SQL statement is to slow. What options would you consider for tuning such a select statement? If it matters assume it is an Oracle database.</p></blockquote>
<p>Just in case you want to apply for a job at a <a href="http://www.lineas.de">LINEAS</a> here is, what I would consider a damn good answer:</p>
<p>First I check if the SQL statement is really slow, for that I&#8217;ll execute the SQL statement with a low level tool (SQL Plus, SQL Developer &#8230;) on the database. I fetch the same amount of rows, as the customer (or his application) does. I do that on the DB machine or one a machine with very good connectivity to the database, in order to take network issues out of the loop. I measure the execution time. If this is as slow as described by the customer, the SQL statement plus DB seem to be the problem. If not probably something else is the problem (network, application logic &#8230;)</p>
<p>I establish a target performance, we need to meet the customer requirements.</p>
<p>I check the execution plan of the statement comparing it with the structure of the schema (tables, indexes, number of rows in tables compared to number of rows actually selected). Are there any obvious problems? For example;</p>
<ul>
<li> Full Table Scans that result only in a small percentage of the rows of that table</li>
<li>Index accesses on a large percentage of rows</li>
<li>Indexes that don&#8217;t get used, although one would expect them to</li>
<li>Identical expensive pieces of the execution plan, on different places  of the execution plan</li>
<li>Estimated number of rows in the execution plan that differ significantly from the actual number of rows returned</li>
</ul>
<p>Depending on what I find I would try some of the following actions, to improve performance</p>
<ul>
<li>gather statistics</li>
<li>create an additional index supporting the where clauses used in the statement</li>
<li>add columns to an index, in order to remove the need for additional table accesses</li>
<li>use hints to force certain execution plans</li>
<li>exchange (not) in with (not) exists and vice versa</li>
<li>rearrange the select statement (e.g. turn a union of  joins into a join of union)</li>
<li>partitioning of tables and indexes</li>
<li>use materialized views</li>
<li>in very special cases use bitmap indexes or dimensions</li>
</ul>
<p>With these options the performance of most of the &#8217;slow&#8217; statements I encountered so far could get brought to acceptable levels. Often improving performance by multiple orders of magnitude. When I am able to identify the problem, but not the solution <a href="http://asktom.oracle.com/pls/apex/f?p=100:1:0">Ask Tom</a> is what I would do next.</p>
<p>There is one answer I get very often, which I consider pretty bad, if it comes without further comment:</p>
<blockquote><p>Replace outer joins with inner joins</p></blockquote>
<p>Outer joins have a different meaning than inner joins. Normally you can&#8217;t replace one for the other. If you actually can, I&#8217;d consider using an outer join a bug, so replacing it with an inner join is bug fixing, not tuning.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/06/27/how-to-tune-a-sql-statement/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Great Books for Becoming a Better Developer</title>
		<link>http://blog.schauderhaft.de/2010/06/12/great-books-for-becoming-a-better-developer/</link>
		<comments>http://blog.schauderhaft.de/2010/06/12/great-books-for-becoming-a-better-developer/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 08:11:59 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[books]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=499</guid>
		<description><![CDATA[You want to become an even better developer? Then you must read these books. If you have already read them. Read them again.
Design Patterns. Elements of Reusable Object-Oriented Software: This is the standard book on patterns and patterns are an extremely important piece of vocabulary for every developer. Not to impress coworkers with your knowledge, [...]]]></description>
			<content:encoded><![CDATA[<p>You want to become an even better developer? Then you must read these books. If you have already read them. Read them again.</p>
<p><a href="http://www.amazon.de/gp/product/0201633612?ie=UTF8&amp;tag=schauderhafte-21&amp;linkCode=as2&amp;camp=1638&amp;creative=19454&amp;creativeASIN=0201633612">Design Patterns. Elements of Reusable Object-Oriented Software:</a> This is the standard book on patterns and patterns are an extremely important piece of vocabulary for every developer. Not to impress coworkers with your knowledge, but to be able to think about bigger concepts than loops and conditionals. It describes each pattern with its purpose, it&#8217;s applicability and an example. Make sure to read a couple of blog posts about each pattern, to make sure you avoid some of the more common mistakes when applying them.</p>
<p><a href="http://www.amazon.de/gp/product/0132350882?ie=UTF8&amp;tag=schauderhafte-21&amp;linkCode=as2&amp;camp=1638&amp;creative=19454&amp;creativeASIN=0132350882">Clean Code: A Handbook of Agile Software Craftsmanship</a>: You want to be proud of your code? You want to be able to go back to your code in a year without swearing? This is the book for you. The author Robert C. Martin (also known as Uncle Bob) is extremely aggressive on what he is accepting as clean. This book really changed the way I look at code, and of course the way I write code. Make sure to really understand the lengthy examples. It is tedious, but it pays.</p>
<p><a href="http://www.amazon.de/gp/product/020161622X?ie=UTF8&amp;tag=schauderhafte-21&amp;linkCode=as2&amp;camp=1638&amp;creative=19454&amp;creativeASIN=020161622X">The Pragmatic Programmer. From Journeyman to Master</a>: In Software Development the professionals often follow practices which the professionals them selfs aren&#8217;t aware of. This book lists and explains many of these practices as well as some obvious once. While this sounds trivial the resulting book is a great source of small and big practices to acquire as a habit.</p>
<p>For the German readers of my blog: Read the english originals. German translations of computer books are horrible on average.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/06/12/great-books-for-becoming-a-better-developer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>3 Books You Should Read When You Want to Go Agile</title>
		<link>http://blog.schauderhaft.de/2010/05/30/3-books-agile/</link>
		<comments>http://blog.schauderhaft.de/2010/05/30/3-books-agile/#comments</comments>
		<pubDate>Sun, 30 May 2010 08:21:09 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[books]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=509</guid>
		<description><![CDATA[If you haven&#8217;t switched to an agile approach to software development yet, you probably should. But be warned. Agile is not about daily stand ups in the morning. It also isn&#8217;t about renaming your &#8220;work break down structure&#8221; into &#8220;backlog&#8221;. Going agile requires changes deep in the internals of your team and/or in your company, [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t switched to an agile approach to software development yet, you probably should. But be warned. Agile is not about daily stand ups in the morning. It also isn&#8217;t about renaming your &#8220;work break down structure&#8221; into &#8220;backlog&#8221;. Going agile requires changes deep in the internals of your team and/or in your company, depending on the level on which you want to go agile. In my experience there is just no reasonable way to gain the required understanding of agile approaches from on-line resources alone. I recommend the following three books instead:</p>
<p><strong><a href="Agile Software Development with Scrum ">Agile  Software Development with Scrum by Ken Schwaber</a></strong>: Obviously this book is about Scrum, which is just one of many agile processes. But even if you don&#8217;t want to go with Scrum, this book will teach you a lot about why Scrum is designed the way it is, and this will be very helpful, no matter what approach you choose. Be warned: the book is small, has crappy graphics and is rather expensive. But it is worth it&#8217;s money.</p>
<p><strong><a href="http://www.amazon.com/gp/product/0321150783?ie=UTF8&amp;tag=schauderhaft-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321150783">Lean Software Development: An Agile Toolkit by Mary and Tom Poppendieck</a></strong>: This book isn&#8217;t tied to any specific process or approach, instead it describes the underlying concepts, which are common to all agile approaches. And these are the ones that really count. Since the Poppendieck have a lot of experience in non software projects they provide lots of examples an cross references which might help convince a stubborn manager, who doesn&#8217;t realizes that doing waterfall software development might be 10years behind standards in software development, but is about 50 years behind when compared with other branches of industry.</p>
<p><strong><a href="http://www.mikecohnsignatureseries.com/books/management-3-0">Management 3.0: Leading Agile Developers, Developing Agile Leaders by Jurgen Appelo</a></strong>: Unfortunately you can&#8217;t buy the book yet. But I had the honor to review an early version of the book and provide Jurgen with my nit-picky comments. This book is very different from the other two, because it focuses completely on the management side of things. Agile doesn&#8217;t mean managers will go away, they don&#8217;t even become useless. Quite the different, they might actually put themselves to good use, and this books will tell you (or your boss) how. Even if you don&#8217;t go fully agile, this book will offer lots of advice and ideas, on how to improve you work as a manager.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/05/30/3-books-agile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Question isn&#8217;t what is Going on at Oracle or SUN</title>
		<link>http://blog.schauderhaft.de/2010/05/09/the-question-isnt-what-is-going-on-at-oracle-or-sun/</link>
		<comments>http://blog.schauderhaft.de/2010/05/09/the-question-isnt-what-is-going-on-at-oracle-or-sun/#comments</comments>
		<pubDate>Sun, 09 May 2010 08:40:00 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=490</guid>
		<description><![CDATA[Since SUN was bought by Oracle, many people are speculating what is going on at Oracle, and how this will affect the future of Java. When you are relying heavily on java for your daily work, it is obviously extremely interesting, what is going to happen with java. At least I don&#8217;t want to become [...]]]></description>
			<content:encoded><![CDATA[<p>Since SUN was bought by Oracle, many people are speculating <a href="http://weblogs.java.net/blog/mkarg/archive/2010/05/01/what-heck-going-former-sun-microsystems">what is going on at Oracle</a>, and how this will affect the <a href="http://java.dzone.com/articles/oracles-roadmap-sun">future</a> of <a href="http://java.dzone.com/articles/oracle-and-jcp">Java</a>. When you are relying heavily on java for your daily work, it is obviously extremely interesting, what is going to happen with java. At least I don&#8217;t want to become a COBOL developer. So speculating about the future comes natural. But as a saying goes: Predictions are difficult, especially when they are concerned with the future. So I think there are two things which are more important to consider then what is actually going on at Oracle:</p>
<p>What are the signs to look out for, that Java is turning into just another kind of COBOL?</p>
<p>What are you going to do when these signs show up?</p>
<p>For many the answer to the first question obviously will be: Java already is the new COBOL. It&#8217;s true in the sense that there exists tons of legacy codes, which will continue to exist for the decades to come. But Java has still a big difference: Tons of new projects get started. This includes commercial products as well as <a href="http://en.wikipedia.org/wiki/Free_and_Open_Source_Software">FOSS</a>.</p>
<p>Another difference compared to COBOL is: Java is moving forward. Java 7 is (still) announced for 2010, and possibly more significant, new languages are living on the JVM, like Scala, Groovy and Clojure.</p>
<p>This leads to the signs, that mark the point in time to switch to something different:</p>
<ul>
<li><strong>No new versions of the JDK</strong>. Java never was especially fast or aggressively evolving, but when new versions stop to come at all, or aren&#8217;t containing the significant improvements we need, that is a bad sign.</li>
<li><strong>Companies stop using Java for new projects</strong>. Actually when they do stop, it is to late. You should get nervous, when serious discussions are started at large companies that are using mainly Java right now. Obviously this is especially true, when these companies are your customers or employers.</li>
<li><strong>The Java FOSS community moves to new languages and platforms. </strong>Keep an eye on <a href="http://sourceforge.net/">Sourceforge</a>, <a href="http://www.apache.org">Apache</a> and Co. are the new and relevant projects still based on and targeted for Java?</li>
<li><strong>Drop in number of events and number of visitors at events targeted for Java</strong>. Does it get empty at your favorite Java conference? Or does it turn into just a couple of booths on a more general targeted conference?</li>
</ul>
<p>So one day you will notice, that to many of the indicators mentioned above are flashing at you. What do you do?</p>
<ul>
<li>Determine if only the language or the Java platform is affected.</li>
<li>Pick Up a new language or a new platform. Actually when you are early to move, two or three, because you won&#8217;t be able to reliable identify the next big thing.</li>
<li>Do real project using these languages. Get involved in open source projects or do your own in order to really learn the new language.</li>
<li>Do marketing for your decision. Once you are heading for a new platform, you don&#8217;t want to go there alone. There are only few projects that are small enough to be done alone. It is hard to learn, when you are on your own. Get a group going, that can learn and work together. This will at least in the small accelerate the move to the new language. And that is a good thing once you are heading that way.</li>
<li>Once you are confident to seriously get some work done, using the new tools, push them for your professional projects.</li>
</ul>
<p>Of course your mileage will vary but for me, it is about time to consider alternatives to Java. That is one of the reasons, why I learned a little Scala, why I will learn groovy and grails, and why I will have a look at Clojure. I&#8217;ll do personal projects doing those. And I am marketing that decision right now.</p>
<p>And even if Java stays healthy and Scala dies, I&#8217;ll profit from learning new languages and concepts.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/05/09/the-question-isnt-what-is-going-on-at-oracle-or-sun/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why I like SQL</title>
		<link>http://blog.schauderhaft.de/2010/04/11/why-i-like-sql/</link>
		<comments>http://blog.schauderhaft.de/2010/04/11/why-i-like-sql/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 14:52:34 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=459</guid>
		<description><![CDATA[This is not a joke! I am serious, I like SQL for the following reasons:

SQL is declarative, which as a side effect means there are now corner cases hidden in if &#8230; then &#8230; else constructs. In practice this means, that once a SQL statement is working for a couple of cases, it is fairly [...]]]></description>
			<content:encoded><![CDATA[<p>This is not a joke! I am serious, I like SQL for the following reasons:</p>
<ul>
<li>SQL is declarative, which as a side effect means there are now corner cases hidden in if &#8230; then &#8230; else constructs. In practice this means, that once a SQL statement is working for a couple of cases, it is fairly limited.</li>
<li>SQL is standard. I worked a lot with Oracle, but when I have to use MySQL, MS SQL Server, DB2 or what ever I&#8217;ll be up to speed soon.</li>
<li>It is pretty powerful. You can do a lot of things with it. Way more than most people thing.</li>
<li>Well I think that&#8217;s about it.</li>
</ul>
<p>Because on all other accounts<a href="/2010/02/15/why-sql-sucks/"> I still hate the current state of SQL</a>. And although I am deeply skeptical about all that NoSQL hype lately, I do hope one thing to come out of it: New ideas for a successor of SQL, with a stronger type system, with a powerful scoping a modularization features and a clean design.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/04/11/why-i-like-sql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why SQL Sucks</title>
		<link>http://blog.schauderhaft.de/2010/02/15/why-sql-sucks/</link>
		<comments>http://blog.schauderhaft.de/2010/02/15/why-sql-sucks/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 22:13:42 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=414</guid>
		<description><![CDATA[About two weeks ago at work we discussed different strategies on testing databases. Although there are ways to do it, they are all more or less painful. And finally we all parted in agreeing: (SQL) databases suck, SQL sucks and testing databases and SQL sucks as well. We are only testing this crap because the [...]]]></description>
			<content:encoded><![CDATA[<p>About two weeks ago at work we discussed different strategies on testing databases. Although there are ways to do it, they are all more or less painful. And finally we all parted in agreeing: (SQL) databases suck, SQL sucks and testing databases and SQL sucks as well. We are only testing this crap because the only thing worse then database tests are untested databases. But complaining doesn&#8217;t help. I have heard you are supposed to ask 5 times &#8220;why?&#8221; when faced with a problem. Maybe it was 7 times? Don&#8217;t know lets start with asking it once:</p>
<p><strong>Why does SQL suck?</strong></p>
<p><strong>Hard to parse for computers</strong>: I don&#8217;t know much about parsing, but it seems to be hard for a computer to identify the bits and pieces of a SQL statement, when it is not really SQL but just almost SQL. This results in really crappy IDE support, because the editor doesn&#8217;t know what you are trying to write, before you finished it.</p>
<p><strong>Hard to parse for humans</strong>: One of the ideas behind the design of SQL was to make it look almost like a sentence. While this works for trivial statements, it completely falls a part when you have some serious statement at hand. For an extremely bad example check out the <a href="/2009/06/18/building-a-turing-engine-in-oracle-sql-using-the-model-clause/">turing engine implemented in Oracle SQL</a>. Side Note: aren&#8217;t some fluent APIs trying to do just that.</p>
<p><strong>Not consistent</strong>:Why does the having clause exist? Why nothing similar for group by? Who came up with the syntax of analytic functions. Why can I reference an expression defined the column list in the where clause, but not in the group by clause?</p>
<p><strong>(Almost) no modularization</strong>: You need this real complex where condition in another select? Copy it. You need to join the same bunch of table, just with a tiny change in one condition? Copy it. You want to use the same group by clause in a different select? Copy it. There are few little things you can do to break your SQL code, but it is nothing compared to the power of a modern language like &#8230; e.g. &#8230; <a href="http://en.wikipedia.org/wiki/Fortran_66#FORTRAN_66">Fortran 66</a>.</p>
<p><strong>No decent development environment appart from the target environment</strong> When you write a java application (and you do it the way it is supposed to be done), most of it can get compiled, and executed in a simple JVM. No web server needed, nor application server. Compare that to SQL. The only thing that can properly compile and execute a SQL statement is a database. And not just any database, but a database with all the accessed tables created and filled with data. This does not help when writing SQL code.</p>
<p>Ok, we have some reasons for SQL sucking so badly. What&#8217;s next. How about asking &#8220;Why?&#8221; again?</p>
<p><strong>Why are all these things true about SQL?</strong></p>
<p>That answer is easy: I have no idea. So let&#8217;s step right ahead and ask again &#8220;Why&#8221;</p>
<p><strong>Why does a curious developer with over 10 years of experience in database development know some reasons why SQL sucks, but can&#8217;t answer the simple question, why these reasons apply?</strong></p>
<p>I think it is because nobody really discusses SQL (at least not loud enough for me to hear). I know there is the No-SQL-Movement, but I happen to think that a relational database is a pretty decent solution to many things. There are also things like the Hibernate Criteria API and HQL. But those do not operate on database schemas, but on classes and objects. While this is fine when you wan&#8217;t to work with classes and objects, it sucks when you just want to process large tables. When processiong 100K database records pulling each out of the database, instanciate an object, manipulating it and writing it back is not an option.</p>
<p>Yet a way to write queries in a more modular, easy to parse and easy to test way would be highly appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/02/15/why-sql-sucks/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>More on JUnit Theories</title>
		<link>http://blog.schauderhaft.de/2010/02/07/junit-theories/</link>
		<comments>http://blog.schauderhaft.de/2010/02/07/junit-theories/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 13:02:21 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=404</guid>
		<description><![CDATA[In my last blog post, I described how to use JUnit Theories to create large amounts of test runs, with very limited amount of work, like so:

import static org.junit.Assume.assumeTrue;


@RunWith&#40;Theories.class&#41;


public class TheorieTest &#123;


&#160;


&#160;@DataPoint


&#160;public static String a = &#34;a&#34;;


&#160;


&#160;@DataPoint


&#160;public static String b = &#34;bb&#34;;


&#160;


&#160;@DataPoint


&#160;public static String c = &#34;ccc&#34;;


&#160;


&#160;@Theory


&#160;public void stringTest&#40;String x, String y&#41; &#123;


&#160; assumeTrue&#40;x.length&#40;&#41; &#38;gt; [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="/2010/01/31/new-feature-of-junit-theories/">blog post</a>, I described how to use JUnit Theories to create large amounts of test runs, with very limited amount of work, like so:</p>
<div class="geshi no java">
<div class="head">import static org.junit.Assume.assumeTrue;</div>
<ol>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> TheorieTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> a = <span class="st0">&quot;a&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> b = <span class="st0">&quot;bb&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> c = <span class="st0">&quot;ccc&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> stringTest<span class="br0">&#40;</span><span class="kw3">String</span> x, <span class="kw3">String</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assumeTrue<span class="br0">&#40;</span>x.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&amp;</span>gt<span class="sy0">;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>x + <span class="st0">&quot; &quot;</span> + y<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The trick is simple to provide data points for every parameter type of the test method. The JUnit Theories Runner will call the test method with every possible combination of datapoints. If you think a little about it you will soon realize some of the limitations of this approach:</p>
<ul>
<li>You&#8217;ll soon end up with lots of data point fields cluttering your code</li>
<li>Parameters of the same type will receive the same set of parameters, even when the usable range of inputs is completely different.</li>
</ul>
<p>Fortunately the developers of JUnit provided really nice solutions to these problems.</p>
<p>Instead of specifying single data points, you can provide a full array of datapoints using the <tt>@Datapoints</tt> annotation, like so (add imports for good measure):</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> TheorieTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoints</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span><span class="br0">&#91;</span><span class="br0">&#93;</span> a = <span class="br0">&#123;</span> <span class="st0">&quot;a&quot;</span>, <span class="st0">&quot;bb&quot;</span>, <span class="st0">&quot;ccc&quot;</span> <span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoints</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">Integer</span><span class="br0">&#91;</span><span class="br0">&#93;</span> j = <span class="br0">&#123;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> someTest<span class="br0">&#40;</span><span class="kw3">String</span> x, <span class="kw3">Integer</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assumeTrue<span class="br0">&#40;</span>x.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>x + <span class="st0">&quot; &quot;</span> + y<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This of course is much less verbose. Instead of an array you may provide a method returning an array, or at least it looks like this should be possible. But when I tried it JUnit seemed unable to handle the types correctly resulting in IllegalArgumentExceptions. Guess I&#8217;ll have to file a bug when finished with this article &#8230; </p>
<p>But we still need to take care of parameters which have the same type, but very different meaning and therefore different useful values. The clean OO way of doing things would be to get rid of the generic types like <tt>String</tt> and use stronger types like <tt>CreditCardNumber</tt> or <tt>Name</tt> instead. But then in a perfect world we wouldn&#8217;t need tests, because our programs wouldn&#8217;t contain any bugs to begin with. So lets try this instead (Again imports omitted):</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">@Retention<span class="br0">&#40;</span>RetentionPolicy.<span class="me1">RUNTIME</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">@ParametersSuppliedBy<span class="br0">&#40;</span>CreditCardSupplier.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> @<span class="kw2">interface</span> AllCreditCards <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">@Retention<span class="br0">&#40;</span>RetentionPolicy.<span class="me1">RUNTIME</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">@ParametersSuppliedBy<span class="br0">&#40;</span>NameSupplier.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> @<span class="kw2">interface</span> AllNames <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> CreditCardSupplier <span class="kw2">extends</span> ParameterSupplier <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> List<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span> getValueSources<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;ParameterSignature signature<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; ArrayList<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span> result = <span class="kw2">new</span> ArrayList<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Amex&quot;</span>, <span class="st0">&quot;Amex&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Master&quot;</span>, <span class="st0">&quot;Master&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Visa&quot;</span>, <span class="st0">&quot;Visa&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">return</span> result<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> NameSupplier <span class="kw2">extends</span> ParameterSupplier <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> List<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span> getValueSources<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;ParameterSignature signature<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; AllNames annotation = signature.<span class="me1">getAnnotation</span><span class="br0">&#40;</span>AllNames.<span class="kw2">class</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;just wanted to show that I can access it &quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; + annotation<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; ArrayList<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span> result = <span class="kw2">new</span> ArrayList<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Alf&quot;</span>, <span class="st0">&quot;Alf&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Willie&quot;</span>, <span class="st0">&quot;Willie&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Tanner&quot;</span>, <span class="st0">&quot;Tanner&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Cat&quot;</span>, <span class="st0">&quot;Cat&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">return</span> result<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> SuppliedByTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> imagineThisIsATest<span class="br0">&#40;</span>@AllCreditCards <span class="kw3">String</span> x, @AllNames <span class="kw3">String</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;consider &quot;</span> + x + <span class="st0">&quot; / &quot;</span> + y + <span class="st0">&quot; tested.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> testIntegers<span class="br0">&#40;</span>@TestedOn<span class="br0">&#40;</span>ints = <span class="br0">&#123;</span> <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span>, <span class="nu0">7</span>, <span class="nu0">13</span>, <span class="nu0">23</span>, <span class="nu0">42</span> <span class="br0">&#125;</span><span class="br0">&#41;</span> <span class="kw4">int</span> i<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Wow, thats a lot of code. Just look at the last piece and see what appears in the console when we run it:</p>
<pre>
just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()
consider Amex / Alf tested.
consider Amex / Willie tested.
consider Amex / Tanner tested.
consider Amex / Cat tested.
just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()
consider Master / Alf tested.
consider Master / Willie tested.
consider Master / Tanner tested.
consider Master / Cat tested.
just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()
consider Visa / Alf tested.
consider Visa / Willie tested.
consider Visa / Tanner tested.
consider Visa / Cat tested.
2
3
4
7
13
23
42
</pre>
<p>Have a look at the row beginning with: &#8220;consider&#8221;. Obviously the Theory <tt>imagineThisIsATest</tt> gets fed with the values from the <tt>CreditCardSupplier</tt> and <tt>NameSupplier</tt>. The parameters and the &#8216;Suppliers&#8217; are connected by the two annotations <tt>@AllNames</tt> and <tt>AllCreditCards</tt>. So whenever you have a parameter to a theory where the type alone is not sufficient for identifying the kind of values that should get used, you can simple create an annotation, which itself is annotated with a reference to a <tt>ParameterSupplier</tt> class and you are all set. You might think this is a lot of code for supplying a handful of parameters. You are right, but remember, that you can reuse your suppliers wherever you need names or credit card values in your tests. </p>
<p>Now let&#8217;s look at the first line of the output:<br />
<tt>just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()</tt><br />
It simply shows of that you get access to the annotation (and actually the signature of the compete test method. This can be very useful, when you want your supplier to behave differently for different theories. Have a look at the <tt>NameSupplier</tt> above to see how this works.</p>
<p>JUnit actually comes with an example where this is used, and I demonstrated it with the other theory in the demonstration code above. The <tt>@TestedOn</tt> annotation takes an array of values to be used as data points for the annotated parameter.</p>
<p>Thats it for today. I hope the power of theories became obvious, as well as the power you have as a developer to extend that mechanism. Again be warned: All this nice stuff is in a package named experimental for good reason. If you use it, you might find bugs, and thing will likely change at least in name in an upcoming version. Taking about versions, I am using junit4.8.1 for the examples.</p>
<p>For next week the conclusion of the little series about JUnit theories is planned, with a few thoughts on use and danger of this kind of testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/02/07/junit-theories/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>New Feature of JUnit: Theories</title>
		<link>http://blog.schauderhaft.de/2010/01/31/new-feature-of-junit-theories/</link>
		<comments>http://blog.schauderhaft.de/2010/01/31/new-feature-of-junit-theories/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 20:14:25 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=399</guid>
		<description><![CDATA[A couple of months ago I blogged about JUnit Rules, one of the new features in JUnit. While fooling around with JUnit Rules, I found a couple more features that you might be interested in. So here it comes: Theories! It turns out Theories are really a piece of cake. Try this:

import static org.junit.Assume.assumeTrue;


import org.junit.experimental.theories.DataPoint;


import [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago I blogged about <a href="/2009/10/04/junit-rules/">JUnit Rules</a>, one of the new features in JUnit. While fooling around with JUnit Rules, I found a couple more features that you might be interested in. So here it comes: Theories! It turns out Theories are really a piece of cake. Try this:</p>
<div class="geshi no java">
<div class="head">import static org.junit.Assume.assumeTrue;</div>
<ol>
<li class="li1">
<div class="de1"><span class="co2">import org.junit.experimental.theories.DataPoint;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import org.junit.experimental.theories.Theories;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import org.junit.experimental.theories.Theory;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import org.junit.runner.RunWith;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> TheorieTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> a = <span class="st0">&quot;a&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> b = <span class="st0">&quot;bb&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> c = <span class="st0">&quot;ccc&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> stringTest<span class="br0">&#40;</span><span class="kw3">String</span> x, <span class="kw3">String</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assumeTrue<span class="br0">&#40;</span>x.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&amp;</span>gt<span class="sy0">;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>x + <span class="st0">&quot; &quot;</span> + y<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>When you run this you&#8217;ll get this as an output (and one successfull test):</p>
<pre>bb a
bb bb
bb ccc
ccc a
ccc bb
ccc ccc</pre>
<p>So what is going on? The first thing to note is that this test is executed by a specialized runner of type Theories. This runner executes all the public methods annotated with @Theory. Differently from normal tests theories have parameters. In order to fill these parameters with values, the Theories runner uses all the public fields of matching Type and annotated with @DataPoint. When you take a look at the output, it should be obvious why this is more powerful then <a href="http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/">parameterized Tests</a>: Every combination of values is tried, thus with e.g. 4 parameters with 4 distinct values each, you end up with 256 test runs.</p>
<p>The idea is to specify a theory about the object under test, that holds for a large class of states and parameters.  Then you provide those as parameters to the test method which will test the theory.</p>
<p>A probably very common case is that a theory is known not to be valid for certain cases. You can exclude these from a test using the Assume class. If an assumption doesn&#8217;t hold, the test is silently ignored. This is used in the example above to prevent &#8216;a&#8217; to be used as a first parameter.</p>
<p>There are some more tweaks to Theories in JUnit, which I will cover in a later blog. Until then enjoy the new feature. But keep in mind the package in which theories reside up to now: <tt>org.junit.<strong>experimental</strong>.theories </tt> so I&#8217;d expect some changes in the API and at least a change in package name.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/01/31/new-feature-of-junit-theories/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
