<?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>erik dörnenburg</title>
	<atom:link href="http://erik.doernenburg.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://erik.doernenburg.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 01 Jul 2009 05:40:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making ESB pain visible</title>
		<link>http://erik.doernenburg.com/2009/07/making-esb-pain-visible/</link>
		<comments>http://erik.doernenburg.com/2009/07/making-esb-pain-visible/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 05:40:39 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=236</guid>
		<description><![CDATA[At some point last year I was asked to review the architecture of the software behind a large and popular website. The resident architect explained how he had followed a modern approach, decoupling the web front-end from back-end services that provide  content. To gain further flexibility he had put the front-end and the services [...]]]></description>
			<content:encoded><![CDATA[<p>At some point last year I was asked to review the architecture of the software behind a large and popular website. The resident architect explained how he had followed a modern approach, decoupling the web front-end from back-end services that provide  content. To gain further flexibility he had put the front-end and the services on an ESB, mostly to cater for the possibility to make the content available to other consumers. In short, the architecture diagram looked a lot like many others: nothing to see here, move on.</p>
<p><img src="http://erik.doernenburg.com/wp-content/uploads/2009/06/high-level-esb.png" alt="Website Architecture" title="Website Architecture" width="320" height="212" class="alignnone size-full wp-image-237" /></p>
<p>The diagram above only shows one of the content services, which for the sake of this article is a service that provides contact details for a person.</p>
<p>Based on conversations with the project sponsors I began to suspect that at least the introduction of the ESB was a case of RDD, ie. Resume-Driven Development, development in which key choices are made with only one question in mind: how good does it look on my CV? Talking to the developers I learned that the ESB had introduced &#8220;nothing but pain.&#8221; But how could something as simple as the architecture in the above diagram cause such pain to the developers? Was this really another case of architect&#8217;s dream, developer&#8217;s nightmare?  </p>
<p><span id="more-236"></span></p>
<p>Often a picture says more than a thousand words, but at the same time traditional architecture diagrams simply do not provide enough information. (I&#8217;ve argued this before, <a href="http://97-things.near-time.net/wiki/show/Get%20the%201000ft%20view">here</a> and <a href="http://www.infoq.com/interviews/erik-doernenburg-software-visualization">here</a> for example.) The answer to my questions, and the anxious project sponsors&#8217; question whether the project would deliver on time, lay in a different picture.</p>
<p>The picture should not only show the layering of the system in more detail than the architecture diagram but it should also visualise the complexity introduced by the ESB. It&#8217;s often possible to produce more detailed diagrams automatically from source code, configuration, or runtime inspection but in this case I couldn&#8217;t find a way to do that, which is which I drew the following:</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2009/06/low-level-esb.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2009/06/low-level-esb-600x533.png" alt="" title="Detailed ESB diagram" width="600" height="533" class="alignnone size-medium wp-image-237" /></a></p>
<p>In the centre column this diagram shows the layers involved, from the Struts action that needs to look up details by an ID down to the back-end that provides a &#8220;RESTful&#8221; service to retrieve the details. The top box contains the front-end layers while the box in the middle represents the ESB. The layers labelled in italics are frameworks or infrastructure software, the rest is project-specific code.</p>
<p>Key to this diagram are obviously the coloured representations of data travelling up and down the stack. Primitive types are grey, Java objects are purple circles, XML documents are shown as turquoise hexagons, and HTTP messages as green blobs. Mule messages, which are Java objects, are shown in dark purple. If one representation is wrapped in another it&#8217;s displayed inside the wrapper. </p>
<p>The diagram highlights that it&#8217;s a simple piece of data that travels all the way down the stack (actually further until it hits a relational database at some point) and, equally, that the data needed by the action is available in the right form from the service. All that the intermediate layers are doing is wrapping and unwrapping the data in various ways. In fact, out of the four layers of project-specific code three layers do nothing but wrapping. No wonder the developers felt some pain.</p>
<p>As a side note: the diagram also makes it obvious that the wrapping is asymmetric between the WsToRestHandler and the RestServiceWrapperImpl, which makes the point of this layer boundary even more questionable.</p>
<p>This diagram made people more interested in the cost of what had just been a simple, innocuous line in the architecture diagram; which was the point of the exercise. So, what would <em>this</em> diagram look like without the ESB? Easy:</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2009/06/Low-Level-ESB2.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2009/06/Low-Level-ESB2-600x305.png" alt="" title="Without ESB" width="600" height="305" class="alignnone size-medium wp-image-237" /></a></p>
<p>Apart for the obvious simplicity the diagram highlights that every layer has a purpose and there is only one wrapping, which is not undone either. Would it surprise you if I told you that the project went live without the ESB?</p>
<h3>Footnotes</h3>
<p>1) This article is by no means implying that any of the mentioned technologies are useless or harmful. Their use was harmful in this case.</p>
<p>2) The pattern of wrapping and unwrapping the same data through multiple layers is not new, it was also quite common in early 3-tier architectures. (In fact, some people called their architectures n-tier; I guess to show that the number of tiers was somewhat arbitrary but large.) We had a term to describe this pattern: the wormhole pattern.</p>
<p>3) This project is not an isolated incident of a Wormhole ESB. I have personally seen several in the past years.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2009/07/making-esb-pain-visible/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>97 Things Every Software Architect Should Know</title>
		<link>http://erik.doernenburg.com/2009/03/97-things/</link>
		<comments>http://erik.doernenburg.com/2009/03/97-things/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 01:48:41 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Clippings]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=211</guid>
		<description><![CDATA[ Admittedly, I&#8217;ve been struggling with the &#8220;Architect&#8221; title in the IT world. It is not that I think there&#8217;s no role for architecture, far from it, but too often I&#8217;ve encountered architects who focus too narrowly on architecture, losing track of the realities of actual software development and the context in which the software [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://erik.doernenburg.com/wp-content/uploads/2009/03/97thingscover-100x150.jpg" alt="97 Things..." title="97 Things..." width="100" height="150" class="alignleft size-thumbnail wp-image-220" /> Admittedly, I&#8217;ve been struggling with the &#8220;Architect&#8221; title in the IT world. It is not that I think there&#8217;s no role for architecture, far from it, but too often I&#8217;ve encountered architects who focus too narrowly on architecture, losing track of the realities of actual software development and the context in which the software will be used. I wonder, if there was no &#8220;Architect&#8221; title and people who are responsible for architecture would be called guide or coach or tech lead, or just <em>the senior developer</em>, whether things would be better.</p>
<p>About a year ago, in a discussion about architects, rather than trying to define what an architect is or does, we looked at what he or she should know. We expressed our ideas as mini-essays, strictly limiting ourselves to one per essay, and it turned out that, at least to my surprise, there was a lot of agreement; maybe because we hadn&#8217;t come up with hard and fast rules but with ideas and guidelines.</p>
<p>Luckily Richard Monson-Haefel was part of that discussion and he had the resolve and means to make our thoughts more widely available. Our list of the 97 things every architect should know was collected and refined on <a href="http://97-things.near-time.net/wiki">this wiki</a>, and is available under a creative commons license. For a more convenient read it has now also been published as a <a href="http://oreilly.com/catalog/9780596522698/preview.html">book by O&#8217;Reilly</a>. As expected, <a href="http://www.theserverside.com/news/thread.tss?thread_id=53819">the discussions have begun</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2009/03/97-things/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JAOO Nights</title>
		<link>http://erik.doernenburg.com/2009/03/jaoo-nights/</link>
		<comments>http://erik.doernenburg.com/2009/03/jaoo-nights/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 06:56:38 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Conferences]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=202</guid>
		<description><![CDATA[The JAOO conference is coming to Australia again, and I think it will be the best technical conference in Australia this year. (I admit that I&#8217;m somewhat biased as a member of the programme committee.) In the lead up to the conference we have organised what we call JAOO Nights in Brisbane, Melbourne, and Sydney. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://erik.doernenburg.com/wp-content/uploads/2009/03/speaking_at_jaoo-150x102.png" alt="speaking_at_jaoo" title="speaking_at_jaoo" width="150" height="102" class="alignright size-thumbnail wp-image-225" />The <a href="http://jaoo.dk/">JAOO conference</a> is coming to Australia again, and I think it will be the best technical conference in Australia this year. (I admit that I&#8217;m somewhat biased as a member of the programme committee.) In the lead up to the conference we have organised what we call JAOO Nights in Brisbane, Melbourne, and Sydney. These are free events, starting at 5pm with two speakers presenting on topics that give a flavour of what the JAOO conference itself will be like.</p>
<p>Check out the <a href="http://www.jaoo.com.au/?cid=435752">JAOO Australia site</a> for information on the sessions, and sign up for your city if your interested!</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2009/03/jaoo-nights/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How toxic is your code?</title>
		<link>http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/</link>
		<comments>http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 00:54:46 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=133</guid>
		<description><![CDATA[If you are somebody who writes code you probably know that moment when you look at some code you didn&#8217;t write, or some code you wrote a long time ago, and you think &#8220;that doesn&#8217;t look good.&#8221; Ok, more realistically, you probably think &#8220;WTF? I wouldn&#8217;t want to touch that with a barge-pole!&#8221; It is [...]]]></description>
			<content:encoded><![CDATA[<p>If you are somebody who writes code you probably know that moment when you look at some code you didn&#8217;t write, or some code you wrote a long time ago, and you think &#8220;that doesn&#8217;t look good.&#8221; Ok, more realistically, you probably think &#8220;WTF? I wouldn&#8217;t want to touch that with a barge-pole!&#8221; It is not even so much about whether the code does what it should do&#8212;that takes a bit longer to figure out&#8212;or whether the code is too slow. Even if it&#8217;s perfectly bug free and performs well, there&#8217;s something to the way it&#8217;s written. This is part of the internal quality of a software system, something that the users and development managers can&#8217;t observe directly; yet, it still affects them because code with poor internal quality is hard to maintain and extend.</p>
<p>Now, as a developer, how do you help managers and business people understand the internal quality of code? They generally want a bit more than &#8220;it&#8217;s horrible&#8221; before they prioritise cleaning up the code over implementing new features that directly deliver business value. Or even: how do you figure out for yourself how bad some code actually is in relation to some other code? These were questions that <a href="http://jroller.com/zoonabar/">Chris Brown</a>, <a href="http://darrenhobbs.com/">Darren Hobbs</a>, and myself were asking ourselves a couple of years ago.</p>
<p>The answer came in the form of a simple bar chart, arguably not the most sophisticated visualisation but a very effective one. And our colleague <a href="http://www.alphaitjournal.com/">Ross Pettit</a> had the perfect name for it: The <strong>Toxicity Chart</strong>. Read on to see what it is and how it&#8217;s created.</p>
<p><span id="more-133"></span></p>
<p>As usual, let&#8217;s start with an example. What follows is the toxicity chart for a version of the Hibernate framework.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/11/toxicity_hibernate.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/11/toxicity_hibernate-600x331.png" alt="Toxicity Chart for Hibernate" title="toxicity_hibernate" width="600" height="331" class="size-medium wp-image-143" /></a></p>
<p>In a toxicity chart each bar represents a class and the height of the bar shows the toxicity score for that class. The score is based on several metrics (more on that below) and the higher the score the more toxic the class. The individual components of the score are coloured. For example, the contribution of the method length metric to the overall score is shown in orange. This makes it possible to see at a glance not only how toxic a codebase is but also how the problems are distributed. If there is a lot of purple and orange in the chart, this indicates long and complex methods, which means that the code is probably hard to test on a unit level. Lastly, classes that score zero points are left off the chart.</p>
<p>The calculation of the toxicity score is based on metrics and thresholds. For example, the method length metric has a threshold of 30. If a class contains a method that is longer it gets points proportional to the length of the method in relation to the threshold, e.g. a method with 45 lines of code would get 1.5 points because its length is 1.5 times the threshold. The score for all elements is added up. So, if a class comprises two methods, one that is 45 lines and another that is 60 lines long, the method length component of the score for the class will be 3.5 points. This means that elements are not just classified as toxic but the score reflects how toxic the element is.</p>
<p>The following table shows the metrics that make up the toxicity score and the corresponding base threshold on which the multipliers are based.</p>
<table border="0" cellpadding="2">
<tr>
<th>Metric</th>
<th>Level</th>
<th>Threshold</th>
</tr>
<tr>
<td>File Length</td>
<td>file</td>
<td>500</td>
</tr>
<tr>
<td>Class Fan-Out Complexity</td>
<td>class</td>
<td>30</td>
</tr>
<tr>
<td>Class Data Abstraction Coupling</td>
<td>class</td>
<td>10</td>
</tr>
<tr>
<td>Anon Inner Length</td>
<td>inner class</td>
<td>35</td>
</tr>
<tr>
<td>Method Length</td>
<td>method</td>
<td>30</td>
</tr>
<tr>
<td>Parameter Number</td>
<td>method</td>
<td>6</td>
</tr>
<tr>
<td>Cyclomatic Complexity</td>
<td>method</td>
<td>10</td>
</tr>
<tr>
<td>Nested If Depth</td>
<td>statement</td>
<td>3</td>
</tr>
<tr>
<td>Nested Try Depth</td>
<td>statement</td>
<td>2</td>
</tr>
<tr>
<td>Boolean Expression Complexity</td>
<td>statement</td>
<td>3</td>
</tr>
<tr>
<td>Missing Switch Default</td>
<td>statement</td>
<td>1</td>
</tr>
</table>
<p>At this point you might wonder where the selection of metrics and thresholds comes from. Well, we made them up. When we designed the toxicity chart we made a call on what constitutes &#8220;toxic&#8221; as opposed to just bad. Of course, staying with the method length metric, we normally wouldn&#8217;t want to see a 15-line method but that might be disputed. However, we hope that nobody thinks that a 30-line method is acceptable. And in case you&#8217;re really uncomfortable with the thresholds, you can obviously change them to build your own toxicity score. We do suggest, though, that you try the values presented here first.</p>
<p>Like many visualisations the creation of the toxicity chart falls into two steps: data acquisition and rendering.</p>
<p><strong>Step 1:</strong> For Java projects <a href="http://checkstyle.sourceforge.net/">Checkstyle</a> is the easiest way to get the metrics. The  score table above easily translates into a Checkstyle configuration file, which, by the way, is included in the Zip archive attached to this page. Assuming this file is named &#8220;metrics.xml&#8221; Checkstyle can be invoked like this:</p>
<pre><code>java -jar checkstyle.jar -c metrics.xml -r <em>&lt;source dir&gt;</em> -f xml -o toxicity.xml</code></pre>
<p>Afterwards, the file &#8220;toxicity.xml&#8221; contains a list of all components for the toxicity score in an XML format.</p>
<p>Similar tools for other languages should be able to create similar output. It is not a huge problem if the output is somewhat different as it&#8217;s always possible to change step 2 to handle different input formats.</p>
<p><strong>Step 2:</strong> What is required next is to read the data from XML, aggregate it on a per-class level, and then render the bar chart. Microsoft Excel is good at the latter two but it needs a bit of help to read the XML file. This help comes in the form of a small piece of VBA code. The attached Excel workbook contains a sheet to load the data, an &#8220;Open XML&#8221; button backed by said VBA code, a pivot table to do the aggregation, and the chart based on the table. So, step 2, really is to open the Excel workbook and load the XML file. That&#8217;s all.</p>
<p>Now, if you know me you know that I&#8217;ve been working with Macs for a very long time. So, naturally, I&#8217;d like to use Excel on the Mac but, alas, in its current version it <a href="http://www.schwieb.com/blog/2006/08/08/saying-goodbye-to-visual-basic/">does not support VBA</a> anymore. So, unfortunately, it&#8217;s VMWare Fusion for this one.</p>
<p><strong>This <a href="http://erik.doernenburg.com/wp-content/uploads/2008/11/toxicitychart.zip">Zip archive</a> contains everything needed to create a toxicity chart for Java. The spreadsheet can be adapted to read input file formats created by tools for different languages; just edit the VBA behind the &#8220;Data&#8221; sheet.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Call Graph Visualisation with AspectJ and Dot</title>
		<link>http://erik.doernenburg.com/2008/09/call-graph-visualisation-with-aspectj-and-dot/</link>
		<comments>http://erik.doernenburg.com/2008/09/call-graph-visualisation-with-aspectj-and-dot/#comments</comments>
		<pubDate>Sat, 27 Sep 2008 17:26:28 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=101</guid>
		<description><![CDATA[One of my favourite tools to render graphs is GraphViz Dot and in an earlier entry I described how to use it to visualise Spring contexts. Today I want to showcase a different application.
Call graphs show how methods call each other, which can be useful for a variety of reasons. The example I use here [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favourite tools to render graphs is <a href="http://www.graphviz.org/">GraphViz Dot</a> and in an <a href="http://erik.doernenburg.com/2008/08/springviz/">earlier entry</a> I described how to use it to visualise Spring contexts. Today I want to showcase a different application.</p>
<p>Call graphs show how methods call each other, which can be useful for a variety of reasons. The example I use here is the graph rooted in a unit test suite, and in this case the graph gives an understanding of how localised the unit tests are, how much they are real unit tests or how close they are to mini-integration tests. In an ideal case the test method should call the method under test and nothing else. However, even with mock objects that&#8217;s not always practical. And if, like myself, you fall into the classicist camp of unit testers, as described by Martin Fowler in <a href="http://martinfowler.com/articles/mocksArentStubs.html">Mocks aren&#8217;t Stubs</a>, you might actually not be too fussed about a few objects being involved in a single test. In either case, looking at the call graph shows you exactly which methods are covered by which unit tests.</p>
<p>There are several ways to generate calls graphs and I&#8217;m opting for dynamic analysis, which simply records the call graph while the code is being executed. A good theoretical reason is that dynamic analysis can handle polymorphism but a more practical reason is that it&#8217;s actually really easy to do dynamic analysis; provided you use the right tools. The approach I describe in this article uses <a href="http://www.eclipse.org/ajdt/">Eclipse AJDT</a> to run the unit tests with a simple Java aspect that records the call graph and writes it out into a format that can be rendered more or less directly with Dot. Of course, this technique is not limited to creating graphs for unit test; it only depends on weaving an AspectJ aspect into a Java application.</p>
<p><span id="more-101"></span></p>
<p>Let&#8217;s start with the result. The following diagram shows the call graph for a few simple unit tests in the test suite for the CruiseControl dashboard. The tests methods are in the leftmost column and method invocations occur from left to right. It&#8217;s clearly visible that the tests are quite localised.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_simple.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_simple-600x250.png" alt="" title="CC Test Suite - Normal Call Graph" width="600" height="250" class="alignnone size-medium wp-image-113" /></a></p>
<p>The next example shows a section that&#8217;s a bit more intertwined. Some tests exercise the same method, some tests call more than one method, and some methods are called indirectly through different paths. All in all pretty reasonable, though.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_normal.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_normal-600x138.png" alt="" title="CC Test Suite - Normal Call Graph" width="600" height="138" class="alignnone size-medium wp-image-110" /></a></p>
<p>Now, I don&#8217;t want to showcase only the good parts. There are also sections in the graph that show how some of the test and dependencies are, well, a bit messy. In fairness, though, this is partly caused by some domain objets that are used across multiple tests.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_knot.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_knot-600x178.png" alt="" title="CC Test Suite - The Knot" width="600" height="178" class="alignnone size-medium wp-image-114" /></a></p>
<p>How hard can it be to create these graphs? Not very, as it turns out.</p>
<p><strong>Step 1</strong> is to create an aspect that intercepts all interesting method calls, which normally means calls to methods that are part of the project. It&#8217;s also possible, though, to include calls to libraries and frameworks. Depending on your experience with AspectJ this is as simple as follows:</p>
<pre><code>package com.example.callgraph;

public aspect CallInterceptor
{
    pointcut allInterestingMethods():
        !within(CallInterceptor) &#038;&#038; !within(CallLogger) &#038;&#038;
          execution(public * com.example..*(..));

    before() : allInterestingMethods() {
        CallLogger.INSTANCE.pushMethod(thisJoinPointStaticPart.getSignature());
        CallLogger.INSTANCE.logCall();
    }

    after() : allInterestingMethods() {
        CallLogger.INSTANCE.popMethod();
    }

}
</code></pre>
<p>The first part of the aspect is the definition of the pointcut: it matches all public methods in &#8220;com.example&#8221; and sub-packages but excludes calls to the two classes that form part of the call graph logger itself.</p>
<p>The <em>before</em> advice, which is run before each method that matches the pointcut, places the signature of the current method on a stack maintained by the graph logger class. With the current method signature as the topmost element and the one that called it as the second from the top, the advice asks the logger to log the corresponding call. The <em>after</em> advice simply pops the topmost call from the stack.</p>
<p>This is almost all the excitement. The implementation of <code>CallLogger</code> deals with maintaining the stack and writing out the call in the format required by Dot. The complete implementation with some comments follows below.</p>
<pre><code>package com.example.callgraph;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Stack;
import java.util.HashSet;
import java.util.Set;

import org.aspectj.lang.Signature;

public class CallLogger
{
    public static CallLogger INSTANCE = new CallLogger();
</code></pre>
<p>An instance is made available as a public singleton. The main reason for using a singleton, rather than creating an instance in the aspect, is that if coherent logging is required across multiple projects in Eclipse it&#8217;s easiest to add a copy of the aspect to each project and have all aspects use the same logger instance.</p>
<pre><code>    private Stack&lt;String&gt; callStack = new Stack&lt;String&gt;();
    private Set&lt;String&gt; callLog = new HashSet&lt;String&gt;();
    private Writer writer;
</code></pre>
<p>The fields hold the actual stack of method names, a set that contains all calls that have been logged already, and a writer to which the actual output is written. The set is a simple optimisation to prevent the same call from being written over and over again.</p>
<pre><code>     public CallLogger() {
        try {
            writer = new BufferedWriter(new FileWriter("calls.txt"));
        } catch (IOException e) {
            throw new RuntimeException("Cannot open 'calls.txt' for writing.", e);
        }
    }

    public void pushMethod(Signature s) {
        String type = s.getDeclaringType().getName();
        String method = type.substring(type.lastIndexOf('.') + 1) + "." + s.getName();
        callStack.push(method);
    }
</code></pre>
<p>If anyone knows a better way to turn the AspectJ signature into a pretty string, please leave a comment.</p>
<pre><code>    public void popMethod() {
        callStack.pop();
    }

    public void logCall() {
        if(callStack.size() < 2)
            return;
        String call = "\"" + top(1) + "\" -> \"" + top(0) +"\"";
        if(!callLog.contains(call)) {
            write(call);
            callLog.add(call);
        }
    }
</code></pre>
<p>Following a guard statement that protects the code from trying to log a call when only one method is on the stack, the code creates a description of the call in the format required by Dot for a graph edge, ie. the origin node in double quotes, followed by a stylised arrow, followed by the target node, again in double quotes.</p>
<p>If this call is not in the call log, the code writes it to the writer and then stores the call in the log so that it does not get written again.</p>
<pre><code>    private Signature top(int i) {
        return callStack.get(callStack.size() - (i + 1));
    }

    private void write(String line) {
        try {
            writer.write(line + "\n");
            writer.flush();
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
    }

}
</code></pre>
<p>Running some code, the unit tests in this example, will now create a file named &#8220;calls.txt&#8221; that contains all calls to methods that match the pointcut description in the aspect. The calls are written in Dot format but to make the output a valid input file for Dot a header and a closing bracket at the end are required.</p>
<p>It would be easy to write the required header when the writer is initialised but realistically it&#8217;s not possible to know when <code>logCall</code> is called for the last time, and thus it&#8217;s not possible to write the closing bracket from the Java code. Therefore&#8230;</p>
<p><strong>Step 2</strong> is a small shell script, or a script in your favourite scripting language, that wraps the required header and footer around the raw graph edges.</p>
<pre><code>#!/bin/bash

cat <<-EOS
strict digraph G {
    graph [ ratio="auto compressed",
            rankdir="LR",
            ranksep=2,
            remincross="true",
            ];
    node  [ shape=box,
            style=filled,
            fillcolor=white,
            fontname=helvetica
            fontsize=10,
            fontcolor=black
            ];
EOS

cat calls.txt

cat <<-EOS
    }
EOS
</pre>
<p></code></p>
<p>I have an admission to make: This script is simplified and would not create the output I've shown further up. With a graph definition like this Dot would not put all unit tests nodes into the leftmost column but instead it would move them just left of the method they call. If, for example, test 1 called method A, which in turn called method B, and test 2 called method B directly, then test 1 would be in the first column, method A and test 2 in the second column and method B in the third column. The following UNIX command line wizardry, when added just below the <code>cat calls.txt</code> line in the script above, solves this problem. The explanation is left as an exercise to the reader...</p>
<pre><code>echo '{ rank = same; '
fgrep Test.test calls.txt | awk -F " -> " '{ print $1 }' | sort -u | awk '{ printf "%s; ", $1 }'
echo '}'
</pre>
<p></code></p>
<p>I hope this article shows how two simple but powerful tools combined make it easy to create a really useful visualisation; and how clever but cryptic UNIX command lines can be.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/09/call-graph-visualisation-with-aspectj-and-dot/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SpringViz and the 1000ft view</title>
		<link>http://erik.doernenburg.com/2008/08/springviz/</link>
		<comments>http://erik.doernenburg.com/2008/08/springviz/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 02:07:30 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=47</guid>
		<description><![CDATA[The Spring framework has become ubiquitous in the Java world, and there are a large number of tools supporting developers of Spring-based applications. In this post I describe SpringViz; or, more accurately, my variant of it.
SpringViz helps developers with what is at the heart of a Spring-based application, the container and the contexts files that [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.springframework.org/about">Spring framework</a> has become ubiquitous in the Java world, and there are a large number of tools supporting developers of Spring-based applications. In this post I describe <a href="http://www.samoht.com/wiki/wiki.pl?SpringViz">SpringViz</a>; or, more accurately, my variant of it.</p>
<p>SpringViz helps developers with what is at the heart of a Spring-based application, the container and the contexts files that describe the beans. In larger projects these context files can grow quite a bit. Newer versions of the Spring framework introduced features that help reduce the clutter and there are vast numbers of blog posts voicing different opinions on what should and what shouldn&#8217;t be in a context file but, no matter what, the number of beans in the context files will grow with the size of a project, and at some point it becomes difficult to understand the overall structure. This is no different from trying to maintain an understanding of a large codebase. In fact, I consider the context files to be code rather than configuration.</p>
<p>I&#8217;ve argued before (<a href="http://97-things.near-time.net/wiki/show/Get%20the%201000ft%20view">here</a> and <a href="http://www.infoq.com/interviews/erik-doernenburg-software-visualization">here</a> for example) that to deal with the complexity and sheer size of software systems we need a 1000ft view. This is a view that uses visualisation techniques to aggregate large amounts of data and multiple metrics into one big picture. SpringViz provides that 1000ft view for Spring context files.</p>
<p><span id="more-47"></span></p>
<p>Let&#8217;s look at an example first. The following diagram shows the context files for the Spring JPetStore example application (click on the image for the full-size version):</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/08/jpetstore_beans.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/08/jpetstore_beans_small.png" alt="Diagram showing the context files and beans contained in them for the Spring JPetSore example application" title="JPetStore Beans" width="600" height="424" class="size-full wp-image-81" /></a></p>
<p>I find that this diagram presents the architecture of the application quite nicely. The web controllers are defined in the petstore-servlet context in the top left and many of them have a dependency on the petStore bean, defined in the main application context. The petStore bean in turn has dependencies on all the DAOs, which are defined in the data access context. In that context we can clearly see how the top four DAOs share the same data source while the <em>Order</em> and <em>Sequence</em> DAOs use a different one. In addition to the individual dependencies we also get an impression of the overall architecture: a simple, multi-layered design with a single central choke point that acts as an adaptor between the presentation and data access layers.</p>
<p>From personal experience I can say that the bigger and more complex a system gets, the more valuable these diagrams become. In many cases they not only help share a common understanding of the architecture in the development team but they also help spot inconsistencies in the design and often trigger and support discussions around important restructurings. Ok, this is all I&#8217;m going to say to convince you to try out SpringViz. In the following paragraphs I&#8217;ll explain how to use it on your own projects.</p>
<p><strong>Step 1:</strong> You need a copy of SpringViz. The version I&#8217;m discussing in this article can be downloaded <a href="http://erik.doernenburg.com/wp-content/uploads/2008/08/springviz-20080813.zip">here</a>. When you unzip the archive you should have an Ant build file, a <em>springviz</em> folder with a few files in it and an <em>example</em> folder that contains the context files for JPetStore.</p>
<p><strong>Step 1b:</strong> SpringViz uses <a href="http://www.graphviz.org/">GraphViz Dot</a> to render the diagram. So, if you don&#8217;t have this on your system you need to download and install it. You should also open the build file and change the <em>dot.command</em> property to point to the Dot executable. On the Mac you should really get <a href="http://www.pixelglow.com/graphviz/">Pixelglow&#8217;s version</a> of GraphViz.</p>
<p><strong>Step 2:</strong> You can now run SpringViz by calling Ant with the default target. This will create a <em>target</em> folder, process the context files with the SpringViz XSLT sheet to create an input file for GraphViz Dot, and then invoke Dot to create <em>beans.png</em>, the final diagram.</p>
<p><strong>Step 3:</strong> To integrate SpringViz into your own projects you can simply add the contents of the build file to your build file, place the <em>springviz</em> folder in an appropriate place, and adjust the configuration properties at the top of the build file.</p>
<p>If you are adventurous you can have a look at the Dot documentation and change the XSLT sheet to create slightly different input for Dot. You could colour the beans according to all sorts of criteria or you could change other aspects of their appearance depending on bean attributes.</p>
<p>There are a few limitations that you should be aware of: In its current version the system can&#8217;t deal with multiple beans with the same name defined in different contexts. So, if you have a <em>defaultUrlMapping</em> bean or similar in multiple files and you have dependencies to/from these the resulting diagram will be wrong. In this case it&#8217;s easiest to modify the style sheet to filter out the problematic beans. Another important point is that in its current form the sheet simply tries to use a bean&#8217;s id and name, which usually works out but will cause problems when beans have both, name and id. And lastly, the XSLT processor will need access to the web to fetch the schema files. I guess this can be avoided but so far this hasn&#8217;t been a problem for me.</p>
<p><strong>If you missed the link above you can download the specific version of SpringViz discussed in this article from <a href="http://erik.doernenburg.com/wp-content/uploads/2008/08/springviz-20080813.zip">this site</a>. The latest version of SpringViz as well as more documentation and resources are available on <a href="http://www.samoht.com/wiki/wiki.pl?SpringViz">Mike Thomas&#8217;s site</a></strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/08/springviz/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Testing Cocoa Controllers with OCMock</title>
		<link>http://erik.doernenburg.com/2008/07/testing-cocoa-controllers-with-ocmock/</link>
		<comments>http://erik.doernenburg.com/2008/07/testing-cocoa-controllers-with-ocmock/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 04:33:27 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=44</guid>
		<description><![CDATA[For a few releases the Apple development tools have included OCUnit and many developers have now started to write unit tests. There are lots of tutorials that explain how this is done for the straight-forward cases but there&#8217;s one area of testing that has proven difficult on most platforms, and that is testing of the [...]]]></description>
			<content:encoded><![CDATA[<p>For a few releases the Apple development tools have included OCUnit and many developers have now started to write unit tests. There are lots of tutorials that explain how this is done for the straight-forward cases but there&#8217;s one area of testing that has proven difficult on most platforms, and that is testing of the user interface. That said, there are a few things that make this an easier problem to solve with Cocoa and in this post I&#8217;ll explain why.</p>
<p><span id="more-44"></span></p>
<p>Let&#8217;s look at an example. <a href="http://ccmenu.sourceforge.net/">CCMenu</a> is a small application that displays the status of <a href="http://cruisecontrol.sourceforge.net/">CruiseControl</a> continuous integration servers. As part of adding a new project to be monitored the user has to enter the URL for the CruiseControl server and a combox provides a history of previously used servers.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/07/ccmenu_snapshot1.png"><img class="alignnone size-full wp-image-45" title="CCMenu Snapshot 1" src="http://erik.doernenburg.com/wp-content/uploads/2008/07/ccmenu_snapshot1.png" alt="" width="449" height="264" /></a></p>
<p>The same dialog has a matrix of buttons to set the type of the server. Actually, by default CCMenu detects the server type automatically but that&#8217;s beside the point. What we&#8217;re interested in is the piece of functionality that is responsible for selecting the correct server type for the URL chosen by the user. In our case, this would be Cruise Control Dashboard.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/07/ccmenu_snapshot2.png"><img class="alignnone size-full wp-image-46" title="CCMenu Snapshot 2" src="http://erik.doernenburg.com/wp-content/uploads/2008/07/ccmenu_snapshot2.png" alt="" width="449" height="264" /></a></p>
<p>One difference between Cocoa and most other UI frameworks is the fact that the user interface is stored by serialising the objects, rather than generating code. For this reason I&#8217;m happy to not insist on test-first for the actual interface. What I do want to test is the code in the controller classes.</p>
<p>Obviously, I could load the serialised objects, locate the elements involved and then use methods such as performClick: to trigger the actions. Sounds convoluted? Yes, I agree. Luckily there&#8217;s a better way.</p>
<p>This is a case where testing is tricky because the object under test interacts with objects that are difficult to deal with. In such cases dynamic mock objects have proven extremely useful. A good introduction can be found <a href="http://martinfowler.com/articles/mocksArentStubs.html">here</a> and <a href="http://mockobjects.com/">mockobject.com</a> lists papers and implementations. For Objective-C I created <a href="http://www.mulle-kybernetik.com/software/OCMock/">OCMock</a>. Let&#8217;s look at how this helps us testing our controllers.</p>
<p>Here are the relevant parts of the interface declaration of the controller. Given that I&#8217;m doing test-driven development there&#8217;s currently no implementation of the methods. I simply created the interface of the controller based on my needs while designing the dialog.</p>
<pre><code>@interface CCMPreferencesController : CCMWindowController
{
    IBOutlet NSComboBox *serverUrlComboBox;
    IBOutlet NSMatrix *serverTypeMatrix;
}

- (IBAction)historyURLSelected:(id)sender;
</code></pre>
<p>In my test I want to use mock objects instead of loading the actual user interface. So, in the test setup, after creating my controller, I create appropriate mock objects and set up the controller to use these.</p>
<pre><code>@implementation CCMPreferencesControllerTest

- (void)setUp
{
    controller = [[[CCMPreferencesController alloc] init] autorelease];
    serverUrlComboBoxMock = [OCMockObject mockForClass:[NSComboBox class]];
    [controller setValue:serverUrlComboBoxMock forKey:@"serverUrlComboBox"];
    serverTypeMatrixMock = [OCMockObject mockForClass:[NSMatrix class]];
    [controller setValue:serverTypeMatrixMock forKey:@"serverTypeMatrix"];
}
</code></pre>
<p>You notice that I&#8217;m using key-value coding to set the variables (outlets). I&#8217;m doing this because the variables are not public and at the same time I don&#8217;t want to write accessor methods for them.</p>
<p>Now, we can start writing the actual test for the functionality. Have a look at the full test first.</p>
<pre><code>- (void)testSelectsServerTypeWhenHistoryURLIsSelected
{
    NSString *selectedUrl = @"http://localhost/cctray.xml";
    [[[serverUrlComboBoxMock stub] andReturn:selectedUrl] stringValue];
    [[serverTypeMatrixMock expect] selectCellWithTag:CCMCruiseControlDashboard];
    [controller historyURLSelected:nil];
}
</code></pre>
<p>What&#8217;s going on here? Firstly, I tell the mock object that stands in for the actual combox box that it should stub the stringValue method. This means that when somebody invokes stringValue on this object it will return the string @&#8221;http://localhost/cctray.xml&#8221;. This is all we&#8217;d have done with the real combo box anyway.</p>
<p>Secondly, I tell the mock that stands in for the server type matrix that it should expect that the method selectCellWithTag: is called with CCMCruiseControlDashboard as an argument.</p>
<p>Lastly, I invoke the method I want to test. What happens, once the implementation is complete, is that the code will go the the combo box and ask it for its string value. The first mock will return the stubbed value. Now, the code should do whatever it needs to do to figure out which server type this corresponds to and then set that in the server type matrix by selecting the cell with the appropriate tag, and this is what we&#8217;ve told the second mock to expect.</p>
<p>Wait, you might say, we don&#8217;t have an implementation yet. So, how does this test fail? It doesn&#8217;t yet. We&#8217;ll have to tell the mock objects to verify that everything we told them to expect actually occurred, and a logical place for this is the test tearDown.</p>
<pre><code>- (void)tearDown
{
    [serverUrlComboBoxMock verify];
    [serverTypeMatrixMock verify];
}
</code></pre>
<p>Strictly speaking, we don&#8217;t have to verify the combo box mock because it doesn&#8217;t have any expectations but it&#8217;s good practice to verify all mocks in the tear down, especially if the same mocks are used for multiple tests. By the way, by default the mocks also have fail-fast behaviour; when they receive a method that wasn&#8217;t stubbed or expected, they raise an exception right away. Detecting something that wasn&#8217;t expected can be done right way, detecting that something that was expected didn&#8217;t occur must be trigger by the user.</p>
<p>Now, if we run this test with an empty implementation of historyURLSelected: it will fail when we tell the server type matrix mock to verify because the expected method hasn&#8217;t been called. The error message will look something like this:</p>
<p><code>Unknown.m:0: error: -[CCMPreferencesControllerTest testSelectsServerTypeWhenHistoryURLIsSelected] : OCMockObject[NSMatrix]: expected method was not invoked: selectCellWithTag:0</code></p>
<p>Adding an implementation like the following one adds the right functionality and makes our test pass.</p>
<pre><code>@implementation CCMPreferencesController

- (void)historyURLSelected:(id)sender
{
    NSString *serverUrl = [serverUrlComboBox stringValue];
    [serverTypeMatrix selectCellWithTag:[serverUrl cruiseControlServerType]];
}
</code></pre>
<p>In summary, testing controllers becomes relatively easy when we follow this pattern:</p>
<p>1. Replace all UI elements with mocks; using key-value coding to access the outlets.</p>
<p>2. Set up stubs with return values for UI elements that the controller will query.</p>
<p>3. Set up expectations for UI elements that the controller should manipulate.</p>
<p>4. Invoke the method in the controller.</p>
<p>5. Verify the expectations.</p>
<p>I find tests following this pattern easier to write and understand than tests that load a NIB file and interact with the actual user interface elements.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/07/testing-cocoa-controllers-with-ocmock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OCMock 1.24 released</title>
		<link>http://erik.doernenburg.com/2008/05/ocmock-124-released/</link>
		<comments>http://erik.doernenburg.com/2008/05/ocmock-124-released/#comments</comments>
		<pubDate>Tue, 13 May 2008 10:02:36 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Releases]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=40</guid>
		<description><![CDATA[Another major improvement of OCMock: it now supports more flexible constraints on the expected arguments. This is done in the Objective-C way and user-defined constraints don&#8217;t have to implement a formal interface, they&#8217;re just methods in the test class. As usual this release also includes several contributions from the community. More details on the OCMock [...]]]></description>
			<content:encoded><![CDATA[<p>Another major improvement of OCMock: it now supports more flexible constraints on the expected arguments. This is done in the Objective-C way and user-defined constraints don&#8217;t have to implement a formal interface, they&#8217;re just methods in the test class. As usual this release also includes several contributions from the community. More details on the <a href="http://www.mulle-kybernetik.com/software/OCMock/">OCMock page</a> at Mulle Kybernetik.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/05/ocmock-124-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Guardian podcast</title>
		<link>http://erik.doernenburg.com/2008/05/guardian-podcast/</link>
		<comments>http://erik.doernenburg.com/2008/05/guardian-podcast/#comments</comments>
		<pubDate>Sun, 04 May 2008 09:28:12 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Clippings]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=43</guid>
		<description><![CDATA[Of the many projects I have worked on the rewrite of the Guardian website is certainly a highlight. And in this case I can even speak about it in detail. In fact, Mat Wall from the Guardian and I presented some of our experiences at several conferences, and now the Software Engineering Radio has published [...]]]></description>
			<content:encoded><![CDATA[<p>Of the many projects I have worked on the rewrite of the <a href="http://guardian.co.uk">Guardian website</a> is certainly a highlight. And in this case I can even speak about it in detail. In fact, Mat Wall from the Guardian and I presented some of our experiences at <a href="http://erik.doernenburg.com/talks/#gu">several conferences</a>, and now the Software Engineering Radio has published a <a href="http://www.se-radio.net/podcast/2008-05/episode-95-new-guardiancouk-website-matt-wall-and-erik-doernenburg"> podcast</a> in which we talk about this project.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/05/guardian-podcast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The ThoughtWorks Anthology</title>
		<link>http://erik.doernenburg.com/2008/03/the-thoughtworks-anthology/</link>
		<comments>http://erik.doernenburg.com/2008/03/the-thoughtworks-anthology/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 07:12:19 +0000</pubDate>
		<dc:creator>erik</dc:creator>
				<category><![CDATA[Clippings]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/2008/03/26/the-thoughtworks-anthology/</guid>
		<description><![CDATA[It has been in the making for some time but now the ThoughtWorks Anthology is available from the Pragmatic Programmers. The Anthology is a collection of essays written by individual authors from ThoughtWorks covering a wide range of topics that we encounter on our projects. My essay finally gets our thinking on Domain Annotations into [...]]]></description>
			<content:encoded><![CDATA[<p>It has been in the making for some time but now the ThoughtWorks Anthology is available from the Pragmatic Programmers. The Anthology is a collection of essays written by individual authors from ThoughtWorks covering a wide range of topics that we encounter on our projects. My essay finally gets our thinking on Domain Annotations into writing, after we had presented the ideas at a <a href="/talks/#domain-annotations">couple of conferences</a>. A more in-depth description of the Anthology can be found on the <a href="http://www.pragprog.com/titles/twa">Pragmatic Bookshelf</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/03/the-thoughtworks-anthology/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
