<?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>Pramatr Blog &#187; Java</title>
	<atom:link href="http://www.pramatr.com/blog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pramatr.com/blog</link>
	<description>A collection of articles from pramatr.com on technology, security, software and anything we find interesting</description>
	<lastBuildDate>Mon, 29 Mar 2010 19:48:11 +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>Fun with Grails</title>
		<link>http://www.pramatr.com/blog/2009/07/19/fun-with-grails/</link>
		<comments>http://www.pramatr.com/blog/2009/07/19/fun-with-grails/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 16:20:04 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://pramatr.com/?p=905</guid>
		<description><![CDATA[Anyone that has been following @pramatrdev on Twitter will have seen the numerous tweets recently about Grails. I first started looking at Grails back in 2006? after a recommendation from a work collegue. It looked quite interesting and I managed to hack together something quite simple but it was a little hard work in places [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone that has been following <a href="http://twitter.com/pramatrdev">@pramatrdev</a> on Twitter will have seen the numerous <a href="http://twitter.com/#search?q=%23grails">tweets</a> recently about <a href="http://www.grails.org/">Grails</a>. I first started looking at <a href="http://www.grails.org/">Grails</a> back in 2006? after a recommendation from a work collegue. It looked quite interesting and I managed to hack together something quite simple but it was a little hard work in places to get what I wanted out of it. It was early days for the project however and it looked very promising.</p>
<p>I&#8217;ve since recommended it several times to other people who were interested in building applications new applications. Most of the this feedback has been extremely positive so I&#8217;ve managed to get around to having another look at it recently. After <a href="http://twitter.com/snaglepus">@snaglepus</a> suggested I check out <a href="http://www.springsource.com/webinar/building-twitter-with-grails-40-minutes">Build twitter in Grails in 40 minutes</a> (registration is required) I have to say I was very impressed. Since watching that I&#8217;ve gone from knowing absolutely nothing about Grails (three years it too long to keep information in memory) to building a sample application very quickly. After finding plugins for many of the tasks I wanted, I&#8217;ve implemented the logic I need and let the tool do the rest of the heavy lifting work which led to a very productive evening of coding.</p>
<p>I really love it when you find technology which just seems to work and does exactly what you expect it to do. There is still much I want to learn about Grails and I&#8217;m sure there are many other tips and tricks I can pick up along the way. At the minute I&#8217;m just looking through the many Grails books to find a winner and I think I have in <a href="http://www.apress.com/book/view/9781590599952">The Definitive Guide to Grails, Second Edition</a>. If you haven&#8217;t looked at Grails before, I really recommend you check it out and see what it can do for you. And if you aren&#8217;t following me on Twitter, I&#8217;m <a href="http://twitter.com/pramatrdev">@pramatrdev</a> and I&#8217;m sure there will be a few more tweets about Grails before the month is over <img src='http://69.89.31.94/~pramatrc/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2009/07/19/fun-with-grails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Recipes: A Problem-Solution Approach</title>
		<link>http://www.pramatr.com/blog/2009/02/17/spring-recipes-a-problem-solution-approach/</link>
		<comments>http://www.pramatr.com/blog/2009/02/17/spring-recipes-a-problem-solution-approach/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 14:04:13 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Resources]]></category>

		<guid isPermaLink="false">http://pramatr.com/?p=421</guid>
		<description><![CDATA[I recently had the pleasure of reading Spring Recipes: A Problem-Solution Approach. I first began reading the sample chapters released on the authors website, so when the book was finally finished I was really looking forward to getting hold of a copy. When it arrived I was amazed to see that it weighed in at [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.apress.com/book/view/1590599799"><img class="alignleft size-medium wp-image-535" title="springrecipes" src="http://pramatr.files.wordpress.com/2009/02/springrecipes.jpg?w=228" alt="springrecipes" width="228" height="300" /></a>I recently had the pleasure of reading <a href="http://apress.com/book/view/1590599799">Spring Recipes: A Problem-Solution Approach</a>. I first began reading the sample chapters released on the authors website, so when the book was finally finished I was really looking forward to getting hold of a copy. When it arrived I was amazed to see that it weighed in at a hefty 752 pages, but that really shouldn&#8217;t put you off.</p>
<p>Spring Recipes covers Spring 2.5 (and earlier versions) taking it from the simplest concepts right through to the most advanced level. The book is divided into nineteen chapters organised into three parts; Core, Fundamentals and Advanced. Each part builds on the previous ones to grow your understanding of all aspects of the Spring framework, for this reason the book is best read cover to cover.</p>
<p>This book is in no way filler and is packed full of content covering not only the <a href="http://www.springsource.org/about">Spring framework</a> and but also several common Spring <a href="http://www.springsource.org/projects">projects</a> including; <a href="http://static.springframework.org/spring-security/site/index.html">Spring Security</a>, <a href="http://www.springsource.org/webflow">Spring Web Flow</a> and <a href="http://static.springframework.org/spring-ws/sites/1.5/">Spring Web Services</a>. For anyone that knows Spring (and it&#8217;s associated projects), this is a huge amount of information to cover in just one book, but this book really does deliver!</p>
<p>As the author puts it; <em>“Reading Spring Recipes will help you master the most important 80 percent of Spring&#8217;s features with just 20 percent of the effort you would need to learn them all.”</em> If you are already using Spring, this book will serve as an invaluable reference and companion guide for future development. If you are a newcomer to Spring, this book will serve as an excellent launch pad to get you up, running and productive with Spring very quickly.</p>
<h2><strong>Structure</strong></h2>
<p>Each of the recipes covered in the book follows; a problem, solution and then how it works approach. This approach lays out many of the common FAQ style questions that come up on <a href="http://forum.springframework.org/">Spring Forum</a> in an accessible way; clearly outlining the problem and mapping the path to the solution. This book also acts an excellent companion to the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/">Spring Reference manual</a>, which is sometimes difficult to read in places and lacks the array of examples this book provides.</p>
<h2><strong>Audience</strong></h2>
<p>The intended audience for Spring Recipes, is any Java programmer hoping to learn more about Spring. No prior knowledge of Spring is required as this book covers it all right from core concepts such as inversion of control (or Ioc). This book is accessible to anyone who has heard the hype about Spring and simply wants to learn more about it and how they can leverage it&#8217;s power. This book gives Java developers hands-on experience using the Spring framework with real-world examples, this experience can be transferred straight into your Spring project.</p>
<h2><strong>Chapters</strong></h2>
<h3><strong>Part 1: Core</strong></h3>
<p>This part focuses on the core concepts and mechanisms of the Spring framework. The chapters in this part aim to familiarize you with Spring&#8217;s core. As a newcomer to Spring <strong>Chapter 1</strong> introduces the concept of inversion of control the core foundation of Spring. It explains how and IoC container works, but also why it is useful and beneficial to use one. This chapter serves as an excellent introduction to IoC and is invaluable to anyone who isn&#8217;t quite sure why they should use a container based approach. This chapter is a <strong>must</strong> for anyone new to Spring or anyone trying to deliver it to a new audience.</p>
<p><strong>Chapters 2-4</strong> build on the IoC introduction, explaining how to setup and configure a new Spring project and apply the basics of Spring configuration. These first four chapters are the foundation for the rest of the book and are a must read for anyone who has never used Spring before and even advanced users may find useful hints and tips within. <strong>Chapters 5-6</strong> cover the extremely powerful approach of <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">AOP</a>, introducing <a href="http://en.wikipedia.org/wiki/Cross-cutting_concern">crosscutting concerns</a> and how these can be configured with Spring. This technique can be extremely useful to provide trace logging, caching and other crosscutting concerns such as security. This is a <strong>must</strong> read for anyone using Spring, even if you are not directly leveraging <a href="http://static.springframework.org/spring/docs/2.5.x/reference/aop.html">Spring AOP</a> or <a href="http://www.eclipse.org/aspectj/">AspectJ</a>. Spring AOP lays the foundation for introducing how Spring provides features such as transparent transaction support and method level authorization checking.</p>
<h3><strong>Part 2 : Fundamentals</strong></h3>
<p>This part has a split focus, it introduces Spring&#8217;s Data Access support, Web integration and also Testing support. <strong>Chapter 7</strong> introduces one of the most important aspects of Spring&#8217;s <a href="http://static.springframework.org/spring/docs/2.5.x/reference/spring-middle-tier.html">data access support</a> in the form of <a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/jdbc/core/JdbcTemplate.html">JdbcTemplate</a>. This shows the reader how they can use JdbcTemplate to prevent the use of direct JDBC and instead let Spring handle all of the boiler plate code. As JdbcTemplate removes the need for the common try/catch/finally/try/catch code seen with JDBC, the code is much cleaner and much more readable. Along with this support, the Spring <a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/dao/DataAccessException.html">DataAccessException</a> hierarchy is introduced which provides a rich set of common exceptions to provide a more granular handling approach.</p>
<p><strong>Chapter 8</strong> introduces Spring&#8217;s excellent <a href="http://static.springframework.org/spring/docs/2.5.x/reference/transaction.html">transaction management</a> support and explains in great detail all of the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/transaction.html#transaction-declarative-txadvice-settings">various</a> propagation behaviours and isolation levels. This chapter is one of my favourites as it not only introduces the concept of Spring transaction support but transactions in general. It&#8217;s accessible to everyone and extremely useful when mentoring new developers.</p>
<p><strong>Chapter 9</strong> discusses how to integrate Spring with existing ORM frameworks, making their usage within the application seamless. This provides multiple approaches to integration allowing you to build on existing Spring classes, or completely decouple your classes from Spring removing the need for any Spring imports within your code. <strong>Chapters 10-11</strong> discuss Spring&#8217;s popular MVC framework and also discuss how many other leading web frameworks can be integrated with a Spring application. As many of the projects I have worked on in the past have used <a href="http://struts.apache.org/">Struts</a>, Chapter 11 was particularly useful to outline all of the options available to use Spring and Struts together.</p>
<p>This part finishes with the introduction of <a href="http://static.springframework.org/spring/docs/2.5.x/reference/testing.html">Spring Testing Support</a>, teaching you not only what features Spring has to offer but also how it integrates with popular testing frameworks such as <a href="http://www.junit.org/">JUnit</a> and <a href="http://testng.org">TestNG</a>. As the Spring Testing support has been overhauled, it was great that this book covered the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/testing.html#testcontext-framework">TestContext</a>framework, but also legacy test classes to cover older versions of JUnit and TestNG. This chapter helps you leverage not only IoC for your tests but also transaction support, mock testing and also MVC integration testing. This approach enables you to easily decouple your code from your tests, providing first class support for all your testing needs.</p>
<h3><strong>Part 3 : Advanced</strong></h3>
<p>For anyone that has mastered the first twelve chapters, <strong>Chapter 13</strong> is where things start to get really interesting. This chapter is dedicated to one of the most scary Spring projects, namely <a href="http://static.springsource.org/spring-security/site/index.html">Spring Security</a> (formerly known as Acegi). Spring Security provides a full range of authentication, authorization and access control options which allow it to secure a standalone Java application or a web-based application. This book focuses on version 2.x onwards which <a href="http://blog.springsource.com/2008/04/17/spring-security-20-final-release-no-more-dead-fairies/">dramatically</a> reduced the size of the required configuration and also its complexity. Spring Security can be a difficult subject to tackle as it&#8217;s often hard for developers to understand it&#8217;s concepts, but this chapter builds security into the example application in piecemeal steps. This demystifies many of the problematic areas of Spring Security whilst addressing the common problems a new developer might face.</p>
<p><strong>Chapter 14</strong> covers the portlet application development using the Spring Portlet MVC framework, highlighting the portlet-specific features which differ from the standard Spring MVC framework. If you don&#8217;t require the portlet features currently within your application, this chapter can safely be skipped as the next one is another gem. <strong>Chapter 15</strong> covers the <a href="http://www.springsource.org/webflow">Spring Web Flow</a> framework which addresses the three major pain–points facing web application developers: user interface navigation control, state management, and modularity. Spring Web Flow offers a <a href="http://static.springframework.org/spring-webflow/docs/2.0.x/reference/htmlsingle/spring-webflow-reference.html#flow-overview">flow</a> definition language which encapsulates a reusable sequence of steps that can execute in different contexts. This framework is extremely flexible and powerful, but this chapter provides a great introduction into what is a very large subject.</p>
<p><strong>Chapter 16</strong> introduces the remoting integration that Spring provides for various technologies such as RMI, Hessian, Burlap, HTTP Invoker, and Web Services. Having already been familiar with many of these technologies, this chapter provides an excellent insight into how these can be integrated with Spring and how quickly this can be achieved. This chapter concludes with a look at Spring Web Services including how to develop contract-first web services. <strong>Chapter 17</strong> discusses how to develop EJB 2.x and 3.0 components with Spring&#8217;s EJB support, and how to use Spring&#8217;s JMS support to simplify sending, receiving, and listening to JMS messages. This introduces a powerful concept of <a href="http://blog.springsource.com/2006/08/11/message-driven-pojos/">message-driven POJOs</a>for those who like the concept of message-driven beans (MDBs) but don&#8217;t want the restrictions of an EJB container.</p>
<p><strong>Chapter 18</strong> demonstrates how easily Spring beans can be exported as JMX MBeans with relative ease in comparison to the typical approach. It continues to extend this by providing remote access, auto-detection and notification. This chapter continues by introducing Spring&#8217;s email support which removes the reliance on <a href="http://java.sun.com/products/javamail/">JavaMail</a> specific mail sessions and exceptions. This provides an abstraction which is independent making it easy to switch to another e-mail API. This chapter finishes with a look at the options available for scheduling tasks within the Spring framework, this integrates with the JDK Timer and also <a href="http://www.opensymphony.com/quartz/">Quartz</a> scheduler.</p>
<p><strong>Chapter 19</strong>, this part and the book concludes with a look at the various scripting languages that can used with Spring applications including; JRuby, Groovy and BeanShell. One of the most powerful features of using scripts is that any changes made to these can be detected by Spring and thus the beans can be <a href="http://static.springframework.org/spring/docs/2.5.x/reference/dynamic-language.html#dynamic-language-refreshable-beans">refreshed</a> automatically. This is of great benefit when you wish to avoid the long compile, deploy and test cycles or the application cannot be taken down regularly for maintenance. This chapter demonstrates how scripted beans can be added to your application quickly and simply using Spring.</p>
<h2><strong>Summary</strong></h2>
<p>This book has undertaken a mammoth task, trying to squeeze Spring and associated projects all into one book. This book is very clearly written, with well-structured, concise examples that get to the heart of the problem without complicating it with unnecessary detail. The examples provide solutions to real world problems instead of taking the form of the simplistic examples found in many other technology books. My only real minor criticism of this book is it&#8217;s scope and size. Given the huge undertaking, there are obviously going to be some areas which only provide a minimal amount of information. This should be expected given the vast array of information covered, but there are a range of <a href="http://www.apress.com">Apress</a> <a href="http://www.apress.com/book/view/1430216247">books</a> which can build on this foundation. I would still give this book five out of five and rate it as one of the best framework specific books I&#8217;ve read.</p>
<p>This book should accompany anyone starting their journey with the Spring framework and even seasoned developers. It can take you from knowing nothing about the Spring framework to knowing pretty much everything you need to create a fully functional web application with all the bells and whistles you&#8217;d expect. It provides information and examples on numerous integration points with other popular open source frameworks and languages allowing you to truly become a Spring expert. Read it, read it again and keep it on your bookshelf as a solid reference for the Spring framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2009/02/17/spring-recipes-a-problem-solution-approach/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Documentation: Update It Or Lose It</title>
		<link>http://www.pramatr.com/blog/2009/02/17/documentation-update-it-or-lose-it/</link>
		<comments>http://www.pramatr.com/blog/2009/02/17/documentation-update-it-or-lose-it/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 06:43:31 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Communication]]></category>
		<category><![CDATA[Discipline]]></category>
		<category><![CDATA[Improvement]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Smells]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Structure]]></category>

		<guid isPermaLink="false">http://pramatr.com/?p=17</guid>
		<description><![CDATA[Over time I&#8217;m frequently restructuring and refactoring the code I work on to improve the design and simplify many of the balls of mud I find along the way. If I break some of that code my unit tests shout at me and if they don&#8217;t my continuous integration environment shouts at me when I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>Over time I&#8217;m frequently <a href="http://pramatr.com/2008/09/26/yes-you-are-making-changes-but-that-doesnt-make-it-refactoring/">restructuring and refactoring</a> the code I work on to improve the design and simplify many of the <a href="http://www.laputan.org/mud/mud.html#BigBallOfMud">balls of mud</a> I find along the way. If I break some of that code my unit tests shout at me and if they don&#8217;t my <a>continuous integration</a> environment shouts at me when I&#8217;ve <a>broken</a> one of the integration tests. One area of the code that doesn&#8217;t receive the same attention however is the documentation.</p>
<p>I love a well documented API as much as the next developer. I also am fighting a constant battle to improve my JavaDoc <a href="http://java.sun.com/j2se/javadoc/writingdoccomments/">skills</a>, trying to write to the same quality that I see in many other projects. But is excellent documentation really enough? It can&#8217;t just be excellent when it&#8217;s written it needs to be constantly maintained to this same level over time.</p>
<p>It&#8217;s a pretty common occurrence that I can look at a section of code and find documentation that has nothing to do with the method it belongs to. The <a href="http://www.c2.com/cgi/wiki/wiki?IntentionRevealingNames">intention revealing name</a> sounds nothing like the documentation which describes what it&#8217;s supposed to be doing. Somewhere in the mists of time, the intention of the method and the documentation parted company; it is documentation no more it is ex-documentation. The problem here however is that there&#8217;s nothing to catch this issue. If an extra argument is added, Eclipse might display a warning telling me it&#8217;s missing from the documentation, but the actual context of the documentation can be completely wrong and I am none the wiser.</p>
<p>Although this incorrect documentation may seem fairly harmless to some, the reason it was supposed to be there in the first place is to the give the user of the API information on the method contract and the expected outcomes. If this information is completely wrong however it introduces confusion and also potential misuse of the API. After much wasted time and several grey hairs later I recently filed a couple of JIRA reports about popular open source frameworks which did exactly this. After my code just didn&#8217;t work and the unit tests failed, it took me a number of minutes to realise that the JavaDoc description of expected events just didn&#8217;t reflect what was actually going on in the code.</p>
<p>One approach to solving this problem is to include the documentation in the <a href="http://pramatr.com/2009/01/27/frequent-code-reviews-the-key-to-success/">frequent code reviews</a>. Code that doesn&#8217;t have documentation or has incorrect documentation can&#8217;t be considered complete, thus doesn&#8217;t pass the review. Developers need to make sure however, that they treat the documentation with the same respect that they give to their code and their tests. When dealing with documentation I&#8217;d much sooner see a method with no documentation than something that is completely wrong. I do want a good level of documentation in the code I work on but there is a very simple rule; update it or lose it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2009/02/17/documentation-update-it-or-lose-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Embedded Database Performance: Handle With Care</title>
		<link>http://www.pramatr.com/blog/2009/02/10/embedded-database-performance-handle-with-car/</link>
		<comments>http://www.pramatr.com/blog/2009/02/10/embedded-database-performance-handle-with-car/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 06:00:38 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Connection Pool]]></category>
		<category><![CDATA[Improvement]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Jdbc]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Smells]]></category>

		<guid isPermaLink="false">http://pramatr.com/?p=11</guid>
		<description><![CDATA[Embedded databases are proving pretty popular with many teams finding their usage extremely appealing. Many embedded databases have impressive features; easy to integrate into the software, low administration requirements, very low footprint and generally extremely fast. It is for this last reason that some people look to these databases, but it is also for this reason [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Embedded_database">Embedded databases</a> are proving pretty popular with many teams finding their usage extremely appealing. Many embedded databases have impressive features; easy to integrate into the software, low administration requirements, very low footprint and generally extremely fast. It is for this last reason that some people look to these databases, but it is also for this reason that they need to be handled with care.</p>
<p>Just <a href="http://hsqldb.org/web/openoffice.html">like</a> OpenOffice, a previous project I worked on used an embedded version of <a href="http://hsqldb.org/">HSQLDB</a>. We had started to have several performance problems so engaged in some profiling to find potential areas for improvement. After staring at several screens full of SQL debug, one thing became apparent; our system was producing a huge number of SQL statements. This was accompanied by the realisation that it was also producing a huge number of exactly the same SQL statements.</p>
<p>As the embedded database was extremely fast, there had never been any performance problems before. In fact the database was not the bottleneck in many of the application operations. As the application grew more complex however, performance problems began to arise quite quickly. These problems only got worse when the numbers started to increase. Complexity + Scaling != Success. The extemely fast embedded database was now starting to be a real problem, some serious changes needed to be made.</p>
<p>Many of the database performance problems were resolved quite quickly by simply re-learning the most basic best practices, just because the embedded database was quick these should not have been ignored;</p>
<ul>
<li>Connection pooling can improve performance</li>
<li>Statement caching can improve performance</li>
<li>Batching statements can improve performance</li>
<li>Performing <a href="http://pramatr.com/2009/02/05/sql-n-1-selects-explained/">SQL n + 1 selects</a> are a bad idea regardless of how quick the database is</li>
<li>Caching common SQL results can improve performance</li>
<li>The database needs to be profiled to see exactly what SQL is getting executed against it</li>
</ul>
<p>It may be possible to ignore these issues early in the project, but as the complexity increases and the application is expected to scale they can begin to be exposed. Embedded databases performance can be impressive, but make sure you handle with care.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2009/02/10/embedded-database-performance-handle-with-car/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit Test Assertions: What Could Possibly Go Wrong?</title>
		<link>http://www.pramatr.com/blog/2008/11/06/unit-test-assertions-what-could-possibly-go-wrong/</link>
		<comments>http://www.pramatr.com/blog/2008/11/06/unit-test-assertions-what-could-possibly-go-wrong/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 21:10:00 +0000</pubDate>
		<dc:creator>pramatr</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Assertion]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[PMD]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Smells]]></category>

		<guid isPermaLink="false">http://pramatr.com/2007/11/06/unit-test-assertions-what-could-possibly-go-wrong/</guid>
		<description><![CDATA[Lots of my development effort is taken up writing unit tests. These tests form a nice warm security blanket for the code that I&#8217;ve developed. If the code is broken, typically these tests are the first feedback that something is wrong. Although some people fear the little red bar, it&#8217;s become a common part of [...]]]></description>
			<content:encoded><![CDATA[<p>Lots of my development effort is taken up writing <a href="http://en.wikipedia.org/wiki/Unit_testing">unit tests</a>. These tests form a nice warm security blanket for the code that I&#8217;ve developed. If the code is broken, typically these tests are the first feedback that something is wrong. Although some people fear the little red bar, it&#8217;s become a common part of development. So with this in mind, it&#8217;s vitally important that when a test fails, is crystal clear what <em>really</em> went wrong.</p>
<p>Over the past few weeks, I&#8217;ve been reading <a href="http://xunitpatterns.com/index.html">xUnit Test Patterns</a> (review to follow in another post), which covers making tests easier to write, understand and maintain. One important issue it discusses, is the use of <a href="http://xunitpatterns.com/Assertion%20Method.html">assertion methods</a>.</p>
<p>Assertions are a fundamental part of writing unit tests, and are typically very simple to use. Within each test, the expected outcome is expressed using assertions, each of which is boolean expression. The assertion should evaluate to true, so if the evaluation of the assertion instead returns false, the test will be failed. <a href="http://www.junit.org/">JUnit</a> provides the <a href="http://junit.sourceforge.net/javadoc/junit/framework/Assert.html">Assert</a> class to handle various types of common assertion. Although <a href="http://junit.sourceforge.net/javadoc/junit/framework/Assert.html">Assert</a> provides a number of very simple methods, I&#8217;ve seen these frequently used inappropriately, causing unit test failures to be unnecessarily difficult to track down.<br />
<span id="more-90"></span></p>
<p><strong>Assertion Selection</strong></p>
<p>When reviewing <a href="http://en.wikipedia.org/wiki/Unit_testing">unit tests</a>, one of the typical activities is to ensure that assertions are being used appropriately. When developers first start to write unit tests, assertions are often used inappropriately and as such reviews can become very time consuming. <a href="http://pramatr.com/2008/08/17/structured-approaches-to-improving-code-quality/">Tools</a> play a major part in development and can automate many of these kind of manual review activities. <a href="http://pmd.sourceforge.net/rules/junit.html">PMD</a> is one such tool, and it provides a rule set to deal with the common <a href="http://www.junit.org/">JUnit</a> problems.</p>
<p>One of the most common selection problems is the use of the <a href="http://xunitpatterns.com/Assertion%20Method.html#Stated%20Outcome%20Assertion">stated outcome assertion</a> instead of the <a href="http://xunitpatterns.com/Assertion%20Method.html#Equality%20Assertion">equality assertion</a>. When the <a href="http://xunitpatterns.com/Assertion%20Method.html#Equality%20Assertion">equality assertion</a> evaluates to false, it includes information about the two objects that should have been equal in the failure message. This information allows a developer to quickly get an idea what went wrong in the test. If the developer had used a <a href="http://xunitpatterns.com/Assertion%20Method.html#Stated%20Outcome%20Assertion">stated outcome</a>, (such as <a href="http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertTrue(boolean)">assertTrue</a>), no information would have been recorded about the object state. This typically leads to extra log statements being added, or the developer has to being debugging the test.</p>
<p>Ensuring that the appropriate assertion is used within each unit test, is a <strong>very</strong> simple thing to do, but it gives very clear feedback about failures and allows developers to quickly track their cause.</p>
<blockquote><pre><span style="font-size:small;"><span style="color:#444444;">// assertTrue("code worked", true);</span>
<span style="color:#444444;">// this should be removed as it serves no purpose</span>

<span style="color:#444444;">// assertTrue("code didn't work", false);</span>
<span style="color:#444444;">// Fails a test with the given message.</span>
<span style="color:#2040a0;">fail</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"code didn't work"</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>

<span style="color:#444444;">// assertTrue("actual is not empty", !actual.isEmpty());</span>
<span style="color:#444444;">// Asserts that a condition is false.</span>
<span style="color:#2040a0;">assertFalse</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"actual is not empty"</span>, <span style="color:#2040a0;">actual</span>.<span style="color:#2040a0;">isEmpty</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>

<span style="color:#444444;">// assertTrue("object is null", actual == null);</span>
<span style="color:#444444;">// Asserts that an object is null.</span>
<span style="color:#2040a0;">assertNull</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"object is null"</span>, <span style="color:#2040a0;">actual</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>

<span style="color:#444444;">// assertTrue("object is not null", actual != null);</span>
<span style="color:#444444;">// Asserts that an object isn't null.</span>
<span style="color:#2040a0;">assertNotNull</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"object is not null"</span>, <span style="color:#2040a0;">actual</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>

<span style="color:#444444;">// assertTrue("objects are the same", expected == actual);</span>
<span style="color:#444444;">// Asserts that two objects refer to the same object.</span>
<span style="color:#2040a0;">assertSame</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"objects are the same"</span>, <span style="color:#2040a0;">expected</span>, <span style="color:#2040a0;">actual</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>

<span style="color:#444444;">// assertTrue("objects are not the same", expected != actual);</span>
<span style="color:#444444;">// Asserts that two objects do not refer to the same object.</span>
<span style="color:#2040a0;">assertNotSame</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"objects are not the same"</span>, <span style="color:#2040a0;">expected</span>, <span style="color:#2040a0;">actual</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span>

<span style="color:#444444;">// assertTrue("objects are equal", expected.equals(actual));</span>
<span style="color:#444444;">// Asserts that two objects are equal.</span>
<span style="color:#2040a0;">assertEquals</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"objects are equal"</span>, <span style="color:#2040a0;">expected</span>, <span style="color:#2040a0;">actual</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span></span></pre>
</blockquote>
<p><strong>Assertion Configuration</strong></p>
<p>The <a href="http://xunitpatterns.com/Assertion%20Method.html#Equality%20Assertion">equality assertion</a> method provides a very simple contract, requiring the expected and the actual value. Although the contract is simple, one of the most common problems I have encountered, is that the arguments passed in to the method are in the wrong order (e.g. the expected value argument is actually the actual value and vice versa).</p>
<p>Although this is a trivial issue to correct, I have seen developers waste large amounts of time trying to work out what&#8217;s wrong with the code when a unit test fails. The developer immediately start to scratch their head as the values don&#8217;t seem to make sense. Without reviewing the unit test, they dive into the code to solve the problem.</p>
<p>I have seen this happen countless times, and every time it happens, the developer in question can&#8217;t believe they didn&#8217;t have a look at the unit test first. The <a href="http://xunitpatterns.com/Assertion%20Method.html#Equality%20Assertion">equality assertion</a> isn&#8217;t difficult to use, you just have to remember; <strong>expected</strong>, <em>actual</em>&#8230;. <strong>expected</strong>, <em>actual</em>&#8230;. <strong>expected</strong>, <em>actual</em>&#8230;. <strong>expected</strong>, <em>actual</em>&#8230;.</p>
<blockquote><pre><span style="font-size:small;"><span style="color:#444444;">// assertEquals("objects are equal", actual, expected);</span>
<span style="color:#444444;">// Asserts that two objects are equal.</span>
<span style="color:#2040a0;">assertEquals</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#008000;">"objects are equal"</span>, <span style="color:#2040a0;">expected</span>, <span style="color:#2040a0;">actual</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span></span></pre>
</blockquote>
<p><strong>Assertion Messages</strong></p>
<p><a href="http://xunitpatterns.com/index.html">xUnit Test Patterns</a> provides an great discussion about the assertion message pattern. Most developers I&#8217;ve spoken to, do add assertion messages to their tests, and I also personally find them very useful. I completely agree with <a href="http://xunitpatterns.com/Assertion%20Message.html">Meszaros</a> when he says;</p>
<blockquote><p><em>A well-crafted Assertion Message makes it very easy to determine which assertion failed and exactly what the symptoms were when it happened.</em></p></blockquote>
<p>The assertion message should spell out exactly what the author of the test was expecting to happen. When the test fails, the message should provide enough detail for the developer to understand exactly what the problem was. The time taken to write a well-crafted assertion message, is more than paid back when the assertion fails!</p>
<blockquote><pre><span style="font-size:small;"><span style="color:#444444;">// assertTrue(actual.startsWith(expected));</span>
<span style="color:#444444;">// Asserts that a condition is true.</span>
<span style="color:#2040a0;">assertTrue</span><span style="color:#4444ff;"><strong>(</strong></span> <span style="color:#008000;">"Actual should start with expected but actual was '"</span>
<span style="color:#4444ff;">+</span> <span style="color:#2040a0;">actual</span> <span style="color:#4444ff;">+</span> <span style="color:#008000;">"' and expected was '"</span> <span style="color:#4444ff;">+</span> <span style="color:#2040a0;">expected</span> <span style="color:#4444ff;">+</span> <span style="color:#008000;">"'"</span>, <span style="color:#2040a0;">actual</span>.<span style="color:#2040a0;">startsWith</span><span style="color:#4444ff;"><strong>(</strong></span><span style="color:#2040a0;">expected</span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;"><strong>)</strong></span><span style="color:#4444ff;">;</span></span></pre>
</blockquote>
<p><strong>Conclusion</strong></p>
<p>If you are writing unit tests, spend a couple of minutes reading over the reference manual to see what assertion methods you have to work with. <a href="http://xunitpatterns.com/index.html">xUnit Test Patterns</a> breaks down the types of assertion into; <a href="http://xunitpatterns.com/Assertion%20Method.html#Single%20Outcome%20Assertion">Single Outcome</a>, <a href="http://xunitpatterns.com/Assertion%20Method.html#Stated%20Outcome%20Assertion">Stated Outcome</a>, <a href="http://xunitpatterns.com/Assertion%20Method.html#Expected%20Exception%20Assertion">Expected Exception</a>, <a href="http://xunitpatterns.com/Assertion%20Method.html#Equality%20Assertion">Equality</a>, <a href="http://xunitpatterns.com/Assertion%20Method.html#Fuzzy%20Equality%20Assertion">Fuzzy Equality</a>. Understand what each assertion does and more importantly, understand when and where to use assertion. Test failures become much easier to track down, when it&#8217;s clear what went wrong. Use the right assertion, use the assertion correctly and give it a meaningful message. It really is a simple as that, what could possibly go wrong?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pramatr.com/blog/2008/11/06/unit-test-assertions-what-could-possibly-go-wrong/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
