<?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>Source Allies Blog &#187; Java</title>
	<atom:link href="http://blogs.sourceallies.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:40:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hibernate Date vs Timestamp</title>
		<link>http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/</link>
		<comments>http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 20:24:21 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[UserType]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2559</guid>
		<description><![CDATA[I encountered a subtle hibernate mapping issue involving Dates and Timestamps.  The following test recreates this issue.


package com.sourceallies.logging;
&#160;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
&#160;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
&#160;
import javax.annotation.Resource;
&#160;
import org.apache.commons.lang.time.DateUtils;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
&#160;
import com.sourceallies.Person;
&#160;
@RunWith&#40;SpringJUnit4ClassRunner.class&#41;
@ContextConfiguration
@TransactionConfiguration&#40;defaultRollback=false&#41;
public class HibernateDateTimeTest &#123;
&#160;
	@Resource
	private SessionFactory sessionFactory;
&#160;
	private Session currentSession;
&#160;
	private Date birthDateWithTime;
	private Date birthDateWithoutTime;
	private Date createDate;
	private [...]]]></description>
			<content:encoded><![CDATA[<p>I encountered a subtle hibernate mapping issue involving <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">Dates</a> and <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">Timestamps</a>.  The following test recreates this issue.<br />
<span id="more-2559"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.logging</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertFalse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertTrue</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Timestamp</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Calendar</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.Resource</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang.time.DateUtils</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.Criteria</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.SessionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.classic.Session</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.ContextConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.junit4.SpringJUnit4ClassRunner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.transaction.TransactionConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.transaction.annotation.Transactional</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.Person</span><span style="color: #339933;">;</span>
&nbsp;
@RunWith<span style="color: #009900;">&#40;</span>SpringJUnit4ClassRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@ContextConfiguration
@TransactionConfiguration<span style="color: #009900;">&#40;</span>defaultRollback<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateDateTimeTest <span style="color: #009900;">&#123;</span>
&nbsp;
	@Resource
	<span style="color: #000000; font-weight: bold;">private</span> SessionFactory sessionFactory<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Session currentSession<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDateWithTime<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDateWithoutTime<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> createDate<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifyDate<span style="color: #339933;">;</span>
&nbsp;
	@Before
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
		currentSession <span style="color: #339933;">=</span> sessionFactory.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		birthDateWithTime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		birthDateWithoutTime <span style="color: #339933;">=</span> DateUtils.<span style="color: #006633;">truncate</span><span style="color: #009900;">&#40;</span>birthDateWithTime, <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DATE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createDate <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modifyDate <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSave<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst&quot;</span>, <span style="color: #0000ff;">&quot;testLast&quot;</span>, 
                      birthDateWithTime, createDate, modifyDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		saveOrUpdate<span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testFind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Person foundPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithoutTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>createDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>modifyDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> saveOrUpdate<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		currentSession.<span style="color: #006633;">saveOrUpdate</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Criteria criteria <span style="color: #339933;">=</span> currentSession.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> criteria.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> people<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is the mapping for person.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">...
@<span style="color: #003399;">Entity</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
&nbsp;
	@Id
	@GeneratedValue<span style="color: #009900;">&#40;</span>strategy<span style="color: #339933;">=</span>GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>1L<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;timestamp&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> createDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifyDate<span style="color: #339933;">;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There are three <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">Date</a> fields: &#8216;birthdate&#8217;, &#8216;createDate&#8217;, and &#8216;modifyDate&#8217;.  The fields &#8216;birthDate&#8217; and &#8216;createDate&#8217; have a specified <a href="http://docs.jboss.org/hibernate/core/3.5/javadoc/org/hibernate/annotations/Type.html">Type</a>.  The field &#8216;modifyDate&#8217; however, does not have a specified Type.  The generated SQL from Hibernate is as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Person <span style="color: #66cc66;">&#40;</span>id bigint generated <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">AS</span> identity <span style="color: #66cc66;">&#40;</span>start <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
birthDate date<span style="color: #66cc66;">,</span> createDate timestamp<span style="color: #66cc66;">,</span> firstName varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
lastName varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> modifyDate timestamp<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We see that Hibernate created &#8216;birthDate&#8217; as a &#8216;Date&#8217; and the other two were created as a &#8216;Timestamp&#8217;.  The primary difference between &#8216;Date&#8217; and &#8216;Timestamp&#8217; in SQL is that &#8216;Timestamp&#8217; holds the date and time while the &#8216;Date&#8217; only holds the date.</p>
<p>At first glance this is not a big deal, but let&#8217;s take a closer look at the test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSave<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst&quot;</span>, <span style="color: #0000ff;">&quot;testLast&quot;</span>, 
                           birthDateWithTime, createDate, modifyDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		saveOrUpdate<span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Person is created with three <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">Dates</a> that all include date and time values.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testFind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Person foundPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithoutTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>createDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>modifyDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When Hibernate retrieves a Person from the database the &#8216;createDate&#8217; and &#8216;modifyDate&#8217; have been converted to <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">Timestamps</a>.  At first glance this doesn&#8217;t appear to be a big deal.  <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">java.sql.Timestamp</a> extends <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">java.util.Date</a>.  But why are they not equal.  I found a helpful answer to this question in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a> page 41.</p>
<p><em><br />
There are some classes in the Java platform libraries that do extend an instantiable<br />
class and add a value component. For example, java.sql.Timestamp<br />
extends java.util.Date and adds a nanoseconds field. The equals implementation<br />
for Timestamp does violate symmetry and can cause erratic behavior if<br />
Timestamp and Date objects are used in the same collection or are otherwise intermixed.<br />
The Timestamp class has a disclaimer cautioning programmers against<br />
mixing dates and timestamps. While you won’t get into trouble as long as you<br />
keep them separate, there’s nothing to prevent you from mixing them, and the<br />
resulting errors can be hard to debug. This behavior of the Timestamp class was a<br />
mistake and should not be emulated. (Bloch, Effective Java, 2nd Ed.)<br />
</em></p>
<p>If you want to read more about &#8216;equals&#8217; read chapter 3, Item 8 in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a>.  Apart from a hack there are two primary ways to solve this issue.  The first way involves typing &#8216;createDate&#8217; and &#8216;modifyDate&#8217; to <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">Timestamp</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	...
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Timestamp</span> createDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Timestamp</span> modifyDate<span style="color: #339933;">;</span>
        ...</pre></div></div>

<p>The second approach uses a custom <a href="http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/usertype/UserType.html">UserType</a>.  Here is a custom UserType that converts Timestamp to Date and vise versa.  There is a side effect with this solution.  Converting Timestamp to Date drops the nanosecond precision.  If this is not acceptable then the previous solution should be used.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.PreparedStatement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.ResultSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Timestamp</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Types</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.HibernateException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.usertype.UserType</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DateTimeUserType  <span style="color: #000000; font-weight: bold;">implements</span> UserType <span style="color: #009900;">&#123;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> sqlTypes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		 <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #003399;">Types</span>.<span style="color: #006633;">TIMESTAMP</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rawtypes&quot;</span><span style="color: #009900;">&#41;</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> returnedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Date</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> x, <span style="color: #003399;">Object</span> y<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		 <span style="color: #000000; font-weight: bold;">return</span> x <span style="color: #339933;">==</span> y <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> y <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> x.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> x<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> x.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> nullSafeGet<span style="color: #009900;">&#40;</span><span style="color: #003399;">ResultSet</span> rs, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> names, <span style="color: #003399;">Object</span> owner<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException, <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Timestamp</span> timestamp <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getTimestamp</span><span style="color: #009900;">&#40;</span>names<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">wasNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span>timestamp.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> nullSafeSet<span style="color: #009900;">&#40;</span><span style="color: #003399;">PreparedStatement</span> st, <span style="color: #003399;">Object</span> value, <span style="color: #000066; font-weight: bold;">int</span> index<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException, <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            st.<span style="color: #006633;">setNull</span><span style="color: #009900;">&#40;</span>index, <span style="color: #003399;">Types</span>.<span style="color: #006633;">TIMESTAMP</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Date</span> date <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
            <span style="color: #003399;">Timestamp</span> timestamp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#40;</span>date.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            st.<span style="color: #006633;">setTimestamp</span><span style="color: #009900;">&#40;</span>index, timestamp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> deepCopy<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isMutable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Serializable</span> disassemble<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		 <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Serializable</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> assemble<span style="color: #009900;">&#40;</span><span style="color: #003399;">Serializable</span> cached, <span style="color: #003399;">Object</span> owner<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> cached<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> replace<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> original, <span style="color: #003399;">Object</span> target, <span style="color: #003399;">Object</span> owner<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> original<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is the way to use it in the Person class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@TypeDefs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		@TypeDef<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dateTimeUserType&quot;</span>, typeClass<span style="color: #339933;">=</span>DateTimeUserType.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
	...
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dateTimeUserType&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> createDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dateTimeUserType&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifyDate<span style="color: #339933;">;</span>
        ...</pre></div></div>

<p>Either of these solutions will prevent the &#8216;equals&#8217; issue that we encountered before.  This issue is not a Hibernate issue it is truly a Java issue as is stated in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a>.  Hibernate can be challenging enough without these subtle issues that are easy to miss until a bug is reported.</p>
<p>Here is the complete code. <a href="http://blogs.sourceallies.com/wp-content/uploads/2012/02/HibernateDateTime3.zip" > Hibernate Logging Zip </a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hibernate Logging</title>
		<link>http://blogs.sourceallies.com/2012/01/hibernate-logging/</link>
		<comments>http://blogs.sourceallies.com/2012/01/hibernate-logging/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 14:54:22 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[interceptor]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2541</guid>
		<description><![CDATA[Through the years I&#8217;ve encountered a recurring requirement.  Clients need to log changes to the database for auditing and legal purposes.  To satisfy this requirement you could add logging to every save/update/delete call in your code.  Or better yet, you could create an aspect that wraps these calls.  While these would [...]]]></description>
			<content:encoded><![CDATA[<p>Through the years I&#8217;ve encountered a recurring requirement.  Clients need to log changes to the database for auditing and legal purposes.  To satisfy this requirement you could add logging to every save/update/delete call in your code.  Or better yet, you could create an <a href="http://goo.gl/DNo6a">aspect</a> that wraps these calls.  While these would certainly work Hibernate provides a convenient <a href="http://goo.gl/K0pJr">interceptor</a>.</p>
<p>In this article I will show you how to add a simple logger to Hibernate.<br />
<span id="more-2541"></span></p>
<h2>Start with a Test</h2>
<p>As an avid <a href="http://goo.gl/1KEI0">TDD </a> practitioner I will of coarse start with a test.  Here is the final version of the test that I used to drive my code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.logging</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">mock</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">verify</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">verifyNoMoreInteractions</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.Resource</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.Log</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.Criteria</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.SessionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.classic.Session</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.ContextConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.junit4.SpringJUnit4ClassRunner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.transaction.annotation.Transactional</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.Person</span><span style="color: #339933;">;</span>
&nbsp;
@RunWith<span style="color: #009900;">&#40;</span>SpringJUnit4ClassRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@ContextConfiguration
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateInterceptorTest <span style="color: #009900;">&#123;</span>
&nbsp;
	@Resource
	<span style="color: #000000; font-weight: bold;">private</span> SessionFactory sessionFactory<span style="color: #339933;">;</span>
&nbsp;
	@Resource
	<span style="color: #000000; font-weight: bold;">private</span> HibernateInterceptor hibernateInterceptor<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Session currentSession<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Log logger<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Date</span> NOW <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@Before
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		logger <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Log.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		currentSession <span style="color: #339933;">=</span> sessionFactory.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		hibernateInterceptor.<span style="color: #006633;">setLogger</span><span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSaveLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		saveOrUpdate<span style="color: #009900;">&#40;</span>createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testUpdateLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		saveOrUpdate<span style="color: #009900;">&#40;</span>createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		Person firstPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		firstPerson.<span style="color: #006633;">setFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		saveOrUpdate<span style="color: #009900;">&#40;</span>firstPerson<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Updating Person [firstName=testFirst2, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testDeleteLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		saveOrUpdate<span style="color: #009900;">&#40;</span>createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		Person firstPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		delete<span style="color: #009900;">&#40;</span>firstPerson<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Deleting Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> saveOrUpdate<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		currentSession.<span style="color: #006633;">saveOrUpdate</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> delete<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		currentSession.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Criteria criteria <span style="color: #339933;">=</span> currentSession.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> criteria.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> people<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Person createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst&quot;</span>, <span style="color: #0000ff;">&quot;testLast&quot;</span>, NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> person<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I used a mock logger to gather information from the interceptor.</p>
<h2>Spring Configuration</h2>
<p>Here is the configuration required for the interceptor.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                ...
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;entityInterceptor&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;hibernateInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         ...	
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context:component-scan</span> <span style="color: #000066;">base-package</span>=<span style="color: #ff0000;">&quot;com.sourceallies&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        ...</pre></div></div>

<p>The &#8216;entityInterceptor&#8217; property on the &#8217;sessionFactory&#8217; takes a Hibernate interceptor.</p>
<h2>Entity</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
&nbsp;
	@Id
	@GeneratedValue<span style="color: #009900;">&#40;</span>strategy<span style="color: #339933;">=</span>GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>1L<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
...
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Person [firstName=&quot;</span> <span style="color: #339933;">+</span> firstName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, lastName=&quot;</span>
				<span style="color: #339933;">+</span> lastName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, birthDate=&quot;</span> <span style="color: #339933;">+</span> birthDate <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For this example you need to implement the &#8216;toString&#8217; method on the entity being logged.</p>
<h2>Interceptor</h2>
<p>Here is the interceptor code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.logging</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.Log</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.LogFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.EmptyInterceptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.type.Type</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Component</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Component</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateInterceptor <span style="color: #000000; font-weight: bold;">extends</span> EmptyInterceptor <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Log logger <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>HibernateInterceptor.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onDelete<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> entity, <span style="color: #003399;">Serializable</span> id, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> state,
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> propertyNames, Type<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Deleting&quot;</span>, id, entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onDelete</span><span style="color: #009900;">&#40;</span>entity, id, state, propertyNames, types<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onFlushDirty<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> entity, <span style="color: #003399;">Serializable</span> id,
			<span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> currentState, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> previousState,
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> propertyNames, Type<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Updating&quot;</span>, id, entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onFlushDirty</span><span style="color: #009900;">&#40;</span>entity, id, currentState, previousState,
				propertyNames, types<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onSave<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> entity, <span style="color: #003399;">Serializable</span> id, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> state,
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> propertyNames, Type<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving&quot;</span>, id, entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onSave</span><span style="color: #009900;">&#40;</span>entity, id, state, propertyNames, types<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setLogger<span style="color: #009900;">&#40;</span>Log logger<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">logger</span> <span style="color: #339933;">=</span> logger<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> log<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> action, <span style="color: #003399;">Serializable</span> id, <span style="color: #003399;">Object</span> entity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span>action <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> entity <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>While the &#8216;onSave&#8217; and &#8216;onDelete&#8217; methods are clearly named the &#8216;onUpdate&#8217; method appears to be missing.  With a little research it quickly becomes obvious that &#8216;onFlushDirty&#8217; is used for updates.</p>
<p>Logging database activity is this simple.</p>
<p>Here is the complete code. <a href="http://blogs.sourceallies.com/wp-content/uploads/2012/01/HibernateLogging.zip" > Hibernate Logging Zip </a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2012/01/hibernate-logging/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Back to Basics: hashCode() &amp; equals()</title>
		<link>http://blogs.sourceallies.com/2011/12/back-to-basics-hashcode-equals/</link>
		<comments>http://blogs.sourceallies.com/2011/12/back-to-basics-hashcode-equals/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 15:53:22 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[guava]]></category>
		<category><![CDATA[hashCode]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2598</guid>
		<description><![CDATA[So we all know that if we implement equals() we must override hashCode() too.  But why?  The best explanation of this commandment can be found in Effective Java (2nd Edition) starting on page 45.  

&#8230; Failure to do so will result in a violation of the general contract for Object.hashCode, which will
prevent [...]]]></description>
			<content:encoded><![CDATA[<p>So we all know that if we implement equals() we must override hashCode() too.  But why?  The best explanation of this commandment can be found in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a> starting on page 45.  </p>
<p><em><br />
&#8230; Failure to do so will result in a violation of the general contract for Object.hashCode, which will<br />
prevent your class from functioning properly in conjunction with all hash-based<br />
collections, including HashMap, HashSet, and Hashtable. (Bloch, Effective Java, 2nd Ed.)<br />
</em><br />
<span id="more-2598"></span><br />
If you&#8217;re like me, you like to see a problem in running code.  Well here you go.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testHashMapRetrieval_WithoutHashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		PersonWithoutHashCode person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithoutHashCode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PersonWithoutHashCode person2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithoutHashCode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Map<span style="color: #339933;">&lt;</span>PersonWithoutHashCode, String<span style="color: #339933;">&gt;</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>PersonWithoutHashCode, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>person, <span style="color: #0000ff;">&quot;friend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertNull<span style="color: #009900;">&#40;</span>map.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>person2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testHashMapRetrieval_WithHashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		PersonWithHashCode person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithHashCode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PersonWithHashCode person2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithHashCode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Map<span style="color: #339933;">&lt;</span>PersonWithHashCode, String<span style="color: #339933;">&gt;</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>PersonWithHashCode, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>person, <span style="color: #0000ff;">&quot;friend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertNotNull<span style="color: #009900;">&#40;</span>map.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>person2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Two equivalent &#8216;PersonWithoutHashCode&#8217; instances are not considered equivalent when the &#8216;get&#8217; method is called on HashMap.  On the other hand the &#8216;PersonWithHashCode&#8217; instances are considered equivalent for the same method.  </p>
<p>While you can implment equals() and hashCode() by hand or generate them with your IDE, you might consider using the <a href="http://docs.guava-libraries.googlecode.com/git-history/v11.0.1/javadoc/com/google/common/base/Objects.html">Objects</a> class within the <a href="http://code.google.com/p/guava-libraries/">Guava Libraries</a>.  Here is the Person class using Guava.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Objects.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span>firstName, lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> obj<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> obj.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		PersonWithGuava other <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PersonWithGuava<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Objects.<span style="color: #006633;">equal</span><span style="color: #009900;">&#40;</span>firstName, other.<span style="color: #006633;">firstName</span><span style="color: #009900;">&#41;</span> 
				<span style="color: #339933;">&amp;&amp;</span> Objects.<span style="color: #006633;">equal</span><span style="color: #009900;">&#40;</span>lastName, other.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here&#8217;s the test for this new code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testHashMapRetrieval_WithGuava<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		PersonWithGuava person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithGuava<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PersonWithGuava person2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithGuava<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Map<span style="color: #339933;">&lt;</span>PersonWithGuava, String<span style="color: #339933;">&gt;</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>PersonWithGuava, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>person, <span style="color: #0000ff;">&quot;friend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertNotNull<span style="color: #009900;">&#40;</span>map.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>person2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So why use this?  This helps make our code <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>.  Much of the generated code for the &#8216;hashCode()&#8217; and &#8216;equals()&#8217; method can be encapsulated.  The Guava Library has done just that for us.  If the implementation needs to be changed it can be updated in one place.</p>
<p>Now if you&#8217;re like me, you are lazy.  As a lazy person I ask myself, &#8220;Do I want to write code that I can generate?&#8221;.  My initial thought is &#8220;no&#8221;.  Luckily you can use this new library and continue to generate your &#8216;equals()&#8217;, &#8216;hashCode()&#8217; and even your &#8216;toString()&#8217; methods.  Simply add the <a href="http://marketplace.eclipse.org/content/guava-eclipse-plugin">Guava Eclipse plugin</a> to your favorite version of <a href="http://www.eclipse.org/">Eclipse</a> and voila. </p>
<p>This plugin adds new menu options.</p>
<p><img src="http://blogs.sourceallies.com/wp-content/uploads/2012/02/menu.PNG" alt="menu" title="menu" width="537" height="272" class="size-full wp-image-2607" /></p>
<p><img src="http://blogs.sourceallies.com/wp-content/uploads/2012/02/wizard1.PNG" alt="wizard" title="wizard" width="347" height="488" class="size-full wp-image-2612" /></p>
<p>This is the code that the plugin generated for the Person class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Objects.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span>firstName, lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>object <span style="color: #000000; font-weight: bold;">instanceof</span> PersonWithGuava<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			PersonWithGuava that <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PersonWithGuava<span style="color: #009900;">&#41;</span> object<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> Objects.<span style="color: #006633;">equal</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">firstName</span>, that.<span style="color: #006633;">firstName</span><span style="color: #009900;">&#41;</span>
				<span style="color: #339933;">&amp;&amp;</span> Objects.<span style="color: #006633;">equal</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastName</span>, that.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>While we all know that we must override &#8216;hashCode()&#8217; and &#8216;equals()&#8217; together, we also know that we should eliminate duplication in our code and encapsulate common functionality.  Install the Guava plugin today and remove another piece of duplication from your code.</p>
<p><strong>Please read the comments below before you use this plugin.</strong></p>
<p>Here&#8217;s the complete code <a href="http://blogs.sourceallies.com/wp-content/uploads/2012/02/HashCode.zip" > HashCode</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2011/12/back-to-basics-hashcode-equals/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Debugging memory leaks with VisualVM</title>
		<link>http://blogs.sourceallies.com/2011/07/debugging-memory-leaks-with-visualvm/</link>
		<comments>http://blogs.sourceallies.com/2011/07/debugging-memory-leaks-with-visualvm/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 12:45:01 +0000</pubDate>
		<dc:creator>Cory Wandling</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[vm]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2389</guid>
		<description><![CDATA[At work I had run into a memory leak when scrolling through large result sets returned from Hibernate.  I thought I had fixed it by performing a evict()/clear()/flush() in the HibernateTemplate that I was using but suddenly the leak was back.  I was using VisualVm to monitor the heap so I started poking [...]]]></description>
			<content:encoded><![CDATA[<p>At work I had run into a memory leak when scrolling through large result sets returned from Hibernate.  I thought I had fixed it by performing a evict()/clear()/flush() in the HibernateTemplate that I was using but suddenly the leak was back.  I was using <a href="http://visualvm.java.net/">VisualVm</a> to monitor the heap so I started poking around to see if there was anything to help diagnose where the leak was.<br />
<span id="more-2389"></span></p>
<p>First thing I did was to take a heap dump.</p>
<p><img src="http://www.jroller.com/clwandling/resource/memory_leak_1.png"></p>
<p>I selected the classes and I see</p>
<p><img src="http://www.jroller.com/clwandling/resource/memory_leak_2.png"></p>
<p>
Well <strong>THAT</strong> was not helpful.  It turns out that the leak is in a class that only has one instance and it is <em><b>holding onto</b></em> thousands of objects.<br />
Then on the summary page I notice the &#8220;Find 20 biggest objects by retained size:&#8221;.</p>
<p><img src="http://www.jroller.com/clwandling/resource/memory_leak_4.png"></p>
<p>I click it and a <em>long</em> time later I see</p>
<p><img src="http://www.jroller.com/clwandling/resource/memory_leak_3.png"></p>
<p>Shazam! There it is! In this faked up example I built there is a single Stack instance but it&#8217;s <em><b>retained size</b></em> is huge.  That because VisualVM computed not just the size of that object but the sizes of all of the objects it was holding onto.  How sweet is that.  I found my memory leak in like 5 minutes.</p>
<p>Another useful tool!</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2011/07/debugging-memory-leaks-with-visualvm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven 3 Tutorial &#8211; Project Setup</title>
		<link>http://blogs.sourceallies.com/2011/06/maven-3-tutorial-project-setup/</link>
		<comments>http://blogs.sourceallies.com/2011/06/maven-3-tutorial-project-setup/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 22:27:52 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Aggregate POM]]></category>
		<category><![CDATA[Dependency Management]]></category>
		<category><![CDATA[M3_HOME]]></category>
		<category><![CDATA[Maven 3]]></category>
		<category><![CDATA[Maven3]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[POM]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2179</guid>
		<description><![CDATA[
Overview

What is Maven?
Plugins
Why not Ant and Ivy?
But It Downloads the Internet


Getting Started

Install
What is a POM?
Convention over Configuration
New Project


Project Management

Parent POM
Local Maven Repository
Multiple Artifacts from a Single Source
Release Plugin
Aggregate POM
Dependency Management
Distribution Zip


Conclusions

Overview
What is Maven?
Maven is a software project management and comprehension tool that includes: build tools, dependency management, project reporting and much more.  I say &#8220;much [...]]]></description>
			<content:encoded><![CDATA[<ul>
<li><strong>Overview</strong>
<ul>
<li>What is Maven?</li>
<li>Plugins</li>
<li>Why not Ant and Ivy?</li>
<li>But It Downloads the Internet</li>
</ul>
</li>
<li><strong>Getting Started</strong>
<ul>
<li>Install</li>
<li>What is a POM?</li>
<li>Convention over Configuration</li>
<li>New Project</li>
</ul>
</li>
<li><strong>Project Management</strong>
<ul>
<li>Parent POM</li>
<li>Local Maven Repository</li>
<li>Multiple Artifacts from a Single Source</li>
<li>Release Plugin</li>
<li>Aggregate POM</li>
<li>Dependency Management</li>
<li>Distribution Zip</li>
</ul>
</li>
<li><strong>Conclusions</strong></li>
</ul>
<h1>Overview</h1>
<h2>What is Maven?</h2>
<p>Maven is a software project management and comprehension tool that includes: build tools, dependency management, project reporting and much more.  I say &#8220;much more&#8221; because at the core Maven is a plugin execution framework.  There are plugins supported by the Maven project (http://maven.apache.org/plugins/index.html), plugins supported by Mojo Project (http://mojo.codehaus.org/plugins.html), and third party plugins.  If you can find or write a plugin, Maven can run it.<br />
<span id="more-2179"></span></p>
<h2>Plugins</h2>
<p>Plugins are configured to run during specific phases in the Maven lifecycle.  The high-level phases that make up the build lifecycle include: validation, compile, test, package, integration-test, verify, install, and deploy.  An exhaustive list of build phases can be found at <a href="http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference.">http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference.</a></p>
<h2>Why not Ant and Ivy?</h2>
<p>People always gravitate to this question of whether Maven is better than Ant and Ivy.  Unfortunately this is not a fare comparison.  Ant is a build tool and Ivy is a dependency management tool.  Maven is not just a build tool or just a dependency management tool.  On the contrary, Maven is a project management tool that embodies software development best practices.</p>
<p>To illustrate this point I will share a story from my early years in software development.  Our shop used Ant without Ivy.  Ivy didn&#8217;t become a full fledged Apache project until October, 2007.  Each team had their own Ant build.  As we began to move from team to team we had to find out which targets needed to be run.  Over time we decided on a set of names that every team needed to implement.  This helped but just because the target was named the same thing it did not mean they worked the same way.</p>
<p>Long story short, we began to meet to create a shared standard.  During this long process we were introduced to Maven.  Ironically, Maven had accepted standards for many of the issues that we were trying address.  Maven was not just a standard for our company it was a standard for all Maven users.  New employees that were familiar with Maven had a good grasp of how we managed our projects.</p>
<p>Maven provided a standard that not only addressed our concerns as software developers.  It provided a standard solution that supported our corporate: audits, disaster recovery, tracability, and the list goes on.  We were trying to reinvent the wheel with Ant when the Maven community had already solved these problems.</p>
<p>Ant and Ivy are powerful ingredients for designing a custom build process.  Maven however is a powerful solution for building and managing software.  In our case we wanted a solution instead of the ingredients and tools to create our own solution.  Maven satisfied our needs and saved us significant time and money.</p>
<h2>But It Downloads the Internet</h2>
<p>I can&#8217;t tell you how many people have told me that they don&#8217;t like Maven because it &#8220;downloads the internet&#8221;. I find it ironic that many of them like Ivy.  I hate to break the news, but Ivy downloads the internet too. I&#8217;m not sure why this is a concern.  Is there a fear that Maven will fill up their hard drive?  Disk space is cheap.  The computer I am working on right now has 150GB of hard disk space and my Maven local repository is eating up 300MB of it.  This computer has pleanty of room for more of the internet.</p>
<p>I recall the first time that I tried to setup a new project without a dependency management tool.  I spent over a week trying to download the correct version of each JAR.  I was ready to take a vacation by the time I finished this ugly puzzle.  After using Maven and Ivy for managing my  dependencies I will never go back.  By the time I downloaded all the JARs that we needed, I felt like I had in fact downloaded the entire internet.</p>
<p>I love dependency management tools and they can have as much of my hard drive as they need.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2011/06/maven-3-tutorial-project-setup/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Testing Spring Wiring</title>
		<link>http://blogs.sourceallies.com/2011/06/testing-spring-wiring/</link>
		<comments>http://blogs.sourceallies.com/2011/06/testing-spring-wiring/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 17:00:20 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[annotation]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2045</guid>
		<description><![CDATA[Overview
Spring is an essential part of my technology stack.  I cann&#8217;t image providing quality software that doesn&#8217;t leverage an IoC container.  However, decoupling components requires some amount of configuration.  Whether this is accomplished through annotations or XML, it&#8217;s fairly easy to mess up.  Fixing these missing or incorrect configurations doesn&#8217;t take [...]]]></description>
			<content:encoded><![CDATA[<h1>Overview</h1>
<p><a href="http://en.wikipedia.org/wiki/Spring_Framework" target="_blank">Spring</a> is an essential part of my technology stack.  I cann&#8217;t image providing quality software that doesn&#8217;t leverage an <a href="http://en.wikipedia.org/wiki/Inversion_of_control" target="_blank">IoC</a> container.  However, decoupling components requires some amount of configuration.  Whether this is accomplished through annotations or XML, it&#8217;s fairly easy to mess up.  Fixing these missing or incorrect configurations doesn&#8217;t take very long.  The real question is how quickly can you identify these errors?</p>
<p>This question of how long, is a feedback loop question.  Unfortunately many teams wait until they fire up the application server to see if their Spring context is wired correctly.  This is too late.</p>
<p>One of our clients suffered from this very issue.  Due to environmental constraints they could not run automated, in-container tests that would have identified misconfigured beans.  After repeatedly committing stupid configuration mistakes, I decided that I would write a Spring wiring test.  As I began to write this I encountered five problems.<br />
<span id="more-2045"></span></p>
<h1>Problem 1: Scope</h1>
<p>The first problem that I encountered was that not all of my beans were being created when I loaded my context.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span>SpringJUnit4ClassRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@ContextConfiguration
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpringWiringTest<span style="color: #009900;">&#123;</span>
&nbsp;
     @Test
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
...</pre></div></div>

<p>I quickly remembered that only beans with the default &#8217;singleton&#8217; scope will be loaded immediately.  The rest of the bean scopes (&#8217;prototype&#8217;, &#8216;request&#8217;, and &#8217;session&#8217;) will not be created until they are requested.  In order to solve this I looped through all of the bean names and retrieved each bean from the context.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> names <span style="color: #339933;">=</span> context.<span style="color: #006633;">getBeanDefinitionNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name <span style="color: #339933;">:</span> names<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     context.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>However, when I ran this I got the following error</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">IllegalStateException</span><span style="color: #339933;">:</span> No Scope registered <span style="color: #000000; font-weight: bold;">for</span> scope <span style="color: #0000ff;">'request'</span></pre></div></div>

<p>In order to correct this issue I added the following setup method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Before
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testCaseSetUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     context.<span style="color: #006633;">getBeanFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">registerScope</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;session&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> SessionScope<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     context.<span style="color: #006633;">getBeanFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">registerScope</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;request&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> RequestScope<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     MockHttpServletRequest request <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockHttpServletRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     ServletRequestAttributes attributes <span style="color: #339933;">=</span>
          <span style="color: #000000; font-weight: bold;">new</span> ServletRequestAttributes<span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     RequestContextHolder.<span style="color: #006633;">setRequestAttributes</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I threw in the session scope too just to be safe.</p>
<h1>Problem 2: Abstract Bean Definitions</h1>
<p>The second problem that I had to solve was requesting abstract beans.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">org.<span style="color: #006633;">springframework</span>.<span style="color: #006633;">beans</span>.<span style="color: #006633;">factory</span>.<span style="color: #006633;">BeanIsAbstractException</span><span style="color: #339933;">:</span> <span style="color: #003399;">Error</span> creating bean
with name <span style="color: #0000ff;">'testBean'</span><span style="color: #339933;">:</span> Bean definition is <span style="color: #000000; font-weight: bold;">abstract</span></pre></div></div>

<p>In order to solve this issue I added a check to verify if a bean definition was abstract.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> names <span style="color: #339933;">=</span> context.<span style="color: #006633;">getBeanDefinitionNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name <span style="color: #339933;">:</span> names<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     BeanDefinition beanDefinition <span style="color: #339933;">=</span> context.<span style="color: #006633;">getBeanDefinition</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>beanDefinition.<span style="color: #006633;">isAbstract</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          context.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h1>Problem 3: Mock Beans</h1>
<p>The third issue I found was environment specific beans.  JNDI references are one type of environment specific bean.</p>
<blockquote><p><code>&lt;jee:jndi-lookup id="testBean" jndi-name="jdbc/test"/&gt;</code></p></blockquote>
<p>In order to solve this issue, I decided to create a test bootstrap context that imported all of the production contexts.  This provided a place for me to override beans that I wanted to create for the test.  I used Mockito to create mock beans in most situations.</p>
<blockquote><p><code>&lt;bean id="testBean" class="org.mockito.Mockito" factory-method="mock"&gt;<br />
&lt;constructor-arg value="com.something.Foo" /&gt;<br />
&lt;/bean&gt;</code></p></blockquote>
<p>While this worked for most beans, I received errors when I wired up mock DataSources.  Instead I used embedded databases to replace real DataSources.</p>
<blockquote><p><code>&lt;bean id="testDataSource"<br />
class="org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean"<br />
p:databaseType="H2"<br />
abstract="true"/&gt;</code></p></blockquote>
<h1>Problem 4: Components</h1>
<p>This fourth issue was harder to identify but equally problematic.  I frequently wired up beans with the @Component annotation and failed to configure an &#8216;component-scan&#8217; for those packages.</p>
<blockquote><p><code>&lt;context:component-scan base-package="com.sourceallies"/&gt;</code></p></blockquote>
<p>In other words I needed to reconcile the classes marked with the @Component annotation with the beans that were available in the Spring context.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ClassPathScanningCandidateComponentProvider scanner <span style="color: #339933;">=</span>
     <span style="color: #000000; font-weight: bold;">new</span> ClassPathScanningCandidateComponentProvider<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
scanner.<span style="color: #006633;">addIncludeFilter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AnnotationTypeFilter<span style="color: #009900;">&#40;</span><span style="color: #003399;">Component</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>BeanDefinition bean <span style="color: #339933;">:</span> scanner.<span style="color: #006633;">findCandidateComponents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.something&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      components.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>bean.<span style="color: #006633;">getBeanClassName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will create a list of components found on the classpath under the specified base package.  Reconciling this list with the beans loaded in the Spring context will identify any components that are not configured in Spring.  This list came in handy for two reasons.  First it identified any components that I needed to configure in Spring.  Second it listed components that were provided in dependent jars.  This list helped us become proactive instead of reactive.</p>
<h1>Problem 5: Duplicate Bean Definitions</h1>
<p>The fifth issue is duplicate bean definitions.  This can occur when more than one bean has the same bean definition name.  This can also occur when the same context is loaded multiple times.  By default Spring allows the new bean definition to overwrite the previous one.  This sounds annoying but harmless.  On the contrary, Spring does not guarantee the loading order.  Furthermore, each deployment environment&#8217;s classloader can load context files in a different order.  The bottom line is that duplicate bean definitions can result in subtle and painful bugs.</p>
<p>While this issue was clear to me the solution was a bit more illusive.  At first I tried to set the allowBeanDefinitionOverriding flag on org.springframework.beans.factory.support.DefaultListableBeanFactory.  This throws an exception if the same bean definition is loaded multiple times.  Case closed, right?</p>
<p>Unfortunately the solution was not this simple.  Problem 3, Mock Beans, requires the creation of a test bootstrap context that overrides bean definitions.  So the allowBeanDefinitionOverriding flag is out.  After crying a little, I started exploring Spring.  Long story short, I overrode the  &#8216;protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)&#8217; method on org.springframework.context.support.ClassPathXmlApplicationContext and injected a delegating proxy.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Override
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> loadBeanDefinitions<span style="color: #009900;">&#40;</span>DefaultListableBeanFactory beanFactory<span style="color: #009900;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">throws</span> BeansException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
	Enhancer enhancer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Enhancer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	enhancer.<span style="color: #006633;">setSuperclass</span><span style="color: #009900;">&#40;</span>DefaultListableBeanFactory.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	BeanohBeanFactoryMethodInterceptor callback <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BeanohBeanFactoryMethodInterceptor<span style="color: #009900;">&#40;</span>
			beanFactory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	callbacks.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	enhancer.<span style="color: #006633;">setCallback</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	DefaultListableBeanFactory proxy <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DefaultListableBeanFactory<span style="color: #009900;">&#41;</span> enhancer
			.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">loadBeanDefinitions</span><span style="color: #009900;">&#40;</span>proxy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I used &#8216;net.sf.cglib.proxy.Enhancer&#8217; and &#8216;net.sf.cglib.proxy.MethodInterceptor&#8217; to create a proxy.  The trick was to delegate to the real &#8216;org.springframework.beans.factory.support.DefaultListableBeanFactory&#8217; instance.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Override
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> intercept<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object, <span style="color: #003399;">Method</span> method, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args,
		MethodProxy methodProxy<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">Method</span> delegateMethod <span style="color: #339933;">=</span> delegateClass.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span>method.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
			method.<span style="color: #006633;">getParameterTypes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;registerBeanDefinition&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>method.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>beanDefinitionMap.<span style="color: #006633;">containsKey</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">List</span> definitions <span style="color: #339933;">=</span> beanDefinitionMap
					.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			definitions.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>BeanDefinition<span style="color: #009900;">&#41;</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">List</span> beanDefinitions <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			beanDefinitions.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>BeanDefinition<span style="color: #009900;">&#41;</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			beanDefinitionMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>, beanDefinitions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">return</span> delegateMethod.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>delegate, args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This passively collects bean definitions that are registered with the registerBeanDefinition method.  This method takes two arguments: a String bean name (arg[0]) and a &#8216;org.springframework.beans.factory.config.BeanDefinition&#8217; (arg[1]).  This map of bean definitions can be inspected later to determine if beans were loaded multiple times.</p>
<h1>Conclusions</h1>
<p>This experience has lead our team to develop a complete, open source solution called <a href="http://beanoh.org">Beanoh</a> (pronounced \&#8217;beanˌō\).  Simply <a href="http://search.maven.org/remotecontent?filepath=com/sourceallies/beanoh/beanoh/1.0.0/beanoh-1.0.0.jar">download</a> the jar or build the project with maven and start using it.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeTest <span style="color: #000000; font-weight: bold;">extends</span> BeanohTestCase <span style="color: #009900;">&#123;</span>
&nbsp;
	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		assertContextLoading<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For more information visit <a href="http://beanoh.org/overview.html">http://beanoh.org/overview.html</a>.</p>
<p>In just a few short weeks this testing approach prevented our team from committing broken Spring configurations.  Furthermore, we took time to evaluate the inherited components that we received through our project dependencies.  This provided valuable insights into our dependency structure and our packaging strategy.</p>
<p>These wiring tests even gave us feedback as we refactored our spring contexts.  We were able to simplify our context files without worrying that we would break something.  We also identified unused and duplicate bean definitions.  Without the quick feedback of this wiring test we would have committed broken changes that effected the whole team.</p>
<p>Automated tests help you manage code behavior rather than allowing code to drive your behavior.  <a href="http://beanoh.org">Download Beanoh</a> today and take control of your Spring configuration.</p>
<p>For more information checkout our YouTube <a href=" http://www.youtube.com/playlist?p=PL7F1B7F11D0D0FB91">video tutorials</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2011/06/testing-spring-wiring/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Mock Me With Fewer Words</title>
		<link>http://blogs.sourceallies.com/2011/05/mock-me-with-fewer-words/</link>
		<comments>http://blogs.sourceallies.com/2011/05/mock-me-with-fewer-words/#comments</comments>
		<pubDate>Sun, 01 May 2011 10:00:20 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[Mockito]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2032</guid>
		<description><![CDATA[With Mockito 1.8.3 or higher you can significantly reduce your test code setup.

Here is the code before.

public class MockitoTest &#123;
&#160;
   private EmailFactory emailFactory;
   private Notifier notifier;
   private Emailer emailer;
   private Email email;
&#160;
   @Before
   public void setUp&#40;&#41;&#123;
      notifier = [...]]]></description>
			<content:encoded><![CDATA[<p>With <a href="http://code.google.com/p/mockito/" target="_blank">Mockito</a> 1.8.3 or higher you can significantly reduce your test code setup.<br />
<span id="more-2032"></span><br />
Here is the code before.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MockitoTest <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> EmailFactory emailFactory<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> Notifier notifier<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> Emailer emailer<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> Email email<span style="color: #339933;">;</span>
&nbsp;
   @Before
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      notifier <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Notifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      emailFactory <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>EmailFactory.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      emailer <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Emailer.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      email <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Email.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      notifier.<span style="color: #006633;">setEmailFactory</span><span style="color: #009900;">&#40;</span>emailFactory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      notifier.<span style="color: #006633;">setEmailer</span><span style="color: #009900;">&#40;</span>emailer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   @Test
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSend<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      when<span style="color: #009900;">&#40;</span>emailFactory.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      notifier.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      verify<span style="color: #009900;">&#40;</span>emailFactory<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      verify<span style="color: #009900;">&#40;</span>emailer<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>emailFactory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>emailer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Notifier<span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">private</span> EmailFactory emailFactory<span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">private</span> Emailer emailer<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         Email email <span style="color: #339933;">=</span> emailFactory.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         emailer.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setEmailer<span style="color: #009900;">&#40;</span>Emailer emailer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">emailer</span> <span style="color: #339933;">=</span> emailer<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setEmailFactory<span style="color: #009900;">&#40;</span>EmailFactory emailFactory<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">emailFactory</span> <span style="color: #339933;">=</span> emailFactory<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is the code using the new annotations.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span>MockitoJUnitRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MockitoAnnotationsTest <span style="color: #009900;">&#123;</span>
&nbsp;
   @Mock <span style="color: #000000; font-weight: bold;">private</span> EmailFactory emailFactory<span style="color: #339933;">;</span>
   @Mock <span style="color: #000000; font-weight: bold;">private</span> Emailer emailer<span style="color: #339933;">;</span>
   @Mock <span style="color: #000000; font-weight: bold;">private</span> Email email<span style="color: #339933;">;</span>
   @InjectMocks <span style="color: #000000; font-weight: bold;">private</span> Notifier notifier <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Notifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   @Test
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSend<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      when<span style="color: #009900;">&#40;</span>emailFactory.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      notifier.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      verify<span style="color: #009900;">&#40;</span>emailFactory<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      verify<span style="color: #009900;">&#40;</span>emailer<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>emailFactory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>emailer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Notifier<span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">private</span> EmailFactory emailFactory<span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">private</span> Emailer emailer<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         Email email <span style="color: #339933;">=</span> emailFactory.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         emailer.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This new annotation style reduced the number of lines of code by over 40%.  The longer a test is the harder it is to understand.  Reducing the amount of test code increases its clarity.</p>
<p>So how does it work?  &#8216;@Mock&#8217; is equivalent &#8216;Mockito.mock(Some.class)&#8217;.  &#8216;@InjectMocks&#8217; is equivalent to setting the mocks on the class under test.  This annotation uses reflection to set properties so you do not have to add setters just for testing.  Lastly, the combination of &#8216;@RunWith(MockitoJUnitRunner.class)&#8217; and &#8216;@InjectMocks&#8217; enables you to remove the &#8216;@Before&#8217; method.  This pair of annotations prepares the class under test before each test method.</p>
<p>I have been using mocks in tests for years and this is a great addition to this powerful framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2011/05/mock-me-with-fewer-words/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rethinking the DAO-Service layer relationship</title>
		<link>http://blogs.sourceallies.com/2011/04/rethinking-the-dao-service-layer-relationship/</link>
		<comments>http://blogs.sourceallies.com/2011/04/rethinking-the-dao-service-layer-relationship/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 17:34:25 +0000</pubDate>
		<dc:creator>Tim Bierbaum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2058</guid>
		<description><![CDATA[Lately I have been thinking that the standard service-calling-the-dao-layer architecture hasn&#8217;t been working out as well as I would hope.  The applications I have been working on have been using Spring and Hibernate with a dao object per model object.  While this does provide a good separation between the two, I have been [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have been thinking that the standard service-calling-the-dao-layer architecture hasn&#8217;t been working out as well as I would hope.  The applications I have been working on have been using Spring and Hibernate with a dao object per model object.  While this does provide a good separation between the two, I have been finding it increasingly difficult to write good tests for the service layer as the project matures.  Past experience has shown that if writing tests is difficult, then it just isn&#8217;t done.  Follow along as I think about possible ways to address this issue.<br />
<span id="more-2058"></span><br />
Let&#8217;s look at what I consider to be the &#8217;standard&#8217; service layer object.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ServiceExample1 <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> FooDAO fooDAO<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> BarDAO barDAO<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchForFooAndBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// do some stuff</span>
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		fooDAO.<span style="color: #006633;">searchForFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// do some more stuff</span>
		<span style="color: #666666; font-style: italic;">// </span>
&nbsp;
		barDAO.<span style="color: #006633;">searchForBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// finish up doing stuff</span>
		<span style="color: #666666; font-style: italic;">//</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createANewFoo<span style="color: #009900;">&#40;</span>Foo foo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		fooDAO.<span style="color: #006633;">createFoo</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createANewBar<span style="color: #009900;">&#40;</span>Bar bar<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		barDAO.<span style="color: #006633;">createBar</span><span style="color: #009900;">&#40;</span>bar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A couple of methods are defined, and they use two difference dao objects.  Writing a test for the searchForFooAndBar method would currently involve providing alternative implementations for both the FooDAO and BarDAO objects.  While this initially wouldn&#8217;t be too difficulty, it is easy to imagine how the number of dao dependencies could grow as the project goes on.  It could also cause problems since we wouldn&#8217;t know which methods in the dao needed to be stubbed out or mocked for testing.  I also worry that it would be too easy to test implementation instead of behavior.  A test failure would be reported if the service changed which dao methods were called.</p>
<p>Another issue I&#8217;ve run into is not knowing which dao&#8217;s are involved in a method when writing the test.  My initial thought was to not have the service object maintain references to the dao objects, but instead have each method require them as method parameters.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ServiceExample2 <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchForFooAndBar<span style="color: #009900;">&#40;</span>FooDAO fooDAO, BarDAO barDAO<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// do some stuff</span>
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		fooDAO.<span style="color: #006633;">searchForFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// do some more stuff</span>
		<span style="color: #666666; font-style: italic;">// </span>
&nbsp;
		barDAO.<span style="color: #006633;">searchForBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// finish up doing stuff</span>
		<span style="color: #666666; font-style: italic;">//</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createANewFoo<span style="color: #009900;">&#40;</span>FooDAO fooDAO, Foo foo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		fooDAO.<span style="color: #006633;">createFoo</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createANewBar<span style="color: #009900;">&#40;</span>BarDAO barDAO, Bar bar<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		barDAO.<span style="color: #006633;">createBar</span><span style="color: #009900;">&#40;</span>bar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p> Looking over this code, I didn&#8217;t care for it from the start.  Maybe if the service layer objects were finer grained.  Instead of three methods in the service from our first example, maybe there should be 3 service classes.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchForFooAndBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// do some stuff</span>
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		fooDAO.<span style="color: #006633;">searchForFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// do some more stuff</span>
		<span style="color: #666666; font-style: italic;">// </span>
&nbsp;
		barDAO.<span style="color: #006633;">searchForBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// finish up doing stuff</span>
		<span style="color: #666666; font-style: italic;">//</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I think this would help fix the problem of having too many dao references, but I still wasn&#8217;t sure I liked the dependency on the dao&#8217;s themselves.  That brought me to the final solution I thought of, which involves creating an interface for each method that defines the operations it requires to be provided.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> FooCreationAdapter <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createFoo<span style="color: #009900;">&#40;</span>Foo foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> BarCreationAdapter <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createBar<span style="color: #009900;">&#40;</span>Bar bar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> SearchAdapter <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchForFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchForBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Service <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchForFooAndBar<span style="color: #009900;">&#40;</span>SearchAdapter searchAdapter<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// do some stuff</span>
		<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
		searchAdapter.<span style="color: #006633;">searchForFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// do some more stuff</span>
		<span style="color: #666666; font-style: italic;">// </span>
&nbsp;
		searchAdapter.<span style="color: #006633;">searchForBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #666666; font-style: italic;">// finish up doing stuff</span>
		<span style="color: #666666; font-style: italic;">//</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createANewFoo<span style="color: #009900;">&#40;</span>FooCreationAdapter adapter, Foo foo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		adapter.<span style="color: #006633;">createFoo</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createANewBar<span style="color: #009900;">&#40;</span>BarCreationAdapter adapter, Bar bar<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		adapter.<span style="color: #006633;">createBar</span><span style="color: #009900;">&#40;</span>bar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>While I feel this is the cleanest solution that would be the easiest to test, I really don&#8217;t think it is practical on a project level.  Does anyone have a solution that has worked well for them?</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2011/04/rethinking-the-dao-service-layer-relationship/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Selenium IDE &#8211; Part II</title>
		<link>http://blogs.sourceallies.com/2011/04/selenium-ide-part-ii/</link>
		<comments>http://blogs.sourceallies.com/2011/04/selenium-ide-part-ii/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 12:50:42 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[selenium]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1946</guid>
		<description><![CDATA[In Part I we covered

Setting Up
Recording Tests
Playing Back Tests
Saving Test Cases
Resuming Recording
Saving Test Suites

All examples will use the sample site https://sites.google.com/site/example4selenium/.
Store Value

There are two ways to store values.
First, you can define custom variables

Second you can use the drop-down &#8217;storeText&#8217; option.


Enter a variable name.

Variables can be used by simply surrounding the variable name with &#8216;${ }&#8217;

Running [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="/2011/03/selenium-ide-part-i">Part I</a> we covered</p>
<ul>
<li>Setting Up</li>
<li>Recording Tests</li>
<li>Playing Back Tests</li>
<li>Saving Test Cases</li>
<li>Resuming Recording</li>
<li>Saving Test Suites</li>
</ul>
<p>All examples will use the sample site <a href="https://sites.google.com/site/example4selenium/">https://sites.google.com/site/example4selenium/</a>.</p>
<h2>Store Value</h2>
<ul>
<li>There are two ways to store values.</li>
<li>First, you can define custom variables</li>
<p><img class="aligncenter size-full wp-image-1908" title="Custom Variables" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/005.JPG" alt="Custom Variables" width="543" height="632" /></p>
<li>Second you can use the drop-down &#8217;storeText&#8217; option.</li>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-1909" title="Store Option" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/006.JPG" alt="Store Option" width="600" height="500" /></p>
<p><span id="more-1946"></span></p>
<li>Enter a variable name.</li>
<p><img class="aligncenter size-full wp-image-1910" title="Enter Variable Name" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/007.JPG" alt="Enter Variable Name" width="326" height="145" /></p>
<li>Variables can be used by simply surrounding the variable name with &#8216;${ }&#8217;</li>
<p><img class="aligncenter size-full wp-image-1911" title="Use Variables" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/008.JPG" alt="Use Variables" width="543" height="632" /></p>
<li>Running this script will result in an insert of both variables separated with an &#8216;and&#8217;.</li>
<p><img class="aligncenter size-full wp-image-1912" title="Variable Results" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/009.JPG" alt="Variable Results" width="698" height="534" /></ul>
<h2>Debug</h2>
<ul>
<li>To debug a script insert a breakpoint.</li>
<p><img class="aligncenter size-full wp-image-1914" title="Insert Breakpoint" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/010.JPG" alt="Insert Breakpoint" width="545" height="633" /></p>
<li>Run the test case.</li>
<p><img class="aligncenter size-full wp-image-1918" title="Run For Debug" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/0111.JPG" alt="Run For Debug" width="543" height="632" /></p>
<li>The <img class="alignnone size-full wp-image-1919" title="Continue" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/012.JPG" alt="Continue" width="28" height="24" /> button resumes the execution of the script.</li>
<li>The <img class="alignnone size-full wp-image-1920" title="Step" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/013.JPG" alt="Step" width="28" height="24" /> button steps through the test case one action at a time.</li>
</ul>
<h2>XPath</h2>
<ul>
<li>Selenium IDE can navigate html using XPath.  If you are not familiar with XPath there is a beginners tutorial at <a href="http://www.w3schools.com/xpath/default.asp" target="_blank">http://www.w3schools.com/xpath/default.asp</a>.</li>
<li>Instead of referring to the id of a target you can locate an element with XPath.</li>
<p><img class="aligncenter size-full wp-image-1923" title="Text Area XPath" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/14.JPG" alt="Text Area XPath" width="543" height="632" /></p>
<li>You can verify your XPath by clicking the &#8216;Find&#8217; button.  The element will be highlighted in the browser with flashing neon green if it exists.</li>
<li>If you want to select elements by position, you may want the following information.  The traditional &#8216;//input[2]&#8216; does not work all the time in Selenium IDE.  If you can not locate your target element by index, use the following syntax &#8216;//input/following::input&#8217;.  This syntax specifies the location of an input that follows another input.  This will result in the selection of the second input.  Simply add another &#8216;/following::&#8217; to get to the next matching element.  Unfortunately this syntax is quite verbose.</li>
</ul>
<h2>Exporting Scripts</h2>
<ul>
<li>Recorded scripts can be extracted into a number of programming languages.</li>
<p><img class="aligncenter size-full wp-image-1925" title="Export Script" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/210.JPG" alt="Export Script" width="544" height="634" /></p>
<li>For this example lets export this test case as &#8216;JUnit 4 (Remote Control)&#8217;.</li>
<li>Copy the resulting code into your favorite IDE.</li>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-1927" title="IDE" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/32.JPG" alt="IDE" width="600" height="500" /></p>
<li>In order to get this to compile you will need to add the following dependencies to your classspath</li>
<ul>
<li><a href="http://repo1.maven.org/maven2/junit/junit/4.8.2/junit-4.8.2.jar">http://repo1.maven.org/maven2/junit/junit/4.8.2/junit-4.8.2.jar</a></li>
<li><a href="http://repo1.maven.org/maven2/org/seleniumhq/selenium/client-drivers/selenium-java-client-driver/1.0.1/selenium-java-client-driver-1.0.1.jar">http://repo1.maven.org/maven2/org/seleniumhq/selenium/client-drivers/selenium-java-client-driver/1.0.1/selenium-java-client-driver-1.0.1.jar</a></li>
</ul>
<li>Download <a href="http://selenium.googlecode.com/files/selenium-server-standalone-2.0b2.jar">http://selenium.googlecode.com/files/selenium-server-standalone-2.0b2.jar</a></li>
<li>Open a new terminal window and navigate to the directory where you downloaded the jar.</li>
<li>run &#8216;java -jar selenium-server-standalone-2.0b2.jar&#8217;</li>
<p><img class="aligncenter size-full wp-image-1936" title="Terminal" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/41.JPG" alt="Terminal" width="669" height="338" /></p>
<li>Run the JUnit test.</li>
<p><img class="aligncenter size-full wp-image-1940" title="Test Results" src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/61.JPG" alt="Test Results" width="600" height="500" /></p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 6105px; width: 1px; height: 1px; overflow: hidden;"><a href="http://selenium.googlecode.com/files/selenium-server-standalone-2.0b2.jar">selenium-server-standalone-2.0b2.jar</a></div>
<li>This test will open two new Firefox windows to execute the tests.</li>
<p><img src="http://blogs.sourceallies.com/wp-content/uploads/2011/02/71.JPG" alt="Remote Control" title="Remote Control" width="600" height="600" class="aligncenter size-full wp-image-1995" /></p>
<ul>
At first glance Selenium IDE may seem like just a QA tool for recording acceptance tests.  While this may be true, this is a powerful tool for communicating defects, preparing for demos, and generating acceptance tests in your language of choice.  Selenium IDE is a rich testing tool-set worth exploring.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2011/04/selenium-ide-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Would you start mocking me?</title>
		<link>http://blogs.sourceallies.com/2010/08/would-you-start-mocking-me/</link>
		<comments>http://blogs.sourceallies.com/2010/08/would-you-start-mocking-me/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 14:54:39 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[Mockito]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1545</guid>
		<description><![CDATA[ ]]></description>
			<content:encoded><![CDATA[<p>One of the primary principles of <a href="http://en.wikipedia.org/wiki/Unit_testing">unit testing</a> is to test a small piece of functionality in isolation.  In order to achieve this, <a href="http://en.wikipedia.org/wiki/Mock_object">mock objects</a> are often necessary.  Historically using mocks could be quite painful.  After using several mock frameworks, my favorite by far is <a href="http://mockito.org">Mockito</a>.</p>
<h1>Tutorial</h1>
<p>In this tutorial we will walk through examples of the most common features of Mockito. My sample project can be downloaded <a href="http://github.com/kesslerdn/mockito-learning">here</a>.</p>
<h2>Interfaces and Implementation</h2>
<p>Some mocking frameworks only supported mocking interfaces.  As a result our projects became bloated with useless interfaces that were only used for testing.  Mockito creates mock objects with interfaces or classes.<br />
<span id="more-1545"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Person<span style="color: #009900;">&#123;</span>
   <span style="color: #003399;">List</span> getChildren<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">Boolean</span> isCitizen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">Integer</span> getAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">Map</span> getSchedule<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066; font-weight: bold;">void</span> haveChild<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> PersonImpl <span style="color: #000000; font-weight: bold;">implements</span> Person<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> getChildren<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Boolean</span> isCitizen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Map</span> getSchedule<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> haveChild<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">assertNotNull<span style="color: #009900;">&#40;</span>Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertNotNull<span style="color: #009900;">&#40;</span>Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Default Values</h2>
<p>Mockito provides stub implementations for all of the methods on the interface/class.  It also provides several common sense defaults out of the box.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertNotNull<span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, mockPerson.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertFalse<span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">isCitizen</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>, mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertNotNull<span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getSchedule</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mockito returns empty Lists and Maps to prevent NPE&#8217;s in your test code.  It defaults to false for Boolean return values.  By default Zero is returned for Integer, Long and Double.  Notice that even though PersonImpl returns null for all of its methods the Mockito version returns reasonable default values.</p>
<h2>Changing Behavior</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">35</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">35</span><span style="color: #009900;">&#41;</span>, mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mockito.when() takes a mock and a method call that returns a value.  Mockito&#8217;s thenReturn() method takes the return value that Mockito will provide to all matching calls.  By default the method getAge() returns Zero, within Mockito, but now it returns 35.  This ability to control behavior is the key to testing functionality in isolation.</p>
<h2>Verifying Method Calls</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verify</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mockito.verify() takes a mock object and verifies that the following method is called.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verify</span><span style="color: #009900;">&#40;</span>mockPerson, Mockito.<span style="color: #006633;">times</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is the same verify as before except a second parameter is passed to the verify() method telling it how many times the trailing method should be called.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verify</span><span style="color: #009900;">&#40;</span>mockPerson, Mockito.<span style="color: #006633;">atLeastOnce</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Sometimes it is not important how many times a method is called as long as it is called at least once.  Mockito.atLeastOnce() method provides this flexibility.</p>
<h2>No More Interactions</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verify</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verifyNoMoreInteractions</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mockito.verifyNoMoreInteractions() ensures that no other methods have been called on the PersonImpl class.  Since Mockito is very helpful in stubbing out all of the methods for a class or interface there is no control over which methods have been called.  Only methods that have been explicitly verified are checked for specific calls.</p>
<h2>Throw Exception</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test<span style="color: #009900;">&#40;</span>expected <span style="color: #339933;">=</span> <span style="color: #003399;">RuntimeException</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testThenThrow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Mockito.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenThrow</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test exception&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The thenThrow() method prepares the method defined in when() to throw an Exception.  While I am not attempting to provide a tutorial on JUnit4 features, I feel it necessary to point out the Annotation verifies that a RuntimeException is thrown in the test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test<span style="color: #009900;">&#40;</span>expected <span style="color: #339933;">=</span> <span style="color: #003399;">RuntimeException</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testDoThrow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Mockito.<span style="color: #006633;">doThrow</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test exception&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">haveChild</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   mockPerson.<span style="color: #006633;">haveChild</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The doThrow() syntax is necessary to throw an Exception for a void method.  The previous example threw an exception for Person.getAge() which is not a void method.  In this example Person.haveChild() is void and requires this alternate syntax in order to compile.</p>
<h2>TDD with Mockito</h2>
<p>Let&#8217;s drive a small class that we will call Bouncer.  This class will check if a Person is old enough to drink.  First let&#8217;s write a test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testCheckAgeOf_UnderAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Mockito.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Bouncer bouncer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bouncer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   assertFalse<span style="color: #009900;">&#40;</span>bouncer.<span style="color: #006633;">checkAgeOf</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Of coarse this does not even compile.  We need to create a Bouncer class with the checkAgeOf() method.  And here is the amazing class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Bouncer<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> checkAgeOf<span style="color: #009900;">&#40;</span>Person customer<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So why did I return false.  Well the test does not require anything more than this to pass.  TDD is about doing the simplest thing that works and then refactor the code.  This is the simplest thing that works.  Now let&#8217;s write another test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testCheckAgeOf_OfAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Mockito.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">21</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Bouncer bouncer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bouncer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   assertTrue<span style="color: #009900;">&#40;</span>bouncer.<span style="color: #006633;">checkAgeOf</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we need real business logic in our method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">return</span> customer.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">21</span><span style="color: #339933;">;</span></pre></div></div>

<p>Green again.  Now it is time to refactor.  In this simple case I am not sure how to simplify the code.  There are a few more tests that should be written.  A test if getAge() returns null and if Person is null and other boundary cases.  Since the focus of this article is on Mockito I will stop here.</p>
<p>Mockito is a powerful mocking framework that simplifies the creation and usage of mock objects.  I encourage you to download my <a href="http://github.com/kesslerdn/mockito-learning">sample code</a> and continue to explore the Mockito framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/08/would-you-start-mocking-me/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

