<?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>Blog &#187; java</title>
	<atom:link href="http://blog.bprog.no/index.php/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bprog.no</link>
	<description>Some personal and some technical posts</description>
	<lastBuildDate>Sun, 14 Jun 2009 19:04:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Embedded Java LDAP server for unit testing</title>
		<link>http://blog.bprog.no/index.php/2009/04/embedded-java-ldap-server-for-unit-testing/</link>
		<comments>http://blog.bprog.no/index.php/2009/04/embedded-java-ldap-server-for-unit-testing/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 19:20:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[apacheds]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[opends]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.bprog.no/?p=114</guid>
		<description><![CDATA[I&#8217;ve just spent about a day trying different embeddable Java LDAP servers. To save you from all the trouble I came across I&#8217;m going to share some of my experiences.
Basically you have three choices when it comes to  embeddable Java LDAP servers (at least open source). First of you have Sun&#8217;s OpenDS, they have a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just spent about a day trying different embeddable Java LDAP servers. To save you from all the trouble I came across I&#8217;m going to share some of my experiences.</p>
<p>Basically you have three choices when it comes to  embeddable Java LDAP servers (at least open source). First of you have Sun&#8217;s <a href="https://opends.dev.java.net/">OpenDS</a>, they have a page dedicated to <a href="https://www.opends.org/wiki/page/TipsAndTricksForWritingUnitTests">Tips and tricks for writing unit tests</a>. I have to say that I&#8217;m not very impressed by OpenDS. Don&#8217;t take me wrong it might be a fantastic LDAP server, but it certainly does not give me the feeling of being very embeddable! First of all the documentation refers to a mysterious class TestCaseUtils. I&#8217;ve checked builds from version 1.0.something to 1.2.something without finding it. The second drawback is that OpenDS is like a flesh-eating-configuration-monster sent back in time to kill the spirit of any developers getting near it. It comes with a 2000+ lines ldif config file, which is supposed to work for servers running embedded as well. It would be nice if they could provide a minimal configuration file for embedded use. To me it complained a lot about errors in the bundled schemas. Oh that&#8217;s right you need to set up a directory structure for config files, schemas and locks. Again, not very embeddable!</p>
<p>The second and third choise is <a href="http://directory.apache.org/">ApacheDS.</a> You have the newer version 1.5. with support for fancy things like triggers and stuff and you have the older 1.0 series. Both LDAPv3 compatible. The project is very well documented and they even maintain something they call Apache Directory Studio, which is a graphical LDAP GUI based on Eclipse. Very nice! If you don&#8217;t need the fancy stuff implemented in the 1.5 series I really recommend that you go for the older 1.0 series for your unit testing. You&#8217;ll find jars in Maven repos and it&#8217;s really simple to get started. It just works the way you expect it to! Just follow this <a href="http://directory.apache.org/apacheds/1.0/using-apacheds-for-unit-tests.html">guide</a> and you will be unit testing your LDAP code in no time.</p>
<p>At the time I&#8217;m writing this 1.5.4 is the newest version of the 1.5 series. They have a <a href="http://directory.apache.org/apacheds/1.5/42-using-apacheds-for-unit-tests.html">guide for doing unit testing with 1.5 as well</a>, but due to some problems it won&#8217;t work with 1.5.4 so you&#8217;ll have to build the trunk for your self or track down a Maven snapshot repo (good luck!).</p>
<p>Happy testing!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bprog.no/index.php/2009/04/embedded-java-ldap-server-for-unit-testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unit testing private methods in Java</title>
		<link>http://blog.bprog.no/index.php/2009/01/unit-testing-private-methods-in-java/</link>
		<comments>http://blog.bprog.no/index.php/2009/01/unit-testing-private-methods-in-java/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 01:26:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.bprog.no/?p=101</guid>
		<description><![CDATA[Want to unit test a private method in Java?
You could obviously change your methods access modifier from private. By making it public you&#8217;re breaking encapsulation and by making it protected you&#8217;re making your API less intuitive. Don&#8217;t do that.
Another way to go about this is to create a new class that&#8217;s extending the one with [...]]]></description>
			<content:encoded><![CDATA[<p>Want to unit test a private method in Java?</p>
<p>You could obviously change your methods access modifier from private. By making it public you&#8217;re breaking encapsulation and by making it protected you&#8217;re making your API less intuitive. Don&#8217;t do that.</p>
<p>Another way to go about this is to create a new class that&#8217;s extending the one with your private method</p>
<p>I find the easiest and most convenient way achieve unit testing of private methods is <a href="http://java.sun.com/docs/books/tutorial/reflect/index.html">reflection</a>. Some of the features the Java reflection API offers is access to private fields and invocation of private methods. This sounds like something useful for our problem, by using reflection we&#8217;re able to test our private methods without making any changes to our classes.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> TestSubject <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * For some reason we want to unit-test this method
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> adder<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> numberOne, <span style="color: #000066; font-weight: bold;">int</span> numberTwo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> answer <span style="color: #339933;">=</span> numberOne <span style="color: #339933;">+</span> numberTwo<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> answer<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the method we want to test, here is how we go around to actually do it:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">TestSubject subjectInstance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TestSubject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">Class</span> testClass <span style="color: #339933;">=</span> subjectInstance.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Find the method. getDeclaredMethod will give you any methods from that</span>
<span style="color: #666666; font-style: italic;">// class whether it's public or not where getMethod only will give you</span>
<span style="color: #666666; font-style: italic;">// methods with public access. The first argument is obviously our method's name,</span>
<span style="color: #666666; font-style: italic;">// where the next arguments are the formal types of the adder methods parameters.</span>
<span style="color: #003399;">Method</span> adderMethod <span style="color: #339933;">=</span> testClass.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;adder&quot;</span>, <span style="color: #000066; font-weight: bold;">int</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000066; font-weight: bold;">int</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Method is a sub class of AccessibleObject if we don't call it's</span>
<span style="color: #666666; font-style: italic;">// setAccessible method we'll get an IllegalAccessException when we</span>
<span style="color: #666666; font-style: italic;">// try to invoke it on our object.</span>
adderMethod.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// Invoke the method on our instance of TestSubject</span>
<span style="color: #000066; font-weight: bold;">int</span> answer <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> adderMethod.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>subjectInstance, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// assert answer = 4</span></pre></div></div>

<p>It&#8217;s more or less just to plug it into your testing framework of choice and you&#8217;ll have unit testing of your private methods.</p>
<p>But hey! Do you <em>really</em> want to unit test your private methods? Doesn&#8217;t it give you that bad feeling in your stomach. You know the feeling you get when you&#8217;re code smells a little? I mean, a part of the reason for those methods being private is that they&#8217;re subject to change.</p>
<p>It&#8217;s the oldest oo rule in the book, hide the inner working of you&#8217;re classes so you later can change the inner workings of the class without exposing any change to your clients. What you really want to do is to unit test the public methods using those private once. If you find that difficult it <em>might</em> be a little hint that you should consider changing your design a little in order to improve it&#8217;s testability.</p>
<p>Why did I bother showing you how to unit test private methods if I consider it bad practice? Well, sometimes when I&#8217;m working on complicated things I like to unit-test private methods while working on the class. And there might be situations where unit testing of private methods is the right way to go. Just be careful so you&#8217;re not writing a lot of tests you&#8217;ll waste a lot of time maintaining later!</p>
<p>Relevant Java API: <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html">Class</a>, <a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/Method.html">Method</a> and <a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/package-summary.html">reflect</a>.</p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bprog.no/index.php/2009/01/unit-testing-private-methods-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to auto generate UML class diagrams in JavaDoc with Maven2 and Apiviz</title>
		<link>http://blog.bprog.no/index.php/2009/01/auto-generating-uml-class-diagrams-in-javadoc/</link>
		<comments>http://blog.bprog.no/index.php/2009/01/auto-generating-uml-class-diagrams-in-javadoc/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 22:43:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javadoc]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[uml]]></category>

		<guid isPermaLink="false">http://blog.bprog.no/?p=72</guid>
		<description><![CDATA[For some time I&#8217;ve been looking for a way to auto generate UML diagrams inside JavaDoc files. The perfect solution would integrate into Maven 2, which is my build system of choice. A few days ago I stumbled across Apiviz, which turned out to be exactly what I&#8217;ve been looking for! It provides simple integration [...]]]></description>
			<content:encoded><![CDATA[<p>For some time I&#8217;ve been looking for a way to auto generate UML diagrams inside JavaDoc files. The perfect solution would integrate into Maven 2, which is my build system of choice. A few days ago I stumbled across <a title="ApiWiz" href="http://code.google.com/p/apiviz/">Apiviz</a>, which turned out to be exactly what I&#8217;ve been looking for! It provides simple integration with Maven, Ant and Eclipse.</p>
<p>By default it draws implementation of interfaces and inheritance and if you add some tags / annotations to your JavaDoc it will draw composition, aggregation, dependencies and navigable relationships as well. There are some undocumented options as well. I&#8217;ve used the <a title="Netty" href="http://www.jboss.org/netty/">Netty</a> source code as a reference as it seems to be the project maintainer of Netty that&#8217;s behind ApiWiz (shoot me if I&#8217;m wrong).</p>
<p>I&#8217;m in progress of uncovering some of the undocumented features. If someone knows something I appreciate a comment below.</p>
<div id="attachment_75" class="wp-caption alignnone" style="width: 310px"><a href="http://www.jboss.org/file-access/default/members/netty/freezone/api/3.0/index.html"><img class="size-medium wp-image-75" title="Example from the Netty project" src="http://blog.bprog.no/wp-content/uploads/2009/01/package-summary-300x131.png" alt="Example from the Netty project" width="300" height="131" /></a><p class="wp-caption-text">Example from the Netty project</p></div>
<p><strong>Update:</strong> After digging through the source code (gotta love open source!) I found the documentation missing from the web page! This documentation shows how to add labels to the edges.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">apiviz<span style="color: #339933;">.</span>uses
<span style="color: #339933;">&lt;</span>FQCN<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;</span>sourceLabel<span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;</span>targetLabel<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;</span>edgeLabel<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span>
&nbsp;
apiviz<span style="color: #339933;">.</span>has
<span style="color: #339933;">&lt;</span>FQCN<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#91;</span>oneway<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;</span>sourceLabel<span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;</span>targetLabel<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;</span>edgeLabel<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span>
&nbsp;
apiviz<span style="color: #339933;">.</span>owns
<span style="color: #339933;">&lt;</span>FQCN<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;</span>sourceLabel<span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;</span>targetLabel<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;</span>edgeLabel<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span>
&nbsp;
apiviz<span style="color: #339933;">.</span>composedOf
<span style="color: #339933;">&lt;</span>FQCN<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;</span>sourceLabel<span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;</span>targetLabel<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;</span>edgeLabel<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span></pre></div></div>

<p><em>FQCN is short for &#8220;Fully Qualified Class Name&#8221;</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bprog.no/index.php/2009/01/auto-generating-uml-class-diagrams-in-javadoc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
