<?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>Megatome &#187; findbugs</title>
	<atom:link href="http://www.megatome.com/tag/findbugs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.megatome.com</link>
	<description>Just another idiot&#039;s ramblings</description>
	<lastBuildDate>Thu, 12 Jan 2012 00:45:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Known Defects and FindBugs</title>
		<link>http://www.megatome.com/2008/05/30/known-defects-and-findbugs/</link>
		<comments>http://www.megatome.com/2008/05/30/known-defects-and-findbugs/#comments</comments>
		<pubDate>Fri, 30 May 2008 16:41:16 +0000</pubDate>
		<dc:creator>iamthechad</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Making]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[annotation]]></category>
		<category><![CDATA[characterization test]]></category>
		<category><![CDATA[findbugs]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[knowndefect]]></category>
		<category><![CDATA[legacy]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.megatome.com/2008/05/30/known-defects-and-findbugs/</guid>
		<description><![CDATA[(Download KnownDefects) I ran into a bit of a quandary the other day. I had to modify a piece of legacy code in our system &#8211; one that has no tests written against it. My first task was to write as many JUnit tests as I could to document the behavior of the component to [...]]]></description>
			<content:encoded><![CDATA[<p>(<a href="http://subversion.megatome.com/projects/KnownDefects/trunk/KnownDefect.zip">Download KnownDefects</a>)  </p>
<p>I ran into a bit of a quandary the other day. I had to modify a piece of legacy code in our system &#8211; one that has no tests written against it. My first task was to write as many <a href="http://www.junit.org/">JUnit</a> tests as I could to document the behavior of the component to help ensure that I didn&#8217;t break existing functionality with my changes.  </p>
<p>After a few tests, however, I began to notice something troubling. The component I was writing tests against had some bugs in it, and in order to make my unit tests pass, I had to code to those bugs. This in itself did not bother me too much, since I had no expectation that the component I was testing was error-free.  </p>
<p>What bothered me is that unit tests are often the best documentation of how a system works, and I would be creating misleading documentation. So, I did what anyone in this situation would do: I emailed <a href="http://langrsoft.com/">someone a lot smarter than me</a>.&nbsp; </p>
<p>The reply email contained several suggestions, all of which I plan to implement. The first suggestion is to carefully name the test methods to make it clear which ones work because of defects. The second suggestion is to create an annotation that can be added to a method to indicate that it works because of a defect, coupled with some way to report on all uses of the annotation.  </p>
<p>After a few hours of coding, I&#8217;m happy to announce that I&#8217;ve created a Java annotation named <code>KnownDefect</code> as well as a detector plugin for <a href="http://findbugs.sourceforge.net/">FindBugs</a> to collect all of the annotation instances.  </p>
<p>In the parlance of &#8220;<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=1212164658&amp;sr=1-1">Working Effectively with Legacy Code</a>&#8220;, I have been creating &#8220;<a href="http://www.artima.com/weblogs/viewpost.jsp?thread=198296">characterization tests</a>&#8220;. These tests look like normal unit tests, but they document the current state of the system, warts and all. Using the <code>KnownDefect</code> annotation helps make the warts stick out better.  </p>
<p>Here&#8217;s an example:  </p>
<p>
<pre name="code" class="brush: java">public void testValidateNullResponse() {
   try {
      ProtocolParser.validateResponse(null);
      fail();
   } catch (InvalidRequestException expected) {
      // Caught expected exception
   }
}</pre>
</p>
<p>This test documents the system as it currently exists, but the behavior is obviously not correct. The <code>ProtocolParser</code> class should be throwing an <code>InvalidResponseException</code>, not an <code>InvalidRequestException</code>. To document this defect, I change the method name and add the <code>KnownDefect</code> annotation to the test method, resulting in the following:
</p>
<p>
<pre name="code" class="brush: java">@KnownDefect("Should throw InvalidResponseException")
public void testValidateNullResponseShowsKnownDefect() {
   try {
      ProtocolParser.validateResponse(null);
      fail();
   } catch (InvalidRequestException expected) {
      // Caught expected exception
   }
}</pre>
</p>
<p>The correct behavior is now documented, and the test still passes.
</p>
<p>Instead of writing a new tool to manage the <code>KnownDefect</code> instances, I ended up writing a new plugin for FindBugs. The FindBugs detector will collect all instances of the annotation and group them under the &#8220;Correctness&#8221; heading so they&#8217;re all in one place. Bug reports can then be created and the defects fixed or not as business needs dictate. (Sadly, the FindBugs plugin for Eclipse does not find the annotation in Groovy code. The standalone version of FindBugs works fine, so I assume there&#8217;s a resource filtering issue with the Eclipse plugin.)</p>
<p>Instructions for using the annotation and the detector are included in the download file. Try it out &#8211; hopefully it will be useful to somebody other than myself.</p>
<p>(<a href="http://subversion.megatome.com/projects/KnownDefects/trunk/KnownDefect.zip">Download KnownDefects</a>) (Subversion: <a href="http://subversion.megatome.com/projects/KnownDefects/trunk">http://subversion.megatome.com/projects/KnownDefects/trunk</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.megatome.com/2008/05/30/known-defects-and-findbugs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FindBugs Is Even Cooler Than I Thought</title>
		<link>http://www.megatome.com/2007/08/29/findbugs-is-even-cooler-than-i-thought/</link>
		<comments>http://www.megatome.com/2007/08/29/findbugs-is-even-cooler-than-i-thought/#comments</comments>
		<pubDate>Wed, 29 Aug 2007 22:21:08 +0000</pubDate>
		<dc:creator>iamthechad</dc:creator>
				<category><![CDATA[findbugs]]></category>
		<category><![CDATA[frame2]]></category>
		<category><![CDATA[java6]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[oss]]></category>

		<guid isPermaLink="false">http://frame2.megatome.com/archives/7</guid>
		<description><![CDATA[I&#8217;ve mentioned before that I&#8217;m using FindBugs to help make sure that the Frame2 code is up to snuff. The Eclipse compiler can catch a lot of things, bug FindBugs has some neat tricks up its sleeve to find things that Eclipse can&#8217;t. FindBugs uses static analysis of the code to search for patterns that [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve mentioned before that I&#8217;m using <a href="http://findbugs.sourceforge.net/index.html">FindBugs</a> to help make sure that the Frame2 code is up to snuff. The Eclipse compiler can catch a lot of things, bug <a href="http://findbugs.sourceforge.net/index.html">FindBugs</a> has some neat tricks up its sleeve to find things that Eclipse can&#8217;t. <a href="http://findbugs.sourceforge.net/index.html">FindBugs</a> uses static analysis of the code to search for patterns that indicate possible issues. Being the Type A person that I am, I always turn on all of the possible matches and whittle the list down. Quite often, however, <a href="http://findbugs.sourceforge.net/index.html">FindBugs</a> will find a false positive or two. Up until now, I&#8217;ve either tried to rework the code to avoid the warning or disabled the category altogether. I&#8217;ve never liked the approach of turning off a whole category just to avoid seeing a warning or two, so I spent the morning muttering to myself how useful it would be if <a href="http://findbugs.sourceforge.net/index.html">FindBugs</a> allowed me to ignore certain warnings like I can do with <code>@SuppressWarnings</code>.</p>
<p>Then I went and <a href="http://findbugs.sourceforge.net/manual/index.html">RTFM</a>.</p>
<p><a href="http://findbugs.sourceforge.net/index.html">FindBugs</a> has a <a href="http://findbugs.sourceforge.net/manual/filter.html">filtering system</a> that makes <code>@SuppressWarnings</code> look amateur. Here&#8217;s an example: in the <code>SoapRequestProcessor</code>, <a href="http://findbugs.sourceforge.net/index.html">FindBugs</a> marked a warning that an <code>Exception</code> was being caught when no <code>Exception</code> was being thrown. After looking at the code and verifying that the try block in question throws several different exceptions, I created a filter entry. The entry looks like this:</p>
<p><code>&lt;Match&gt;<br />
&lt;Class name="org.megatome.frame2.front.SoapRequestProcessor"/&gt;<br />
&lt;Method name="getEvents"/&gt;<br />
&lt;Bug pattern="REC_CATCH_EXCEPTION"/&gt;<br />
&lt;/Match&gt;</code></p>
<p>This tells <a href="http://findbugs.sourceforge.net/index.html">FindBugs</a> to match a specific bug type in a specified method in a desired class. This entry can be used in both an inclusion or exclusion filter &#8211; I use it to exclude that warning from the results.</p>
<p>Here&#8217;s a more complex example:</p>
<p><code>&lt;Match&gt;<br />
&lt;Class name="~.*introspector\.Bean\d+" /&gt;<br />
&lt;Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2"/&gt;<br />
&lt;/Match&gt;</code></p>
<p>There are some test classes that don&#8217;t exactly follow good rules of programming when it comes to dealing with mutability. Since they are test classes, I don&#8217;t really care to fix them. Instead I set up the filter to match all classes in an <code>introspector</code> package named <code>Bean1</code>, <code>Bean2</code>, etc. Simple as can be!</p>
<p>If you aren&#8217;t using <a href="http://findbugs.sourceforge.net/index.html">FindBugs</a>, you should be. <a href="http://findbugs.sourceforge.net/index.html">Go check it out</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.megatome.com/2007/08/29/findbugs-is-even-cooler-than-i-thought/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

