<?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>xterm.it &#187; Java</title>
	<atom:link href="http://www.xterm.it/blog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xterm.it/blog</link>
	<description>time to think -  by Marco Campana</description>
	<lastBuildDate>Sun, 18 Oct 2009 20:59:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A sample Java test runner using annotations</title>
		<link>http://www.xterm.it/blog/2009/04/a-sample-java-test-runner-using-annotations/</link>
		<comments>http://www.xterm.it/blog/2009/04/a-sample-java-test-runner-using-annotations/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 19:23:00 +0000</pubDate>
		<dc:creator>marco</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.xterm.it/blog/?p=106</guid>
		<description><![CDATA[public class RunTests {      int passed = 0, failed = 0;      for (Method m : Class.forName(args[0]).getMethods()) {               passed++;                 "Test %s failed: %s %n",                  m,                failed++; ]]></description>
			<content:encoded><![CDATA[<p>I have found this toy code on <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html">java.sun.com</a> that describes how Java annotations could be used to write a simple Test runner. This script gets as argument the name of a class and tries to execute all the methods of that class that are annotated as @Test.<br />
The interesting aspect of this code is that is shows clearly how annotations can be used to execute/manipulate source code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RunTests <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">int</span> passed <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>, failed <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Method</span> m <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>m.<span style="color: #006633;">isAnnotationPresent</span><span style="color: #009900;">&#40;</span>Test.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
              m.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              passed<span style="color: #339933;">++;</span>
           <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;Test %s failed: %s %n&quot;</span>,
                m,
                ex.<span style="color: #006633;">getCause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
              <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              failed<span style="color: #339933;">++;</span>
           <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Passed: %d, Failed %d%n&quot;</span>, passed, failed<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.xterm.it/blog/2009/04/a-sample-java-test-runner-using-annotations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
