<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Alistair Israel</title>
	<atom:link href="http://alistairisrael.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://alistairisrael.wordpress.com</link>
	<description>Just another developer blog.</description>
	<lastBuildDate>Sun, 25 Dec 2011 18:26:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='alistairisrael.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Alistair Israel</title>
		<link>http://alistairisrael.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://alistairisrael.wordpress.com/osd.xml" title="Alistair Israel" />
	<atom:link rel='hub' href='http://alistairisrael.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Apple&#8217;s Billion Dollar Data Center</title>
		<link>http://alistairisrael.wordpress.com/2011/01/17/apples-billion-dollar-data-center/</link>
		<comments>http://alistairisrael.wordpress.com/2011/01/17/apples-billion-dollar-data-center/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 08:17:30 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[Musings]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=292</guid>
		<description><![CDATA[What do I think? Time Machine to the cloud.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=292&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What do I think?</p>
<p>Time Machine to the cloud.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/292/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=292&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2011/01/17/apples-billion-dollar-data-center/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
		<item>
		<title>Steam on Mac, case-sensitive workaround</title>
		<link>http://alistairisrael.wordpress.com/2010/05/13/steam-on-mac-case-sensitive-workaround/</link>
		<comments>http://alistairisrael.wordpress.com/2010/05/13/steam-on-mac-case-sensitive-workaround/#comments</comments>
		<pubDate>Thu, 13 May 2010 09:12:05 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[OS X]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=280</guid>
		<description><![CDATA[Steam requires that Steam.app/Contents/MacOS be created on a case-insensitive  file system, with read-write access. Here's how to work around that.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=280&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So, <a href="http://store.steampowered.com/news/3818/">Steam for OS X</a> just went live.</p>
<p>Cool.</p>
<p>Then they announce <a href="http://store.steampowered.com/freeportal/">Portal is FREE!</a> (until May 24th, 2010 at least).</p>
<p>Awesome!</p>
<p>So I download the Steam .dmg to my Mac, copy Steam over to <tt>/Applications</tt>, try to run it, and promptly get:</p>
<p><b>Steam requires that Steam.app/Contents/MacOS be created on a case-insensitive  file system, with read-write access.</b></p>
<p>Wut!?</p>
<p>Why!?</p>
<p>(I mean, why couldn&#8217;t they have ported the thing properly and made sure the paths/file names in the code were all lower case or something?)</p>
<p>You know—there <em>are</em> people who like case-sensitive file systems because it forces them to be more careful with how they code. You know—not every file system is like, FAT32 where &#8220;readme.txt&#8221;, &#8220;README.TXT&#8221; and &#8220;ReadMe.tXt&#8221; all refer to the same file!</p>
<p>Anyway—while I love to rant, I also like to fix things. So here goes:</p>
<h4>1. Create a new partition, formatted simply as Mac OS Extended (Journaled)</h4>
<p>Note—<em>NOT</em> case-sensitive. I made mine 10GB, and labeled it <tt>steam</tt>. Fortunately, Disk Utility lets us play around with partitions. Who needs Partition Magic?</p>
<h4>2. Copy Steam.app to the new partition</h4>
<p>But wait, there&#8217;s more! If you try to run Steam now, it&#8217;ll complain about <tt>~/Library/Application Support/Steam</tt> (and later, <tt>~/Documents/Steam Content</tt>). Hang in there with me for a second.</p>
<h4>3. Create the necessary folders on your new partition</h4>
<p>I ended up creating <tt>/Application Support/Steam</tt> and <tt>/Documents/Steam Content</tt> on the <tt>steam</tt> volume.</p>
<h4>4. Create symbolic links to these new folders from where Steam looks for them</h4>
<p>First make sure that the folders <tt>~/Library/Application Support/Steam</tt> and <tt>~/Documents/Steam Content</tt> don&#8217;t exist yet (the Steam app will try to create these for you). If they do, just delete them.</p>
<p>Time for some Terminal magic. Run Terminal, and go<br />
<code><br />
cd ~/Library/Application\ Support/<br />
ln -ns /Volumes/steam/Application\ Support/Steam/ Steam<br />
cd ~/Documents<br />
ln -ns /Volumes/steam/Documents/Steam\ Content/ "Steam Content"<br />
sudo ln -s /Volumes/ /volumes<br />
</code></p>
<p>The last line will require you to enter your administrator password. I just added that in courtesy of <a href="http://blog.andersonshatch.com/2010/05/13/using-steam-on-mac-with-case-sensitive-drive/">Josh Anderson&#8217;s own post</a>. Without that other symbolic link, it seems Steam will have trouble finding the content/games it&#8217;s already downloaded.</p>
<h4>5. Run Steam.</h4>
<h4>6. Download Portal</h4>
<h4>7. &#8230;</h4>
<h4>8. Profit!</h4>
<p>You&#8217;re welcome.</p>
<p><b>Added:</b> Also, <a href="http://blog.andersonshatch.com/2010/05/13/using-steam-on-mac-with-case-sensitive-drive/">here&#8217;s another workaround by Josh Anderson</a> that doesn&#8217;t require you to create a new partition, but uses a case-insensitive disk image instead.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/280/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/280/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/280/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=280&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2010/05/13/steam-on-mac-case-sensitive-workaround/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
		<item>
		<title>A Simple, Efficient P(n,k) Algorithm</title>
		<link>http://alistairisrael.wordpress.com/2009/09/22/simple-efficient-pnk-algorithm/</link>
		<comments>http://alistairisrael.wordpress.com/2009/09/22/simple-efficient-pnk-algorithm/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 15:00:14 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[permutations]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=237</guid>
		<description><![CDATA[Drawing heavily from <a href="http://www.freewebs.com/permute/soda_submit.html">“SEPA: A
Simple, Efficient Permutation Algorithm”</a> by Jeffrey A. Johnson, this article adapts and refines Johnson's algorithm to generate all permutations of <i>n</i>items taken <i>k</i> at a time, in lexicographic order.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=237&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Jeffrey A. Johnson&#8217;s <a href="http://www.freewebs.com/permute/soda_submit.html">SEPA: A Simple, Efficient Permutation Algorithm</a> is indeed a fast and simple way to generate all the permutations of <i>n</i> items or P(<i>n</i>) in lexicographic order. In this article, I describe a similar algorithm for generating partial permutations of <i>n</i> items taken <i>k</i> at a time, which we denote as P(<i>n</i>, <i>k</i>).</p>
<h2><a name="_SEPA_Explained"></a>SEPA Explained</h2>
<p>Johnson discovered that a set of logical steps could be taken from one permutation to the next. Repeatedly calling the algorithm on the last output obtained generates all permutations in sorted order.</p>
<p>It works by first scanning for the rightmost <a href="http://en.wikipedia.org/wiki/Permutation#Ascents.2C_descents_and_runs">ascent</a> or pair of numbers where the first number is less than the one immediately after it.</p>
<table style="border:1px solid;border-collapse:collapse;" cellspacing="0" cellpadding="0">
<caption>Table 1: P(5)<sub>17..24</sub> with rightmost ascents highlighted.</caption>
<col width="66" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;"><i>P<sub>i</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>0</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>1</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>2</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>3</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">&#8942;</td>
<td colspan="5" style="text-align:center;width:5cm;">&#8942;</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">17</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">18</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">19</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">20</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">21</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">22</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">2</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">23</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">24</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">&#8942;</td>
<td colspan="5" style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
</tr>
</table>
<p>If no ascent is found, then all numbers are in descending order and this is the last, lexicographic permutation.</p>
<p>Otherwise, the number at the start of the ascent is swapped with the smallest higher number to its right.</p>
<table style="border:1px solid;border-collapse:collapse;" cellspacing="0" cellpadding="0">
<caption>Table 2: P&#8217;(5)<sub>17..24</sub> after swaps (in red). Highlighted cells are about to be reversed.</caption>
<col width="66" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;"><i>P&#8217;<sub>i</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>0</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>1</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>2</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>3</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">&#8942;</td>
<td colspan="5" style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">17&#8242;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><span style="color:red;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">18&#8242;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">19&#8242;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><span style="color:red;">1</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">20&#8242;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">1</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">21&#8242;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">22&#8242;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">1</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">23&#8242;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><span style="color:red;">0</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">24&#8242;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">&#8942;</td>
<td colspan="5" style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
</tr>
</table>
<p>Finally, all the numbers to the right (which will be in descending order) are &#8216;flipped&#8217; or reversed (to ascending order). The resulting array is the next permutation.</p>
<h2><a name="_P_n__to_P_n_k_"></a>P(n) to P(n,k)</h2>
<p>To begin generating P(n,k), let&#8217;s take a look at the first few permutations of 5 elements taken 3 at a time, P(5,3) and compare this with P(5):</p>
<p><span id="more-237"></span></p>
<table style="border:1px solid;border-collapse:collapse;" cellspacing="0" cellpadding="0">
<caption>Table 3: P(5, 3)<sub>0&#8230;5</sub></caption>
<col width="66" />
<col width="44" />
<col width="44" />
<col width="44" />
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i></i>P<sub>i</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>0</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>1</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>2</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">5</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;" colspan="3">&#8942;</td>
</tr>
</table>
<p>If we look carefully at the first 3 values of P(5), <i>a<sub>0</sub></i>, <i>a<sub>1</sub></i> and <i>a<sub>2</sub></i>, we actually find P(5, 3). Except, to get P(5, 3) from P(5) we need to &#8216;skip&#8217; a few permutations.</p>
<table style="border:1px solid;border-collapse:collapse;" cellspacing="0" cellpadding="0">
<caption>Table 4: P(5) with P(5, 3) highlighted</caption>
<col width="66" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;"><i>P(5)<sub>i</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><i>a<sub>0</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><i>a<sub>1</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><i>a<sub>2</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>3</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">5</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">6</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">7</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">8</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">9</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">10</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">&#8942;</td>
<td colspan="5" style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
</tr>
</table>
<p>Basically, we can safely &#8216;skip&#8217; all permutations in P(5) that don&#8217;t permute the first 3 positions. For purposes of discussion, we designate the <i>k</i><sup>th</sup> or 3<sup>rd</sup> position, <i>a<sub>2</sub></i> as the &#8216;edge&#8217;.</p>
<table style="border:1px solid;border-collapse:collapse;" cellspacing="0" cellpadding="0">
<caption>Table 5: P(5) where <i>a<sub>0</sub></i>, <i>a<sub>1</sub></i> and <i>a<sub>2</sub></i> change. The 3<sup>rd</sup> position, or <i>a<sub>2</sub></i> is the &#8216;edge&#8217;</caption>
<col width="66" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>P<sub>i</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><i>a<sub>0</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><i>a<sub>1</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;"><i>a<sub>2</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>3</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">6</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">8</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">10</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">12</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">14</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">16</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">18</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">20</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
<td colspan="5" style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
</tr>
</table>
<p>Another way to look at this is that in generating P(n) using Johnson&#8217;s SEPA, we look for the rightmost ascent. In P(n, k) generation, we&#8217;re only interested in ascents at the &#8216;edge&#8217; or to the left of the &#8216;edge&#8217;.</p>
<p>We first assume that the edge contains our &#8216;ascent&#8217; and look for the next higher number to the right of the edge.</p>
<table style="border:1px solid;border-collapse:collapse;" cellspacing="0" cellpadding="0">
<caption>Table 6: P(5) &rarr; P(5, 3), showing the edge about to be swapped with the next higher number</caption>
<col width="66" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;"><i>P<sub>i</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>0</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>1</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><i>a<sub>2</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>3</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td rowspan="12" style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">&#8942;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
</tr>
<tr>
<td colspan="5" style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
</tr>
</table>
<p>If all numbers to the right of the edge are smaller, then we know that the ascent must be to the left of the edge.</p>
<p>However, unlike with regular P(n) generation, at this point all numbers to the right of the edge will be in ascending order. We know this since we began with all numbers in ascending order, and all our operations at this point simply swap the value at the edge with the next higher number.</p>
<p>Fortunately, by simply reversing everything to the right of the edge we return to a state similar to P(n) where all values to the right of the actual ascent are in descending order. We can now proceed as with the regular SEPA.</p>
<table style="border:1px solid;border-collapse:collapse;" cellspacing="0" cellpadding="0">
<caption>Table 7: P(5, 3), showing the edge about to be swapped, or, after reversal, the actual ascent and values to swap (as in SEPA)</caption>
<col width="66" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;"><i>P<sub>i</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>0</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>1</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>2</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>3</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td rowspan="12" style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">&#8942;</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">4</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">0</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">1</span></td>
</tr>
<tr>
<td colspan="5" style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
</tr>
</table>
<h2 style="margin-top:1em;"><a name="_A_Simple__Efficient_Algorithm_for_Generating_P_n__k_"></a>A Simple, Efficient Algorithm for Generating P(n, k)</h2>
<p>This leads us to the adapted algorithm for generating permutations of <i>n</i> items taken <i>k</i> at a time, in lexicographic order.</p>
<blockquote><pre style="line-height:95%;">
def <i>a</i> = { 0, 1, 2 &hellip; (n - 1) }
def <i>edge</i> = <i>k</i> - 1

// find <i>j</i> in (<i>k</i>&hellip;<i>n</i>-1) where <i>a<sub>j</sub></i> &gt; <i>a<sub>edge</sub></i>
<i>j</i> = <i>k</i>
while <i>j</i> &lt; <i>n</i> and <i>a<sub>edge</sub></i> &gt;= <i>a<sub>j</sub></i>,
    ++<i>j</i>

if <i>j</i> &lt; <i>n</i> {
    swap <i>a<sub>edge</sub></i>, <i>a<sub>j</sub></i>
} else {
    reverse <i>a<sub>k</sub></i> to <i>a<sub>n-1</sub></i>

    // find rightmost ascent to left of edge
    <i>i</i> = <i>edge</i> - 1
    while <i>i</i> &gt; 0 and <i>a<sub>i</sub></i> &gt;= <i>a<sub>i</sub></i>+1,
          --<i>i</i>

    if <i>i</i> &lt; 0,
          // no more permutations
          return 0

    // find <i>j</i> in (<i>n</i>-1&hellip;<i>i</i>+1) where <i>a<sub>j</sub> </i>&gt; <i>a<sub>i</sub></i>
    <i>j</i> = <i>n</i> - 1
    while <i>j</i> &gt; <i>i</i> and <i>a<sub>i</sub></i> &gt;= <i>a<sub>j</sub></i>
          --<i>j</i>

    swap <i>a<sub>i</sub></i>, <i>a<sub>j</sub></i>
    reverse <i>a<sub>i+1</sub></i> to <i>a<sub>n-1</sub></i>
}

output <i>a<sub>0</sub></i>, <i>a<sub>1</sub></i> &hellip; <i>a<sub>k-1</sub></i>
</pre>
</blockquote>
<h2><a name="_SEP_n_k__Algorithm_Illustrated"></a>SEP(n,k) Algorithm Illustrated</h2>
<p>To show how the algorithm works, let&#8217;s step through the major operations showing the state of the array <i>a</i> at each step.</p>
<table style="border:1px solid;border-collapse:collapse;" cellspacing="0" cellpadding="0">
<caption>Table 8: P(5, 3). <i>a<sub>2</sub></i> is the &#8216;edge&#8217;. Red values are about to be swapped, yellow cells are about to be reversed.</caption>
<col width="66" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="44" />
<col width="175" />
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>P<sub>i</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>0</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>1</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><i>a<sub>2</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>3</sub></i></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><i>a<sub>4</sub></i></td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">Step</td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>2</sub></i>, <i>a<sub>3</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>2</sub></i>, <i>a<sub>4</sub></i></td>
</tr>
<tr>
<td rowspan="3" style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">reverse <i>a<sub>3..4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>1</sub></i>, <i>a<sub>4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">reverse <i>a<sub>2..4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>2</sub></i>, <i>a<sub>3</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>2</sub></i>, <i>a<sub>4</sub></i></td>
</tr>
<tr>
<td rowspan="3" style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">5</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">reverse <i>a<sub>3..4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>1</sub></i>, <i>a<sub>3</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">reverse <i>a<sub>2..4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">6</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>2</sub></i>, <i>a<sub>3</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">7</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">4</span></td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>2</sub></i>, <i>a<sub>4</sub></i></td>
</tr>
<tr>
<td rowspan="3" style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">8</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">reverse <i>a<sub>3..4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">4</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>1</sub></i>, <i>a<sub>2</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">reverse <i>a<sub>2..4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">9</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>2</sub></i>, <i>a<sub>3</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">10</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>2</sub></i>, <i>a<sub>4</sub></i></td>
</tr>
<tr>
<td rowspan="3" style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">11</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">reverse <i>a<sub>3..4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">0</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">1</span></td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>0</sub></i>, <i>a<sub>4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">3</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">2</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ffffcc;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">reverse <i>a<sub>1..4</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1.5cm;padding:4px;">12</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">1</td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">0</td>
<td style="border:1px solid;text-align:center;width:1cm;background-color:#ccccff;padding:4px;"><span style="color:red;">2</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;"><span style="color:red;">3</span></td>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">4</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">swap <i>a<sub>2</sub></i>, <i>a<sub>3</sub></i></td>
</tr>
<tr>
<td style="border:1px solid;text-align:center;width:1cm;padding:4px;">&#8942;</td>
<td colspan="5" style="border:1px solid;text-align:center;width:5cm;padding:4px;">&#8942;</td>
<td style="border:1px solid;text-align:center;width:4cm;padding:4px;">&#8942;</td>
</tr>
</table>
<h2 style="margin-top:1em;">Java Implementation</h2>
<p>A Java implementation of this algorithm is provided as <code><a href="http://github.com/AlistairIsrael/jcombinatorics/blob/master/src/main/java/jcombinatorics/permutations/SepaPnkIterator.java">SepaPnkIterator</a></code>. It is part of <code><a href="http://wiki.github.com/AlistairIsrael/jcombinatorics">jcombinatorics</a></code>, the <a>Java Combinatorics Library</a> up on GitHub and released under the MIT license.</p>
<p>Ad-hoc performance testing was conducted on a 2.4 GHz Intel Core 2 Duo with 2GB RAM running OS X 10.5.8 and Apple&#8217;s Java 1.6.0_15. Results revealed that Johnson&#8217;s SEPA can generate all 479,001,600 permutations of P(12) at over to 38,000 permutations/ms. The above algorithm computes P(12,12) at around 32,000 permutations/ms which makes it nearly 85% as fast as SEPA P(<i>n</i>).</p>
<p>This article is also available for <a href="http://alistairisrael.files.wordpress.com/2009/09/sepnka1.pdf">download as a PDF</a>.</p>
<p>I&#8217;d love to hear from others if you have any thoughts to share on a formal proof of this algorithm, or of analysis of its running time. Meanwhile, Enjoy!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/237/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=237&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2009/09/22/simple-efficient-pnk-algorithm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
		<item>
		<title>Functional HTTP testing revisited using JUnit 4.7 Interceptors</title>
		<link>http://alistairisrael.wordpress.com/2009/09/03/functional-http-testing-revisited-using-junit-interceptors/</link>
		<comments>http://alistairisrael.wordpress.com/2009/09/03/functional-http-testing-revisited-using-junit-interceptors/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 03:07:13 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Java 6]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=195</guid>
		<description><![CDATA[This post revisits our functional HTTP test and demonstrate how to use JUnit 4.7's new Interceptors feature make our test fixture more reusable and portable. We also exhibit a convenient HttpHandler implementation that simplifies some of the effort required in responding to HTTP requests.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=195&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>(I originally planned this to be a single article, but because of the scope decided to split it into two parts. Read the <a href="http://alistairisrael.wordpress.com/2009/09/02/functional-testing-with-sun-java-6-httpserver/">first part</a> for the the basics of using Sun&#8217;s <code>HttpServer</code> to conduct functional HTTP testing. Here we revisit our functional test and rewrite it using JUnit 4.7&#8242;s new Interceptors feature.)</p>
<h3>Recap</h3>
<p>In my <a href="http://alistairisrael.wordpress.com/2009/09/02/functional-testing-with-sun-java-6-httpserver/">previous post</a>, I demonstrated how to use Sun&#8217;s <code><a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html">HttpServer</a></code> API to write a functional test of an HTTP &#8216;conversation&#8217;.</p>
<p>Recall that I thought my initial solution seemed inelegant. It was verbose, with some start up and shutdown code that would have to be repeated for each test, and which I felt cluttered the actual test code.</p>
<p>It was also tedious, in the sense that the raw <code><a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpHandler.html">HttpHandler</a></code> and <code><a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpExchange.html">HttpExchange</a></code> API required us to do quite a few things manually, and unintuitively (such as having to compute and write out the length of our response <i>before</i> the response itself).</p>
<p>In this post, we&#8217;ll explore how to use the new Interceptors feature &#8216;quietly&#8217; released with JUnit 4.7 to write reusable, portable pre and post-test behaviour. I&#8217;ll also exhibit a convenient <code><a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpHandler.html">HttpHandler</a></code> implementation that simplifies some of the effort required in responding to HTTP requests.</p>
<p><span id="more-195"></span></p>
<h3>JUnit Interceptors at a glance</h3>
<p>Developers familiar to JUnit will know that prior to JUnit 4.x, to perform pre/post test behaviour such as setting up and tearing down test scaffolding or external resources, we had no choice but to override <code><a href="http://junit.org/apidocs/junit/framework/TestCase.html#setUp%28%29">TestCase#setup()</a></code> and <code><a href="http://junit.org/apidocs/junit/framework/TestCase.html#tearDown%28%29">TestCase#tearDown()</a></code>. To make this reusable across test classes, we had to extend <code><a href="http://junit.org/apidocs/junit/framework/TestCase.html">TestCase</a></code> and our actual tests had to extend that custom class.</p>
<p>JUnit 4.x introduced annotation-driven testing using the <code><a href="http://junit.org/apidocs/org/junit/Test.html">@Test</a></code> annotation, which allowed us to write tests that were simple POJOs. However, this didn&#8217;t help when we wanted reusable pre/post test behavior. We still had to write our custom base class, this time with methods annotated with <code><a href="http://junit.org/apidocs/org/junit/Before.html">@Before</a></code> and <code><a href="http://junit.org/apidocs/org/junit/After.html">@After</a></code>.</p>
<p>JUnit 4.7 introduces a new feature called &#8220;Interceptors&#8221; that aims to bring back to JUnit the ability to to &#8216;meta-testing&#8217; with a much cleaner and simpler API. See Kent Beck&#8217;s<sup><a href="#f1">1</a></sup> and David Saff&#8217;s<sup><a href="#f2">2</a></sup> blog posts for more of the inside scoop.</p>
<h3>Writing an Interceptor</h3>
<p>A good place to start is to look at the source for <code><a href="http://github.com/KentBeck/junit/blob/a8629da96207e1ce71ead9ba9f85bc324f09bcab/src/main/java/org/junit/rules/ExternalResource.java">ExternalResource</a></code>.</p>
<p>If we need complete control over our interceptor&#8217;s behavior, we&#8217;d need to implement <code><a href="http://github.com/KentBeck/junit/blob/a8629da96207e1ce71ead9ba9f85bc324f09bcab/src/main/java/org/junit/rules/MethodRule.java">MethodRule</a></code> from scratch.</p>
<p>In our case, we can get by simply by extending <code>ExternalResource</code>. We setup our HTTP server in the <code>before()</code> method, and tear it down in our <code>after()</code> method. We also provide a way to specify the port to listen on in our constructor:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;"><span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000000;font-weight:bold;">class</span> HttpServerInterceptor <span style="color:#000000;font-weight:bold;">extends</span> ExternalResource <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">private</span> <span style="color:#000000;font-weight:bold;">final</span> InetSocketAddress address<span style="color:#339933;">;</span></p>
<p>&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">private</span> HttpServer httpServer<span style="color:#339933;">;</span></p>
<p>&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">public</span> HttpServerInterceptor<span style="color:#009900;">&#40;</span><span style="color:#000000;font-weight:bold;">final</span> <span style="color:#000066;font-weight:bold;">int</span> port<span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">this</span>.<span style="color:#006633;">address</span> <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> InetSocketAddress<span style="color:#009900;">&#40;</span>port<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <span style="color:#009900;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">protected</span> <span style="color:#000000;font-weight:bold;">final</span> <span style="color:#000066;font-weight:bold;">void</span> before<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#000000;font-weight:bold;">throws</span> <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Athrowable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">Throwable</span></a> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">super</span>.<span style="color:#006633;">before</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; httpServer <span style="color:#339933;">=</span> HttpServer.<span style="color:#006633;">create</span><span style="color:#009900;">&#40;</span>address, 0<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; httpServer.<span style="color:#006633;">start</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <span style="color:#009900;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">protected</span> <span style="color:#000000;font-weight:bold;">final</span> <span style="color:#000066;font-weight:bold;">void</span> after<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; httpServer.<span style="color:#006633;">stop</span><span style="color:#009900;">&#40;</span>0<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">super</span>.<span style="color:#006633;">after</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <span style="color:#009900;">&#125;</span></div>
<p>Next, we provide a delegate method to register handlers:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000066;font-weight:bold;">void</span> addHandler<span style="color:#009900;">&#40;</span><a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">String</span></a> path, HttpHandler handler<span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; httpServer.<span style="color:#006633;">createContext</span><span style="color:#009900;">&#40;</span>path, handler<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <span style="color:#009900;">&#125;</span></div>
<h3>Using Interceptors</h3>
<p>To use our new JUnit interceptor, all we need to do is annotate a public field with the <code>org.junit.Rule</code> annotation. Note that the field has to be <i>public</i>&mdash;this might raise a warning if you use Checkstyle and personally, I wish JUnit would allow private <code>@Rule</code>s.</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">@Rule<br />
<span style="color:#000000;font-weight:bold;">public</span> HttpServerInterceptor httpServer <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> HttpServerInterceptor<span style="color:#009900;">&#40;</span>8000<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span>
</div>
<p>The JUnit runner does the rest. It&#8217;ll transparently call our interceptor&#8217;s <code>before()</code> method, run the <code>@Test</code> then call our interceptor&#8217;s <code>after()</code> method.</p>
<p>Armed with this, we can rewrite the rest of our HTTP functional test as:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">
@Test<br />
<span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000066;font-weight:bold;">void</span> testHttpServer<span style="color:#009900;">&#40;</span><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><br />
&nbsp; &nbsp; httpServer.<span style="color:#006633;">addHandler</span><span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;/1234.xml&quot;</span>, <span style="color:#000000;font-weight:bold;">new</span> HttpHandler<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000066;font-weight:bold;">void</span> handle<span style="color:#009900;">&#40;</span>HttpExchange exchange<span style="color:#009900;">&#41;</span> <span style="color:#000000;font-weight:bold;">throws</span> <span style="color:#003399;">IOException</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000066;font-weight:bold;">byte</span><span style="color:#009900;">&#91;</span><span style="color:#009900;">&#93;</span> response <span style="color:#339933;">=</span> <span style="color:#0000ff;">&quot;&lt;?xml version=<span style="color:#000099;font-weight:bold;">\&quot;</span>1.0<span style="color:#000099;font-weight:bold;">\&quot;</span>?&gt;<span style="color:#000099;font-weight:bold;">\n</span>&lt;resource id=<span style="color:#000099;font-weight:bold;">\&quot;</span>1234<span style="color:#000099;font-weight:bold;">\&quot;</span> name=<span style="color:#000099;font-weight:bold;">\&quot;</span>test<span style="color:#000099;font-weight:bold;">\&quot;</span> /&gt;<span style="color:#000099;font-weight:bold;">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color:#006633;">getBytes</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange.<span style="color:#006633;">sendResponseHeaders</span><span style="color:#009900;">&#40;</span><span style="color:#003399;">HttpURLConnection</span>.<span style="color:#006633;">HTTP_OK</span>, response.<span style="color:#006633;">length</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange.<span style="color:#006633;">getResponseBody</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span>.<span style="color:#006633;">write</span><span style="color:#009900;">&#40;</span>response<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange.<span style="color:#006633;">close</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#009900;">&#125;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></p>
<p>&nbsp; &nbsp; <span style="color:#666666;font-style:italic;">// exercise our client code</span><br />
&nbsp; &nbsp; <span style="color:#003399;">URL</span> url <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <span style="color:#003399;">URL</span><span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;http://localhost:8000/1234.xml&quot;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <span style="color:#003399;">URLConnection</span> conn <span style="color:#339933;">=</span> url.<span style="color:#006633;">openConnection</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <span style="color:#003399;">BufferedReader</span> in <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <span style="color:#003399;">BufferedReader</span><span style="color:#009900;">&#40;</span><span style="color:#000000;font-weight:bold;">new</span> <span style="color:#003399;">InputStreamReader</span><span style="color:#009900;">&#40;</span>conn.<span style="color:#006633;">getInputStream</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; assertEquals<span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;&lt;?xml version=<span style="color:#000099;font-weight:bold;">\&quot;</span>1.0<span style="color:#000099;font-weight:bold;">\&quot;</span>?&gt;&quot;</span>, in.<span style="color:#006633;">readLine</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; assertEquals<span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;&lt;resource id=<span style="color:#000099;font-weight:bold;">\&quot;</span>1234<span style="color:#000099;font-weight:bold;">\&quot;</span> name=<span style="color:#000099;font-weight:bold;">\&quot;</span>test<span style="color:#000099;font-weight:bold;">\&quot;</span> /&gt;&quot;</span>, in.<span style="color:#006633;">readLine</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
<span style="color:#009900;">&#125;</span></div>
<p></p>
<h3>Simplifying our Handlers</h3>
<p>We&#8217;ve managed to cut out some of our test scaffolding code. Now let&#8217;s simplify our actual handler. For this, I wrote <code><a href="http://github.com/AlistairIsrael/junit-rules/blob/e9377a9d19eb2fc1d6dad35404555407baacb947/src/main/java/junit/interceptors/httpserver/SimpleHttpHandler.java">SimpleHttpHandler</a></code>, which is an <code>HttpHandler</code> which provides delegate methods to the actual <code>HttpExchange</code>. That is, by extending <code>SimpleHttpHandler</code> we can simply go <code>getRequestURI()</code> instead of having to go <code>httpExchange.getRequestURI()</code>.</p>
<p>Additionally, <code><a href="http://github.com/AlistairIsrael/junit-rules/blob/e9377a9d19eb2fc1d6dad35404555407baacb947/src/main/java/junit/interceptors/httpserver/SimpleHttpHandler.java">SimpleHttpHandler</a></code> uses an internal <code>ByteArrayOutputStream</code>, exposed via a <code>PrintWriter</code> in the <code>getResponse()</code> method. This lets us simply write our response as if, say, to <code>System.out</code>, and it takes care of computing the total response length later on when we call the <code>sendResponse()</code> method.</p>
<p>Here&#8217;s the same handler above rewritten using <code>SimpleHttpHandler</code>:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">&nbsp; &nbsp; httpServer.<span style="color:#006633;">addHandler</span><span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;/&quot;</span>, <span style="color:#000000;font-weight:bold;">new</span> SimpleHttpHandler<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">protected</span> <span style="color:#000066;font-weight:bold;">void</span> onGet<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#000000;font-weight:bold;">throws</span> <span style="color:#003399;">IOException</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getResponse<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span>.<span style="color:#006633;">println</span><span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;&lt;?xml version=<span style="color:#000099;font-weight:bold;">\&quot;</span>1.0<span style="color:#000099;font-weight:bold;">\&quot;</span>?&gt;&quot;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getResponse<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span>.<span style="color:#006633;">println</span><span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;&lt;resource id=<span style="color:#000099;font-weight:bold;">\&quot;</span>1234<span style="color:#000099;font-weight:bold;">\&quot;</span> name=<span style="color:#000099;font-weight:bold;">\&quot;</span>test<span style="color:#000099;font-weight:bold;">\&quot;</span> /&gt;&quot;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendResponse<span style="color:#009900;">&#40;</span>HTTP_OK<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#009900;">&#125;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></div>
<p>The complete source to <code>HttpServerInterceptor</code> and <code>SimpleHttpHandler</code> are provided as part of <code><a href="http://wiki.github.com/AlistairIsrael/junit-rules">junit-rules</a></code>, a &#8216;pet&#8217; project I&#8217;ve made to explore and showcase JUnit Rules.</p>
<p>Hopefully, I&#8217;ll be able to add other, useful rules to <code><a href="http://wiki.github.com/AlistairIsrael/junit-rules">junit-rules</a></code>. Feel free to download, study and use it. Better yet, if you want to share any useful rules of your own, by all means, please fork or contribute to it!</p>
<p>(Edit 2009/10/16: Changed links/references to <code>junit-interceptors</code> to <code>junit-rules</code>, all because I misread Kent Beck&#8217;s post—they were renaming it <em>to</em> Rules <em>from</em> Interceptors, and not the other way around!)</p>
<h3>References</h3>
<p><sup><a name="f1">1</a></sup> <a href="http://www.threeriversinstitute.org/blog/?p=155">Interceptors in JUnit</a><br />
<sup><a name="f2">2</a></sup> <a href="http://greenbar.saff.net/?p=17">JUnit 4.7: Interceptors: expected exceptions</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/195/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=195&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2009/09/03/functional-http-testing-revisited-using-junit-interceptors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Sun Java 6 HttpServer to write a functional HTTP test</title>
		<link>http://alistairisrael.wordpress.com/2009/09/02/functional-http-testing-with-sun-java-6-httpserver/</link>
		<comments>http://alistairisrael.wordpress.com/2009/09/02/functional-http-testing-with-sun-java-6-httpserver/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 15:23:33 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Java 6]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=143</guid>
		<description><![CDATA[This post demonstrates the use of <code>HttpServer</code> in Sun's Java 6 to conduct functional HTTP testing.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=143&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>(I originally planned this to be a single article, but because of the scope decided to split it into two parts. This first part explores the basics of using Sun&#8217;s <code>HttpServer</code> to conduct functional HTTP testing. <a href="http://alistairisrael.wordpress.com/2009/09/03/functional-http-testing-revisited-using-junit-interceptors/">Part 2</a> revisits the following test using JUnit 4.7&#8242;s new interceptors (rules) feature and demonstrates a simpler HTTP handler.)</p>
<h3>Forces</h3>
<p>At work, we recently had the need to perform functional testing of a custom client that used HTTP as a transport. This isn&#8217;t strictly unit testing since we&#8217;re conducting actual HTTP over a socket &amp; port instead of stubbing out or mocking the server, but in this case that was the only real way to test the client.</p>
<p>I could&#8217;ve fired up a standalone Web server and used that, but decided against it for a couple of reasons.</p>
<p>First, I wanted to have the server respond in a specific way to a particular client request. For example, if the request was for <code>GET /1234.xml</code> I might want to respond with an <code>HTTP 200</code> and an XML response body. Another request for <code>GET /0.xml</code> might return an <code>HTTP 404</code> instead.</p>
<p>To do that using, say, a Servlet container would mean writing multiple Servlets (mapped to various request URI) or a &#8216;rich&#8217; Servlet with additional complexity. I didn&#8217;t want to have to write tests to test my test scaffolding!</p>
<p>Secondly, a standalone server would have to be started and stopped outside of our standard compile/test/package process (using <a href="http://maven.apache.org/">Maven</a>). Other people wouldn&#8217;t be able to run the tests successfully without having the test server up as well.</p>
<p>Clearly, the best way to go was to use an embedded HTTP server, which would allow us to provide specific responses tailored for each unit test.</p>
<p>As luck would have it, it turns out that Sun&#8217;s Java 6 implementation comes with a lightweight HTTP server API built in. Read on as I demonstrate the basic use of Sun&#8217;s HTTP server classes to write a functional test.</p>
<p><span id="more-143"></span></p>
<h3>HTTP server in a box</h3>
<p>The heart of our test solution involves taking advantage of the lightweight HTTP server API included in Sun&#8217;s Java 6 implementation. Note that since this isn&#8217;t part of the Java core API this package may not be available on all Java platforms. If this is a problem, you might be better off using another embedded HTTP server such as <a href="http://www.mortbay.org/jetty/">Jetty</a>.</p>
<p>The class itself is <a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html">com.sun.net.httpserver.HttpServer</a>, and here&#8217;s how to use it in a nutshell:</p>
<ol>
<li>Create the server</li>
<li>Create a server context and register a request handler</li>
<li>Start the server</li>
<li>Perform your test</li>
<li>Stop the server, and verify/assert your expected behavior</li>
</ol>
<p>Now let&#8217;s look at each step in detail with corresponding code.</p>
<h4>Create the server</h4>
<p>We create an <code>HttpServer</code> using <code><a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html#create%28%29">HttpServer.create()</a></code>. To specify a port, we need to pass in an <code><a href="http://java.sun.com/javase/6/docs/api/java/net/InetSocketAddress.html">InetSocketAddress</a></code>:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">&nbsp; &nbsp; InetSocketAddress address <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> InetSocketAddress<span style="color:#009900;">&#40;</span>8000<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; HttpServer httpServer <span style="color:#339933;">=</span> HttpServer.<span style="color:#006633;">create</span><span style="color:#009900;">&#40;</span>address, 0<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></div>
<p>The second parameter to <code>HttpServer.create()</code> is the &#8216;backlog&#8217;, &#8220;the maximum number of queued incoming connections to allow on the listening socket&#8221;. Since that doesn&#8217;t really affect us, we can just pass in a <code>0</code> and a system default value is used.</p>
<h3>Creating and registering a request handler</h3>
<p>Now we get to the meat of actually stubbing our server&#8217;s behavior by creating and registering a request handler. <code>HttpServer</code> provides the <code><a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html#createContext%28java.lang.String,%20com.sun.net.httpserver.HttpHandler%29">createContext(String path, HttpHandler handler)</a></code> method to do just that.</p>
<p>Now, <code><a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpHandler.html">HttpHandler</a></code> is an interface which declares one method: <code><a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpHandler.html#handle%28com.sun.net.httpserver.HttpExchange%29">handle(HttpExchange)</a></code></p>
<p>Obviously, <code><a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpExchange.html">HttpExchange</a></code> is the class we need to work with when responding to HTTP requests. Let&#8217;s look at some code and I&#8217;ll explain what it does afterwards:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">&nbsp; &nbsp; HttpHandler handler <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> HttpHandler<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000066;font-weight:bold;">void</span> handle<span style="color:#009900;">&#40;</span>HttpExchange exchange<span style="color:#009900;">&#41;</span> <span style="color:#000000;font-weight:bold;">throws</span> <span style="color:#003399;">IOException</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000066;font-weight:bold;">byte</span><span style="color:#009900;">&#91;</span><span style="color:#009900;">&#93;</span> response <span style="color:#339933;">=</span> <span style="color:#0000ff;">&quot;&lt;?xml version=<span style="color:#000099;font-weight:bold;">\&quot;</span>1.0<span style="color:#000099;font-weight:bold;">\&quot;</span>?&gt;<span style="color:#000099;font-weight:bold;">\n</span>&lt;resource id=<span style="color:#000099;font-weight:bold;">\&quot;</span>1234<span style="color:#000099;font-weight:bold;">\&quot;</span> name=<span style="color:#000099;font-weight:bold;">\&quot;</span>test<span style="color:#000099;font-weight:bold;">\&quot;</span> /&gt;<span style="color:#000099;font-weight:bold;">\n</span>&quot;</span>.<span style="color:#006633;">getBytes</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange.<span style="color:#006633;">sendResponseHeaders</span><span style="color:#009900;">&#40;</span><a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahttpurlconnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">HttpURLConnection</span></a>.<span style="color:#006633;">HTTP_OK</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color:#006633;">length</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange.<span style="color:#006633;">getResponseBody</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span>.<span style="color:#006633;">write</span><span style="color:#009900;">&#40;</span>response<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange.<span style="color:#006633;">close</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#009900;">&#125;</span><span style="color:#339933;">;</span></div>
<p>Basically: we convert our response string into a byte array. We send an <code>HTTP 200</code> (OK) along with the number of bytes we&#8217;re about to send as the response body. We then write out the bytes of our response body, then close the <code>HttpExchange</code>. The complete <code>HttpExchange</code> life cycle is detailed in its <a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpExchange.html">API documentation</a>.</p>
<p>We now create a context for the URI we&#8217;re interested in, passing in our newly created <code>HttpHandler</code>, then start the server:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">&nbsp; &nbsp; httpServer.<span style="color:#006633;">createContext</span><span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;/1234.xml&quot;</span>, handler<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; httpServer.<span style="color:#006633;">start</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></div>
<p>At this point, an actual HTTP server will start running in a background thread ready to respond to requests. Let&#8217;s exercise our client code:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">&nbsp; &nbsp; <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">URL</span></a> url <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">URL</span></a><span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;http://localhost:8000/1234.xml&quot;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurlconnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">URLConnection</span></a> conn <span style="color:#339933;">=</span> url.<span style="color:#006633;">openConnection</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">BufferedReader</span></a> in <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">BufferedReader</span></a><span style="color:#009900;">&#40;</span><span style="color:#000000;font-weight:bold;">new</span> <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstreamreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">InputStreamReader</span></a><span style="color:#009900;">&#40;</span>conn.<span style="color:#006633;">getInputStream</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; assertEquals<span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;&lt;?xml version=<span style="color:#000099;font-weight:bold;">\&quot;</span>1.0<span style="color:#000099;font-weight:bold;">\&quot;</span>?&gt;&quot;</span>, in.<span style="color:#006633;">readLine</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; assertEquals<span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;&lt;resource id=<span style="color:#000099;font-weight:bold;">\&quot;</span>1234<span style="color:#000099;font-weight:bold;">\&quot;</span> name=<span style="color:#000099;font-weight:bold;">\&quot;</span>test<span style="color:#000099;font-weight:bold;">\&quot;</span> /&gt;&quot;</span>, in.<span style="color:#006633;">readLine</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></div>
<p>Our client code connects to our server, retrieves the associated URI, then, using a <code>BufferedReader</code> reads in lines from the input stream. We make a few JUnit assertions on what we received.</p>
<p>The only thing left to do is to stop our <code>HttpServer</code>:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">&nbsp; &nbsp; &nbsp;httpServer.<span style="color:#006633;">stop</span><span style="color:#009900;">&#40;</span>0<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></div>
<p>The parameter is the number of <i>seconds</i> (NOTE: not milliseconds) to block to wait for our <code>HttpServer</code> to shutdown properly. Since we know we&#8217;re not serving any other requests, we can safely tell our <code>HttpServer</code> to shut down immediately.</p>
<h3>At a glance</h3>
<p>Here&#8217;s our functional test at a glance:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;">&nbsp; &nbsp; <span style="color:#666666;font-style:italic;">// create the HttpServer</span><br />
&nbsp; &nbsp; InetSocketAddress address <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> InetSocketAddress<span style="color:#009900;">&#40;</span>8000<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; HttpServer httpServer <span style="color:#339933;">=</span> HttpServer.<span style="color:#006633;">create</span><span style="color:#009900;">&#40;</span>address, 0<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></p>
<p>&nbsp; &nbsp; <span style="color:#666666;font-style:italic;">// create and register our handler</span><br />
&nbsp; &nbsp; HttpHandler handler <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> HttpHandler<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000066;font-weight:bold;">void</span> handle<span style="color:#009900;">&#40;</span>HttpExchange exchange<span style="color:#009900;">&#41;</span> <span style="color:#000000;font-weight:bold;">throws</span> <span style="color:#003399;">IOException</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000066;font-weight:bold;">byte</span><span style="color:#009900;">&#91;</span><span style="color:#009900;">&#93;</span> response <span style="color:#339933;">=</span> <span style="color:#0000ff;">&quot;&lt;?xml version=<span style="color:#000099;font-weight:bold;">\&quot;</span>1.0<span style="color:#000099;font-weight:bold;">\&quot;</span>?&gt;<span style="color:#000099;font-weight:bold;">\n</span>&lt;resource id=<span style="color:#000099;font-weight:bold;">\&quot;</span>1234<span style="color:#000099;font-weight:bold;">\&quot;</span> name=<span style="color:#000099;font-weight:bold;">\&quot;</span>test<span style="color:#000099;font-weight:bold;">\&quot;</span> /&gt;<span style="color:#000099;font-weight:bold;">\n</span>&quot;</span>.<span style="color:#006633;">getBytes</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange.<span style="color:#006633;">sendResponseHeaders</span><span style="color:#009900;">&#40;</span><a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahttpurlconnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">HttpURLConnection</span></a>.<span style="color:#006633;">HTTP_OK</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color:#006633;">length</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange.<span style="color:#006633;">getResponseBody</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span>.<span style="color:#006633;">write</span><span style="color:#009900;">&#40;</span>response<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange.<span style="color:#006633;">close</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#009900;">&#125;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; httpServer.<span style="color:#006633;">createContext</span><span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;/1234.xml&quot;</span>, handler<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></p>
<p>&nbsp; &nbsp; <span style="color:#666666;font-style:italic;">// start the server</span><br />
&nbsp; &nbsp; httpServer.<span style="color:#006633;">start</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></p>
<p>&nbsp; &nbsp; <span style="color:#666666;font-style:italic;">// verify our client code</span><br />
&nbsp; &nbsp; <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">URL</span></a> url <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">URL</span></a><span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;http://localhost:8000/1234.xml&quot;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurlconnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">URLConnection</span></a> conn <span style="color:#339933;">=</span> url.<span style="color:#006633;">openConnection</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">BufferedReader</span></a> in <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">BufferedReader</span></a><span style="color:#009900;">&#40;</span><span style="color:#000000;font-weight:bold;">new</span> <a style="color:#000060;" href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstreamreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">InputStreamReader</span></a><span style="color:#009900;">&#40;</span>conn.<span style="color:#006633;">getInputStream</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; assertEquals<span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;&lt;?xml version=<span style="color:#000099;font-weight:bold;">\&quot;</span>1.0<span style="color:#000099;font-weight:bold;">\&quot;</span>?&gt;&quot;</span>, in.<span style="color:#006633;">readLine</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; assertEquals<span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;&lt;resource id=<span style="color:#000099;font-weight:bold;">\&quot;</span>1234<span style="color:#000099;font-weight:bold;">\&quot;</span> name=<span style="color:#000099;font-weight:bold;">\&quot;</span>test<span style="color:#000099;font-weight:bold;">\&quot;</span> /&gt;&quot;</span>, in.<span style="color:#006633;">readLine</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></p>
<p>&nbsp; &nbsp; <span style="color:#666666;font-style:italic;">// stop the server</span><br />
&nbsp; &nbsp; httpServer.<span style="color:#006633;">stop</span><span style="color:#009900;">&#40;</span>0<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></div>
<p>While functional, I find the above code rather verbose. If I want to write more HTTP tests, I certainly don&#8217;t want to keep repeating myself going <code>httpServer.start();</code> &#8230; <code>httpServer.stop(0);</code>. I especially find it tedious to have to compute the length of the HTTP response body beforehand, then send it along with our HTTP response code <i>before</i> writing out the actual body.</p>
<p>In the <a href="http://alistairisrael.wordpress.com/2009/09/03/functional-http-testing-revisited-using-junit-interceptors/">next part</a> of this article, we look at using some shiny new features in JUnit 4.7 that lets us write cleaner, more reusable tests that involve pre and post test behavior. Also, I&#8217;ll exhibit a helper class that simplifies some of the effort involved in implementing an <code>HttpHandler</code>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/143/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=143&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2009/09/02/functional-http-testing-with-sun-java-6-httpserver/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby script to install JFace JARs into your local Maven repository</title>
		<link>http://alistairisrael.wordpress.com/2009/07/16/ruby-script-install-jface-into-maven-repo/</link>
		<comments>http://alistairisrael.wordpress.com/2009/07/16/ruby-script-install-jface-into-maven-repo/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 13:17:01 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[jface]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=122</guid>
		<description><![CDATA[A Ruby shell script to find the JFace JARs under the Eclipse plugins directory and add them to the local Maven repository with appropriate group and artifact ids.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=122&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After figuring out how to use <a href="http://alistairisrael.wordpress.com/2009/07/15/writing-eclipse-swt-apps-using-maven/">Maven to write SWT applications</a>, I figured it was time to tackle <a href="http://wiki.eclipse.org/index.php/JFace">JFace</a>.</p>
<p>If you read through the JFace wiki page above, though, it says that to use JFace + SWT outside of Eclipse we basically have to <a href="http://wiki.eclipse.org/index.php/JFace#Identify_the_Required_External_JAR_Files_for_SWT_and_JFace">locate several dependencies</a>, namely:
<ul>
<li><code>org.eclipse.core.commands_<em>&lt;version info&gt;</em>.jar</code></li>
<li><code>org.eclipse.equinox.common_<em>&lt;version info&gt;</em>.jar</code></li>
<li><code>org.eclipse.jface_<em>&lt;version info&gt;</em>.jar</code></li>
<li><code>org.eclipse.osgi_<em>&lt;version info&gt;</em>.jar</code></li>
<li><code>org.eclipse.ui.workbench_<em>&lt;version info&gt;</em>.jar</li>
<p></code></ul>
<p>All of these are available in the standard installation of Eclipse IDE in the <code><em>${ECLIPSE_HOME}</em>/plugins</code> directory.</p>
<p>Now, it would be a 10 minute job to manually search for, copy and paste the actual JAR filenames and execute the Maven commands to install those into the local repository with proper group and artifact ids.</p>
<p>Since I&#8217;m &#8216;lazy&#8217;, though, instead of 10 minutes I decided to spend a couple of hours writing a Ruby script to do this all for me (I originally started to write it in bash but I realized it&#8217;d be a lot easier in Ruby).</p>
<p>Here it is. <span id="more-122"></span>Save this as <code>mvn_install_jface.rb</code> somewhere, and make it executable. To use the script, simply go:</p>
<blockquote><p><code>mvn_install_jface.rb <em>${ECLIPSE_HOME}</em></code></p></blockquote>
<p>Obviously, replace <code><em>${ECLIPSE_HOME}</em></code> with the directory where you installed Eclipse into.</p>
<p>If you don&#8217;t supply the directory, the script defaults to <code>/Applications/eclipse</code> which is where Eclipse would normally be installed in OS X.</p>
<div class="ruby" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;"><span style="color:#008000;font-style:italic;">#!/usr/bin/env ruby</span></p>
<p><span style="color:#CC0066;font-weight:bold;">require</span> <span style="color:#996600;">&#8216;find&#8217;</span></p>
<p><span style="color:#008000;font-style:italic;"># Some utility code. Skip to # MAIN for main logic</span></p>
<p>CHECK_MESSAGES=<span style="color:#006600;font-weight:bold;">&#123;</span><br />
&nbsp; <span style="color:#ff3333;font-weight:bold;">:exists</span> <span style="color:#006600;font-weight:bold;">=&gt;</span> <span style="color:#996600;">&#8216;not found&#8217;</span>,<br />
&nbsp; <span style="color:#ff3333;font-weight:bold;">:file</span> <span style="color:#006600;font-weight:bold;">=&gt;</span> <span style="color:#996600;">&#8216;is not a file&#8217;</span>,<br />
&nbsp; <span style="color:#ff3333;font-weight:bold;">:directory</span> <span style="color:#006600;font-weight:bold;">=&gt;</span> <span style="color:#996600;">&#8216;is not a directory&#8217;</span>,<br />
&nbsp; <span style="color:#ff3333;font-weight:bold;">:readable</span> <span style="color:#006600;font-weight:bold;">=&gt;</span> <span style="color:#996600;">&#8216;is not readable&#8217;</span>,<br />
&nbsp; <span style="color:#ff3333;font-weight:bold;">:executable</span> <span style="color:#006600;font-weight:bold;">=&gt;</span> <span style="color:#996600;">&#8216;is not executable&#8217;</span><br />
<span style="color:#006600;font-weight:bold;">&#125;</span></p>
<p><span style="color:#9966CC;font-weight:bold;">def</span> assert_path<span style="color:#006600;font-weight:bold;">&#040;</span>path, <span style="color:#006600;font-weight:bold;">*</span>checks<span style="color:#006600;font-weight:bold;">&#041;</span><br />
&nbsp; checks.<span style="color:#9900CC;">each</span> <span style="color:#006600;font-weight:bold;">&#123;</span> <span style="color:#006600;font-weight:bold;">|</span>check<span style="color:#006600;font-weight:bold;">|</span><br />
&nbsp; &nbsp; <span style="color:#9966CC;font-weight:bold;">unless</span> <span style="color:#CC0066;font-weight:bold;">eval</span><span style="color:#006600;font-weight:bold;">&#040;</span><span style="color:#996600;">&quot;File.#{check}?(path)&quot;</span><span style="color:#006600;font-weight:bold;">&#041;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\&quot;</span>#{path}<span style="color:#000099;">\&quot;</span> #{CHECK_MESSAGES[check]}!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\a</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066;font-weight:bold;">exit</span> 1<br />
&nbsp; &nbsp; <span style="color:#9966CC;font-weight:bold;">end</span><br />
&nbsp; <span style="color:#006600;font-weight:bold;">&#125;</span><br />
<span style="color:#9966CC;font-weight:bold;">end</span></p>
<p><span style="color:#008000;font-style:italic;"># MAIN</span></p>
<p><span style="color:#008000;font-style:italic;"># First we do our environmental checks</span></p>
<p><span style="color:#008000;font-style:italic;"># Determine Eclipse installation directory parameter</span><br />
ECLIPSE_HOME=ARGV<span style="color:#006600;font-weight:bold;">&#091;</span>0<span style="color:#006600;font-weight:bold;">&#093;</span> <span style="color:#006600;font-weight:bold;">||</span> <span style="color:#CC00FF;font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600;font-weight:bold;">&#040;</span><span style="color:#996600;">&#8216;/&#8217;</span>, <span style="color:#996600;">&#8216;Applications&#8217;</span>, <span style="color:#996600;">&#8216;eclipse&#8217;</span><span style="color:#006600;font-weight:bold;">&#041;</span><br />
assert_path ECLIPSE_HOME, <span style="color:#ff3333;font-weight:bold;">:exists</span>, <span style="color:#ff3333;font-weight:bold;">:directory</span>, <span style="color:#ff3333;font-weight:bold;">:readable</span><br />
<span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Using Eclipse home: <span style="color:#000099;">\&quot;</span>#{ECLIPSE_HOME}<span style="color:#000099;">\&quot;</span>&quot;</span></p>
<p><span style="color:#008000;font-style:italic;"># Now for the plugins directory under ECLIPSE_HOME</span><br />
PLUGINS_DIR=<span style="color:#CC00FF;font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600;font-weight:bold;">&#040;</span>ECLIPSE_HOME, <span style="color:#996600;">&#8216;plugins&#8217;</span><span style="color:#006600;font-weight:bold;">&#041;</span><br />
assert_path PLUGINS_DIR, <span style="color:#ff3333;font-weight:bold;">:exists</span>, <span style="color:#ff3333;font-weight:bold;">:directory</span>, <span style="color:#ff3333;font-weight:bold;">:readable</span></p>
<p><span style="color:#008000;font-style:italic;"># Next, we look for the Maven executable</span><br />
MVN=<span style="color:#996600;">`which mvn`</span>.<span style="color:#CC0066;font-weight:bold;">chomp</span> <span style="color:#008000;font-style:italic;"># chomp to get rid of trailing &#8216;\n&#8217;</span><br />
assert_path MVN, <span style="color:#ff3333;font-weight:bold;">:exists</span>, <span style="color:#ff3333;font-weight:bold;">:file</span>, <span style="color:#ff3333;font-weight:bold;">:executable</span><br />
<span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Using Maven: <span style="color:#000099;">\&quot;</span>#{MVN}<span style="color:#000099;">\&quot;</span>&quot;</span></p>
<p><span style="color:#CC0066;font-weight:bold;">puts</span></p>
<p><span style="color:#008000;font-style:italic;"># The libraries we&#8217;re interested in</span><br />
LIBS=<span style="color:#996600;">&quot;org.eclipse.core.commands <span style="color:#000099;">\</span><br />
org.eclipse.equinox.common <span style="color:#000099;">\</span><br />
org.eclipse.jface <span style="color:#000099;">\</span><br />
org.eclipse.osgi <span style="color:#000099;">\</span><br />
org.eclipse.ui.workbench <span style="color:#000099;">\</span><br />
org.eclipse.jface.databinding <span style="color:#000099;">\</span><br />
org.eclipse.jface.text <span style="color:#000099;">\</span><br />
org.eclipse.jface&quot;</span>.<span style="color:#CC0066;font-weight:bold;">split</span></p>
<p><span style="color:#008000;font-style:italic;"># As filename glob patterns</span><br />
globs=LIBS.<span style="color:#9900CC;">inject</span><span style="color:#006600;font-weight:bold;">&#040;</span><span style="color:#006600;font-weight:bold;">&#123;</span><span style="color:#006600;font-weight:bold;">&#125;</span><span style="color:#006600;font-weight:bold;">&#041;</span> <span style="color:#006600;font-weight:bold;">&#123;</span><span style="color:#006600;font-weight:bold;">|</span>map, lib<span style="color:#006600;font-weight:bold;">|</span> map<span style="color:#006600;font-weight:bold;">&#091;</span>lib<span style="color:#006600;font-weight:bold;">&#093;</span> = <span style="color:#CC00FF;font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600;font-weight:bold;">&#040;</span>PLUGINS_DIR, <span style="color:#996600;">&quot;#{lib}_*.jar&quot;</span><span style="color:#006600;font-weight:bold;">&#041;</span>; map<span style="color:#006600;font-weight:bold;">&#125;</span></p>
<p>found_jars=<span style="color:#006600;font-weight:bold;">&#123;</span><span style="color:#006600;font-weight:bold;">&#125;</span></p>
<p><span style="color:#CC00FF;font-weight:bold;">Find</span>.<span style="color:#9900CC;">find</span><span style="color:#006600;font-weight:bold;">&#040;</span>PLUGINS_DIR<span style="color:#006600;font-weight:bold;">&#041;</span> <span style="color:#006600;font-weight:bold;">&#123;</span> <span style="color:#006600;font-weight:bold;">|</span>path<span style="color:#006600;font-weight:bold;">|</span><br />
&nbsp; globs.<span style="color:#9900CC;">delete_if</span> <span style="color:#006600;font-weight:bold;">&#123;</span> <span style="color:#006600;font-weight:bold;">|</span>lib, glob<span style="color:#006600;font-weight:bold;">|</span><br />
&nbsp; &nbsp; <span style="color:#9966CC;font-weight:bold;">if</span> <span style="color:#CC00FF;font-weight:bold;">File</span>.<span style="color:#9900CC;">fnmatch</span>?<span style="color:#006600;font-weight:bold;">&#040;</span>glob, path<span style="color:#006600;font-weight:bold;">&#041;</span><br />
&nbsp; &nbsp; &nbsp; assert_path path, <span style="color:#ff3333;font-weight:bold;">:file</span>, <span style="color:#ff3333;font-weight:bold;">:readable</span><br />
&nbsp; &nbsp; &nbsp; found_jars<span style="color:#006600;font-weight:bold;">&#091;</span>lib<span style="color:#006600;font-weight:bold;">&#093;</span> = path<br />
&nbsp; &nbsp; <span style="color:#9966CC;font-weight:bold;">end</span><br />
&nbsp; <span style="color:#006600;font-weight:bold;">&#125;</span><br />
<span style="color:#006600;font-weight:bold;">&#125;</span></p>
<p><span style="color:#9966CC;font-weight:bold;">unless</span> globs.<span style="color:#9900CC;">empty</span>?<br />
&nbsp; <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Couldn&#8217;t find any JARS for the following libraries:&quot;</span><br />
&nbsp; globs.<span style="color:#9900CC;">each_pair</span> <span style="color:#006600;font-weight:bold;">&#123;</span><span style="color:#006600;font-weight:bold;">|</span>lib, glob<span style="color:#006600;font-weight:bold;">|</span> <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{lib} =&gt; #{glob}&quot;</span><span style="color:#006600;font-weight:bold;">&#125;</span><br />
&nbsp; <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\a</span>&quot;</span><br />
&nbsp; <span style="color:#CC0066;font-weight:bold;">exit</span> 2<br />
<span style="color:#9966CC;font-weight:bold;">end</span></p>
<p><span style="color:#008000;font-style:italic;"># Utility struct just to make things a little tidier</span><br />
Dependency = <span style="color:#CC00FF;font-weight:bold;">Struct</span>.<span style="color:#9900CC;">new</span> <span style="color:#ff3333;font-weight:bold;">:groupId</span>, <span style="color:#ff3333;font-weight:bold;">:artifactId</span>, <span style="color:#ff3333;font-weight:bold;">:version</span></p>
<p>DEPS=LIBS.<span style="color:#9900CC;">map</span> <span style="color:#006600;font-weight:bold;">&#123;</span> <span style="color:#006600;font-weight:bold;">|</span>lib<span style="color:#006600;font-weight:bold;">|</span><br />
&nbsp; parts=lib.<span style="color:#CC0066;font-weight:bold;">split</span><span style="color:#006600;font-weight:bold;">&#040;</span><span style="color:#996600;">&#8216;.&#8217;</span><span style="color:#006600;font-weight:bold;">&#041;</span><br />
&nbsp; artifactId=parts.<span style="color:#9900CC;">pop</span><br />
&nbsp; groupId=parts.<span style="color:#9900CC;">join</span><span style="color:#006600;font-weight:bold;">&#040;</span><span style="color:#996600;">&#8216;.&#8217;</span><span style="color:#006600;font-weight:bold;">&#041;</span><br />
&nbsp; file = found_jars<span style="color:#006600;font-weight:bold;">&#091;</span>lib<span style="color:#006600;font-weight:bold;">&#093;</span><br />
&nbsp; version=<span style="color:#CC00FF;font-weight:bold;">File</span>.<span style="color:#9900CC;">basename</span><span style="color:#006600;font-weight:bold;">&#040;</span>file<span style="color:#006600;font-weight:bold;">&#041;</span> <span style="color:#006600;font-weight:bold;">&#091;</span><span style="color:#006600;font-weight:bold;">/</span>^.<span style="color:#006600;font-weight:bold;">+</span>_<span style="color:#006600;font-weight:bold;">&#040;</span>.<span style="color:#006600;font-weight:bold;">+</span><span style="color:#006600;font-weight:bold;">&#041;</span>\.<span style="color:#9900CC;">jar</span>$<span style="color:#006600;font-weight:bold;">/</span>, <span style="color:#006666;">1</span><span style="color:#006600;font-weight:bold;">&#093;</span><br />
&nbsp; command=<span style="color:#996600;">&quot;#{MVN} install:install-file <span style="color:#000099;">\</span><br />
&nbsp; &nbsp; -DgroupId=#{groupId} <span style="color:#000099;">\</span><br />
&nbsp; &nbsp; -DartifactId=#{artifactId} <span style="color:#000099;">\</span><br />
&nbsp; &nbsp; -Dversion=#{version} <span style="color:#000099;">\</span><br />
&nbsp; &nbsp; -Dpackaging=jar <span style="color:#000099;">\</span><br />
&nbsp; &nbsp; -Dfile=#{file}&quot;</span>.<span style="color:#CC0066;font-weight:bold;">split</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600;font-weight:bold;">&#123;</span><span style="color:#006600;font-weight:bold;">|</span>s<span style="color:#006600;font-weight:bold;">|</span> s.<span style="color:#9900CC;">strip</span><span style="color:#006600;font-weight:bold;">&#125;</span><br />
&nbsp; <span style="color:#CC0066;font-weight:bold;">system</span> <span style="color:#006600;font-weight:bold;">*</span>command<br />
&nbsp; Dependency.<span style="color:#9900CC;">new</span><span style="color:#006600;font-weight:bold;">&#040;</span>groupId, artifactId, version<span style="color:#006600;font-weight:bold;">&#041;</span><br />
<span style="color:#006600;font-weight:bold;">&#125;</span></p>
<p><span style="color:#CC0066;font-weight:bold;">puts</span><br />
<span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&#8216;Include the following dependencies in your pom.xml:&#8217;</span></p>
<p>DEPS.<span style="color:#9900CC;">each</span> <span style="color:#006600;font-weight:bold;">&#123;</span> <span style="color:#006600;font-weight:bold;">|</span>dep<span style="color:#006600;font-weight:bold;">|</span><br />
&nbsp; <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot; &nbsp; &nbsp;&lt;dependency&gt;&quot;</span><br />
&nbsp; <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;#{dep.groupId}&lt;/groupId&gt;&quot;</span><br />
&nbsp; <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;#{dep.artifactId}&lt;/artifactId&gt;&quot;</span><br />
&nbsp; <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot; &nbsp; &nbsp; &nbsp;&lt;version&gt;#{dep.version}&lt;/version&gt;&quot;</span><br />
&nbsp; <span style="color:#CC0066;font-weight:bold;">puts</span> <span style="color:#996600;">&quot; &nbsp; &nbsp;&lt;/dependency&gt;&quot;</span><br />
<span style="color:#006600;font-weight:bold;">&#125;</span></p>
<p><span style="color:#008000;font-style:italic;"># kthxbye</span><br />
&nbsp;</div>
<p>First (after the environmental/sanity checks), the script tries to search for all the JFace JARs and will exit with an error if it can&#8217;t find any of the JARs we need.</p>
<p>The script will run all the necessary <code>mvn install:install-file ...</code> commands and figure out/supply the appropriate groupId, artifactId and version for all the JARs.</p>
<p>Finally, it&#8217;ll helpfully print out a bunch of <code>&lt;dependency&gt;...&lt;/dependency&gt;</code> snippets you can copy &amp; paste directly into your Maven pom.xml to use the found JFace JARs immediately. You&#8217;re welcome. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I&#8217;ve only tried this on OS X, and would love to hear from people who&#8217;ve tried it and gotten it work on other operating systems, especially Windows. I figure under Windows, if you already have Ruby and Maven on the path the command should be something like:</p>
<blockquote><p><code>ruby mvn_install_jface.rb C:\eclipse</code></p></blockquote>
<p>But I don&#8217;t have a Windows machine with Ruby, Java, Eclipse and Maven handy right now to test it myself.</p>
<p><em>Added 2009.07.24:</em> I just realized the <code>`which mvn`</code> part won&#8217;t work at all under Windows, so Windows users will have to edit that part out—just make sure <code>mvn</code> is on the path.</p>
<p>(Oh and legalese blah blah <a href="http://en.wikipedia.org/wiki/MIT_License">MIT License</a>.)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/122/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=122&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2009/07/16/ruby-script-install-jface-into-maven-repo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
		<item>
		<title>Writing Eclipse SWT apps using Maven</title>
		<link>http://alistairisrael.wordpress.com/2009/07/15/writing-eclipse-swt-apps-using-maven/</link>
		<comments>http://alistairisrael.wordpress.com/2009/07/15/writing-eclipse-swt-apps-using-maven/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 07:52:21 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[SWT]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=111</guid>
		<description><![CDATA[Learn how to set up an Eclipse SWT project built using Maven.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=111&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been trying to get my feet wet writing code for the Eclipse platform. I figured a good first step is to become familiar with <a href="http://www.eclipse.org/swt/">Eclipse SWT</a>—the Standard Widget Toolkit, and work my way up from there.</p>
<p>Since I use Maven, I had to struggle a bit finding out how to configure my Maven project properly so that it&#8217;ll build an SWT app that&#8217;ll run <em>both</em> under Eclipse and from the terminal.</p>
<p>A little Googling brought me to Brice Lambi&#8217;s post on <a href="http://blambi.blogspot.com/2007/07/maven2-swt-builds.html">Maven SWT builds</a>, but that was a little outdated.</p>
<p>In particular (and only after careful reading), the guide to <a href="http://www.eclipse.org/swt/macosx/">Deploying SWT apps on Mac OS X</a> says that the <code>-Djava.library.path=..</code> option is needed for Eclipse 3.2.2 and earlier. Apparently, starting with 3.4 all you need is your OS-specific JAR (see <a href="http://stackoverflow.com/questions/292548/swt-and-maven">here</a>).</p>
<p>Anyway, after much fiddling about, I was able to get it all working like so: <span id="more-111"></span></p>
<h3>Download and Install SWT to your local repository</h3>
<p>First, you&#8217;ll want to download and extract the SWT release specific to your OS from <a href="http://download.eclipse.org/eclipse/downloads/">eclipse.org</a>.</p>
<p>(I&#8217;m using <code>swt-3.4.2-carbon-macosx.zip</code> for the rest of this article.)</p>
<p>Next, you&#8217;ll want to install the extracted <code>swt.jar</code> into your local Maven repository with a suitable groupId and artifactId. Following the conventions on the <a href="http://mirrors.ibiblio.org/pub/mirrors/maven2/org/eclipse/swt/">Ibiblio Maven repository</a>, I used <code>org.eclipse.swt.carbon</code> and <code>macosx</code>, respectively. Here&#8217;s the Maven command to do this:</p>
<blockquote><p><code>mvn install:install-file -DgroupId=org.eclipse.swt.carbon -DartifactId=macosx -Dversion=3.4.2 -Dpackaging=jar -Dfile=swt.jar</code></p></blockquote>
<p>You might also want to point Maven to the SWT sources, so that if you use <a href="http://m2eclipse.codehaus.org/">m2eclipse</a> it can &#8216;attach&#8217; those sources to the library JAR appropriately:</p>
<blockquote><p><code>mvn install:install-file -DgroupId=org.eclipse.swt.carbon -DartifactId=macosx -Dversion=3.4.2 -Dpackaging=jar -Dfile=src.zip -Dclassifier=sources</code></p></blockquote>
<h3>Configure your POM</h3>
<p>A thousand XML elements is worth a thousand words:</p>
<div class="xml" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;"><span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;project</span> <span style="color:#000066;">xmlns</span>=<span style="color:#ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color:#000066;">xmlns:xsi</span>=<span style="color:#ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color:#009900;"> <span style="color:#000066;">xsi:schemaLocation</span>=<span style="color:#ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color:#000000;font-weight:bold;">&gt;</span></span><br />
&nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;modelVersion<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>4.0.0<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/modelVersion<span style="color:#000000;font-weight:bold;">&gt;</span></span></span></p>
<p>&nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;groupId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>com.example<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/groupId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;artifactId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>swt-test<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/artifactId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;packaging<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>jar<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/packaging<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;version<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>0.1<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/version<span style="color:#000000;font-weight:bold;">&gt;</span></span></span></p>
<p>&nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;dependencies<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;dependency<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;groupId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>${swt.groupId}<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/groupId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;artifactId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>${swt.artifactId}<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/artifactId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;version<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>3.4.2<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/version<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/dependency<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/dependencies<span style="color:#000000;font-weight:bold;">&gt;</span></span></span></p>
<p>&nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;profiles<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;profile<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;id<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>mac<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/id<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;activation<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;os<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;name<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>mac os x<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/name<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/os<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/activation<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;properties<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;swt.groupId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>org.eclipse.swt.carbon<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/swt.groupId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;swt.artifactId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>macosx<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/swt.artifactId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/properties<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/profile<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;profile<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;id<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>windows<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/id<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;activation<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;os<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;family<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>windows<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/family<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/os<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/activation<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;properties<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;swt.groupId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>org.eclipse.swt.win32.win32<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/swt.groupId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;swt.artifactId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span>x86<span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/swt.artifactId<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/properties<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/profile<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp; <span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/profiles<span style="color:#000000;font-weight:bold;">&gt;</span></span></span></p>
<p><span style="color:#009900;"><span style="color:#000000;font-weight:bold;">&lt;/project<span style="color:#000000;font-weight:bold;">&gt;</span></span></span><br />
&nbsp;</div>
<p>If you&#8217;re only ever going to target one OS, then you can do without the <code>&lt;profiles... &gt;</code> section and &#8216;hard-code&#8217; your OS-specific dependencies directly. I&#8217;ve left it there as a template to make your Maven project more portable.</p>
<h3>Run it!</h3>
<p>To run this from the command line, I made a simple bash script that basically goes:</p>
<blockquote><p><code>java -XstartOnFirstThread -cp ~/.m2/repository/org/eclipse/swt/carbon/macosx/3.4.2/macosx-3.4.2.jar:<br />
target/swt-test-0.1.jar<br />
com.example.swt.test.Main</code></p></blockquote>
<p>The real trick there is the <code>-XstartOnFirstThread</code> option to the Java runtime.</p>
<p>This is the same trick you&#8217;ll need to run the app under Eclipse. All you need to do is in <code>Run -&gt; Run Configurations...</code>, in the &#8220;Arguments&#8221; tab specify <code>-XstartOnFirstThread</code> in the &#8220;VM arguments&#8221; field and you&#8217;re all set!</p>
<p><em>(Edited 7/16/09 to align the SWT group and artifact id&#8217;s with those on the Ibiblio Maven repository, and added the Windows profile.)</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=111&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2009/07/15/writing-eclipse-swt-apps-using-maven/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveTest &#8211; Instantiation, mocking, and injection for the lazy</title>
		<link>http://alistairisrael.wordpress.com/2009/05/30/activetest-instantiation-mocking-and-injection-for-the-lazy/</link>
		<comments>http://alistairisrael.wordpress.com/2009/05/30/activetest-instantiation-mocking-and-injection-for-the-lazy/#comments</comments>
		<pubDate>Sat, 30 May 2009 02:31:28 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Java 5]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[Mockito]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=99</guid>
		<description><![CDATA[David Saff wrote in on Introducing MagicTest, asking why not just instantiate the variable in-line (private Foo foo = new Foo&#40;&#41;;). Which brings me to the real reason for coming up with MagicTest—ActiveTest. A code sample is worth a thousand words. Suppose we have a Spring JPA data access object: public class WidgetDao extends JpaDaoSupport [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=99&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>David Saff wrote in on <a href="http://alistairisrael.wordpress.com/2009/05/28/introducing-magictest/">Introducing MagicTest</a>, asking why not just instantiate the variable in-line (<code><span style="color:#000000;font-weight:bold;">private</span> Foo foo <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">new</span> Foo<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span></code>).</p>
<p>Which brings me to the real reason for coming up with <code>MagicTest</code>—<code>ActiveTest</code>.</p>
<p>A code sample is worth a thousand words. Suppose we have a Spring JPA data access object:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;"><span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000000;font-weight:bold;">class</span> WidgetDao <span style="color:#000000;font-weight:bold;">extends</span> JpaDaoSupport <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; @Autowired<br />
&nbsp; <span style="color:#000000;font-weight:bold;">public</span> WidgetDao<span style="color:#009900;">&#40;</span><span style="color:#000000;font-weight:bold;">final</span> EntityManager entityManager<span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; setEntityManager<span style="color:#009900;">&#40;</span>entityManager<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; <span style="color:#009900;">&#125;</span></p>
<p>&nbsp; <span style="color:#000000;font-weight:bold;">public</span> Widget find<span style="color:#009900;">&#40;</span><span style="color:#000066;font-weight:bold;">long</span> id<span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">return</span> getJpaTemplate<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span>.<span style="color:#006633;">find</span><span style="color:#009900;">&#40;</span>Widget.<span style="color:#000000;font-weight:bold;">class</span>, id<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; <span style="color:#009900;">&#125;</span></p>
<p><span style="color:#009900;">&#125;</span></div>
<p><code>WidgetDao</code> needs a JPA <code>EntityManager</code> provided to it at construction time. Normally, to write a unit test for <code>WidgetDao</code> we&#8217;d have to create our mock objects and setup our test scaffolding manually.</p>
<p>Using <code>ActiveTest</code>, however, all we need to write is:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;"><span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000000;font-weight:bold;">class</span> WidgetDaoTest <span style="color:#000000;font-weight:bold;">extends</span> ActiveTest<span style="color:#339933;">&lt;</span>WidgetDao<span style="color:#339933;">&gt;</span> <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; <span style="color:#000000;font-weight:bold;">private</span> WidgetDao widgetDao<span style="color:#339933;">;</span></p>
<p>&nbsp; @Mock<br />
&nbsp; <span style="color:#000000;font-weight:bold;">private</span> EntityManager em<span style="color:#339933;">;</span></p>
<p>&nbsp; @Test<br />
&nbsp; <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000066;font-weight:bold;">void</span> testFind<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; widgetDao.<span style="color:#006633;">find</span><span style="color:#009900;">&#40;</span>42<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; Mockito.<span style="color:#006633;">verify</span><span style="color:#009900;">&#40;</span>em<span style="color:#009900;">&#41;</span>.<span style="color:#006633;">find</span><span style="color:#009900;">&#40;</span>Widget.<span style="color:#000000;font-weight:bold;">class</span>, 42<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; <span style="color:#009900;">&#125;</span></p>
<p><span style="color:#009900;">&#125;</span></div>
<p>I&#8217;m lazy like that.<br />
<span id="more-99"></span></p>
<h3>Under the hood</h3>
<p><code>ActiveTest</code> uses the <code>ParameterizedType</code> hack to discover the actual type parameter (the class under test) at run-time.</p>
<p>It then inspects the test class instance for any fields annotated with Mockito&#8217;s <code>@Mock</code>, and has Mockito construct those mock objects. At the same time, each mock object is registered into an internal Spring auto-wiring capable bean factory.</p>
<p>Finally, that Spring bean factory is itself used to construct an instance of the class under test. Because we&#8217;re using Spring itself to perform construction, we fully support field, setter or construction-based <code>@Autowired</code> injection of dependencies.</p>
<p>See the full source code of <a href="http://code.google.com/p/dirty-mockito/source/browse/trunk/src/main/java/dirty/mockito/ActiveTest.java"><code>ActiveTest</code></a> on the <a href="http://code.google.com/p/dirty-mockito/">dirty-mockito</a> project site on Google Code</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/99/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=99&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2009/05/30/activetest-instantiation-mocking-and-injection-for-the-lazy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
		<item>
		<title>Introducing MagicTest</title>
		<link>http://alistairisrael.wordpress.com/2009/05/28/introducing-magictest/</link>
		<comments>http://alistairisrael.wordpress.com/2009/05/28/introducing-magictest/#comments</comments>
		<pubDate>Thu, 28 May 2009 13:45:00 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[Java 5]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=84</guid>
		<description><![CDATA[Introducing MagicTest, a JUnit 4 parameterized base class that automagically discovers and instantiates the test object.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=84&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve written enough JUnit 4 tests, then you should be familiar with code that looks like:</p>
<div class="java5" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;"><span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000000;font-weight:bold;">class</span> FooTest <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; <span style="color:#000000;font-weight:bold;">private</span> Foo foo<span style="color:#339933;">;</span></p>
<p>&nbsp; @Before<br />
&nbsp; <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#006600;font-weight:bold;">void</span> setUp<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; foo = <span style="color:#000000;font-weight:bold;">new</span> Foo<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; <span style="color:#009900;">&#125;</span></p>
<p>&nbsp; @Test<br />
&nbsp; <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#006600;font-weight:bold;">void</span> testBar<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; assertTrue<span style="color:#009900;">&#40;</span>foo.<span style="color:#006633;">bar</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; <span style="color:#009900;">&#125;</span><br />
<span style="color:#009900;">&#125;</span></div>
<p>Now, how often have you wished it were possible to simply go straight to <code>@Test</code>, do not pass <code>setUp()</code>, do not have to <code><strong>new</strong> Foo()</code>?</p>
<h3>Introducing <code>MagicTest</code></h3>
<p>So I came up with <code>MagicTest</code>, a parameterized base class for JUnit 4 tests that&#8217;ll let us do just that. <span id="more-84"></span><br />
It lets us rewrite the above simply as:</p>
<div class="java5" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;"><span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000000;font-weight:bold;">class</span> FooTest <span style="color:#000000;font-weight:bold;">extends</span> MagicTest<span style="color:#339933;">&lt;</span>Foo<span style="color:#339933;">&gt;</span> <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; <span style="color:#000000;font-weight:bold;">private</span> Foo foo<span style="color:#339933;">;</span></p>
<p>&nbsp; @Test<br />
&nbsp; <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#006600;font-weight:bold;">void</span> testBar<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; assertTrue<span style="color:#009900;">&#40;</span>foo.<span style="color:#006633;">bar</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; <span style="color:#009900;">&#125;</span><br />
<span style="color:#009900;">&#125;</span></div>
<p>It was bound to happen. I was bound to put to use my &#8216;hack&#8217; on <a href="http://alistairisrael.wordpress.com/2009/04/15/determining-the-type-parameter-of-a-generic-base-class/">how to discover the type parameter to a generic base class</a>.</p>
<p>Without further ado, here&#8217;s a simplified version of MagicTest:</p>
<div class="java" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;"><span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000000;font-weight:bold;">class</span> MagicTest<span style="color:#339933;">&lt;</span>T<span style="color:#339933;">&gt;</span> <span style="color:#009900;">&#123;</span></p>
<p>&nbsp; <span style="color:#000000;font-weight:bold;">private</span> <span style="color:#000000;font-weight:bold;">final</span> Class<span style="color:#339933;">&lt;</span>T<span style="color:#339933;">&gt;</span> classUnderTest<span style="color:#339933;">;</span></p>
<p>&nbsp; @SuppressWarnings<span style="color:#009900;">&#40;</span><span style="color:#0000ff;">&quot;unchecked&quot;</span><span style="color:#009900;">&#41;</span><br />
&nbsp; <span style="color:#000000;font-weight:bold;">public</span> Base<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; Class<span style="color:#339933;">&lt;?&gt;</span> actualClassOfSubclass <span style="color:#339933;">=</span> <span style="color:#000000;font-weight:bold;">this</span>.<span style="color:#006633;">getClass</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; ParameterizedType parameterizedType <span style="color:#339933;">=</span> <span style="color:#009900;">&#40;</span>ParameterizedType<span style="color:#009900;">&#41;</span> actualClassOfSubclass.<span style="color:#006633;">getGenericSuperclass</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; Type firstTypeParameter <span style="color:#339933;">=</span> parameterizedType.<span style="color:#006633;">getActualTypeArguments</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#009900;">&#91;</span>0<span style="color:#009900;">&#93;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">this</span>.<span style="color:#006633;">klazz</span> <span style="color:#339933;">=</span> <span style="color:#009900;">&#40;</span><span style="color:#000000;font-weight:bold;">Class</span><span style="color:#009900;">&#41;</span> firstTypeParameter<span style="color:#339933;">;</span><br />
&nbsp; <span style="color:#009900;">&#125;</span></p>
<p>&nbsp; @Before<br />
&nbsp; <span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000066;font-weight:bold;">void</span> findAndInstantiateObjectToTest<span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span>  <span style="color:#000000;font-weight:bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">Exception</span></a> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">for</span> <span style="color:#009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afield+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color:#003399;">Field</span></a> field <span style="color:#339933;">:</span> <span style="color:#000000;font-weight:bold;">this</span>.<span style="color:#006633;">getClass</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span>.<span style="color:#006633;">getDeclaredFields</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><br />
&nbsp; &nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">if</span> <span style="color:#009900;">&#40;</span>field.<span style="color:#006633;">getType</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span>.<span style="color:#006633;">equals</span><span style="color:#009900;">&#40;</span>classUnderTest<span style="color:#009900;">&#41;</span><span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; T object <span style="color:#339933;">=</span> classUnderTest.<span style="color:#006633;">newInstance</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000066;font-weight:bold;">boolean</span> accessible <span style="color:#339933;">=</span> field.<span style="color:#006633;">isAccessible</span><span style="color:#009900;">&#40;</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">if</span> <span style="color:#009900;">&#40;</span><span style="color:#339933;">!</span>accessible<span style="color:#009900;">&#41;</span> <span style="color:#009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.<span style="color:#006633;">setAccessible</span><span style="color:#009900;">&#40;</span><span style="color:#000066;font-weight:bold;">true</span><span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; field.<span style="color:#006633;">set</span><span style="color:#009900;">&#40;</span><span style="color:#000000;font-weight:bold;">this</span>, object<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; field.<span style="color:#006633;">setAccessible</span><span style="color:#009900;">&#40;</span>accessible<span style="color:#009900;">&#41;</span><span style="color:#339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#009900;">&#125;</span><br />
&nbsp; <span style="color:#009900;">&#125;</span><br />
<span style="color:#009900;">&#125;</span><br />
&nbsp;</div>
<p>That really wasn&#8217;t too hard, now, wasn&#8217;t it?</p>
<p>Why call it <code>MagicTest</code>? Well, because I also happen to like using the <a href="http://mockito.org/">Mockito</a> mocking framework, <em>and</em> I happen to already have a unit test base class called <code>MagicMocker</code>. Get it? <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/84/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=84&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2009/05/28/introducing-magictest/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
		<item>
		<title>Shell script to uninstall ports with dependents</title>
		<link>http://alistairisrael.wordpress.com/2009/04/30/uninstall-ports-with-dependents/</link>
		<comments>http://alistairisrael.wordpress.com/2009/04/30/uninstall-ports-with-dependents/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 03:38:17 +0000</pubDate>
		<dc:creator>Alistair</dc:creator>
				<category><![CDATA[OS X]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[macports]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://alistairisrael.wordpress.com/?p=67</guid>
		<description><![CDATA[A shell script to uninstall ports along with any dependencies.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=67&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Before we begin</strong>, I seem to be getting a lot of people visiting this post when searching for &#8220;uninstall macports&#8221;. Read <a href="http://guide.macports.org/chunked/installing.macports.uninstalling.html">2.5 Uninstall</a> from the MacPorts Guide you want to uninstall MacPorts itself.</p>
<p><strong>EDIT:</strong> <em>steve k</em> wrote in and opened my eyes to:</p>
<blockquote><p><code>sudo port uninstall <span>-</span>-follow-dependents <em>portname</em></code></p></blockquote>
<p>So use that instead, and read on only if you like <code>bash</code>.</p>
<p>(I have another script that does the inverse—traverses <em>up</em> the dependency chain removing &#8216;leaf&#8217; nodes—that is, ports with no further dependencies. Maybe I&#8217;ll make a new post on that one—after making sure it&#8217;s also not already available as some hidden option. :p )<br />
<span id="more-67"></span><br />
If you use <a href="http://www.macports.org">MacPorts</a>, then at some point you&#8217;ve probably tried to uninstall a port that had other ports as dependents. (If you&#8217;re a developer on OS X and you don&#8217;t use MacPorts, then you probably should.)</p>
<p>Even when using a friendly GUI like, say, <a href="http://porticus.alittledrop.com">Porticus</a>, you&#8217;re prevented from uninstalling a port with dependents. Of course, you can force uninstall a port but MacPorts themselves warn us that doing so might cause problems later on (when rebuilding the dependents).</p>
<p>So, I used to manually traverse down the dependency tree and uninstall the leaf nodes (ports with no dependents) one by one until I could uninstall the &#8216;root&#8217; port that I wanted to uninstall in the first place.</p>
<p>If you like doing that, then by all means keep doing so.</p>
<p>If, like me, you&#8217;re tired of having to do it by hand, then here&#8217;s a short shell script I wrote that can do all that for you.</p>
<p>Note that <b>the script will fail if you have more than one installed version of any port</b>. MacPorts itself will helpfully tell you which ports in the dependency tree have multiple versions installed. Maybe one of these days I&#8217;ll get motivated and challenged enough to update this script to handle (and uninstall) multiple versions properly.</p>
<p>I initially thought of writing this in Ruby, but since I used MacPorts to install Ruby itself I realized I very well couldn&#8217;t use the same script to uninstall anything up the Ruby dependency chain. Fortunately, I found that simple shell scripting (plus common tools like <code>awk</code>) was more than adequate for the task.</p>
<p>With that out of the way, here&#8217;s my <code>deport.sh</code> script:</p>
<div class="bash" style="font-family:monospace;color:#006;border:1px solid #d0d0d0;background-color:#f0f0f0;"><span style="color:#666666;font-style:italic;">#!/bin/sh</span></p>
<p><span style="color:#000000;font-weight:bold;">function</span> deport <span style="color:#7a0874;font-weight:bold;">&#123;</span><br />
&nbsp; <span style="color:#007800;">port</span>=<span style="color:#ff0000;">&quot;$1&quot;</span></p>
<p>&nbsp; <span style="color:#000000;font-weight:bold;">if</span> port dependents <span style="color:#ff0000;">&quot;<span style="color:#007800;">$port</span>&quot;</span><span style="color:#000000;font-weight:bold;">|</span><span style="color:#c20cb9;font-weight:bold;">grep</span> <span style="color:#ff0000;">&quot;depends on <span style="color:#007800;">$port</span>&quot;</span>; <span style="color:#000000;font-weight:bold;">then</span><br />
&nbsp; &nbsp; <span style="color:#666666;font-style:italic;"># depth-first traversal of dependents</span><br />
&nbsp; &nbsp; port dependents <span style="color:#ff0000;">&quot;<span style="color:#007800;">$port</span>&quot;</span><span style="color:#000000;font-weight:bold;">|</span><span style="color:#c20cb9;font-weight:bold;">awk</span> <span style="color:#ff0000;">&#8216;/depends on/ {print $1}&#8217;</span><span style="color:#000000;font-weight:bold;">|</span><span style="color:#000000;font-weight:bold;">while</span> <span style="color:#c20cb9;font-weight:bold;">read</span> dependent; <span style="color:#000000;font-weight:bold;">do</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; deport <span style="color:#ff0000;">&quot;<span style="color:#007800;">$dependent</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color:#000000;font-weight:bold;">done</span><br />
&nbsp; <span style="color:#000000;font-weight:bold;">fi</span><br />
&nbsp; <span style="color:#666666;font-style:italic;"># make sure no more dependents</span><br />
&nbsp; <span style="color:#000000;font-weight:bold;">if</span> port dependents <span style="color:#ff0000;">&quot;<span style="color:#007800;">$port</span>&quot;</span><span style="color:#000000;font-weight:bold;">|</span><span style="color:#c20cb9;font-weight:bold;">grep</span> <span style="color:#ff0000;">&quot;<span style="color:#007800;">$port</span> has no dependents\!&quot;</span>; <span style="color:#000000;font-weight:bold;">then</span><br />
&nbsp; &nbsp; <span style="color:#7a0874;font-weight:bold;">echo</span> <span style="color:#ff0000;">&quot;port uninstall <span style="color:#007800;">$port</span>&quot;</span><br />
&nbsp; &nbsp; port uninstall <span style="color:#ff0000;">&quot;<span style="color:#007800;">$port</span>&quot;</span><br />
&nbsp; <span style="color:#000000;font-weight:bold;">fi</span><br />
<span style="color:#7a0874;font-weight:bold;">&#125;</span></p>
<p><span style="color:#000000;font-weight:bold;">if</span> <span style="color:#7a0874;font-weight:bold;">&#91;</span> <span style="color:#660033;">-z</span> <span style="color:#ff0000;">&quot;$1&quot;</span> <span style="color:#7a0874;font-weight:bold;">&#93;</span>; <span style="color:#000000;font-weight:bold;">then</span><br />
&nbsp; <span style="color:#7a0874;font-weight:bold;">echo</span> <span style="color:#ff0000;">&quot;Usage:&quot;</span><br />
&nbsp; <span style="color:#7a0874;font-weight:bold;">echo</span> <span style="color:#ff0000;">&quot; &nbsp;deport.sh &lt;portname&gt;&quot;</span><br />
<span style="color:#000000;font-weight:bold;">else</span><br />
&nbsp; deport <span style="color:#ff0000;">&quot;$1&quot;</span><br />
<span style="color:#000000;font-weight:bold;">fi</span></div>
<p>If you find this useful, then you&#8217;re welcome! If it doesn&#8217;t work for you, please drop me a line. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> g</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alistairisrael.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alistairisrael.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alistairisrael.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alistairisrael.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alistairisrael.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alistairisrael.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alistairisrael.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alistairisrael.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alistairisrael.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alistairisrael.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alistairisrael.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alistairisrael.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alistairisrael.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alistairisrael.wordpress.com/67/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alistairisrael.wordpress.com&amp;blog=577128&amp;post=67&amp;subd=alistairisrael&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alistairisrael.wordpress.com/2009/04/30/uninstall-ports-with-dependents/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe9bb29f576661916e29a8d327493d6b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alistair</media:title>
		</media:content>
	</item>
	</channel>
</rss>
