<?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>wuetender-junger-mann.de &#187; Opensource</title>
	<atom:link href="http://wuetender-junger-mann.de/wordpress/category/opensource/feed/" rel="self" type="application/rss+xml" />
	<link>http://wuetender-junger-mann.de/wordpress</link>
	<description>Der Freiheit eine Gasse</description>
	<lastBuildDate>Tue, 08 May 2012 11:59:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Java Lib to Launch External Processes</title>
		<link>http://wuetender-junger-mann.de/wordpress/2010/12/java-lib-to-launch-external-processes/</link>
		<comments>http://wuetender-junger-mann.de/wordpress/2010/12/java-lib-to-launch-external-processes/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 01:20:27 +0000</pubDate>
		<dc:creator>felix</dc:creator>
				<category><![CDATA[Held der Kommandozeile]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://wuetender-junger-mann.de/wordpress/?p=1096</guid>
		<description><![CDATA[I recently redesigned some of the code I tend to use to spawn external processes (pdflatex anyone?) in java. The implementation is still a bit buggy, but I am more interested in people&#8217;s opinions about the API (non-blocking killable invocations are not yet supported). The project on google code is called jproc. Here is the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently redesigned some of the code I tend to use to spawn external processes (pdflatex anyone?) in java. The implementation is still a bit buggy, but I am more interested in people&#8217;s opinions about the API (non-blocking killable invocations are not yet supported). The project on google code is called <a href="http://code.google.com/p/jproc/">jproc</a>. Here is the cookbook so far:
</p>
<p>To launch an external program  we&#8217;ll use a <code>ProcBuilder</code>. The run method<br />
builds and spawns the actual process and blocks until the process exits.<br />
 The process takes care of writing the output to a stream (as opposed to the standard<br />
facilities in the JDK that expect the client to actively consume the<br />
output from an input stream:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">ByteArrayOutputStream</span> output <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;echo&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withOutputStream</span><span style="color: #009900;">&#40;</span>output<span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, output.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The input can be read from an arbitrary input stream, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">ByteArrayInputStream</span> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello cruel World&quot;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;wc&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withArgs</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-w&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withInputStream</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;3&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If all you want to get is the string that gets returned and if there<br />
is not a lot of data, using a streams is quite cumbersome. So for convenience<br />
if no stream is provdied the output is captured by default and can be<br />
obtained from the result.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;echo&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, result.<span style="color: #006633;">getExitValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;echo <span style="color: #000099; font-weight: bold;">\&quot;</span>Hello World!<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>, result.<span style="color: #006633;">getProcString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For providing input there is a convenience method too:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cat&quot;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #006633;">withInput</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is a string&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is a string&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Some external programs are using environment variables. These can also<br />
be set using the <code>withVar</code> method</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bash&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">withArgs</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-c&quot;</span>, <span style="color: #0000ff;">&quot;echo $MYVAR&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">withVar</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MYVAR&quot;</span>,<span style="color: #0000ff;">&quot;my value&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my value<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bash -c <span style="color: #000099; font-weight: bold;">\&quot;</span>echo $MYVAR<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>, result.<span style="color: #006633;">getProcString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A common usecase for external programs is batch processing of data.<br />
These programs might always run into difficulties. Therefore a timeout can be<br />
specified. There is a default timeout of 5000ms. If the program does not terminate within the timeout<br />
interval it will be terminated and the failure is indicated through<br />
an exception:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sleep&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withTimeoutMillis</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    builder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    fail<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Should time out&quot;</span><span style="color: #009900;">&#41;</span><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>TimeoutException ex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Process 'sleep 2' timed out after 1000ms.&quot;</span>, ex.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Even if the process does not timeout, we might be interested in the<br />
execution time. It is also available through the result:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sleep&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0.5&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withTimeoutMillis</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertTrue<span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">getExecutionTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">500</span> <span style="color: #339933;">&amp;&amp;</span> result.<span style="color: #006633;">getExecutionTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>By default the new program is spawned in the working directory of<br />
the parent process. This can be overidden:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pwd&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withWorkingDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It is a time honoured tradition that programs signal a failure<br />
by returning a non-zero exit value. However in java failure is<br />
signalled through exceptions. Non-Zero exit values therefore<br />
get translated into an exception, that also grants access to<br />
the output on standard error.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ls&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xyz&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    builder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    fail<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Should throw exception&quot;</span><span style="color: #009900;">&#41;</span><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>ExternalProcessFailureException ex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ls: xyz: No such file or directory<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, ex.<span style="color: #006633;">getStderr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, ex.<span style="color: #006633;">getExitValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ls xyz&quot;</span>, ex.<span style="color: #006633;">getCommand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ls: xyz: No such file or directory<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, ex.<span style="color: #006633;">getStderr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertTrue<span style="color: #009900;">&#40;</span>ex.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Input and output can also be provided as <code>byte[]</code>.<br />
<code>ProcBuilder</code> also copes with large amounts of<br />
data.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> MEGA <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1024</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span> <span style="color: #339933;">*</span> MEGA<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> data.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    data<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">round</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">255</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">128</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;gzip&quot;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #006633;">withInput</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>
   .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertTrue<span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">getOutputBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> MEGA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The builder allows to build and spawn several processes from<br />
the same builder instance:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;uuidgen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> uuid1 <span style="color: #339933;">=</span> builder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> uuid2 <span style="color: #339933;">=</span> builder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertNotNull<span style="color: #009900;">&#40;</span>uuid1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertNotNull<span style="color: #009900;">&#40;</span>uuid2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertTrue<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>uuid1.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>uuid2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For convenience there is also a static method that just runs a<br />
program and captures the ouput:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> output <span style="color: #339933;">=</span> ProcBuilder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;echo&quot;</span>, <span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Also there is a static method that filters a given string through<br />
a program:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> output <span style="color: #339933;">=</span> ProcBuilder.<span style="color: #006633;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x y z&quot;</span>,<span style="color: #0000ff;">&quot;sed&quot;</span> ,<span style="color: #0000ff;">&quot;s/y/a/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x a z<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://wuetender-junger-mann.de/wordpress/2010/12/java-lib-to-launch-external-processes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Spreadsheet Driven</title>
		<link>http://wuetender-junger-mann.de/wordpress/2008/09/spreadsheet-driven/</link>
		<comments>http://wuetender-junger-mann.de/wordpress/2008/09/spreadsheet-driven/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 09:55:41 +0000</pubDate>
		<dc:creator>felix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://wuetender-junger-mann.de/wordpress/?p=612</guid>
		<description><![CDATA[I had a quite a few chats these days with people fro a more QAish background. Originally I intended just to look at selenium and webdriver. I showed some of the stuff, which I wrote in Java, to my QA colleagues. I used a demo webapp that implements a phonebook. My testcase just added a [...]]]></description>
			<content:encoded><![CDATA[<p>
I had a quite a few chats these days with people fro a more QAish background. Originally I intended just to look at selenium and webdriver. I showed some of the stuff, which I wrote in Java, to my QA colleagues. I used a demo webapp that implements a phonebook. My testcase just added a new contact and verified, that the user got a success message.
</p>
<p>
After they saw a bit of it, they asked me how they could separate out the testdata. I was a bit stunned, because, well it was all there &#8211; at least to me as a java developer. I had separated out everything into a  method somewhat like this:
</p>
<div style="font-size:11pt">

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"> createNewContact<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Felix&quot;</span>, <span style="color: #0000ff;">&quot;Leipold&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p>
So I could test for other inputs as well:
</p>
<div style="font-size:11pt">

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"> createNewContact<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span>, <span style="color: #0000ff;">&quot;Invalid&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 createNewContact<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Invalid&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 createNewContact<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Holden&quot;</span>, <span style="color: #0000ff;">&quot;Caulfield&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p>
Obviously the expectation of QAs was to have an external file, e.g. a spreadsheet that feeds the data into the test code. I am very critical of this approach, but I felt like playing with junit 4 a bit. Junit 4 allows you to specify a runner four your test and indeed they have got one prepackaged that does parameterized tests. So I thought how about having a runner reads a spreadsheet and feeds the data into the test. As an example I use a very simplistic function:
</p>
<div style="font-size:11pt">

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000066; font-weight: bold;">double</span> add<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> a, <span style="color: #000066; font-weight: bold;">double</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>
So to test this I created this table and saved it as mathTest.xls:
</p>
<p>
<a href="http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2008/09/mathtable1.png"><img src="http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2008/09/mathtable1.png" alt="" title="mathtable1" width="290" height="130" class="alignnone size-full wp-image-615" /></a>
</p>
<p>
Then I went on to create a test case, like this:
</p>
<div style="font-size:11pt">

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span>SpreadsheetDriven.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@Spreadsheet<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mathTest.xls&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MathTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">double</span> a<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">double</span> b<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">double</span> expectedSum<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> MathTest<span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span> a, <span style="color: #003399;">Double</span> b, <span style="color: #003399;">Double</span> expectedSum<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">a</span> <span style="color: #339933;">=</span> a<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">b</span> <span style="color: #339933;">=</span> b<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">expectedSum</span> <span style="color: #339933;">=</span> expectedSum<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testAdd<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        assertEquals<span style="color: #009900;">&#40;</span>expectedSum, add<span style="color: #009900;">&#40;</span>a,b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">double</span> add<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> a, <span style="color: #000066; font-weight: bold;">double</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>
Essentially this thing parses the file specified in the annotation and passes it into the constructor. It&#8217;s not doing any fancy type conversions. All numerics are passed in as double, booleans as booleans, and everything else should be a string. Implementation and tests are to be found <a href='http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2008/09/spreadsheet.zip'>here</a> and depend on apache poi being on the classpath for reading the excel file.
</p>
<p>
I am also quite proud of the reporting I get:
</p>
<p><a href="http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2008/09/reporting.png"><img src="http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2008/09/reporting.png" alt="" title="reporting" width="306" height="221" class="alignnone size-full wp-image-619" /></a></p>
<p>
I am wondering, if all this is a good idea. But if it helps keeping people from using fit, it&#8217;s a start. What I realised by now is that actually table or data driven testing is much more appropriate for unit tests. Funnily enough that people actually do it the other way round. For unit tests I would actually recommend not to use a spreadsheet, but the original JUnit <code>Parameterized</code> runner, which lets you define your data like this:</p>
<div style="font-size:11pt">

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @Parameterized.<span style="color: #006633;">Parameters</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">List</span> getParameters<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Arrays</span>.<span style="color: #006633;">asList</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>
                <span style="color: #009900;">&#123;</span>
                        <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span>,
                        <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">7</span><span style="color: #009900;">&#125;</span>,
                        <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span>,
                        <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
]]></content:encoded>
			<wfw:commentRss>http://wuetender-junger-mann.de/wordpress/2008/09/spreadsheet-driven/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Opensource Vector Graphics Editor</title>
		<link>http://wuetender-junger-mann.de/wordpress/2007/05/opensource-vector-graphics-editor/</link>
		<comments>http://wuetender-junger-mann.de/wordpress/2007/05/opensource-vector-graphics-editor/#comments</comments>
		<pubDate>Fri, 11 May 2007 17:24:15 +0000</pubDate>
		<dc:creator>felix</dc:creator>
				<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Productivity]]></category>

		<guid isPermaLink="false">http://wuetender-junger-mann.de/wordpress/?p=484</guid>
		<description><![CDATA[Alexey pointed me this week to Inkscape a GTK-based vector graphics editor that from first use seems comparable to CorelDraw (at least the historic version I used years ago). It feels much more intuitive than the Gimp.]]></description>
			<content:encoded><![CDATA[<p>Alexey pointed me this week to <a href="http://www.inkscape.org/">Inkscape</a> a GTK-based vector graphics editor that from first use seems comparable to CorelDraw (at least the historic version I used years ago). It feels much more intuitive than the Gimp. </p>
]]></content:encoded>
			<wfw:commentRss>http://wuetender-junger-mann.de/wordpress/2007/05/opensource-vector-graphics-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

