<?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>Schauderhaft &#187; Java</title>
	<atom:link href="http://blog.schauderhaft.de/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Sun, 05 Feb 2012 20:46:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How Scala changed the way I think about my Java Code</title>
		<link>http://blog.schauderhaft.de/2011/08/14/how-scala-changed-the-way-i-think-about-my-java-code/</link>
		<comments>http://blog.schauderhaft.de/2011/08/14/how-scala-changed-the-way-i-think-about-my-java-code/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 12:22:17 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=885</guid>
		<description><![CDATA[Some people advocate their preferred language as the only way to enlightenment and productivity boosts way in the two digit percentage range compared to another language in the same category. I don&#8217;t believe it. (It&#8217;s probably true when you compare things like Java and Assembler, but few do that) There are others that tell you [...]]]></description>
			<content:encoded><![CDATA[<p>Some people advocate their preferred language as the only way to enlightenment and productivity boosts way in the two digit percentage range compared to another language in the same category. I don&#8217;t believe it. (It&#8217;s probably true when you compare things like Java and Assembler, but few do that)</p>
<p>There are others that tell you the language doesn&#8217;t matter at least not between languages like Scala and Java. I think they are wrong as well. The reason is: Although I don&#8217;t actually use Scala during my day job it does affect my Java Coding. Here are some of the things I noticed.</p>
<ul>
<li>I appreciate immutability. I was aware for a long time of the benefits of immutability. But in Java using immutable data structures is so clumsy that I only used this approach in small areas, like rather simple value objects. But now I find my self more often tempted to use an immutable data structure even for relatively complex things.</li>
<li>Lots of small classes. Classes in Scala are unbelievable small. It&#8217;s easy to write a single page of code with half a dozen classes in it if you count the &#8216;class&#8217;, &#8216;object&#8217; and &#8216;trait&#8217; keywords and even more when you look at what the Scala compiler creates from closures and similar. Although creating a class is much more work in Java I find myself thinking much strict about the single responsibility principle, resulting in more smaller classes, which are easier to test.</li>
<li>Thinking more in DSLs. Scala is great for creating internal DSLs. Lots of libraries use that approach for designing their APIs and I do it in my little projects as well. Java is pretty useless for DSLs. But there are things you can do. Even if it is simple stuff like method chaining. I use that a lot lately, especially when testing is concerned, because here readability of code is even more important then with normal code.</li>
<li>I pretty much gave up on Java Generics. After seeing what is possible with a good type system, even when the compiled code runs on the same JVM as Java, made me also see the severe limitations of Java Generics. Before that I tended to think its just me who is to stupid. While this assumption is often true. Many things I tried to do in the past with Java Generics just can&#8217;t work, because Java is lacking the necessary power (e.g. covariance and contravariance). So I drop generics from my code as soon as more then one type parameter gets involved.</li>
</ul>
<p>So go ahead and learn a new language. Even if you can&#8217;t use it in your normal job. You still will learn useful stuff. But be warned. I might as well make you hate parts of your day job:</p>
<p>I really, really hate semicolons, primitives, arrays and collection APIs without higher order functions. And switching on Strings is a joke.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2011/08/14/how-scala-changed-the-way-i-think-about-my-java-code/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>It&#8217;s not in the Genes its in the Environment</title>
		<link>http://blog.schauderhaft.de/2011/02/13/its-not-in-the-genes-its-in-the-environment/</link>
		<comments>http://blog.schauderhaft.de/2011/02/13/its-not-in-the-genes-its-in-the-environment/#comments</comments>
		<pubDate>Sun, 13 Feb 2011 20:18:09 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Management]]></category>
		<category><![CDATA[complexity]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=724</guid>
		<description><![CDATA[A late comparision of Ruby on Rails with Open Xava demonstrates that you can develop Java based applications fast. This lead another blogger to the conclusiong that java developers do love complicated architectures. I think this view ia a little to simplistic. Java developers like fast results and progress just as all the other developers. [...]]]></description>
			<content:encoded><![CDATA[<p>A late comparision of <a href="http://eclipse.sys-con.com/node/965189">Ruby on Rails with Open Xava</a> demonstrates that you can develop Java based applications fast. This lead another blogger to the conclusiong that <a href="http://java.dzone.com/articles/java-kicks-rails-butt-are-java">java developers do love complicated architectures</a>.</p>
<p>I think this view ia a little to simplistic. Java developers like fast results and progress just as all the other developers. But if you have a look at the environments in which java is used, you&#8217;ll often find large corporations and that is causing problems. If you are lucky enough not to have worked in a large company let me describe this wonderful experience.</p>
<p>In the typical large company you can&#8217;t just talk to the future users, ask them about their problems and solve those using software. You also have to adhere to all kinds of rules. Rules about how to implement the login. Rules about how to talk to other systems. Rules how to format your code. Rules about how to blow your nose. All those rules easily amount to one or two thousand pages.</p>
<p>While the amount of rules certainly is a problem rule alone aren&#8217;t a problem. I&#8217;m an absolute fan of rules in order to steer software developments. The problem with those &#8220;Large Company Rules&#8221; (LCR) is they are based from the largest most complex (or complicated) system the company ever encountered. Thus they dictate an architecture fit for a system consisting of 20 subsystems doing some thousand transactions per minute.</p>
<p>But surprisingly most of the systems are really quite simple with only few users. Yet they have to use everything IBM sells.</p>
<p>So all the java developers get used to EJBs, SOAP, Messaging and what not and many think this is the only way to write systems.</p>
<p>And that is the thing I blame many developers for: To many ignore what happens outside their current project. They don&#8217;t read, they don&#8217;t learn. And while I think the companies are guilty of making everybody think development in java is slow and complicated every single developer is guilty when at some day in the future his current project ends and he realizes there is no more demand for his skill set.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2011/02/13/its-not-in-the-genes-its-in-the-environment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Exit Strategy</title>
		<link>http://blog.schauderhaft.de/2010/11/21/my-exit-strategy/</link>
		<comments>http://blog.schauderhaft.de/2010/11/21/my-exit-strategy/#comments</comments>
		<pubDate>Sun, 21 Nov 2010 11:34:28 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[future]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=645</guid>
		<description><![CDATA[The last months have been extremely busy. Three different projects causing lots of work and sometimes worries, three different conferences I attended as a speaker and various talks at the local JUG. This made it hard to actually think about what I want to do next year. Now all the busy stuff is over (although [...]]]></description>
			<content:encoded><![CDATA[<p>The last months have been extremely busy. Three different projects causing lots of work and sometimes worries, three different conferences I attended as a speaker and various talks at the local JUG. This made it hard to actually think about what I want to do next year. Now all the busy stuff is over (although somebody mentioned some kind of holiday coming up). So I made up my mind, what I want to do in the next year:</p>
<ul>
<li>I want to as much Scala development as possible. I really like that language and it offers tons of inspirations while still allowing me to build on my Java experience.</li>
<li>I want to learn a little Clojure. Just because I like making knots in my brain.</li>
<li>I will do some web development. My skills in that area are a little rusty and I don&#8217;t need much of them in my current main project, but I sure don&#8217;t want to do Swing development for ever.</li>
<li>And of course I will continue to read as much as possible about a wide range of topics.</li>
</ul>
<p>I&#8217;m doing this because I actually think it is time to say good bye to Java. The development of Java stagnated in the past and I don&#8217;t see any sign that its new owner is going to change that. So I need an alternative. For me that&#8217;s Scala.</p>
<p>On the other hand I&#8217;m kind of confident, that the JVM will survive for quite some time in a state that makes it a strong, interesting platform.</p>
<p>Of course I&#8217;m no prophet. Maybe Google buys Java and within two years it is the coolest language on the planet &#8230; if so, some Scala experience won&#8217;t hurt, because a cool Java will certainly contain lots of features from Scala.</p>
<p>Maybe Scala dies or something bad happens to the JVM. In that case  the broad range of programming concepts will make it easy to switch to any language of interest. So I&#8217;m feeling confident that my future as a software developer stays safe and interesting.</p>
<p>If I&#8217;d stayed with Java as my main language I still would feel safe &#8230; but I&#8217;d be not so sure about the &#8216;interesting&#8217; part.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/11/21/my-exit-strategy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More on JUnit Theories</title>
		<link>http://blog.schauderhaft.de/2010/02/07/junit-theories/</link>
		<comments>http://blog.schauderhaft.de/2010/02/07/junit-theories/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 13:02:21 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=404</guid>
		<description><![CDATA[In my last blog post, I described how to use JUnit Theories to create large amounts of test runs, with very limited amount of work, like so: import static org.junit.Assume.assumeTrue; @RunWith&#40;Theories.class&#41; public class TheorieTest &#123; &#160; &#160;@DataPoint &#160;public static String a = &#34;a&#34;; &#160; &#160;@DataPoint &#160;public static String b = &#34;bb&#34;; &#160; &#160;@DataPoint &#160;public static [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="/2010/01/31/new-feature-of-junit-theories/">blog post</a>, I described how to use JUnit Theories to create large amounts of test runs, with very limited amount of work, like so:</p>
<div class="geshi no java">
<div class="head">import static org.junit.Assume.assumeTrue;</div>
<ol>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> TheorieTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> a = <span class="st0">&quot;a&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> b = <span class="st0">&quot;bb&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> c = <span class="st0">&quot;ccc&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> stringTest<span class="br0">&#40;</span><span class="kw3">String</span> x, <span class="kw3">String</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assumeTrue<span class="br0">&#40;</span>x.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>x + <span class="st0">&quot; &quot;</span> + y<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The trick is simple to provide data points for every parameter type of the test method. The JUnit Theories Runner will call the test method with every possible combination of datapoints. If you think a little about it you will soon realize some of the limitations of this approach:</p>
<ul>
<li>You&#8217;ll soon end up with lots of data point fields cluttering your code</li>
<li>Parameters of the same type will receive the same set of parameters, even when the usable range of inputs is completely different.</li>
</ul>
<p>Fortunately the developers of JUnit provided really nice solutions to these problems.</p>
<p>Instead of specifying single data points, you can provide a full array of datapoints using the <tt>@Datapoints</tt> annotation, like so (add imports for good measure):</p>
<div class="geshi no java">
<div class="head">@RunWith(Theories.class)</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> TheorieTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoints</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span><span class="br0">&#91;</span><span class="br0">&#93;</span> a = <span class="br0">&#123;</span> <span class="st0">&quot;a&quot;</span>, <span class="st0">&quot;bb&quot;</span>, <span class="st0">&quot;ccc&quot;</span> <span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoints</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">Integer</span><span class="br0">&#91;</span><span class="br0">&#93;</span> j = <span class="br0">&#123;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> someTest<span class="br0">&#40;</span><span class="kw3">String</span> x, <span class="kw3">Integer</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assumeTrue<span class="br0">&#40;</span>x.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>x + <span class="st0">&quot; &quot;</span> + y<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This of course is much less verbose. Instead of an array you may provide a method returning an array, or at least it looks like this should be possible. But when I tried it JUnit seemed unable to handle the types correctly resulting in IllegalArgumentExceptions. Guess I&#8217;ll have to file a bug when finished with this article &#8230;</p>
<p>But we still need to take care of parameters which have the same type, but very different meaning and therefore different useful values. The clean OO way of doing things would be to get rid of the generic types like <tt>String</tt> and use stronger types like <tt>CreditCardNumber</tt> or <tt>Name</tt> instead. But then in a perfect world we wouldn&#8217;t need tests, because our programs wouldn&#8217;t contain any bugs to begin with. So lets try this instead (Again imports omitted):</p>
<div class="geshi no java">
<div class="head">@Retention(RetentionPolicy.RUNTIME)</div>
<ol>
<li class="li1">
<div class="de1">@ParametersSuppliedBy<span class="br0">&#40;</span>CreditCardSupplier.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> @<span class="kw2">interface</span> AllCreditCards <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">@Retention<span class="br0">&#40;</span>RetentionPolicy.<span class="me1">RUNTIME</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">@ParametersSuppliedBy<span class="br0">&#40;</span>NameSupplier.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> @<span class="kw2">interface</span> AllNames <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> CreditCardSupplier <span class="kw2">extends</span> ParameterSupplier <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw3">List</span> getValueSources<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;ParameterSignature signature<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">ArrayList</span> result = <span class="kw2">new</span> <span class="kw3">ArrayList</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Amex&quot;</span>, <span class="st0">&quot;Amex&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Master&quot;</span>, <span class="st0">&quot;Master&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Visa&quot;</span>, <span class="st0">&quot;Visa&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">return</span> result<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> NameSupplier <span class="kw2">extends</span> ParameterSupplier <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw3">List</span> getValueSources<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;ParameterSignature signature<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; AllNames annotation = signature.<span class="me1">getAnnotation</span><span class="br0">&#40;</span>AllNames.<span class="kw2">class</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;just wanted to show that I can access it &quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; + annotation<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">ArrayList</span> result = <span class="kw2">new</span> <span class="kw3">ArrayList</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Alf&quot;</span>, <span class="st0">&quot;Alf&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Willie&quot;</span>, <span class="st0">&quot;Willie&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Tanner&quot;</span>, <span class="st0">&quot;Tanner&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Cat&quot;</span>, <span class="st0">&quot;Cat&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">return</span> result<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> SuppliedByTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> imagineThisIsATest<span class="br0">&#40;</span>@AllCreditCards <span class="kw3">String</span> x, @AllNames <span class="kw3">String</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;consider &quot;</span> + x + <span class="st0">&quot; / &quot;</span> + y + <span class="st0">&quot; tested.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> testIntegers<span class="br0">&#40;</span>@TestedOn<span class="br0">&#40;</span>ints = <span class="br0">&#123;</span> <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span>, <span class="nu0">7</span>, <span class="nu0">13</span>, <span class="nu0">23</span>, <span class="nu0">42</span> <span class="br0">&#125;</span><span class="br0">&#41;</span> <span class="kw4">int</span> i<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Wow, thats a lot of code. Just look at the last piece and see what appears in the console when we run it:</p>
<pre>just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()
consider Amex / Alf tested.
consider Amex / Willie tested.
consider Amex / Tanner tested.
consider Amex / Cat tested.
just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()
consider Master / Alf tested.
consider Master / Willie tested.
consider Master / Tanner tested.
consider Master / Cat tested.
just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()
consider Visa / Alf tested.
consider Visa / Willie tested.
consider Visa / Tanner tested.
consider Visa / Cat tested.
2
3
4
7
13
23
42
</pre>
<p>Have a look at the row beginning with: &#8220;consider&#8221;. Obviously the Theory <tt>imagineThisIsATest</tt> gets fed with the values from the <tt>CreditCardSupplier</tt> and <tt>NameSupplier</tt>. The parameters and the &#8216;Suppliers&#8217; are connected by the two annotations <tt>@AllNames</tt> and <tt>AllCreditCards</tt>. So whenever you have a parameter to a theory where the type alone is not sufficient for identifying the kind of values that should get used, you can simple create an annotation, which itself is annotated with a reference to a <tt>ParameterSupplier</tt> class and you are all set. You might think this is a lot of code for supplying a handful of parameters. You are right, but remember, that you can reuse your suppliers wherever you need names or credit card values in your tests.</p>
<p>Now let&#8217;s look at the first line of the output:<br />
<tt>just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()</tt><br />
It simply shows of that you get access to the annotation (and actually the signature of the compete test method. This can be very useful, when you want your supplier to behave differently for different theories. Have a look at the <tt>NameSupplier</tt> above to see how this works.</p>
<p>JUnit actually comes with an example where this is used, and I demonstrated it with the other theory in the demonstration code above. The <tt>@TestedOn</tt> annotation takes an array of values to be used as data points for the annotated parameter.</p>
<p>Thats it for today. I hope the power of theories became obvious, as well as the power you have as a developer to extend that mechanism. Again be warned: All this nice stuff is in a package named experimental for good reason. If you use it, you might find bugs, and thing will likely change at least in name in an upcoming version. Taking about versions, I am using junit4.8.1 for the examples.</p>
<p>For next week the conclusion of the little series about JUnit theories is planned, with a few thoughts on use and danger of this kind of testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/02/07/junit-theories/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Mixins, Inheritance and Delegation</title>
		<link>http://blog.schauderhaft.de/2010/01/24/mixin-inheritance-delegation/</link>
		<comments>http://blog.schauderhaft.de/2010/01/24/mixin-inheritance-delegation/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 11:27:49 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mixin]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[trait]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=385</guid>
		<description><![CDATA[A week ago I started learning Scala. One of the features I found pretty interesting are mixins and traits. That was just the point of time, when I read this little tweet of GeekyL: &#8220;i am still not sure if mixins are super cool or dark magic.&#8221; Of course I was instantly reminded of the [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_391" class="wp-caption alignright" style="width: 310px"><a href="http://blog.schauderhaft.de/wp-content/uploads/2010/01/962589_79025341x.jpg"><img class="size-medium wp-image-391" title="962589_79025341x" src="http://blog.schauderhaft.de/wp-content/uploads/2010/01/962589_79025341x-300x299.jpg" alt="Blender" width="300" height="299" /></a><p class="wp-caption-text">Blender</p></div>
<p>A week ago I started learning <a href="http://www.scala-lang.org/">Scala</a>. One of the features I found pretty interesting are <a href="http://www.scala-lang.org/node/117">mixins and traits</a>. That was just the point of time, when I read this little <a href="http://twitter.com/GeekyL/status/8037135496">tweet</a> of <a href="http://twitter.com/GeekyL">GeekyL</a>:</p>
<p>&#8220;i am still not sure if mixins are super cool or dark magic.&#8221;</p>
<p>Of course I was instantly reminded of the time when the dinosaurs dominated the world and I was learning the first little bits of OO and C++. I thought inheritance and polymorphism was great and the solution for every possible programming problem there is. It turned out that was not the case. Actually inheritance can result in pretty ugly code.</p>
<p>So GeekyL&#8217;s tweet got me thinking: Do mixins have the same problem? What exactly are the problems with inheritance anyway? Time for a new blog post.</p>
<p><strong>What are the problems of inheritance?</strong></p>
<p><strong>Inheritance for code reuse</strong>: It is tempting to have a class inherit from a superclass just because the superclass has some useful feature. Like this.</p>
<div class="geshi no java">
<div class="head">import java.beans.PropertyChangeSupport;</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> PropertySupport <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">protected</span> <span class="kw3">PropertyChangeSupport</span> propSup<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> UserEntity <span class="kw2">extends</span> PropertySupport <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">private</span> <span class="kw3">String</span> firstName<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> setFirstName<span class="br0">&#40;</span><span class="kw3">String</span> aFirstName<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">String</span> oldValue = firstName<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; firstName = aFirstName<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; propSup.<span class="me1">firePropertyChange</span><span class="br0">&#40;</span><span class="st0">&quot;firstName&quot;</span>, oldValue, firstName<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The problem with such code is that inheritance suggests a is-a-relationship, which in this case is just wrong. A UserEntity is not a PropertySupport, it just uses one.</p>
<p><strong>Multiple inheritance</strong>: At least in java a class can only inherit from one superclass. But many classes are many things. For example, a Cat is a Carnivor, it is a FourLeggedAnimal and a FurryAnimal. Without multiple inheritance there is just no way how to model this with class inheritance. With multple inheritance you can extend all these classes at the same time, but now you inherit the top class Animal three times, which at least is ugly.</p>
<p>So the proper way to do this kind of stuff in java is to use delegation and interfaces:</p>
<div class="geshi no java">
<div class="head">public class Cat implements Furry, FourLegged {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;FurryDelegate fd<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;FourLeggedDelegate fld<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> pet<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; fd.<span class="me1">pet</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> runFast<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; fld.<span class="me1">runFast</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Which works in the sense that you can pretty much express the kind of things you need to do, and which you might be tempted to solve with class inheritance. Of course the draw back is that you&#8217;ll have to create all theses trivial delegate methods.</p>
<p>In Scala you have the option to use Traits. Like so</p>
<div class="geshi no scala">
<div class="head">class Animal</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">class Cat extends Animal with Furry with FourLegs{
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">trait Furry{
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;def pet() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; println(&quot;purrrrr&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">trait FourLegs{
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;def runFast() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; println(&quot;I&#39;m gone&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">}</div>
</li>
</ol>
</div>
<p>Obviously this contains much less code duplication then the java version. Does it have drawbacks? You bet.</p>
<p>Mixins are firmly tied to the traits they use. Imagine a more complex trait, which itself uses many other clases and resources. Once you mix in such a trait you have a strong dependency. Therefor I suggest the following pattern for getting the reduction of code duplication from traits and the flexibility of delegates: Use Traits, which don&#8217;t have a real implementation, but use a delegate for doing all the real stuff, this way you can switch the implementation with touching just a single place:</p>
<div class="geshi no scala">
<div class="head">class Superclass</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">class Mixin extends Superclass with Trait {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; // &#8230; more stuff goes here
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">trait Trait {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; val delegate = new TraitImpl // this should be some kind of DI or lookup in a real system
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; def aMethod() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; delegate.aMethod()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">class TraitImpl extends Trait {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; override def aMethod(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; println(&quot;you just called me&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">}</div>
</li>
</ol>
</div>
<p>Summary: Traits can be used to solve some of the problems of inheritance, but they might introduce strong coupling, which you can easily be avoided.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/01/24/mixin-inheritance-delegation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Moving a Server Certificate and Private Key from Windows to a Java Key Store</title>
		<link>http://blog.schauderhaft.de/2009/10/18/server-certificate-private-key-windows-java/</link>
		<comments>http://blog.schauderhaft.de/2009/10/18/server-certificate-private-key-windows-java/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 08:55:04 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[The Rest]]></category>
		<category><![CDATA[certificate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[keystore]]></category>
		<category><![CDATA[keytool]]></category>
		<category><![CDATA[pfx]]></category>
		<category><![CDATA[pki]]></category>
		<category><![CDATA[private key]]></category>
		<category><![CDATA[public key]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=268</guid>
		<description><![CDATA[Some days ago I was asked to set up a tomcat server for SSL. You&#8217;d think this is a no brainer. And it actually is when you proceed along the paved path. But if you deviate only a little, you are in big trouble. I solved the trouble I got in, and here is my [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_269" class="wp-caption alignleft" style="width: 310px"><img class="size-medium wp-image-269" title="1180565_82303190" src="http://blog.schauderhaft.de/wp-content/uploads/2009/10/1180565_82303190-300x279.jpg" alt="1180565_82303190" width="300" height="279" /><p class="wp-caption-text">keys</p></div>
<p>Some days ago I was asked to set up a tomcat server for SSL. You&#8217;d think this is a <a href="http://tomcat.apache.org/tomcat-5.5-doc/ssl-howto.html">no brainer</a>. And it actually is when you proceed along the paved path. But if you deviate only a little, you are in big trouble. I solved the trouble I got in, and here is my story as a warning (and a helper for you).</p>
<p>Normally you would use the java <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/keytool.html">keytool</a>, to generate a certificate request, get that request signed by a certification authority (CA) and import it back into the the keystore. A little configuration in tomcat itself and you are done. Pretty easy.</p>
<p>But with the task at hand the problem was: The certificate was already there. Signed by a CA. Somewhere &#8216;in windows&#8217;. So the first question was: where the heck is this certificate stored in windows? It is in the certificate store, which I found under</p>
<pre>Start\All programs\Administrative  tools\Public key management</pre>
<p>Of course the exact location will vary, depending on your Windows version and language used. Once you find the certificate store, you&#8217;ll have to find the correct certificate. It should be under</p>
<pre>Certificates (Local Computer) -&gt; Personal -&gt; Certificates</pre>
<p>If you find more then one, you must identify the correct one. It should have the name of the domain you want to serve and it should have &#8216;Intended Purposes&#8217; of &#8216;Server Authentication&#8217;.</p>
<p>Now that you have found and identified the certificate to use, the question is: how do you get it into the tomcat/java keystore? Well, first you have to export it into a file. In order to do that, select the certificate. Right click on it and select</p>
<pre>All Taks -&gt; Export ...</pre>
<p>Most of the screens of the following wizard can be left as they are, with one important exception: You must ensure that the private key is exported along with the certificate. Of course you need to remember the location where you store the file, and the password you use for protection.</p>
<p><img class="alignright size-medium wp-image-273" title="screenshotCertificateExportWizard" src="http://blog.schauderhaft.de/wp-content/uploads/2009/10/screenshotCertificateExportWizard-300x230.png" alt="screenshotCertificateExportWizard" width="300" height="230" />See the screenshot on the side. So all that is left to do is to load that certificate into the keystore, right? Well yes. The problem is: the Java keytool doesn&#8217;t support importing of private keys, and even for certificates without private key, the keytool doesn&#8217;t understand the <tt>pfx</tt> format which Microsoft uses.</p>
<p>Fortunatly other people had the same problem before and implemented a solution: The developers of <a href="http://jetty.mortbay.com/jetty/">Jetty</a>. The Jetty download contains a jar File which we can use to load the certificate. You just need the Jetty jar, e.g.</p>
<pre>jetty-6.1.3.jar</pre>
<p>Which is contained in the <a href="http://dist.codehaus.org/jetty/jetty-6.1.x/ ">normal download</a><br />
and can issue the following command.</p>
<pre>java -classpath jetty-6.1.3.jar org.mortbay.jetty.security.PKCS12Import &lt;PFX-Datei&gt; &lt;keystorename&gt;</pre>
<p>obviously you&#8217;ll have to replaces the pointy brackets with the file name of the exported certifcate and the keystore. <strong>Warning: I don&#8217;t know what happens if the keystore already exists, I only tried it with a not existing one.</strong></p>
<p>Voila your keystore is ready for use with Tomcat.</p>
<p>If you like this article you should also thank Chris Barber, who&#8217;s <a href="http://www.cb1inc.com/2007/04/30/converting-pfx-certificates-to-java-keystores">article was very helpful</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2009/10/18/server-certificate-private-key-windows-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New Feature of JUnit: Rules</title>
		<link>http://blog.schauderhaft.de/2009/10/04/junit-rules/</link>
		<comments>http://blog.schauderhaft.de/2009/10/04/junit-rules/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 10:12:20 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[Rule]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=252</guid>
		<description><![CDATA[I am always surprised how many unknown feature hide in a supposedly simple library. Todays example is JUnit. When inspecting the newest version (4.7) I noted an annotation I hadn&#8217;t noticed before: @Rule. WTF? I am looking at a testing framework and not at a rules engine, am I? So naturally I tried to find [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_258" class="wp-caption alignleft" style="width: 310px"><img class="size-medium wp-image-258" title="1117482_76414816" src="http://blog.schauderhaft.de/wp-content/uploads/2009/10/1117482_76414816-300x200.jpg" alt="card board box" width="300" height="200" /><p class="wp-caption-text">card board box</p></div>
<p>I am always surprised how many unknown feature hide in a supposedly simple library. Todays example is <a href="http://junit.org/">JUnit</a>. When inspecting the newest version (4.7) I noted an annotation I hadn&#8217;t noticed before: <tt>@Rule</tt>. WTF? I am looking at a testing framework and not at a rules engine, am I? So naturally I tried to find out what it is good for, and since not to much documentation is available my little research resulted in this blog post. Enjoy.</p>
<p>The purpose of the <tt>@Rule</tt> annotation is to mark public fields of a test class. These fields  must be of type <tt>MethodRule</tt>, or an implementing class. Such <tt>MethodRule</tt>s behave similar to a AOP aspects, of course without use of any AOP library and specialized for Tests. They can execute code before, after or instead of a test method.  Example use cases listed in the <a href="http://github.com/KentBeck/junit/raw/23ffc6baf5768057e366e183e53f4dfa86fbb005/doc/ReleaseNotes4.7.txt">release notes</a> include:</p>
<ul>
<li>Notification on tests</li>
<li>Setting up or tearing down resources, especially when they are used in multiple test classes</li>
<li>Special checks performed after every test, possibly causing a test to fail.</li>
<li>Making information about the test available inside the test</li>
</ul>
<p>But ready made examples are boring, so I decided to try to implement something I was looking for all the time.</p>
<p>When writing acceptance tests with <a href="http://fit.c2.com/">Fit</a> or a similiar framework, you might end up with tons of failing tests, simply because the features aren&#8217;t implemented yet. But I don&#8217;t want this red lights hide a real red light, i.e. a test that worked before, but fails now. Therefore I&#8217;d like tests to get ignored just as if they where accordingly annotated, until they succeed for the first time. After that they should run (and possibly fail) just as a normal.</p>
<p>Let&#8217;s get started. First we need a little test case that can fail or succeed as we want. And this test case of course needs our annotated field:</p>
<div class="geshi no java">
<div class="head">public class DummyTest {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @Rule</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> IgnoreLeadingFailure ilf = <span class="kw2">new</span> IgnoreLeadingFailure<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @Test</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> testTest<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<span class="br0">&#40;</span><span class="kw2">false</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Please note that I stripped all package and import statements for brevity. As you can see I already named my <tt>MethodRule</tt> implementation <tt>IgnoreLeadingFailure</tt>. It needs some way to track which tests ran successfully at least once. Since I am lazy, I stored this information in a property file.</p>
<div class="geshi no java">
<div class="head">public class IgnoreLeadingFailure implements MethodRule {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">final</span> <span class="kw2">static</span> <span class="kw3">String</span> PROPERTY_FILE_NAME = <span class="st0">&quot;activatedTests.properties&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">final</span> <span class="kw2">static</span> <span class="kw3">Properties</span> activatedTests = <span class="kw2">new</span> <span class="kw3">Properties</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">static</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activatedTests.<span class="me1">load</span><span class="br0">&#40;</span><span class="kw2">new</span> <span class="kw3">FileInputStream</span><span class="br0">&#40;</span>PROPERTY_FILE_NAME<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><span class="kw3">IOException</span> e<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// actually this is to be expected on the first run</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;Couldn&#39;t load Properties from file&quot;</span> + e<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw3">Statement</span> apply<span class="br0">&#40;</span><span class="kw2">final</span> <span class="kw3">Statement</span> base, <span class="kw2">final</span> FrameworkMethod method,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">final</span> <span class="kw3">Object</span> target<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>activatedTests.<span class="me1">containsKey</span><span class="br0">&#40;</span>getFullTestMethodName<span class="br0">&#40;</span>method, target<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> base<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> <span class="kw2">new</span> <span class="kw3">Statement</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> evaluate<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <span class="kw3">Throwable</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base.<span class="me1">evaluate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activateTest<span class="br0">&#40;</span>getFullTestMethodName<span class="br0">&#40;</span>method, target<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><span class="kw3">Throwable</span> t<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">throw</span> <span class="kw2">new</span> AssumptionViolatedException<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;This test never succeeded before, and failed again with: &quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + t.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> <span class="kw4">void</span> activateTest<span class="br0">&#40;</span><span class="kw3">String</span> fullTestMethodName<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activatedTests.<span class="me1">put</span><span class="br0">&#40;</span>fullTestMethodName,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> <span class="kw3">SimpleDateFormat</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">format</span><span class="br0">&#40;</span><span class="kw2">new</span> <span class="kw3">Date</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activatedTests.<span class="me1">store</span><span class="br0">&#40;</span><span class="kw2">new</span> <span class="kw3">FileOutputStream</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PROPERTY_FILE_NAME<span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;tests that ran successfully at least once&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><span class="kw3">IOException</span> io<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;failed to store properties&quot;</span> + io<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw3">String</span> getFullTestMethodName<span class="br0">&#40;</span><span class="kw2">final</span> FrameworkMethod method,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">Object</span> target<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> target.<span class="me1">getClass</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span> + <span class="st0">&quot; &quot;</span> + method.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The interesting part is the single method contained in the <tt>MethodRule</tt> interface:</p>
<pre lang="java">public Statement apply(final Statement base, final FrameworkMethod method, final Object target)</pre>
<p><strong>base</strong> is the object that encapsulates the execution of the test method. The purpose of <tt>apply(..)</tt> is to provide a Statement. This Statement will get executed (or in the lingo of the interface &#8216;evaluated&#8217;). Typically you&#8217;ll return a <tt>Statement</tt> implementation, that wraps the <tt>Statement</tt> instance passed as a parameter.</tt></p>
<p><strong>method</strong> is the test method that will eventually get called and</p>
<p><strong>target</strong> is the object containing that method. Note that you normally do not execute the method, but use it mainly to get more information about the test case the <tt>MethodRule</tt> is applied to.</p>
<p>With this information it should be easy to understand what my little class does: It checks if the test at hand was already executed successfully before. If this is the case, the same <tt>Statement</tt> is returned, that was passed as a parameter. Otherwise an anonymous implementation is returned which replaces any failure by a <tt>AssumptionViolatedException</tt>, and saves a successful test run in the property file. Note that the <tt>AssumptionViolatedException</tt> actually does not result in a ignored test, but gets displayed as a successful test, at least with the default runner, which doesn't really fit my needs, but one could easily fix that by a minor change in the runner but not without that change.</p>
<p>So here is my opinion about this new JUnit feature: It certainly can come in handy for (integration) tests, where one tends to need a lot of resources that need quite some setup and/or tear down. Implementation of a <tt>MethodRule</tt> is fairly easy, although not exactly straight forward. The biggest problem is that it is next to impossible for an average java developer who doesn't know this feature to understand what is going on. The annotated field is easily missed, since in many cases it isn't used at all in the test class. Once more all this could be straight forward when java would support natively things like aspect oriented programming or open classes.</p>
<p>If you want more information I suggest you drop by <a href="http://www.threeriversinstitute.org/blog/?p=155">this blog post about 'Interceptors' which was the name for rules</a> at the time of writing of that post.</p>
<p>So, what do you think of this feature? Any ideas for features that could be implemented using rules?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2009/10/04/junit-rules/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Passwortprüfung in Java</title>
		<link>http://blog.schauderhaft.de/2007/11/24/passwortprufung-in-java/</link>
		<comments>http://blog.schauderhaft.de/2007/11/24/passwortprufung-in-java/#comments</comments>
		<pubDate>Sat, 24 Nov 2007 21:48:04 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[password]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/2007/11/24/passwortprufung-in-java/</guid>
		<description><![CDATA[Single Sign On ist in weiten Bereichen immer noch ein frommer Wunsch, und so kommt es, dass immer noch jede neue Anwendung eine Log On Maske benötigt, und zu einer solchen Log On Maske gehört natürlich das Eingabefeld für das Passwort. Mit dieser Aufgabe befinden sich wohl die allermeisten Entwickler auf ganz dünnem Eis. Wir [...]]]></description>
			<content:encoded><![CDATA[<p>Single Sign On ist in weiten Bereichen immer noch ein frommer Wunsch, und so kommt es, dass immer noch jede neue Anwendung eine Log On Maske benötigt, und zu einer solchen Log On Maske gehört natürlich das Eingabefeld für das Passwort.</p>
<p>Mit dieser Aufgabe befinden sich wohl die allermeisten Entwickler auf ganz dünnem Eis. Wir sprechen von IT-Sicherheit und Verschlüsselung und damit von Dingen die schwer zu verstehen sind und die richtig unangenehm werden, wenn sie schief gehen.</p>
<p>Wie schwierig das zu sein scheint sieht man, wenn man danach <a title="Suche nach java passwort verschlüsseln" href="http://www.google.de/search?q=java+passwort+verschl%C3%BCsseln" target="_blank">googelt.</a></p>
<p>Denn dort ist zumindest zur Zeit fast nur Blödsinn zu finden, dabei ist eine sinnvolle Lösung oft einfacher, als das was da so vorgeschlagen wird:</p>
<ol>
<li><a title="Eine Passwortverschlüsselungs Bibliothek aus einer Klasse" href="http://www.mindrot.org/projects/jBCrypt/" target="_blank">jBCrypt</a> herunterladen und mit in den Sourcepfad mit aufnehmen</li>
<li>Passwort hashen:
<pre>String hashed = BCrypt.hashpw(password, BCrypt.gensalt());</pre>
<p>Das Ergebnis dieses Aufrufes kann dann entspannt in der Datenbank oder wo auch immer gespeichert werden.</li>
<li>Wenn ein Benutzer sich anmeldet, kann mit folgendem Aufruf, dass eingegebene Passwort überprüft werden:
<pre>if (BCrypt.checkpw(candidate, hashed))
    System.out.println("It matches");
else
    System.out.println("It does not match");</pre>
</li>
</ol>
<p>Viel einfacher geht es ja nun wirklich nicht. Bleibt noch die Frage:</p>
<p><strong>Warum so und nicht anders?</strong></p>
<p>Ich gehe einfach die Varianten durch, die ich bisher zu Gesicht bekommen habe:</p>
<ul>
<li>&#8220;Wir müssen die Passwörter nicht verschlüsseln, so wichtig sind die Daten nicht und da kommen ja eh nur interne Mitarbeiter dran.&#8221;</li>
</ul>
<p>Na dann lasst die Passwortabfrage ganz weg. Wenn der Benutzer genötigt wird ein Passwort einzugeben, wird er mit großer Wahrscheinlichkeit ein Passwort verwenden, dass er auch wo anders verwendet. Und er wird es im Zweifelsfall nicht witzig finden, wenn der DBA das im Klartext in der Datenbank vor findet.</p>
<ul>
<li>Base64 Encoding, ROT13</li>
</ul>
<p>Man denkt es ist ein Scherz, aber ist es aber leider nicht. Beides sind keine Verschlüsselungsverfahren, bzw. können von einem intelligenten Grundschüler mit Papier und Bleistift &#8220;entschlüsselt&#8221; werden.</p>
<ul>
<li>Echte Verschlüsselungsverfahren (DES, AES &#8230;)</li>
</ul>
<p>Schon nicht schlecht.  Aber haben diese Verfahren haben einen Nachteil: Man kann das Passwort wieder entschlüsseln. Das ist aber gar nicht notwendig, da es reicht beim Login, das eingegebene Passwort auch zu verschlüsseln und das Ergebnis mit dem gespeichertem verschlüsseltem Passwort zu vergleichen. Die Möglichkeit das Passwort wieder zu entschlüsseln stellt nur ein unnötiges Sicherheitsrisiko dar. Verfahren die nicht umkehrbar sind heißen <a href="http://de.wikipedia.org/wiki/Hash#Zus.C3.A4tzliche_Forderungen_f.C3.BCr_kryptographisch_sichere_Hash-Funktionen" target="_blank">Hash </a>oder Digest.</p>
<ul>
<li>MD5</li>
</ul>
<p>MD5 ist ein solcher Digest. Aber er hat mehrere Probleme. Erstens gilt er seit geraumer Zeit nicht mehr als sonderlich sicher, auch nicht für den Bereich für den er prinzipiell geeignet und gedacht ist, die Signatur von Dokumenten. Zweitens und das ist das Entscheidende: Er ist schnell. Das ist gut, für Server die Dokumente mit Hilfe von MD5 verarbeiten wollen, und es ist gut, für den Angreifer, der eine Liste von verschlüsselten Passworten per Brute Force angreifen will.</p>
<p>Was wir brauchen ist ein langsamer Algorithmus. Der einen solche Brute Force Angriff unmöglich macht. Um einen Algorithmus langsam zu machen gibt es ein einfaches Mittel: Man wendet ihn sehr oft hintereinander an. Es bleibt aber immer noch ein Problem: Sogenannte Rainbowtables. Der Angreifer berechnet einfach für alle möglichen Passwörter bis zu einer gewissen Maximallänge den entsprechenden Hashwert und speichert das Ergebnis in einer Tabelle. Um ein bestimmtes Passwort zu ermitteln muss er nur den Hashwert in der Tabelle nachschlagen. Um dies zu verhindern, muss das Passwort lang genug sein. Um dies sicherzustellen wird es vom Algorithmus zunächst umeine beachtliche Anzahl zusätzliche Zeichen verlängert, bevor es gehasht wird, dies nennt man &#8220;salzen&#8221;. Das Salz wir mit abgespeichert, da sein Wert an sich irrelevant ist und nur dazu dient, dass keine Standard Rainbowtables verwendet werden können, sondern dass jeder Angriffsversuch jedes Passwort einzeln angreifen muss.</p>
<p>Dies haben andere natürlich auch schon wesentlich genauer und auf englisch <a href="http://www.matasano.com/log/958/enough-with-the-rainbow-tables-what-you-need-to-know-about-secure-password-schemes/" target="_blank">beschrieben</a>.</p>
<p>Mann kann nun anfangen dies alles selbst zu implementieren. Das sollte nicht so schwierig sein, aber man kann auch einfach einen fertig implementierten Algorithmus nehmen.</p>
<p>Zu guter letzt noch eine Warnung: Ich bin kein Kryptologe oder dergleichen. Ich habe nur ein wenig Internetrecherche betrieben als ich den letzten Log On Dialog gebaut habe. Wenn ihr etwas baut, was wirklich sicherheitskritisch ist, konsultiert jemanden, der sich damit auskennt.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2007/11/24/passwortprufung-in-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

