<?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>Talk Nerdy To Me - Java, C#, .Net &#187; Software</title>
	<atom:link href="http://www.gavaghan.org/blog/category/technical-articles/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gavaghan.org/blog</link>
	<description>Mike Gavaghan blogs on Java, C#, .Net, and the software industry</description>
	<lastBuildDate>Mon, 22 Feb 2010 22:09:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>TCP/IP Parameter Tuning for Rapid Client Connections</title>
		<link>http://www.gavaghan.org/blog/2010/02/17/tcpip-parameter-tuning-for-rapid-client-connections/</link>
		<comments>http://www.gavaghan.org/blog/2010/02/17/tcpip-parameter-tuning-for-rapid-client-connections/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 19:37:36 +0000</pubDate>
		<dc:creator>Mike Gavaghan</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.gavaghan.org/blog/2010/02/17/tcpip-parameter-tuning-for-rapid-client-connections/</guid>
		<description><![CDATA[Applications that open and close a large number of client TCP/IP sockets run the risk of running out of available socket ports.  This can happen in a load and performance testing scenario using a tool like LISA Test from iTKO, or it could happen in a production environment if an active application simply needs to rapidly open and close a large number of outbound connections.

On the .NET platform, the exception raised reads "System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted <host>:<port>".

In Java, the exception is "java.net.BindException: Address already in use: connect".  

Both exceptions are misleading because they are generally associated with server socket conflicts – not outbound client socket connections.  However, a better understanding of the TCP state machine sheds some light on this behavior - and a solution.]]></description>
			<content:encoded><![CDATA[<p>Applications that open and close a large number of client TCP/IP sockets run the risk of running out of available socket ports.  This can happen in a load and performance testing scenario using a tool like <a target="_blank" href="http://www.itko.com/products/lisatest.jsp" title="LISA Test - QA Software for Composite Enterprise Applications and SOA">LISA Test</a> from <a target="_blank" href="http://www.itko.com/" title="LISA Virtualization and Validation Software for Modern Applications: SOA, Cloud, and BPM">iTKO</a>, or it could happen in a production environment if an active application simply needs to rapidly open and close a large number of outbound connections.</p>
<p>On the <strong>.NET</strong> platform, the exception raised reads &#8220;<code><a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socketexception.aspx" title="SocketException">System.Net.Sockets.SocketException</a>: Only one usage of each socket address (protocol/network address/port) is normally permitted &lt;host&gt;:&lt;port&gt;</code>&#8220;. </p>
<p><strong>In Java</strong>, the exception is &#8220;<code><a target="_blank" href="http://java.sun.com/javase/6/docs/api/java/net/BindException.html" title="Java BindException">java.net.BindException</a>: Address already in use: connect</code>&#8220;. </p>
<p>Both exceptions are misleading because they are generally associated with server socket conflicts – not outbound client socket connections.  However, a better understanding of the <a target="_blank" href="http://www.night-ray.com/TCPIP_State_Transition_Diagram.pdf" title="TCP Finite State Machine">TCP state machine</a> sheds some light on this behavior &#8211; and a solution.</p>
<p><span id="more-69"></span><strong>Common Port Conflict Exceptions</strong></p>
<p>Whenever TCP/IP encounters a port conflict, you can expect one of the two following exceptions to be thrown depending upon your environment:</p>
<p>In a C# environment, you&#8217;ll see this exception:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted &lt;host&gt;:&lt;port&gt;</p></blockquote>
</blockquote>
<p>In Java, you&#8217;ll see this:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>java.net.BindException: Address already in use: connect</p></blockquote>
<blockquote class="codeline1"><p>at java.net.PlainSocketImpl.socketConnect(Native Method)</p></blockquote>
<blockquote class="codeline1"><p>at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)</p></blockquote>
<blockquote class="codeline1"><p>at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)</p></blockquote>
<blockquote class="codeline1"><p>at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)</p></blockquote>
<blockquote class="codeline1"><p>at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)</p></blockquote>
<blockquote class="codeline1"><p>at java.net.Socket.connect(Socket.java:519)</p></blockquote>
<blockquote class="codeline1"><p>at java.net.Socket.connect(Socket.java:469)</p></blockquote>
</blockquote>
<p>If you see these exceptions thrown when a <u>server</u> socket attempts to listen for incoming connections, the cause is obvious: the port you&#8217;re attempting to listen on is already in use.  For example, if you bring up a Web server, but another Web server is already running, an exception will be thrown because port 80 (or 8080) is already listening on behalf of another thread or application.</p>
<p>These exceptions are often confusing, however, when thrown setting up a <u>client</u> connection.  Client TCP connections are always assigned an OS-selected port on the local side, so why is the operating system selecting an active port?  The truth is the exception indicates <u><strong>no</strong></u> local port numbers are available to the client.  This misreporting of the error by the OS is half the confusion.</p>
<p><strong>Tuning Local Client Port Range</strong></p>
<p>The problem is two-fold.  First, Linux and Windows make only a certain number of ports available to client sockets – the default is in the range of 1024 to 5000.  Hence, you may have only 3,976 active client connections at a time.  For most systems, this is plenty.  However, in specific circumstances on systems requiring a large number of outbound connections, this limit can be exhausted.</p>
<p>This range, however, can be tuned.  On Windows,  the upper bound for client port assignments can be adjusted using the <a target="_blank" href="http://technet.microsoft.com/en-us/library/cc758002%28WS.10%29.aspx" title="MaxUserPort"><code>MaxUserPort</code></a> <code>DWORD</code> value on this registry key:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>HKLM\System\CurrentControlSet\Services\Tcpip\Parameters</p></blockquote>
</blockquote>
<p>Of course, rather than using the cumbersome <code>regedit</code>, you can <a target="_blank" href="http://www.gavaghan.org/blog/free-source-code/ip-tuner/" onclick="javascript:urchinTracker('/outgoing/free_source_code_iptuner');" title="Gavaghan TCP/IP Parameter Tuning for Windows">download IPTuner for free</a> to quickly optimize your Windows IP stack</p>
<p>On Linux, both the lower and upper bounds can be set using the following parameter:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>net.ipv4.ip_local_port_range = 32768 65536</p></blockquote>
</blockquote>
<p>How this parameter is set in Linux varies depending upon the flavor and version of Linux you&#8217;re using – and you&#8217;ll need to restart networking after you change it.</p>
<p><strong>Tuning TCP TIME_WAIT Timeout Value</strong></p>
<p>The second cause of these exceptions has to do with the <a target="_blank" href="http://www.night-ray.com/TCPIP_State_Transition_Diagram.pdf" title="TCP State Model">TCP state model</a> and the way sockets are closed.  Even after a socket has officially been &#8220;closed&#8221;, it hangs around in a <code>TIME_WAIT</code> state as a safety mechanism to deal with stray packets.  The default wait time on all operating systems, generally, is ridiculously long (240 seconds on Windows).  So, even if an application doesn’t require a lot of concurrent connections, it can still run out of available ports in a high load situation.  If even one connection is repeatedly opened and closed fast enough, you&#8217;ll soon have all available local sockets hanging around in a <code>TIME_WAIT</code> state and none available for new clients.</p>
<p>The <code>TIME_WAIT</code> state duration, however, is also tunable.</p>
<p>On Windows, using the same registry key, the <code><a target="_blank" href="http://technet.microsoft.com/en-us/library/cc938217.aspx" title="TcpTimedWaitDelay">TCPTimedWaitDelay</a></code> value can be used to adjust the <code>TIME_WAIT</code> duration from 30 to 300 seconds.  Of course, rather than using the cumbersome <code>regedit</code>, you can <a target="_blank" href="http://www.gavaghan.org/blog/free-source-code/ip-tuner/" onclick="javascript:urchinTracker('/outgoing/free_source_code_iptuner');" title="Gavaghan TCP/IP Parameter Tuning for Windows">download IPTuner for free</a> to quickly optimize your Windows IP stack.</p>
<p>On Linux, the wait delay is configured using the following parameter:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>net.ipv4.tcp_fin_timeout = 30</p></blockquote>
</blockquote>
<p>By decreasing the TCP wait delay, closed sockets spend less time in the <code>TIME_WAIT</code> state and get returned to the pool of available client ports faster. However, to avoid communication problems, do not lower this value below 30 seconds.</p>
<p><strong>Administrator Privileges Required</strong></p>
<p>On both Linux and Windows (even if using <strong>IP tuner</strong> or <code>regedit</code>), you&#8217;ll require administrator privileges to change these parameters.  However, anyone can view these settings to at least verify if they make sense.</p>
<ul class="download">
<li><strong><a href="http://www.gavaghan.org/blog/free-source-code/ip-tuner/" onclick="javascript:urchinTracker('/outgoing/free_source_code_iptuner');" title="Gavaghan TCP/IP Parameter Tuner for Windows">Download <strong>IPTuner</strong> for Windows here.</a></strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.gavaghan.org/blog/2010/02/17/tcpip-parameter-tuning-for-rapid-client-connections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The .NET Asynchronous I/O Design Pattern</title>
		<link>http://www.gavaghan.org/blog/2010/02/11/the-net-asynchronous-io-design-pattern/</link>
		<comments>http://www.gavaghan.org/blog/2010/02/11/the-net-asynchronous-io-design-pattern/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 19:08:49 +0000</pubDate>
		<dc:creator>Mike Gavaghan</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.gavaghan.org/blog/2010/02/11/the-net-asynchronous-io-design-pattern/</guid>
		<description><![CDATA[Asynchronous operations allow a program to perform time consuming tasks on a background thread while the main application continues to execute.  For example, consider when a program makes a request to a remote system.  In a single-threaded scenario, the call is made and the CPU goes idle as the caller waits on the server's processing time and the network latency.  If this waiting time can be delegated to a separate thread of execution, the program can complete other tasks until it receives notification the background work is complete.

However, managing multiple threads and cross-thread communication adds complexity to your code.  Fortunately, the .NET Framework has a useful design pattern applied to its I/O classes which easily enables asynchronous calls.  Let's take a look at an example.]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://en.wikipedia.org/wiki/Asynchronous_I/O" title="Asynchronous I/O">Asynchronous operations</a> allow a program to perform time consuming tasks on a background thread while the main application continues to execute.  For example, consider when a program makes a request to a remote system.  In a single-threaded scenario, the call is made and the CPU goes idle as the caller waits on the server&#8217;s processing time and the network latency.  If this waiting time can be delegated to a separate thread of execution, the program can complete other tasks until it receives notification the background work is complete.</p>
<p>However, managing multiple threads and cross-thread communication adds complexity to your code.  Fortunately, the .NET Framework has a useful design pattern applied to its I/O classes which easily enables asynchronous calls.  Let&#8217;s take a look at an example.</p>
<p><span id="more-68"></span><strong>Async I/O for a DNS lookup</strong> </p>
<p>Suppose you need to lookup the IP address of a host.  The simplest way to do this is to use the <code><a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.net.dns.aspx" title="System.Net.Dns">System.Net.Dns</a></code> class:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>IPAddress[] hostAddresses = Dns.GetHostAddresses(&#8220;www.gavaghan.org&#8221;);</p></blockquote>
</blockquote>
<p>A DNS lookup doesn&#8217;t take terribly long and, in most cases, the synchronous example above is fine.  DNS servers are highly efficient, and local DNS servers will cache authoritative data to optimize response times.</p>
<p>However, suppose you&#8217;re implementing a high performance mail server that detects spam by querying multiple <a target="_blank" href="http://www.gavaghan.org/blog/2008/06/11/kill-spam-with-real-time-dns-blacklists/" title="DNS Blacklist - DNSBL">real time DNS blacklists</a>.  For every incoming message, you must execute a dozen DNS operations:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>IPAddress[] spamcop = Dns.GetHostAddresses(&#8220;22.154.199.213.bl.spamcop.net&#8221;);</p></blockquote>
<blockquote class="codeline0"><p>IPAddress[] spamhaus = Dns.GetHostAddresses(&#8220;22.154.199.213.pbl.spamhaus.org&#8221;);</p></blockquote>
<blockquote class="codeline0"><p>IPAddress[] fiveten =</p></blockquote>
<blockquote class="codeline5"><p>Dns.GetHostAddresses(&#8220;22.154.199.213.blackholes.five-ten-sg.com&#8221;);</p></blockquote>
<blockquote class="codeline0"><p>//</p></blockquote>
<blockquote class="codeline0"><p>// . . . and a dozen others</p></blockquote>
<blockquote class="codeline0"><p>//</p></blockquote>
</blockquote>
<p>Each synchronous lookup blocks waiting for a response before moving on to the next lookup.  The cumulative effect of these delays will get costly.  Ideally, you&#8217;d want to perform each lookup on its own thread and let the requests run concurrently.</p>
<p>So, let&#8217;s implement a method that looks like this:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public class AsyncDNSExample</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline2"><p>public List&lt;IPAddress&gt; MultiHostLookup(List&lt;string&gt; hosts)</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
</blockquote>
<p>This method will accept a list of host names, execute concurrent DNS lookups for all of them, and return with a list of resolved addresses.  Here&#8217;s how we might use this method:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>class Program</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline2"><p>static void Main()</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline4"><p>// build list of hosts to lookup</p></blockquote>
<blockquote class="codeline4"><p>List&lt;string&gt; hosts = new List&lt;string&gt;();</p></blockquote>
<blockquote class="codeline4"><p>hosts.Add(&#8220;www.gavaghan.org&#8221;);</p></blockquote>
<blockquote class="codeline4"><p>hosts.Add(&#8220;www.itko.com&#8221;);</p></blockquote>
<blockquote class="codeline4"><p>hosts.Add(&#8220;sombrita.com&#8221;);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline4"><p>// perform the concurrent lookup</p></blockquote>
<blockquote class="codeline4"><p>AsyncDNSExample lookup = new AsyncDNSExample();</p></blockquote>
<blockquote class="codeline4"><p>List&lt;IPAddress&gt; addressList = lookup.MultiHostLookup(hosts);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline4"><p>// write out the results</p></blockquote>
<blockquote class="codeline4"><p>foreach (IPAddress address in addressList)</p></blockquote>
<blockquote class="codeline4"><p>{</p></blockquote>
<blockquote class="codeline6"><p>Console.WriteLine(address);</p></blockquote>
<blockquote class="codeline4"><p>}</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p><strong>Begin and End methods</strong></p>
<p>Many of .NET&#8217;s I/O classes have asynchronous versions of their synchronous methods.  For example, the <code>Read()</code> and <code>Write()</code> methods on <code><a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.io.stream.aspx" title="System.IO.Strean">System.IO.Stream</a></code> have respective <code>BeginRead()</code> and <code>BeginWrite()</code> counterparts.  <code><a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx" title="System.Net.Sockets.Socket">System.Net.Sockets.Socket</a></code> has <code>BeginAccept()</code> and <code>BeginConnect()</code>.  And, in benefit of this example, <code>Dns</code> has <code>BeginGetHostAddresses()</code>.</p>
<p>All of the <code>Begin*</code> methods cause the object&#8217;s work to execute on a worker thread in the .NET thread pool.  These methods take the same parameters as their synchronous counterparts plus two additional parameters supporting the async framework.</p>
<p>For example, here&#8217;s the signature for <code>BeginGetHostAddresses()</code>:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public static IAsyncResult BeginGetHostAddresses(</p></blockquote>
<blockquote class="codeline5"><p>string hostNameOrAddress,</p></blockquote>
<blockquote class="codeline5"><p>AsyncCallback requestCallback,</p></blockquote>
<blockquote class="codeline5"><p>Object state</p></blockquote>
<blockquote class="codeline0"><p>)</p></blockquote>
</blockquote>
<p>One added parameter is an <code><a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.asynccallback.aspx" title="AsyncCallback">AsyncCallback</a></code> delegate.  The delegate identifies the callback method .NET will invoke once asynchronous processing has completed.  The callback method takes a single parameter of type <code><a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.iasyncresult.aspx" title="IAsyncResult">IAsyncResult</a></code>.  The <code>IAsyncResult</code> object must be used to access the result of the asynchronous call.</p>
<p>The second added parameter is an arbitrary state object (possibly null) that may be used to coordinate between the caller and the callback.  The state object is made available to the callback method through the <code>IAsyncResult</code> parameter.  An example is included a little later.</p>
<p>For each <code>Begin*</code> call, a corresponding <code>End*</code> call must be invoked to get the results of the method.  <code>End*</code> methods block synchronously until processing has been completed.  However, when called from within the callback method, <code>End*</code> methods return immediately because, at that point, the work is known to be done.</p>
<p>Let&#8217;s take a look at how our <code>MultiHostLookup()</code> method can be implemented using the asynchronous version of <code>GetHostAddresses()</code>:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public class AsyncDNSExample</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline2"><p>public List&lt;IPAddress&gt; MultiHostLookup(List&lt;string&gt; hosts)</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline4"><p>// we&#8217;ll fill this list with the result of the DNS lookups</p></blockquote>
<blockquote class="codeline4"><p>List&lt;IPAddress&gt; addressList = new List&lt;IPAddress&gt;();</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline4"><p>foreach (string host in hosts)</p></blockquote>
<blockquote class="codeline4"><p>{</p></blockquote>
<blockquote class="codeline6"><p>// begin an asynchronous lookup for each host</p></blockquote>
<blockquote class="codeline6"><p>Dns.BeginGetHostAddresses(</p></blockquote>
<blockquote class="codeline8"><p>host,</p></blockquote>
<blockquote class="codeline8"><p>GetHostAddressesCallback,</p></blockquote>
<blockquote class="codeline8"><p>addressList</p></blockquote>
<blockquote class="codeline6"><p>);</p></blockquote>
<blockquote class="codeline4"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline4"><p>//</p></blockquote>
<blockquote class="codeline4"><p>// we can do additional work here while the</p></blockquote>
<blockquote class="codeline4"><p>// DNS lookups continue in parallel</p></blockquote>
<blockquote class="codeline4"><p>//</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline4"><p>lock (addressList)</p></blockquote>
<blockquote class="codeline4"><p>{</p></blockquote>
<blockquote class="codeline6"><p>// ensure all lookups have returned, otherwise wait</p></blockquote>
<blockquote class="codeline6"><p>while (addressList.Count != hosts.Count)</p></blockquote>
<blockquote class="codeline6"><p>{</p></blockquote>
<blockquote class="codeline8"><p>Monitor.Wait(addressList);</p></blockquote>
<blockquote class="codeline6"><p>}</p></blockquote>
<blockquote class="codeline4"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline4"><p>return addressList;</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>This method begins by allocating the <code>List&lt;IPAddress&gt;</code> object we&#8217;ll use to return our resolved addresses.  Then, we loop over each of the host names in the hosts <code>List&lt;string&gt;</code> and call <code>BeginGetHostAddresses()</code>.  Once this loop completes, all of the DNS queries are executing in parallel.</p>
<p>Notice the first parameter to <code>BeginGetHostAddresses()</code> is a host name &#8211; just like its synchronous counterpart.  For the second parameter, we pass a reference to our callback method, <code>GetHostAddressesCallback()</code>, which is defined below.  The third parameter is our result <code>List&lt;IPAddress&gt;</code>.  This will make the <code>List</code> available to the callback method.  When each DNS query completes, the callback method can update the list with each resolved address.</p>
<p>At this point, if we wanted to, we could code other logic to execute as we wait for the queries to complete.</p>
<p>Finally, we check the length of the list of resolved IP addresses to see if it&#8217;s the same length as the list of host names.  To do this, we must first lock the address list object (after all, we don&#8217;t want our address list modified on a callback thread at the same time we&#8217;re trying to inspect it).  If the list sizes are equal, we know all lookups have completed and we can return from the method.  Otherwise, we release the lock on the address list and block until receiving notification from the callback thread.</p>
<p>It&#8217;s all pretty simple.  Now, let&#8217;s see how to implement the callback method:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public class AsyncDNSExample</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline2"><p>private void GetHostAddressesCallback(IAsyncResult result)</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline4"><p>// This method may fail with a SocketException, particularly</p></blockquote>
<blockquote class="codeline4"><p>// if the host is not found. A more robust solution would</p></blockquote>
<blockquote class="codeline4"><p>// handle such cases.</p></blockquote>
<blockquote class="codeline4"><p>IPAddress[] addresses = Dns.EndGetHostAddresses(result);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline4"><p>// for simplicity, we&#8217;ll take the first address</p></blockquote>
<blockquote class="codeline4"><p>IPAddress address = addresses[0];</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline4"><p>// the address list we passed in is accessbile from AsyncState</p></blockquote>
<blockquote class="codeline4"><p>List&lt;IPAddress&gt; addressList = (List&lt;IPAddress&gt;)result.AsyncState;</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline4"><p>// we need to ensure updates to the address list are threadsafe</p></blockquote>
<blockquote class="codeline4"><p>lock (addressList)</p></blockquote>
<blockquote class="codeline4"><p>{</p></blockquote>
<blockquote class="codeline6"><p>addressList.Add(address);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline6"><p>// notify listeners that another address has been added</p></blockquote>
<blockquote class="codeline6"><p>Monitor.PulseAll(addressList);</p></blockquote>
<blockquote class="codeline4"><p>}</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline2"><p>// this is our public method for performing multiple, concurrent</p></blockquote>
<blockquote class="codeline2"><p>// DNS requests</p></blockquote>
<blockquote class="codeline2"><p>public List&lt;IPAddress&gt; MultiHostLookup(List&lt;string&gt; hosts)</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline4"><p>. . . .</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>The first thing that happens is the call to <code>EndGetHostAddresses()</code> with the <code>IAsyncResult</code> object passed in.  This call returns immediately with the result of the DNS query (it returns an array of IP addresses but, for simplicity, we&#8217;ll assume the first one is all we need).</p>
<p>Next, we get a reference to the <code>List&lt;IPAddress&gt;</code> object passed in by the <code>Begin*</code> call. This is where we&#8217;re going to save our resolved IP addresses.  However, for thread safety, we can only add our result from within a lock block.  We don&#8217;t want to be manipulating the list at the same time as another callback!</p>
<p>Finally, we pulse all threads listening on the result object.  This schedules the thread executing <code>MultiHostLookup()</code> to check if all of the results have been received.</p>
<p><strong>Conclusion</strong></p>
<p>Using asynchronous I/O can make your applications faster and your user interfaces more responsive &#8211; particularly when executing long running tasks and tasks that would otherwise leave the CPU idle. However, even with the .NET design pattern, multithreaded programming always adds to code complexity. So, only leverage this framework where performance optimization is required.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavaghan.org/blog/2010/02/11/the-net-asynchronous-io-design-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Understanding SSL – Part 1: Certificates and Keys</title>
		<link>http://www.gavaghan.org/blog/2009/10/14/understanding-ssl-%e2%80%93-part-1-certificates-and-keys/</link>
		<comments>http://www.gavaghan.org/blog/2009/10/14/understanding-ssl-%e2%80%93-part-1-certificates-and-keys/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 01:35:00 +0000</pubDate>
		<dc:creator>Mike Gavaghan</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.gavaghan.org/blog/2009/10/14/understanding-ssl-%e2%80%93-part-1-certificates-and-keys/</guid>
		<description><![CDATA[The technology behind Secure Sockets Layer (SSL) network connections is often perceived as a bit of &#8220;black magic&#8221; – smoke and mirrors securing our Internet connections from snooping.  When banking and shopping online, even a novice user understands their browser sets up an HTTPS connection (which is simply HTTP over SSL) to protect the transaction.  [...]]]></description>
			<content:encoded><![CDATA[<p>The technology behind <a target="_blank" href="http://en.wikipedia.org/wiki/Transport_Layer_Security" title="Transport Layer Security">Secure Sockets Layer (SSL)</a> network connections is often perceived as a bit of &#8220;black magic&#8221; – smoke and mirrors securing our Internet connections from snooping.  When banking and shopping online, even a novice user understands their browser sets up an <a target="_blank" href="http://en.wikipedia.org/wiki/Https" title="HTTP Secure">HTTPS</a> connection (which is simply HTTP over SSL) to protect the transaction.  It’s easy to simply surf to a secure URL and know that, somehow, SSL is magically keeping you safe.</p>
<p>Developing software that <em>uses </em>SSL is an entirely different matter.  The simplicity quickly fades, and the developer must confront the complexities of certificate management, trust stores, handshaking, and a host of other details that must be perfectly aligned to make the secure communication work.  In Part 1, we’ll cover a very high level of SSL concepts.  In subsequent posts, we’ll take a deeper dive into making these connections happen in both Java and C#.</p>
<p><span id="more-59"></span><strong>Understanding SSL</strong></p>
<p>SSL uses <a target="_blank" href="http://en.wikipedia.org/wiki/Public_key" title="Public-key cryptography">public key/private key cryptography</a> for three purposes. The most fundamental use is to encrypt data communication between the server and client.  However, it is also used to allow the server to prove its identity to the client and prevent <a target="_blank" href="http://en.wikipedia.org/wiki/Man-in-the-middle_attack" title="Man-in-the-middle attack">man-in-the-middle attacks</a> (where a malicious intermediary intercepts messages from the client and masquerades as the intended server).</p>
<p>A third use is for allowing the client to prove its identity to the server.  <a target="_blank" href="http://en.wikipedia.org/wiki/Mutual_authentication" title="Mutual Authentication">Mutual authentication</a> is an important and powerful feature of SSL, and it’s probably underused.  For now, we’ll just focus on the semantics of server authentication.  If you understand server authentication, you’ll be well on your way to understanding client authentication on your own.</p>
<p><strong>About Certificates</strong></p>
<p>Three fundamental components are involved in setting up an SSL connection between a server and client: a certificate, a public key, and a private key.</p>
<p><a target="_blank" href="http://en.wikipedia.org/wiki/Digital_certificate" title="Digital Certificate">Digital certificates</a> are used to identify an entity.  The entity could be a person (when used for secure email), or it could be a computer (when used for SSL).  There is quite a bit of information stored in a digital certificate, but the most important part is the name of the entity it is identifying.</p>
<p>The identity, also known as the “subject”, is specified as an <a target="_blank" href="http://en.wikipedia.org/wiki/Distinguished_Name" title="Distinguished Name">X.509 distinguished name</a>.  A distinguished name contains multiple components.  For example, the distinguished name on the certificate used to setup an HTTPS connection for Amazon.com’s shopping cart check-out looks like this:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>CN=www.amazon.com,</p></blockquote>
<blockquote class="codeline0"><p>O=Amazon.com Inc.,</p></blockquote>
<blockquote class="codeline0"><p>L=Seattle,</p></blockquote>
<blockquote class="codeline0"><p>S=Washington,</p></blockquote>
<blockquote class="codeline0"><p>C=US</p></blockquote>
</blockquote>
<p>How do we find this information?  From Internet Explorer, browse to any secure web page (one with an <code>https://</code> protocol in the URL).  Right-click on the page and select &#8220;Properties&#8221;.  From the properties page, click the &#8220;Certificates&#8221; button.  On the &#8220;Details&#8221; tab, we can see all of the information embedded in the certificate.  By selecting the &#8220;Subject&#8221; entry, we can see the entity the certificate identifies:</p>
<p><img src="http://www.gavaghan.org/blog/wp-content/uploads/2009/10/iecertificateview1.gif" alt="Certificate Viewer in Internet Explorer" /></p>
<p>For the purposes of establishing an SSL connection to a server, the only interesting part of the distinguished name is the “Common Name” specified by the “CN” component.  This is the name the server uses to identify the domain name of the host.</p>
<p>To establish a secure connection to Amazon.com, a client first resolves the domain name <code>www.amazon.com</code>.  After the SSL connection has been initiated, one of the first things the server will do is send its digital certificate.  The client will perform a number of validation steps before determining if it will continue with the connection.</p>
<p>Most importantly, the client will compare the domain name of the server it intended to connect to (in this case, <code>www.amazon.com</code>) with the common name (the “CN” field) found in the subject’s identity on the certificate.  If these names do not match, it means the client does not trust the identity of the server (and the client will likely choose to terminate the connection).</p>
<p>Although the server name may be correct, the client must still verify the integrity of the certificate to determine if it has been forged or tampered with.  The client does this by verifying the digital signature on the certificate.  We’ll talk more about digital signatures in a moment.</p>
<p>The client will also ensure the certificate is being used within a valid time frame.  All certificates contain an “issue date” and an “expiration date”.  A certificate is considered invalid outside of that date range.</p>
<p><strong>Public and Private Keys</strong></p>
<p>Public keys and private keys are number pairs with a special relationship.  Any data encrypted with one key can be decrypted with the other.  This is known as <a target="_blank" href="http://en.wikipedia.org/wiki/Asymmetric_encryption" title="Asymmetric encryption">asymmetric encryption</a>.  The security of asymmetric encryption lies in the difficulty of cracking encrypted data even when the key used for encryption is known.</p>
<p>The server’s public key is embedded within its certificate.  The public key is freely distributed so anyone wishing to establish an encrypted channel with the server may encrypt their data using the server’s public key.  The server will decrypt this message using its private key.  For this reason, private keys are closely guarded and kept secure.</p>
<p><strong>Digital Signatures</strong></p>
<p>Just as data may be encrypted with a public key and decrypted with a private key, the reverse is also true.  Data encrypted with a private key may be decrypted with the corresponding public key.</p>
<p>This property of keys is used to ensure the integrity of a digital certificate in a process called <a target="_blank" href="http://en.wikipedia.org/wiki/Digital_signature" title="Digital signature">digital signing</a>.</p>
<p>A <a target="_blank" href="http://en.wikipedia.org/wiki/Hashing_algorithm" title="Hashing algorithm">hashing algorithm</a> (such as <a target="_blank" href="http://en.wikipedia.org/wiki/Sha1" title="SHA1 Hashing">SHA1</a> or <a target="_blank" href="http://en.wikipedia.org/wiki/MD5" title="MD5 hashing">MD5</a>) is a means of processing all of the bytes of a message and producing a numeric “hash value”.  Hash values have long been used to ensure the integrity of messages that may become corrupted during transport.  A sender will transmit a message followed by the hash value it calculated for the message it intends to send.  The receiver calculates the hash value for the message it receives.  If the receiver calculates a different hash value than the one that was sent, the receiver concludes the message was corrupted during transit (and generally asks the sender to resend).</p>
<p>A hashing algorithm may also be used to determine if a message has been forged or tampered with.  Complicating this, however, is that a malicious third party could intercept a message, modify it, and simply recalculate the hash.  Asymmetric encryption technology solves this.</p>
<p>If a message sender wants to convince a recipient that his message is authentic and has not been tampered with, he will do two things.  First, the sender will calculate a hash value for the message.  This hash value will then be encrypted using the sender’s private key (a key which the sender, and only the sender, knows).  When the client receives the message, the client decrypts the hash value using the sender’s public key.  If the message has been tampered with, or if the message has been signed with anything other than the sender’s private key, the hash values will not agree and the client will not consider the message authentic.</p>
<p><strong>Certificate Signing</strong></p>
<p>When a certificate is created, it is digitally signed.  The digital signature is used to verify the authenticity of the certificate.  In an SSL connection, the client will attempt to verify the signature on the certificate presented by the server before deciding to continue establishing the connection.</p>
<p><strong>Self-signed certificates</strong></p>
<p>The simplest certificate is a <a target="_blank" href="http://en.wikipedia.org/wiki/Self-signed_certificate" title="Self-signed Certificates">self-signed certificate</a>.  The signature on a self-signed certificate is calculated using the same private key associated with the public key found on the certificate.  In a software development environment, self-signed certificates are an easy way to build testing environments that establish SSL connections without having to deal with the time and expense of obtaining a certificate through an establish certificate authority.</p>
<p><strong>Certificate Authority signed certificates</strong></p>
<p>Certificates may also be signed by a <a target="_blank" href="http://en.wikipedia.org/wiki/Certificate_authority" title="Certificate authority">certificate authority (CA)</a>.  A CA is a trusted third party that digitally signs certificates for entities that have gone through an established vetting process.  The CA, itself, also has a certificate that can be analyzed for authenticity – and the CA’s certificate might also be signed by yet another trusted third party (in this case, the CA is known as an intermediate CA).  All of these certificates, together, form a certificate chain.  At the top of the chain is a certificate authority called a <a target="_blank" href="http://en.wikipedia.org/wiki/Root_certificate" title="Root Certificate">Root CA</a> that uses a self-signed certificate.</p>
<p>Returning to the Amazon.com example, we can examine the server certificate and see that it is not a self signed certificate – it is signed by a CA with a distinguished named of:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>CN=VeriSign Class 3 Secure Server CA &#8211; G2,</p></blockquote>
<blockquote class="codeline0"><p>OU=Terms of use at https://www.verisign.com/rpa (c)09,</p></blockquote>
<blockquote class="codeline0"><p>OU=VeriSign Trust Network,</p></blockquote>
<blockquote class="codeline0"><p>O=VeriSign, Inc.,</p></blockquote>
<blockquote class="codeline0"><p>C=US</p></blockquote>
</blockquote>
<p>From the certificate details tab we looked at earlier, we can find the name of the certificate issuer by selecting the &#8220;Issuer&#8221; entry:</p>
<p><img src="http://www.gavaghan.org/blog/wp-content/uploads/2009/10/iecertificateview2.gif" alt="Certificate Viewer in Internet Explorer" /></p>
<p>VeriSign is a trusted third party that issues certificates for, among other things, eCommerce applications.</p>
<p>Why is it important to have a certificate signed by a trusted third party?  It’s important because new HTTPS-based Web applications are being deployed all of the time.  In terms of browser-based, retail eCommerce applications, it’s simply impractical for users to manage a list of all of the server certificates they have decided to &#8220;trust&#8221;.  Furthermore, reliably and securely obtaining a web site&#8217;s real server certificate would be too problematic.</p>
<p>Consider a new Amazon.com customer.  When the shopping cart checkout sends Amazon.com’s certificate identifying itself as <code>www.amazon.com</code>, how does the customer know to trust the certificate?  Although the certificate may have a valid name and signature on it, how does the customer decide to trust the certificate?  If it is self-signed, it could have been signed by <em>anyone</em> – including a malicious man-in-the-middle.  What the customer needs is a reliable means of receiving Amazon’s certificate that is protected from forgery.</p>
<p>To do this, Amazon chose not to use a self-signed certificate.  Instead, for a fee, it requested that VeriSign sign its online shopping certificate.  When the customer receives Amazon.com’s certificate, she says “I trust that I have a legitimate copy of VeriSign’s CA certificate, I trust VeriSign to only sign certificates of the real domain name owners, and I can see that Amazon.com’s certificate is signed by VeriSign.  Therefore, I believe the server responding at <code>www.amazon.com</code> is truly owned and managed by the same entity owning the <code>www.amazon.com</code> domain name.”</p>
<p><strong>Where Do Trusted CA’s Come From?</strong></p>
<p>Why does the customer believe she has a legitimate copy of VeriSign’s CA certificate?  She believes this because a set of &#8220;trusted&#8221; CAs came pre-installed with her Web browser software.  Internet Explorer, Firefox, and all other leading browser vendors pre-configure their browsers to trust well known CAs such as <a target="_blank" href="http://www.verisign.com/" title="Verisign">VeriSign</a>, <a target="_blank" href="http://www.thawte.com/" title="Thawte">Thawte</a>, and <a target="_blank" href="http://www.networksolutions.com/" title="Network Solutions">Network Solutions</a>.</p>
<p>C# programs will generally access the Windows <a target="_blank" href="http://en.wikipedia.org/wiki/Cryptographic_Service_Provider" title="Windows Cryptographic Service Provider">Cryptographic Service Provider</a> for trusted certificates.  The CSP is a shared OS resource usable by any program, and is also the same trust store consulted by Internet Explorer.</p>
<p>Similarly, the Java Runtime Environment comes with a pre-configure set of trusted certificate authorities.  The collection of trusted certificates can be found at <code>[JRE_HOME]/lib/security/cacerts</code>.  The <a target="_blank" href="http://java.sun.com/javase/6/docs/technotes/tools/windows/keytool.html" title="Java keytool"><code>keytool</code></a>, a command line utility found in the SDK, can be used to inspect and manipulate this file.  The default password for the <code>cacerts</code> keystore is “<code>changeit</code>”.</p>
<p><strong>Summary</strong></p>
<p>Public key cryptography is at the heart of SSL.  While most developers are aware this technology is used to encrypt a data channel, most are unfamiliar with its use of digital signing for identity authentication and message validation.  It is the lack of understanding of these other uses that generally stymie their efforts to implement SSL.</p>
<p>Stayed tuned for more posts on the lower level details of implementing SSL technology,</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavaghan.org/blog/2009/10/14/understanding-ssl-%e2%80%93-part-1-certificates-and-keys/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Kill Spam With Real-Time DNS Blacklists</title>
		<link>http://www.gavaghan.org/blog/2008/06/11/kill-spam-with-real-time-dns-blacklists/</link>
		<comments>http://www.gavaghan.org/blog/2008/06/11/kill-spam-with-real-time-dns-blacklists/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 19:27:43 +0000</pubDate>
		<dc:creator>Mike Gavaghan</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.gavaghan.org/blog/2008/06/11/kill-spam-with-real-time-dns-blacklists/</guid>
		<description><![CDATA[A great Open Source project for gaining understanding about e-mail systems, including an in-depth look at SMTP and POP3, is the Java-based Apache JAMES Project.  Although JAMES has the unfortunate shortcoming of being built around the now defunct and unsupported Apache Avalon Framework, it’s still a fantastic learning tool for understanding email protocols, mail delivery, and spam filtering.  Not only that, it’s a fully functional, enterprise-ready mail server that can be up and running with minimal configuration.

One technology implemented by JAMES for spam filtering is real-time DNS blacklists.  DNSBLs identify the IP addresses of potential spam sources or machines known to be delivering spam (as determined by the sometimes controversial policies of the list owner).  Spam blacklists date back to 1996 with Paul Vixie’s Mail Abuse Prevention System, and are now used by ISPs and corporate mail systems around the world.  Countless organizations maintain blacklists, and Web sites like MX Toolbox permit ad hoc queries of IP addresses against dozens of published lists.]]></description>
			<content:encoded><![CDATA[<p>A great Open Source project for gaining understanding about e-mail systems, including an in-depth look at SMTP and POP3, is the Java-based <a target="_blank" href="http://james.apache.org/" title="Apache JAMES">Apache JAMES Project</a>.  Although JAMES has the unfortunate shortcoming of being built around the now defunct and unsupported <a target="_blank" href="http://avalon.apache.org/closed.html" title="Apache Avalon Project">Apache Avalon Framework</a>, it’s still a fantastic learning tool for understanding email protocols, mail delivery, and spam filtering.  Not only that, it’s a fully functional, enterprise-ready mail server that can be up and running with minimal configuration.</p>
<p>One technology implemented by JAMES for spam filtering is real-time DNS blacklists.  DNSBLs identify the IP addresses of potential spam sources and machines known to be delivering spam (as determined by the sometimes controversial policies of the list owner).  Spam blacklists date back to 1996 with Paul Vixie’s <a target="_blank" href="http://en.wikipedia.org/wiki/Mail_Abuse_Prevention_System" title="Mail Abuse Prevention System">Mail Abuse Prevention System</a>, and are now used by ISPs and corporate mail systems around the world.  Countless organizations maintain blacklists, and Web sites like <a target="_blank" href="http://www.mxtoolbox.com/" title="MX Toolbox">MX Toolbox</a> permit ad hoc queries of IP addresses against dozens of published lists.</p>
<p><span id="more-58"></span><strong>How It Works</strong></p>
<p>Built around the UDP-based DNS protocol, a DNSBL is an efficient and lightweight mechanism for checking the IP addresses of incoming messages against a list of email senders a mail server may wish to avoid.  This is much like the concept of today’s <a target="_blank" href="http://en.wikipedia.org/wiki/Service_oriented_architecture" title="Service Oriented Architecture">Service Oriented Architectures</a> – providing an uncoupled, standards-based interface consumed by arbitrary clients – except years ahead of its time when created.</p>
<p>Querying an IP address is as simple as reversing the octets of the address and appending the domain name of the list publisher.  Perform a DNS lookup of the <nobr>&#8220;A record&#8221;</nobr> for this string.  If a record is returned, the IP address is on the blacklist (some DNSBLs also return the reason for the listing in the TXT record).  If no record is found, the address isn’t listed.</p>
<p>Java, C#, and most other high level programming languages provide a means for performing a DNS lookup.  A simple way to try it out, however, is from a DOS prompt.  Suppose you want to check out the IP address <code>213.199.154.22</code> using the list maintained by <a target="_blank" href="http://www.five-ten-sg.com/" title="510 Software Group">510 Software Group</a> at <code>blackholes.five-ten-sg.com</code>.  You could use the <code>nslookup</code> command like this:</p>
<blockquote><p><code>nslookup 22.154.199.213.blackholes.five-ten-sg.com</code></p></blockquote>
<p>This should reply with “Non-existent domain”.  In other words, the address is “clean”.<br />
To lookup the same address on the <a target="_blank" href="http://www.spamcop.net/" title="SpamCop">SpamCop</a> blacklist at <code>bl.spamcop.net</code>, you would use this command:</p>
<blockquote><p><code>nslookup 22.154.199.213.bl.spamcop.net</code></p></blockquote>
<p>SpamCop also has a mechanism for simulating a positive response.  Technically, <code>127.0.0.2</code> is a local address.  But, SpamCop will provided a record for it:</p>
<blockquote><p><code>nslookup 2.0.0.127.bl.spamcop.net</code></p></blockquote>
<p>Try it out.  You’ll get back a valid “A record” indicating the address is listed – simulating the response you’d get for a blacklisted host.</p>
<p><strong>How Addresses Get Listed</strong></p>
<p>IP addresses get added to blacklists based on the policies selected by the list owners.  This is important to understand before blindly adding all available blacklists to your mail server.  Some lists are more aggressive than others, and the more aggressive a policy is the more likely you are to have legitimate email filtered out of your inbox.</p>
<p>Some spammers are identified by trusted sources forwarding spam messages to list managers.  The mail headers will identify the IP address of the sender.  Other spammers are identified when list owners plant <a target="_blank" href="http://en.wikipedia.org/wiki/Honeypot_%28computing%29" title="Honeypot (computing)">honeypots</a> – bogus email addresses posted online in order to identify spammers harvesting email address off of Web pages.</p>
<p>Some blacklists contain the IP addresses allocated to residential Internet subscribers regardless of whether they’ve been definitively identified as a spam source.  The rationale is that residential Internet users will use their ISP’s mail server to send and receive email.  Any email coming directly from a subscriber’s computer is either a deliberate spam campaign attempting to circumvent the ISP’s safeguards, or it’s a message generated by a <a target="_blank" href="http://en.wikipedia.org/wiki/Zombie_computer" title="Zombie computer">zombie</a> – a computer compromised by a virus and controlled by a hacker for the purpose of delivering spam.</p>
<p>Another category of blacklisted IP addresses belong to <a target="_blank" href="http://en.wikipedia.org/wiki/Open_relay" title="Open mail relay">open relays</a> – mail servers that don’t require authentication and thus provide a “hop” for spam messages to freely pass through.  Open relays allow spammers to hide the true origin of their messages (because the originating IP address might already be blacklisted).  This abuse of the open mail server often occurs without knowledge of the server owner.</p>
<p>All of these policies carry with them a bit of controversy.  A single spammer on a large network might cause thousands of innocent users on the same network to have their outbound email blocked.  Open relay owners are also regarded more as victims than active participants in spamming (although they should be summarily reprimanded for not applying the most basic of security measures to their mail exchange: <a target="_blank" href="http://en.wikipedia.org/wiki/SMTP-AUTH" title="SMTP-AUTH">SMTP authentication</a>).  Also, when blacklists are used by ISPs, customers might unknowingly fail to receive wanted messages that were filtered out based on someone else’s definition of spam.</p>
<p><strong>Available DNS Blacklists</strong></p>
<p>DNSBLs are intended for use by mail service providers – not individual email system users.  If you administer a mail server, a comparison of available blacklists you may consider configuring can be found at the <a target="_blank" href="http://stats.dnsbl.com/" title="DNSBL Resource">DNSBL Resource</a> Stats Center.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavaghan.org/blog/2008/06/11/kill-spam-with-real-time-dns-blacklists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JUnit Factory Part 3: Improving Code Coverage</title>
		<link>http://www.gavaghan.org/blog/2008/02/10/junit-factory-part-3-improving-code-coverage/</link>
		<comments>http://www.gavaghan.org/blog/2008/02/10/junit-factory-part-3-improving-code-coverage/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 03:49:47 +0000</pubDate>
		<dc:creator>Mike Gavaghan</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.gavaghan.org/blog/2008/02/10/junit-factory-part-3-improving-code-coverage/</guid>
		<description><![CDATA[JUnit Factory is rather clever how it analyzes and executes your code to generate characterization tests. However, legacy Java code was generally not written with testability in mind. This sometimes makes it difficult for JUnit Factory to attain complete coverage of your code due to the need for objects to exist in a complex state or the need to interact with an external resource such as a database.

JUnit Factory is often able to generate mock instances automatically  for problematic classes. When automocking fails, the developer can improve coverage by either extracting behaviors into private methods or by providing hints to JUnit Factory in the form of test data helpers.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.junitfactory.com" title="JUnit Factory" target="_blank">JUnit Factory</a> is rather clever how it analyzes and executes your code to generate characterization tests. However, legacy Java code was generally not written with testability in mind. This sometimes makes it difficult for JUnit Factory to attain complete coverage of your code due to the need for objects to exist in a complex state or the need to interact with an external resource such as a database.</p>
<p>JUnit Factory is often able to generate mock instances automatically  for problematic classes.  When automocking fails, the developer can improve coverage by either extracting behaviors into private methods or by providing hints to JUnit Factory in the form of test data helpers.</p>
<p><span id="more-54"></span>This post is part of a series:</p>
<p style="padding-left: 20px"> 1. <a href="http://www.gavaghan.org/blog/2008/01/04/characterization-tests-how-to-deal-with-legacy-java-code/" title="Characterization Tests: How To Deal With Legacy Java Code">Characterization Tests: How To Deal With Legacy Java Code</a><br />
2. <a href="http://www.gavaghan.org/blog/2008/01/15/junit-factory-part-1-generating-tests/" title="JUnit Factory Part 1: Generating Tests">JUnit Factory Part 1: Generating Tests</a><br />
3. <a href="http://www.gavaghan.org/blog/2008/01/24/junit-factory-part-2-finding-regressions/" title="JUnit Factory Part 2: Finding Regressions">JUnit Factory Part 2: Finding Regressions</a><br />
4. <strong>JUnit Factory Part 3: Improving Code Coverage</strong></p>
<p><strong>Handling External Resources</strong></p>
<p>A classic unit testing problem is testing business logic that depends on an external resource.  The external resource is usually a database, but it could also be a Web service, the file system, or a user interface.  This is challenging because a true unit test executes in the absence of external dependencies, but creating mocks is an expensive, laborious process for the developer.</p>
<p>JUnit Factory&#8217;s answer to this is the <a href="http://www.junitfactory.com/articles/mockingbird/" title="JUnit Factory Mockingbird" target="_blank">Mockingbird</a> framework.  When code to be tested must retrieve data from a database, JUnit Factory will repeatedly see some failure (like a <code>SQLException</code>) being thrown every time it tries to execute a database call.  To move beyond the database call, JUnit Factory will create a mock implementation of the failing method.</p>
<p>Let&#8217;s look at a modified version of the <code>makePurchase()</code> method we discussed earlier:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void makePurchase(Purchase purchase) throws CreditCardException</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>validateBalance(balance + purchase.getAmount());</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>CreditCardDAO dao = CreditCardDAO.getDAOImplementation();</p></blockquote>
<blockquote class="codeline1"><p>dao.recordPurchase(accountNumber, purchase);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>balance = balance + purchase.getAmount();</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>We&#8217;ve introduced a data access object called <code>CreditCardDAO</code> encapsulating our interface to an external system.  In this case, it&#8217;s probably a database.  There&#8217;s likely one or more subtypes of <code>CreditCardDAO</code>, such as <code>CreditCardOracleDAO</code> or <code>CreditCardMySqlDAO</code>, providing implementation-specific behaviors for the data storage system.</p>
<p>Notice there are no subtypes of <code>CreditCardDAO</code> in the <a href="http://www.gavaghan.org/blog/characterization-tests-sample-code/" title="Characterization Tests Sample Code">example Eclipse project archive</a>.  They wouldn&#8217;t be usable, anyway, since a properly written unit test should execute in the absence of a database.  A developer would need to create a mock instance of <code>CreditCardDAO</code> in order to write the test.</p>
<p>If we submit this class to JUnit Factory to autogenerate a characterization test, we can see how the call to <code>CreditCardDAO.recordPurchase()</code> gets mocked out using the Mockingbird framework:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void testMakePurchaseWithAggressiveMocks() throws Throwable</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>CreditCard creditCard =</p></blockquote>
<blockquote class="codeline4"><p>(CreditCard) Mockingbird.getProxyObject(CreditCard.class, true);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>Purchase purchase =</p></blockquote>
<blockquote class="codeline4"><p>(Purchase) Mockingbird.getProxyObject(Purchase.class);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>CreditCardDAO creditCardDAO = new CreditCardDAO();</p></blockquote>
<blockquote class="codeline1"><p>setPrivateField(creditCard, &#8220;creditLimit&#8221;, new Double(0.0));</p></blockquote>
<blockquote class="codeline1"><p>setPrivateField(creditCard, &#8220;accountNumber&#8221;, &#8220;&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>setPrivateField(creditCard, &#8220;balance&#8221;, new Double(0.0));</p></blockquote>
<blockquote class="codeline1"><p>setPrivateField(purchase, &#8220;amount&#8221;, new Double(0.0));</p></blockquote>
<blockquote class="codeline1"><p>CreditCardDAO.setDAOImplementation(creditCardDAO);</p></blockquote>
<blockquote class="codeline1"><p>Mockingbird.enterRecordingMode();</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>Mockingbird.setReturnValue(false,</p></blockquote>
<blockquote class="codeline4"><p>creditCardDAO,</p></blockquote>
<blockquote class="codeline4"><p>&#8220;recordPurchase&#8221;,</p></blockquote>
<blockquote class="codeline4"><p>&#8220;(java.lang.String,example2.Purchase)void&#8221;,</p></blockquote>
<blockquote class="codeline4"><p>null,</p></blockquote>
<blockquote class="codeline4"><p>1</p></blockquote>
<blockquote class="codeline1"><p>);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>Mockingbird.enterTestMode(CreditCard.class);</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePurchase(purchase);</p></blockquote>
<blockquote class="codeline1"><p>assertEquals(&#8220;creditCard.getBalance()&#8221;,</p></blockquote>
<blockquote class="codeline4"><p>0.0,</p></blockquote>
<blockquote class="codeline4"><p>creditCard.getBalance(),</p></blockquote>
<blockquote class="codeline4"><p>1.0E-6</p></blockquote>
<blockquote class="codeline1"><p>);</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
</blockquote>
<p>First, notice the suffix &#8220;WithAggressiveMocks&#8221; added to the end of the test method name.  This indicates JUnit Factory needed to create mocks for a few classes in order to create a test.  Aggressive mocks aren&#8217;t a problem, although they can make tests harder to read.  Later, we&#8217;ll discuss test data helpers which can sometimes be used to avoid aggressive mocking and create clearer test code.</p>
<p>The test method begins with Mockingbird creating a number of proxy instances to create a hook into the call to be mocked.  The actual insertion of the mock behavior occurs on the line that begins &#8220;<code>Mockingbird.setReturnValue</code>&#8220;.</p>
<p>Instead of instantiating a subtype of <code>CreditCardDAO</code>, JUnit Factory simply uses the base type.  However, the base implementation of <code>recordPurchase()</code> always throws a <code>RuntimeException</code> indicating it must be overridden by a subclass.  The &#8220;<code>setReturnValue()</code>&#8221; call adjusts the behavior of <code>recordPurchase()</code> to return normally instead.  This allows the rest of the <code>makePurchase()</code> method to execute and update the balance.</p>
<p><strong>These are unit tests &#8211; not integration tests</strong></p>
<p>You&#8217;ll see Mockingbird used in a number of scenarios, not just where calls to external resources would otherwise fail.  Mockingbird is applied anywhere JUnit Factory needs to control the return value of dependent types in order to continue executing a method.</p>
<p>This may seem a little disconcerting at first.  After all, how good are these tests if we&#8217;ve mocked the behavior of dependent classes?  This is okay.  Remember these are <a href="http://www.gavaghan.org/blog/2008/01/04/characterization-tests-how-to-deal-with-legacy-java-code/" title="Characterization Tests: How To Deal With Legacy Java Code"><em>characterization tests</em></a>.  This isn&#8217;t about asserting the correct behavior of our code.  We&#8217;re only trying to capture the behavior of legacy code so we can find regressions.</p>
<p><strong>Method extraction and test data helpers</strong></p>
<p>Next, let&#8217;s take a look at a method called <code>fraudCheck()</code>.  This implements an admittedly simplistic rule that flags account activity as suspicious if there are several small purchases (less than $10) that add up to over $100.</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public boolean fraudCheck()</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>CreditCardDAO dao = CreditCardDAO.getDAOImplementation();</p></blockquote>
<blockquote class="codeline1"><p>List purchases = dao.getRecentPurchases(accountNumber);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>boolean isSuspicious;</p></blockquote>
<blockquote class="codeline1"><p>double purchaseTotal = 0;</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>for (Iterator iter = purchases.iterator(); iter.hasNext();)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>Purchase purchase = (Purchase) iter.next();</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline2"><p>if (purchase.getAmount() &lt; 10.00)</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline3"><p>purchaseTotal += purchase.getAmount();</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>if (purchaseTotal &gt; 100.00)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>isSuspicious = true;</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline1"><p>else</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>isSuspicious = false;</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>return isSuspicious;</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
</blockquote>
<p>When we execute <code>CreditCardAgitaTest</code>, we can look at the coverage information provided by the JUnit Factory plug-in on the left side of the editor window:</p>
<p><img src="http://www.gavaghan.org/blog/wp-content/uploads/2008/02/missingcoverage.gif" alt="Missing Coverage" border="1" /></p>
<p>Here, we see line 138 is never executed when we run the test.  For a series of purchases to be flagged as suspicious, we&#8217;d need at least 11 transactions of $9.99.  Although JUnit Factory can mock the return value of <code>getRecentPurchases()</code>, it was unable to discover the proper state of a purchase list that would result in the execution of this line. So, we&#8217;re not able to reach all branches of our code.</p>
<p><strong>Controlling preconditions</strong></p>
<p>We know how to create a list of purchases that satisfies the criteria for suspicious activity, but we need some way of inserting that list into a test.</p>
<p>A unit test involves creating a set of preconditions to a method (the parameters) and asserting a set of postconditions (the state manipulated by the method). Before we can control the list of purchases as a test precondition, we need to refactor our code to turn the list of purchases into a method parameter.</p>
<p>The safest way to do this is to use Eclipse&#8217;s refactoring tools.  Select the code in <code>fraudCheck()</code> from the first line <em>after</em> the call to <code>getRecentPurchases()</code> and on to the end of the method.  Right-click the highlighted code in the editor and select &#8220;Refactor -&gt; Extract Method&#8221;.  We&#8217;ll name the new method &#8220;<code>hasSuspiciousActivity()</code>&#8220;.</p>
<p><img src="http://www.gavaghan.org/blog/wp-content/uploads/2008/02/extractmethod.gif" alt="Extract Method" border="1" /></p>
<p>This is completely safe because extracting this code into its own method doesn&#8217;t actually change the behavior of the <code>fraudCheck()</code> method.  All we&#8217;ve done is make the <code>fraudCheck()</code> method <em>testable</em>.  The new code looks like this:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public boolean fraudCheck()</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>CreditCardDAO dao = CreditCardDAO.getDAOImplementation();</p></blockquote>
<blockquote class="codeline1"><p>List purchases = dao.getRecentPurchases(accountNumber);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>return hasSuspiciousActivity(purchases);</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline0"><p>private boolean hasSuspiciousActivity(List purchases)</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>boolean isSuspicious;</p></blockquote>
<blockquote class="codeline1"><p>double purchaseTotal = 0;</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>for (Iterator iter = purchases.iterator(); iter.hasNext();)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>Purchase purchase = (Purchase) iter.next();</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline2"><p>if (purchase.getAmount() &lt; 10.00)</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline3"><p>purchaseTotal += purchase.getAmount();</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>if (purchaseTotal &gt; 100.00)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>isSuspicious = true;</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline1"><p>else</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>isSuspicious = false;</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>return isSuspicious;</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>What we&#8217;ve achieved is the creation of a method that takes a list of purchases as a parameter.  Now, we can give JUnit Factory a hint about how to create this parameter in such a way that <code>isSuspicious</code> will sometimes be set to &#8220;true&#8221;.</p>
<p><strong>Giving hints using test data helpers</strong></p>
<p>A test data helper is simply a class that creates objects JUnit Factory can pass as parameters to methods being tested.  Test data helper methods must 1) have a name that begins &#8220;create&#8221;, 2) take no parameters, and 3) return an instance of the target data type.  You may have as many test data helper methods on a test helper class as you want.</p>
<p>In the <a href="http://www.gavaghan.org/blog/characterization-tests-sample-code/" title="Characterization Tests Sample Code">sample Eclipse project archive</a>, look for a source folder named &#8220;testhelpers&#8221;.  In there you&#8217;ll find a class named <code>PurchaseListTestHelper</code>. This class implements <code>com.agitar.lib.TestHelper</code>, so JUnit Factory will recognize it as a global test helper (as opposed to the <code>ScopedTestHelper</code> interface which ties the helper methods to a particular type).   The class contains a single test data helper method named <code>createSuspiciousPurchaseList()</code>.  Here is the complete class definition:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>package example2;</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline0"><p>import java.util.*;</p></blockquote>
<blockquote class="codeline0"><p>import com.agitar.lib.TestHelper;</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline0"><p>public class PurchaseListTestHelper implements TestHelper</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>public static List createSuspiciousPurchaseList()</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>ArrayList purchaseList = new ArrayList();</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline2"><p>try</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline3"><p>for (int i = 0; i &lt; 11; i++)</p></blockquote>
<blockquote class="codeline3"><p>{</p></blockquote>
<blockquote class="codeline4"><p>Date purchaseDate = new Date(1000);</p></blockquote>
<blockquote class="codeline4"><p>double amount = 9.99;</p></blockquote>
<blockquote class="codeline4"><p>Purchase purchase = new Purchase( purchaseDate, amount );</p></blockquote>
<blockquote class="codeline4"><p>purchaseList.add(purchase);</p></blockquote>
<blockquote class="codeline3"><p>}</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="codeline2"><p>catch( CreditCardException exc )</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline3"><p>throw new RuntimeException(&#8220;Failed to create purchase list&#8221;);</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline2"><p>return purchaseList;</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>This helper create a list of 11 purchases of $9.99 each.  If this object is passed to our <code>hasSuspiciousActivity()</code> method, we can expect it to execute the line setting <code>isSuspicious</code> to <code>true</code>.</p>
<p>Can we be sure JUnit Factory will use our test helper?  Not necessarily.  JUnit Factory prefers to use developer provided test data helpers whenever possible &#8211; as long as they provide unique code coverage or an interesting outcome.  If you have multiple test helpers, but they all provide the same test coverage, JUnit Factory will only use one of them.</p>
<p>If no test helper provides a desired execution flow,  JUnit Factory will create an instance of an object on its own by reflecting constructors off of the <code>Class</code> object.  It is only when this final strategy fails that JUnit Factory resorts to aggressive mocks and the Mockingbird framework.</p>
<p>Will JUnit Factory use the test data helper we just created?  We can generate tests for <code>CreditCard</code> again and find out.  JUnit Factory will search our entire Eclipse project for all available test data helpers.  There&#8217;s nothing to configure, because all classes implementing <code>TestHelper</code> or <code>ScopedTestHelper</code> will be used as candidates.</p>
<p>After regenerating tests for <code>CreditCard</code>, we can see the answer is &#8220;yes,&#8221; JUnit Factory found our test data helper and used it.  Some of the test cases for <code>hasSuspiciousActivity()</code> use <code>PurchaseListTestHelper </code> as shown in the generated test:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void testHasSuspiciousActivity() throws Throwable</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>CreditCard creditCard = new CreditCard(&#8220;3317 3013 6259 0300&#8243;, 100.0, 0.0);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>List suspiciousPurchaseList =</p></blockquote>
<blockquote class="codeline5"><p>PurchaseListTestHelper.createSuspiciousPurchaseList();</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>boolean add =</p></blockquote>
<blockquote class="codeline5"><p>suspiciousPurchaseList.add(new Purchase(new Date(100L), 100.0));</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>boolean result = ((Boolean) callPrivateMethod( &#8220;example2.CreditCard&#8221;,</p></blockquote>
<blockquote class="codeline5"><p>&#8220;hasSuspiciousActivity&#8221;,</p></blockquote>
<blockquote class="codeline5"><p>new Class[] {List.class},</p></blockquote>
<blockquote class="codeline5"><p>creditCard,</p></blockquote>
<blockquote class="codeline5"><p>new Object[] {suspiciousPurchaseList})</p></blockquote>
<blockquote class="codeline1"><p>).booleanValue();</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>assertTrue(&#8220;result&#8221;, result);</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>When we execute <code>CreditCardAgitarTest</code>, the code coverage bars in the editor window show JUnit Factory is now covering all of our business logic in <code>CreditCard</code>.  It has also created additional assertions in <code>CreditCardAgitarTest</code> to check the outcome when <code>isSuspicious</code> returns <code>true</code>.</p>
<p><strong>Test coverage strategy</strong></p>
<p>With so many tools at your your disposal, where do you begin?</p>
<p>First, don&#8217;t concern yourself with aggressive mocks.  Aggressive mocks happen automatically, and they&#8217;re okay.  They&#8217;re merely a sign your legacy code wasn&#8217;t written for testability.  Aggressive mocks give you the code coverage you need to detect changes in the behavior of your code.  If you have 100% code coverage already, the only reason you might look into one of the other two strategies is if you want to improve the readability of your tests.</p>
<p>If aggressive mocks don&#8217;t give you enough coverage, consider refactoring your code using the Eclipse tool to extract a method.  This allows you to create method parameters out of local variables that JUnit Factory couldn&#8217;t get into an appropriate state.  Often times, you won&#8217;t even need to create a test data helper after refactoring your code.  If the required state for a parameter is straightforward enough, JUnit Factory can figure out on its own how to create it properly.</p>
<p>Reach for test data helpers last.  They&#8217;re a powerful tool.  But, they require writing additional code &#8211; and that&#8217;s just more code you&#8217;ll need to maintain.  Use them only when refactoring doesn&#8217;t give you the coverage you need, or when team development policies preclude modifying existing code.</p>
<p>Lastly, always consider whether the missing coverage is worth solving.  You might be dealing with truly dead code that can never be reached.  You might also be dealing with one or two minor lines that simply aren&#8217;t worth the effort.  Aiming for 100% code coverage is a costly and, generally, foolish objective. Consider the cost against the benefit.</p>
<p>After all, if you&#8217;ve brought your test coverage from 0% up to 80% with the single click of a button, aren&#8217;t you profoundly better off than before?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavaghan.org/blog/2008/02/10/junit-factory-part-3-improving-code-coverage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JUnit Factory Part 2: Finding Regressions</title>
		<link>http://www.gavaghan.org/blog/2008/01/24/junit-factory-part-2-finding-regressions/</link>
		<comments>http://www.gavaghan.org/blog/2008/01/24/junit-factory-part-2-finding-regressions/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 02:59:34 +0000</pubDate>
		<dc:creator>Mike Gavaghan</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.gavaghan.org/blog/2008/01/24/junit-factory-part-2-finding-regressions/</guid>
		<description><![CDATA[Characterization tests provide a safety net for your legacy Java code by helping identify unintended changes in software behavior caused by code maintenance.  JUnit Factory (http://www.junitfactory.com) from Agitar Software (http://www.agitar.com) may be used to automatically generate these tests for you.  In this post, we’ll take a look at what happens to these characterization tests when a simple code change is made.]]></description>
			<content:encoded><![CDATA[<p>Characterization tests provide a safety net for your legacy Java code by helping identify unintended changes in software behavior caused by code maintenance.  <a href="http://www.junitfactory.com/" onclick="javascript:urchinTracker('/outgoing/junitfactory');" title="JUnit Factory" target="_blank">JUnit Factory</a> from <a href="http://www.agitar.com/" onclick="javascript:urchinTracker('/outgoing/agitar');" title="Agitar Software" target="_blank">Agitar Software</a> may be used to automatically generate these tests for you.  In this post, we’ll take a look at what happens to these characterization tests when a simple code change is made.</p>
<p><span id="more-52"></span>This post is part of a series:</p>
<p style="padding-left: 20px"> 1. <a href="http://www.gavaghan.org/blog/2008/01/04/characterization-tests-how-to-deal-with-legacy-java-code/" title="Characterization Tests: How To Deal With Legacy Java Code">Characterization Tests: How To Deal With Legacy Java Code</a><br />
2. <a href="http://www.gavaghan.org/blog/2008/01/15/junit-factory-part-1-generating-tests/" title="JUnit Factory Part 1: Generating Tests">JUnit Factory Part 1: Generating Tests</a><br />
<strong>3. JUnit Factory Part 2: Finding Regressions</strong><br />
4. <a href="http://www.gavaghan.org/blog/2008/02/10/junit-factory-part-3-improving-code-coverage/" title="JUnit Factory Part 3: Improving Code Coverage">JUnit Factory Part 3: Improving Code Coverage</a></p>
<p><strong>A simple requirements change</strong></p>
<p>Suppose we need to implement a simple requirements change to the <code>CreditCard</code> class we&#8217;ve already created.  Presently, the <code>validateBalance()</code> method disallows negative balances.  The business leadership of our company, however, has decided to allow customers to overpay their credit card balances.</p>
<p>Before writing any code, we must first verify that all of our current characterization tests still pass.  The fastest way to do this is to select the source folder containing our characterization tests in the Eclipse Package Explorer.  Right click on the folder and select &#8220;Run As / Agitar JUnit Test&#8221;.</p>
<p>If any tests fail, it means some other developer has already introduced a behavior change into the system without addressing how this affects tests.  If all of the tests pass, we know we have a safety net.  We may now move forward making changes confident any regressions will be detected before we commit our work to version control.</p>
<p>Credit card payments are recorded in the <code>makePayment()</code> method:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void makePayment( double amount )  throws CreditCardException</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>if (amount &lt;= 0)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>throw new CreditCardException(&#8220;Payment amount must be positive&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>validateBalance( balance &#8211; amount );</p></blockquote>
<blockquote class="codeline1"><p>balance = balance &#8211; amount;</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>After ensuring a postive payment amount has been made, the method invokes <code>validateBalance()</code> on the proposed new balance to check if it&#8217;s legal.</p>
<p>Presently, <code>validateBalance()</code> disallows negative values, but we&#8217;ll comment out the section of the method that performs this check:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>private void validateBalance( double balance )  throws CreditCardException</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>//if (balance &lt; 0.00)</p></blockquote>
<blockquote class="codeline1"><p>//{</p></blockquote>
<blockquote class="codeline1"><p>// throw new CreditCardException(&#8220;Balance can&#8217;t go below minimum balance&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>//}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>if (balance &gt; creditLimit)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>throw new CreditCardException(&#8220;Balance can&#8217;t exceed credit limit&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>The <code>makePurchase()</code> method now allows payments that exceed the outstanding balance on the credit card.</p>
<p><strong>Rerun the tests to find behavior changes</strong></p>
<p>When we rerun our characterization tests, we discover that three of the tests are now failing.  It shouldn&#8217;t come as any surprise, though, since changing the behavior of the class is what we intended to do.</p>
<p>So, we should just regenerate the <code>CreditCard</code> tests, right?  Absolutely not!  We must first analyze each of the test failures to determine if all of the failures reflect an expected change in behavior.</p>
<p>One failing test is for <code>validateBalance()</code>:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void testValidateBalanceThrowsCreditCardException() throws Throwable</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>CreditCard creditCard = new CreditCard(&#8220;2298 9812 4566 1184&#8243;, 100.0, 0.0);</p></blockquote>
<blockquote class="codeline1"><p>try</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>callPrivateMethod(&#8220;example1.CreditCard&#8221;,</p></blockquote>
<blockquote class="codeline5"><p>&#8220;validateBalance&#8221;,</p></blockquote>
<blockquote class="codeline5"><p>new Class[] {double.class},</p></blockquote>
<blockquote class="codeline5"><p>creditCard,</p></blockquote>
<blockquote class="codeline5"><p>new Object[] {new Double(-1.0)}</p></blockquote>
<blockquote class="codeline2"><p>);</p></blockquote>
<blockquote class="codeline2"><p>fail(&#8220;Expected CreditCardException to be thrown&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline1"><p>catch (CreditCardException ex)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>assertEquals(&#8220;ex.getMessage()&#8221;, &#8220;Balance can&#8217;t go below minimum balance&#8221;, ex.getMessage());</p></blockquote>
<blockquote class="codeline2"><p>assertThrownBy(CreditCard.class, ex);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>This test passes -1.0 to <code>validateBalance()</code> and expects a <code>CreditCardException</code> to be thrown.  This test passed when run against the old code, but it fails now because an exception is no longer thrown for negative balances.  This is an <em>expected</em> test failure.  The failing test reflects an intended change of behavior.</p>
<p>As you might expect, there&#8217;s also a failing test for <code>makePayment()</code>:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void testMakePaymentThrowsCreditCardException() throws Throwable</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>CreditCard creditCard = new CreditCard(&#8220;2298 9812 4566 1184&#8243;, 1000.0, 100.0);</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePayment(0.10000000149011612);</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePurchase(new Purchase(new Date(0L), 1.4178674221038818));</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePurchase(new Purchase(new Date(100L), 100.0));</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePurchase(new Purchase(new Date(1000L), 3.9999998989515E-5));</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePurchase(new Purchase(new Date(1L), 1.0E-5));</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePurchase(new Purchase(new Date(-1L), 3.6909053325653076));</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePayment(100.0);</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePayment(5.630099296569824);</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePayment(1.8264453411102295);</p></blockquote>
<blockquote class="codeline1"><p>creditCard.makePayment(97.55227811549801);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>try</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>creditCard.makePayment(0.0010);</p></blockquote>
<blockquote class="codeline2"><p>fail(&#8220;Expected CreditCardException to be thrown&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline1"><p>catch (CreditCardException ex)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>assertEquals(&#8220;ex.getMessage()&#8221;,</p></blockquote>
<blockquote class="codeline5"><p>&#8220;Balance can&#8217;t go below minimum balance&#8221;,</p></blockquote>
<blockquote class="codeline5"><p>ex.getMessage());</p></blockquote>
<blockquote class="codeline2"><p>assertThrownBy(CreditCard.class, ex);</p></blockquote>
<blockquote class="codeline2"><p>assertEquals(&#8220;creditCard.getBalance()&#8221;,</p></blockquote>
<blockquote class="codeline5"><p>0.0,</p></blockquote>
<blockquote class="codeline5"><p>creditCard.getBalance(),</p></blockquote>
<blockquote class="codeline5"><p>1.0E-6);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>This is somewhat predictable since <code>makePayment()</code> depends on <code>validateBalance()</code>.  This tests performs a series of purchase and payment transactions and checks that a payment creating a negative balance throws an exception.  We no longer want such transactions to throw an exception, so this, too, is an expected test failure.</p>
<p>The third test failure, however, is cause for concern.  This test of the constructor fails:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void testConstructorThrowsCreditCardException1() throws Throwable</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>try</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>new CreditCard(&#8220;2298 9812 4566 1184&#8243;, 15000.0, -1.0);</p></blockquote>
<blockquote class="codeline2"><p>fail(&#8220;Expected CreditCardException to be thrown&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline1"><p>catch (CreditCardException ex)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>assertEquals(&#8220;ex.getMessage()&#8221;,</p></blockquote>
<blockquote class="codeline5"><p>&#8220;Balance can&#8217;t go below minimum balance&#8221;,</p></blockquote>
<blockquote class="codeline5"><p>ex.getMessage()</p></blockquote>
<blockquote class="codeline2"><p>);</p></blockquote>
<blockquote class="codeline2"><p>assertThrownBy(CreditCard.class, ex);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>This test checks that an exception is thrown when <em>creating</em> a <code>CreditCard</code> with a negative opening balance.  Our requirements change permits a balance to go negative when making a payment, but not when opening a new account.  However, we inadvertently changed the behavior of the constructor because the constructor, just like the <code>makePayment()</code> method, is dependent on <code>validateBalance()</code>!</p>
<p><strong>Real regressions are usually far more obscure</strong></p>
<p>This is a rather simplistic example of where a change in one part our code creates a regression somewhere else.  In our <code>CreditCard</code> class, it would be trivial for a developer to grasp the entire behavior of the class and foresee the impact to the behavior of the constructor.</p>
<p>In real world applications, however, large legacy code bases contain complex relationships spanning hundreds of classes.  It&#8217;s simply not possible for a developer to approach unfamiliar code and understand all of the interdependencies.  Characterization tests will highlight behavior changes introduced by the developer, and allow the developer to analyzes those changes for correctness.</p>
<p>So, what shall we do about the regression we introduced in <code>CreditCard</code>?  One solution is to create a new method for validating the opening balance.  Invoke that method in the constructor instead of <code>validateBalance()</code>:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public CreditCard( String accountNumber, double creditLimit,</p></blockquote>
<blockquote class="codeline5"><p>double balanceTransfer )  throws CreditCardException</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>// &#8230; validate and assign account number and credit limit</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>// validate the balance</p></blockquote>
<blockquote class="codeline1"><p>//validateBalance( balanceTransfer );</p></blockquote>
<blockquote class="codeline1"><p>validateOpeningBalance(balanceTransfer);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>this.balance = balanceTransfer;</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline0"><p>private void validateOpeningBalance( double balance )</p></blockquote>
<blockquote class="codeline5"><p>throws CreditCardException</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>if (balance &lt; 0.00)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>throw new CreditCardException(&#8220;Opening balance can&#8217;t be negative&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>if (balance &gt; creditLimit)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>throw new CreditCardException(&#8220;Balance can&#8217;t exceed credit limit&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>When we run our characterization tests now, the constructor test passes and the only failing tests are the expected failures.</p>
<p><strong>Regenerate tests </strong><strong>and commit changes </strong></p>
<p>Now that we&#8217;re convinced we haven&#8217;t introduced any behavior changes into the code except for the changes we meant to implement, we can commit the updated <code>CreditCard</code> class to version control.</p>
<p>At the same time, we also  should regenerate our <code>CreditCard</code> characterization tests and commit the new tests to version control.  The update characterization tests will reflect the new behavior, and they&#8217;ll be available the next time a requirements change is needed.</p>
<p>In my next post, we&#8217;ll start to look at code coverage issues.  Sometimes, JUnit Factory is unable to figure out how to execute all paths of your code.  This could be due to the need for objects to exist in a complex state, the need to interact with an external resource such as a database, or simply due to dead code.  Look for this post in the next few weeks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavaghan.org/blog/2008/01/24/junit-factory-part-2-finding-regressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JUnit Factory Part 1: Generating Tests</title>
		<link>http://www.gavaghan.org/blog/2008/01/15/junit-factory-part-1-generating-tests/</link>
		<comments>http://www.gavaghan.org/blog/2008/01/15/junit-factory-part-1-generating-tests/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 17:03:28 +0000</pubDate>
		<dc:creator>Mike Gavaghan</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.gavaghan.org/blog/2008/01/15/junit-factory-part-1-generating-tests/</guid>
		<description><![CDATA[JUnit Factory ( http://www.junitfactory.com/ ) is a free Eclipse plug-in from Agitar Software ( http://www.agitar.com/ ) that generates characterization tests for your Java code. For more background on what characterizations tests are, and how you use them, you’ll want read my post “Characterization Tests: How To Deal With Legacy Java Code”.

This article describes how to generate tests for a simple Java class and how to read the tests. Not all of your real code will be this simple, and not all generated tests will be this simple, either. But, bear with me as we start small and work our way up.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.junitfactory.com/" onclick="javascript:urchinTracker('/outgoing/junitfactory');" title="JUnit Factory" target="_blank">JUnit Factory</a> is a free Eclipse plug-in from <a href="http://www.agitar.com/" onclick="javascript:urchinTracker('/outgoing/agitar');" title="Agitar Software" target="_blank">Agitar Software</a> that generates <a href="http://en.wikipedia.org/wiki/Characterization_Tests" title="Characterization Test" target="_blank">characterization tests</a> for your Java code.  For more background on what characterization tests are, and how you use them, you’ll want read my post <a href="http://www.gavaghan.org/blog/2008/01/04/characterization-tests-how-to-deal-with-legacy-java-code/" title="Characterization Tests: How To Deal With Legacy Java Code" target="_blank">“Characterization Tests: How To Deal With Legacy Java Code”</a>.</p>
<p>This article describes how to generate tests for a simple Java class and how to read the tests.  Not all of your real code will be this simple, and not all generated tests will be this simple, either.  But, bear with me as we start small and work our way up.</p>
<p><span id="more-47"></span>This post is part of a series:</p>
<p style="padding-left: 20px"> 1. <a href="http://www.gavaghan.org/blog/2008/01/04/characterization-tests-how-to-deal-with-legacy-java-code/" title="Characterization Tests: How To Deal With Legacy Java Code">Characterization Tests: How To Deal With Legacy Java Code</a><br />
2. <strong>JUnit Factory Part 1: Generating Tests</strong><br />
3. <a href="http://www.gavaghan.org/blog/2008/01/24/junit-factory-part-2-finding-regressions/" title="JUnit Factory Part 2: Finding Regressions">JUnit Factory Part 2: Finding Regressions</a><br />
4. <a href="http://www.gavaghan.org/blog/2008/02/10/junit-factory-part-3-improving-code-coverage/" title="JUnit Factory Part 3: Improving Code Coverage">JUnit Factory Part 3: Improving Code Coverage</a></p>
<p><strong>Let’s create a simple class</strong></p>
<p>If you want to follow along, you can <a href="http://www.gavaghan.org/blog/characterization-tests-sample-code/" title="Characterization Tests Sample Code" target="_blank">download the characterization tests sample code as an Eclipse project archive</a>.</p>
<p>The sample code defines a simplistic <code>CreditCard</code> type.  It’s admittedly inappropriate for real world use, but it’ll make for excellent illustration.  The complete class definition can be found in the sample code download.  For brevity, let’s start by looking just at the constructor:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public class CreditCard</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>private String accountNumber;</p></blockquote>
<blockquote class="codeline1"><p>private double creditLimit;</p></blockquote>
<blockquote class="codeline1"><p>private double balance;</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>/**</p></blockquote>
<blockquote class="codeline2"><p>* Construct a new CreditCard</p></blockquote>
<blockquote class="codeline2"><p>* @throws CreditCardException if any parameter is invalid</p></blockquote>
<blockquote class="codeline2"><p>*/</p></blockquote>
<blockquote class="codeline1"><p>public CreditCard( String accountNumber, double creditLimit,</p></blockquote>
<blockquote class="codeline5"><p>double balanceTransfer )  throws CreditCardException</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>// validate the account number</p></blockquote>
<blockquote class="codeline2"><p>if (!accountNumber.matches(&#8220;(\\d{4}[ ]){3}\\d{4}&#8221;))</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline3"><p>throw new CreditCardException(&#8220;Invalid credit card number&#8221;);</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="codeline2"><p>this.accountNumber = accountNumber;</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline2"><p>// validate the credit limit</p></blockquote>
<blockquote class="codeline2"><p>if (creditLimit &lt;= 0)</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline3"><p>throw new CreditCardException(&#8220;Credit limit must be positive&#8221;);</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline2"><p>if (creditLimit &gt; 15000)</p></blockquote>
<blockquote class="codeline2"><p>{</p></blockquote>
<blockquote class="codeline3"><p>throw new CreditCardException(&#8220;Credit limit may not exceed 15,000&#8243;);</p></blockquote>
<blockquote class="codeline2"><p>}</p></blockquote>
<blockquote class="codeline2"><p>this.creditLimit = creditLimit;</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline2"><p>// validate the balance</p></blockquote>
<blockquote class="codeline2"><p>validateBalance( balanceTransfer );</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline2"><p>this.balance = balanceTransfer;</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>public double getBalance()</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>return balance;</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>public double getCreditLimit()</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>return creditLimit;</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>//</p></blockquote>
<blockquote class="codeline1"><p>// &#8230;other methods omitted for clarity</p></blockquote>
<blockquote class="codeline1"><p>//</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>The constructor is pretty straightforward.  It accepts an account number, a credit limit, and a starting balance (our credit card company allows new accounts to be created with a transferred opening balance).</p>
<p>The behavior of our constructor is to validate all three of these parameters.  The account number must be of the form &#8220;nnnn nnnn nnnn nnnn&#8221;.  The credit limit must be positive, and our cautious credit card company never grants a credit limit greater than $15,000.  Finally, the opening balance is validated by a method called <code>validateBalance()</code> which we&#8217;ll look at later.</p>
<p>We&#8217;ll generate characterization tests for this class by selecting the class in the Eclipse package explorer and clicking the test generation button on the toolbar as shown in the screenshot below:<br />
<img src="http://www.gavaghan.org/blog/wp-content/uploads/2008/01/junitfactory1.gif" alt="JUnit Factory Test Generate Button" /></p>
<p>This will send our project to JUnit Factory which will analyze and execute our code, make observations about its behavior, and generate a series of unit tests which will capture those observations.  This will take several seconds.</p>
<p><strong>The generated tests </strong></p>
<p>If you&#8217;re already familiar with <a href="http://www.junit.org" title="JUnit" target="_blank">JUnit</a>, you know JUnit test cases for a class under test will extend <code>junit.framework.TestCase</code>.  JUnit Factory extends this type with <code>AgitarTestCase</code>.  This is the base class of all JUnit Factory generated tests.  So, our <code>CreditCard</code> test case is declared like this:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>import com.agitar.lib.junit.AgitarTestCase;</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline0"><p>public class CreditCardAgitarTest extends AgitarTestCase</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>//</p></blockquote>
<blockquote class="codeline1"><p>// &#8230;generated tests</p></blockquote>
<blockquote class="codeline1"><p>//</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p><code>AgitarTestCase</code> provides additional data gathering and utility methods used by the tests. You don&#8217;t need to worry about adding the Agitar jar files to your classpath because the Eclipse plug-in will automatically configure your project the first time you send a test generation request.</p>
<p>If you&#8217;re following along, the tests you get back might look slightly different than what we&#8217;re about to look at &#8211; particularly if you&#8217;re typing it in by hand.  Tests will vary based on the presence (or absence) of property getters and setters, other methods which might produce execution coverage, and the latest test generation techniques developed by Agitar Labs.</p>
<p>You can expect to get back several tests for the constructor.  One positive test case looks like this:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void testConstructor1() throws Throwable {</p></blockquote>
<blockquote class="codeline1"><p>CreditCard creditCard = new CreditCard(&#8220;2298 9812 4566 1184&#8243;,</p></blockquote>
<blockquote class="codeline6"><p>13.872842788696289,</p></blockquote>
<blockquote class="codeline6"><p>13.872842788696289);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>assertEquals(&#8220;creditCard.accountNumber&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>&#8220;2298 9812 4566 1184&#8243;,</p></blockquote>
<blockquote class="codeline6"><p>getPrivateField(creditCard, &#8220;accountNumber&#8221;));</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>assertEquals(&#8220;creditCard.getCreditLimit()&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>13.872842788696289,</p></blockquote>
<blockquote class="codeline6"><p>creditCard.getCreditLimit(),</p></blockquote>
<blockquote class="codeline6"><p>1.0E-6);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>assertEquals(&#8220;creditCard.getBalance()&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>13.872842788696289,</p></blockquote>
<blockquote class="codeline6"><p>creditCard.getBalance(),</p></blockquote>
<blockquote class="codeline6"><p>1.0E-6);</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>This test starts by instantiating a <code>CreditCard</code> with legal values (we’ll cover negative test cases later).  Notice JUnit Factory figured out how to create credit card numbers in the required format.  How is this possible?  When analyzing the bytecode, JUnit Factory found this line:</p>
<blockquote><p>if (!accountNumber.matches(&#8220;(\\d{4}[ ]){3}\\d{4}&#8221;))</p></blockquote>
<p>Because <a href="http://en.wikipedia.org/wiki/Regex" title="Regular Expressions" target="_blank">regex</a> was used validate the account number string, JUnit Factory used this information to create a legal parameter.</p>
<p>Arbitrary values appear for the credit limit and starting balance.  These values, of course, have far too many digits after the decimal point to be legitimate monetary values.  This is because there’s nothing in the code to indicate there should be only two digits after the decimal point.  JUnit Factory simply chose values that allowed the constructor to execute without throwing an exception.</p>
<p>Following the constructor are a series of assertions reflecting the state of the <code>CreditCard</code> object observed by JUnit Factory.  First, JUnit Factory observed the private attribute <code>accountNumber</code> was set to the same value as the <code>accountNumber</code> parameter.  Notice this assertion uses reflection because <code>accountNumber</code> is a private attribute.  There is no public getter for <code>accountNumber</code>.</p>
<p>The assertions for the credit limit and balance are similar, but notice JUnitFactory discovered JavaBeans-style getters for these attributes and uses them instead of reflection.  This creates a test that is more readable.  This also creates a test that is sensitive to changes in both the class’s constructor and some of its method members.</p>
<p>Here&#8217;s another interesting test generated for the constructor:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void testConstructor2() throws Throwable {</p></blockquote>
<blockquote class="codeline1"><p>CreditCard creditCard = new CreditCard(&#8220;2298 9812 4566 1184&#8243;,</p></blockquote>
<blockquote class="codeline6"><p>15000.0,</p></blockquote>
<blockquote class="codeline6"><p>0.0010);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>assertEquals(&#8220;creditCard.accountNumber&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>&#8220;2298 9812 4566 1184&#8243;,</p></blockquote>
<blockquote class="codeline6"><p>getPrivateField(creditCard,</p></blockquote>
<blockquote class="codeline6"><p>&#8220;accountNumber&#8221;));</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>assertEquals(&#8220;creditCard.getCreditLimit()&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>15000.0,</p></blockquote>
<blockquote class="codeline6"><p>creditCard.getCreditLimit(),</p></blockquote>
<blockquote class="codeline6"><p>1.0E-6);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>assertEquals(&#8220;creditCard.getBalance()&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>0.0010, creditCard.getBalance(),</p></blockquote>
<blockquote class="codeline6"><p>1.0E-6);</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>This test looks similar to the previous test, but note that 15000.0 is passed for the credit limit.  This time, the credit limit value is <em>not </em>arbitrary.  By analyzing the constructor bytecode, JUnit Factory noticed that 15000.0 is an interesting value &#8211; a branch of execution is triggered based off this corner condition.  Thus, JUnit Factory generates a test for this scenario.</p>
<p>JUnit Factory also generates negative test cases – tests that capture the behavior of a method when things go wrong.  Here’s one such test generated to ensure an exception is thrown when the credit limit parameter is less than zero:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void testConstructorThrowsCreditCardException3()  throws Throwable {</p></blockquote>
<blockquote class="codeline1"><p>try</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>new CreditCard(&#8220;2298 9812 4566 1184&#8243;, -0.0010, 100.0);</p></blockquote>
<blockquote class="codeline2"><p>fail(&#8220;Expected CreditCardException to be thrown&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline1"><p>catch (CreditCardException ex)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>assertEquals(&#8220;ex.getMessage()&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>&#8220;Credit limit must be positive&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>ex.getMessage());</p></blockquote>
<blockquote class="codeline2"><p>assertThrownBy(CreditCard.class, ex);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
</blockquote>
<p>How many tests does JUnit Factory generate? In this case, four tests for normal outcomes, and sevens tests for exceptional outcomes. JUnit Factory attempts to generate just enough tests for 100% code coverage.  You might wonder why there aren&#8217;t tests using a richer data set with hundreds of possible input combinations.  The answer is simple: those tests don&#8217;t add value.  There are no other input parameters that would provide additional code coverage.</p>
<p><strong>Unit testing private methods</strong></p>
<p>JUnit Factory easily supports testing of private methods by invoking those methods through reflection.</p>
<p>The second to last line of the <code>CreditCard</code> constructor invokes <code>validateBalance()</code> to test whether the opening balance is legal.  Here&#8217;s how <code>validateBalance()</code> is implemented:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>private void validateBalance( double balance )  throws CreditCardException</p></blockquote>
<blockquote class="codeline0"><p>{</p></blockquote>
<blockquote class="codeline1"><p>if (balance &lt; 0.00)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>throw new CreditCardException(&#8220;Balance can&#8217;t go below minimum balance&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>if (balance &gt; creditLimit)</p></blockquote>
<blockquote class="codeline1"><p>{</p></blockquote>
<blockquote class="codeline2"><p>throw new CreditCardException(&#8220;Balance can&#8217;t exceed credit limit&#8221;);</p></blockquote>
<blockquote class="codeline1"><p>}</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
</blockquote>
<p>This method tests whether a proposed balance value falls within a range.  The behavior has been extracted to a private method because it is shared by multiple methods.  JUnit Factory is able to independently test this behavior using reflection.  Four such tests, two positive and two negative, are generated for <code>validateBalance()</code>.  Here&#8217;s one example:</p>
<blockquote class="codeblock">
<blockquote class="codeline0"><p>public void testValidateBalance() throws Throwable {</p></blockquote>
<blockquote class="codeline1"><p>CreditCard creditCard = new CreditCard(&#8220;2298 9812 4566 1184&#8243;,</p></blockquote>
<blockquote class="codeline6"><p>100.0, 0.0);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>callPrivateMethod(&#8220;example1.CreditCard&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>&#8220;validateBalance&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>new Class[] {double.class},</p></blockquote>
<blockquote class="codeline6"><p>creditCard,</p></blockquote>
<blockquote class="codeline6"><p>new Object[] {new Double(0.0)}</p></blockquote>
<blockquote class="codeline1"><p>);</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
<blockquote class="codeline1"><p>assertEquals(&#8220;creditCard.getCreditLimit()&#8221;,</p></blockquote>
<blockquote class="codeline6"><p>100.0,</p></blockquote>
<blockquote class="codeline6"><p>creditCard.getCreditLimit(),</p></blockquote>
<blockquote class="codeline6"><p>1.0E-6);</p></blockquote>
<blockquote class="codeline0"><p>}</p></blockquote>
<blockquote class="blankcodeline"></blockquote>
</blockquote>
<p>This test creates a <code>CreditCard</code> object and invokes <code>validateBalance()</code> by passing 0.0.  Afterward, the test asserts that the validation method, which utilizes the <code>creditLimit</code> attribute, leaves the credit limit value unchanged.</p>
<p>This test is harder to read, but it is this sort of complexity that makes unit testing private methods costly and practically nonexistent in tests created by hand. With JUnit Factory, the test is written with the click of a button.</p>
<p>This feature adds tremendous value.  Independently testing private methods improves a developer&#8217;s ability to track down the source of behavioral changes whenever tests fail.</p>
<p>This post is continued:<br />
<strong><a href="http://www.gavaghan.org/blog/junit-factory-part-1-generating-tests-page-2/" title="JUnit Factory Part 1: Generating Tests (page 2)"> JUnit Factory Part 1: Generating Tests (page 2) »</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavaghan.org/blog/2008/01/15/junit-factory-part-1-generating-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Characterization Tests: How To Deal With Legacy Java Code</title>
		<link>http://www.gavaghan.org/blog/2008/01/04/characterization-tests-how-to-deal-with-legacy-java-code/</link>
		<comments>http://www.gavaghan.org/blog/2008/01/04/characterization-tests-how-to-deal-with-legacy-java-code/#comments</comments>
		<pubDate>Sat, 05 Jan 2008 02:59:26 +0000</pubDate>
		<dc:creator>Mike Gavaghan</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.gavaghan.org/blog/2008/01/04/characterization-tests-how-to-deal-with-legacy-java-code/</guid>
		<description><![CDATA[Companies have invested billions of dollars over the last decade building components and applications based on the Java framework. This work represents a wealth of expertise and collective knowledge that firms must protect and maintain. Unfortunately, in the dynamic field of software development where programmers change jobs, on average, every 18 months, the original developers on these past projects probably aren’t around anymore.

As a result, Java developers seldom have the luxury of working on true greenfield projects. Instead, they are faced with adding enhancements and fixing bugs on projects built upon a code base they didn’t write and don’t fully understand. How can developers safely make changes to legacy code without accidentally breaking something unrelated?

Characterization tests provide a safety net – a change detection engine – that identifies behavioral changes in legacy code in order to remedy regressions early in the development process.  Fixing regressions early shortens development timelines, increases code quality, and allows a team to become more agile. You can automatically generate characterization tests using the free JUnit Factory for Java (http://www.junitfactory.com) from Agitar Software.]]></description>
			<content:encoded><![CDATA[<p>Companies have invested billions of dollars over the last decade building components and applications based on the Java framework.  This work represents a wealth of expertise and collective knowledge that firms must protect and maintain.  Unfortunately, in the dynamic field of software development where programmers change jobs, on average, every 18 months, the original developers on these past projects probably aren’t around anymore.</p>
<p>As a result, Java developers seldom have the luxury of working on true <a href="http://en.wikipedia.org/wiki/Greenfield_project" title="Greenfield Project" target="_blank">greenfield</a> projects.  Instead, they are faced with adding enhancements and fixing bugs on projects built upon a code base they didn’t write and don’t fully understand.  How can developers safely make changes to legacy code without accidentally breaking something unrelated?</p>
<p><a href="http://en.wikipedia.org/wiki/Characterization_Tests" title="Characterization Test" target="_blank">Characterization tests</a> provide a safety net – a change detection engine – that identifies behavioral changes in legacy code in order to remedy regressions early in the development process.  Fixing regressions early shortens development timelines, increases code quality, and allows a team to become more agile.  You can automatically generate your team&#8217;s characterization tests using the free <a href="http://www.junitfactory.com/" onclick="javascript:urchinTracker('/outgoing/junitfactory');" target="_blank" title="JUnitFactory">JUnit Factory</a> for Java from <a href="http://www.agitar.com/" onclick="javascript:urchinTracker('/outgoing/agitar');" title="Agitar Software" target="_blank">Agitar Software</a>.</p>
<p><span id="more-46"></span>This post is part of a series:</p>
<p style="padding-left: 20px"><strong>1. Characterization Tests: How To Deal With Legacy Java Code</strong><br />
2. <a href="http://www.gavaghan.org/blog/2008/01/15/junit-factory-part-1-generating-tests/" title="JUnit Factory Part 1: Generating Tests">JUnit Factory Part 1: Generating Tests</a><br />
3. <a href="http://www.gavaghan.org/blog/2008/01/24/junit-factory-part-2-finding-regressions/" title="JUnit Factory Part 2: Finding Regressions">JUnit Factory Part 2: Finding Regressions</a><br />
4. <a href="http://www.gavaghan.org/blog/2008/02/10/junit-factory-part-3-improving-code-coverage/" title="JUnit Factory Part 3: Improving Code Coverage">JUnit Factory Part 3: Improving Code Coverage</a></p>
<p><strong>Use JUnit Tests for Regression Testing – If You’re Lucky Enough to Have Any</strong></p>
<p>If you’re lucky, you inherit legacy Java code that already comes with a full <a href="http://www.junit.org/" title="JUnit" target="_blank">JUnit</a> test suite.  Running unit tests written by the previous development team can detect unintended behavior changes introduced by new modifications.  Running these tests on a regular basis can identify regressions long before they reach QA (where they&#8217;re more costly to fix).</p>
<p>The reality of existing unit tests, however, is they generally cover only a small subset of the behavior of the code – core business logic or especially complex algorithms.  There is seldom substantial coverage of the entire code base, and there is even less focus on negative test cases (tests that assert a particular response when things go wrong).</p>
<p>What’s even worse is, more often than not, unit tests are nonexistent.  Is this because developers are lazy?  Maybe.  The more likely explanation, however, is that maintaining unit tests is hard!  It is both time consuming and expensive.</p>
<p>Unit testing is a combinatorial problem &#8211; every boolean decision statement requires at least two tests: one with an outcome of &#8220;true&#8221; and one with an outcome of &#8220;false&#8221;.  Developers need to write <a href="http://searchsoftwarequality.techtarget.com/originalContent/0,289142,sid92_gci1273161,00.html" title="Alberto Savoia sings the praises of software testing" target="_blank">3 to 5 lines of test code for every line of production code to be tested</a>. It is enough work to write tests in the first place, but refactoring the application code requires rewriting the corresponding unit tests, too.</p>
<p>Developers aren’t punished for bugs – they’re punished for missing deadlines.  Consequently, when given a choice between delivering on time or delivering quality software, software quality – along with the unit tests intended to ensure it – gets ignored at crunch time.</p>
<p><strong>Characterization Tests are not Functional Tests</strong></p>
<p>If you’ve ever written a unit test, you probably wrote it to assert particular behaviors in the code you just wrote.  If you’re in a <a href="http://en.wikipedia.org/wiki/Test_driven_development" title="Test-Driven Development" target="_blank">Test-Driven Development</a> environment, you wrote the tests <em>first</em>, and then you went on to write just enough code to get your tests to pass.</p>
<p>Characterization tests are different.   The term was coined by Michael Feathers in his book <a href="http://www.amazon.com/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1199484434&amp;sr=8-1" title="Michael Feathers Working Effectively with Legacy Code" target="_blank">&#8220;Working Effectively with Legacy Code&#8221;</a>.  Characterization tests are usually generated using an automated tool like the free <a href="http://www.junitfactory.com/" onclick="javascript:urchinTracker('/outgoing/junitfactory');" target="_blank" title="JUnitFactory">JUnit Factory</a> for Java from <a href="http://www.agitar.com/" onclick="javascript:urchinTracker('/outgoing/agitar');" title="Agitar Software" target="_blank">Agitar Software</a>.  Rather than asserting the correctness of a code unit, characterization tests simply capture the behavior of the code, as written, in order to detect behavioral changes later.</p>
<p>Obtaining meaningful code coverage from hand written JUnit tests is difficult, and aiming for any substantial coverage target is generally not cost effective.  Remember, you’ll be writing 3 to 5 times more Java code if you’re trying to achieve 100% coverage.  Automatically generating characterization tests using JUnit Factory, however, can give you 80% coverage or better with just the click of a button.</p>
<p><strong>How to Use Characterization Tests</strong></p>
<p>The JUnit tests generated by JUnit Factory simply capture how your legacy code behaves today.  You’ll want to generate tests for your entire legacy code base before you begin new work.  This will create a comprehensive test suite, providing at least 80% coverage, that will detect behavioral changes as you implement new functionality.</p>
<p>With this safety net in place, you can write new code with confidence.  But, what does it mean when your tests fail?</p>
<p>A failed characterization test doesn’t always mean a bug.  In fact, if you’re intentionally adding new functionality, you should <em>expect </em>some of your tests to fail.  After all, altering the behavior of your code was the goal you set out to achieve.</p>
<p>However, be sure you can explain the failure of <em>every </em>test as an intended change.  If a unit test fails that you wouldn’t expect to fail, it means you’ve introduced a regression. Continue working on your code until those tests pass.</p>
<p>Distinguishing between expected and unexpected characterization test failures is where the analytical skills of the developer come into play. Work is complete when the only failing tests are the ones expected to fail.  At that point, you may safely commit your changes to version control with a high level of confidence you haven&#8217;t broken anything.</p>
<p>You&#8217;ll also want to use JUnit Factory to generate a new set of tests for the Java classes you modified.  This will capture the behavior of your new code, identify future regressions, and eliminate the tests that failed due to deliberate changes.  After all, today’s greenfield projects are tomorrow’s legacy code!</p>
<p><strong>How Does JUnit Factory Do It?</strong></p>
<p>JUnit Factory is far more sophisticated than a static analysis tool.  In addition to analyzing, and attempting to reach, all of the boundary conditions and branches in your code, JUnit Factory will actually execute your code – instantiating complex parameter objects by reflecting constructors – and record outcome states and exceptions.</p>
<p>What about code that requires data from some external source like a database or a Web service?  JUnit Factory is surprisingly effective at auto-mocking external resources using its <a href="http://www.junitfactory.com/articles/mockingbird/" onclick="javascript:urchinTracker('/outgoing/junitfactory/mockingbird');" title="JUnit Factory mocking bird tests" target="_blank">Mockingbird framework</a>.  External dependencies are eliminated to create repeatable unit tests that are independent of their environment.</p>
<p><strong>How To Get Started</strong></p>
<p>Download the free <a href="http://www.junitfactory.com/downloads/" onclick="javascript:urchinTracker('/outgoing/junitfactory');" title="JUnit Factory test generation" target="_blank">Eclipse plugin for JUnit Factory</a> and try it out on some of the classes in your own projects.  Inspect the tests it generates, and you’ll get a feel for what’s going on.  Unresolved dependencies get mocked out, private methods get called by reflection, and both positive and negative tests are created.</p>
<p>The Eclipse plugin will also report on the code coverage achieved by the generated tests.  Sometimes, certain fragments of code require classes to enter a complex state in order to get executed.  If JUnit Factory can’t figure out how to create this precondition, you’ll have some untested sections of code.  However, JUnit Factory can, if necessary, be configured with test data helpers that provide hints on how to create this coverage and improve the quality of your tests.</p>
<p>Is this the end of functional testing?  No.  It’s still a good idea to create functional tests to assert the behavior of your newly written code.  After all, that’s the only way to verify intended behavior.  Use your hand coded tests as a complement to JUnit Factory change detection tests, and see them all reported together in JUnit Factory’s testing dashboard.</p>
<p>Legacy code is often ugly, but it isn’t going away &#8211; companies have invested too many resources to scrap it and start over.  Characterization tests make ugly legacy code maintainable, and JUnit Factory produces characterization tests with low cost, high coverage, and a lack of drudgery for which all developers will be grateful.</p>
<p><strong>Go to <a href="http://www.gavaghan.org/blog/2008/01/15/junit-factory-part-1-generating-tests/" title="JUnit Factory Part 1: Generating Tests">&#8220;JUnit Factory Part 1: Generating Tests&#8221; »</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavaghan.org/blog/2008/01/04/characterization-tests-how-to-deal-with-legacy-java-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

