<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Affine &#187; Rüdiger Herrmann</title>
	<atom:link href="http://www.codeaffine.com/author/rherrmann/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codeaffine.com</link>
	<description>Clean code that works</description>
	<lastBuildDate>Wed, 10 Apr 2013 07:53:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>A Simple way to extend SWTBot</title>
		<link>http://www.codeaffine.com/2012/10/26/a-simple-way-to-extend-swtbot/</link>
		<comments>http://www.codeaffine.com/2012/10/26/a-simple-way-to-extend-swtbot/#comments</comments>
		<pubDate>Fri, 26 Oct 2012 06:21:24 +0000</pubDate>
		<dc:creator>Rüdiger Herrmann</dc:creator>
				<category><![CDATA[Common]]></category>
		<category><![CDATA[Eclipse]]></category>

		<guid isPermaLink="false">http://www.codeaffine.com/?p=2295</guid>
		<description><![CDATA[SWTBot is an Eclipse project that enables you to write functional tests for SWT and Workbench based applications in Java. It has API to remote control the application under test and matcher to find widgets and examine their state. For each widget there is a corresponding bot class with methods to control the widget. To [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://eclipse.org/swtbot">SWTBot</a> is an Eclipse project that enables you to write functional tests for <a href="http://eclipse.org/swt">SWT</a> and Workbench based applications in Java.<br />
It has API to remote control the application under test and matcher to find widgets and examine their state. For each widget there is a corresponding bot class with methods to control the widget. To enter some text into a text widget for example, your code would look like this:</p>
<pre class="brush:java">
Text text = new Text( parent, SWT.BORDER );
...
SWTBotText textBot = new SWTBotText( text );
textBot.typeText( "Hello World" );
</pre>
<p>Though SWTBot covers most of the API that SWT has to offer, there are some gaps. Say you want to maximize a Shell and afterwards ensure that certain widgets within that Shell behave as epected. And just the SWTBotShell.maximize() method is one of these missing ones. Or your application uses a custom widget that no one has written a bot for yet.</p>
<p>So help yourself and write your own bot.</p>
<p>To understand how a bot works, let us first look into one aspect of SWTBot, the threading model. Running a test with SWTBot usually involves two threads. The widgets under test run on the <a href="http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/guide/swt_threading.htm">UI thread</a>. The bot runs on an extra thread and communicates with the UI thread in that it posts events to the UI thread or uses Display.syncE&shy;xec() to run code on the UI thread.</p>
<p>This is usually necessary because the UI code at some point calls Display.sleep()  &ndash; e.g. from the main event loop or to create a blocking dialog. sleep() suspends the UI thread and waits until an event is received. If the test code (which <em>generates</em> the events sleep() is waiting for) was running on the same thread, it would be blocked infinitely.<br />
However, if you can ensure that the code under test doesn&#8217;t call sleep(), then of course you can safely run your test code on the UI thread.</p>
<p>Once you have a clear understanding of that, writing your own bot is easy <sup class='footnote'><a href='#fn-2295-1' id='fnref-2295-1'>1</a></sup>.</p>
<pre class="brush:java">
public class SWTBotShellExtension extends AbstractSWTBot {

  public SWTBotShellExtension( Shell shell ) 
    throws WidgetNotFoundException
  {
    super( shell );
  }

  public void maximize() {
    syncE xec( new VoidResult() {
      public void run() {
        widget.setMaximized( true );
      }
    } );
  }
}
</pre>
<p>The superclass AbstractSWTBot brings syncE&shy;xec() methods that run the a given piece of code on the UI thread. There are several overloaded variants that accept runnables with different return types. For now we will use its simplest form, syncE&shy;xec(VoidResult) to maximize the shell without returning anything.</p>
<p>The test code can now execute <code>new SWTBotShellExtension( shell ).maximize()</code> and voila, the given shell will be maximized. If you prefer you can inherit from SWTBotShell to have access to the other bot methods from within the same instance.</p>
<p>As inter-thread communication is involved, you may want the bot to be more fail safe. Take a look a the snippet below:</p>
<pre class="brush:java">
  public void maximize() {
    new SWTBot().waitUntil( new DefaultCondition() {

      public boolean test() {
        return syncE xec( new BoolResult() {
          public Boolean run() {
            widget.setMaximized( true );
            return Boolean.valueOf( widget.getMaximized() );
          }
        } );
      }

      public String getFailureMessage() {
        return "Timed out waiting for shell to get maximized";
      }
      
    } );
  }
</pre>
<p>waitUntil() executes the test method in intervals until it either returns true or a timeout is reached. If the operation times out, an exception is thrown with a message from getFailureMessage()</p>
<p>This is where another of the syncE&shy;xec() method becomes useful: syncE&shy;xec(BoolResult). Now, the code maximizes the shell, and reports back whether the shell was actually maximized. Due to the waitUntil(), this is executed as long as takes to succeed &ndash; or until the time limit is reached.</p>
<p>The AbstractSWTBot also provides a notify() method that can be used send events. In our case, the maximize() method issues a resize event as if the action was initiated by a user. Other actions might not always behave so in which case you can use notify() to simulate the event.</p>
<p>There is certainly much more to it. But creating a simple custom bot is pretty easy, isn&#8217;t it? If you have any questions or comments, please post them here.</p>
<hr />
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-2295-1'>Excuse the artificial space in syncE&nbsp;xec, a security facility on our hosted server wouldn&#8217;t let the text pass without. <span class='footnotereverse'><a href='#fnref-2295-1'>&#8617;</a></span></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codeaffine.com/2012/10/26/a-simple-way-to-extend-swtbot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How do you test equals and hashCode?</title>
		<link>http://www.codeaffine.com/2012/06/25/how-do-you-test-equals-and-hashcode/</link>
		<comments>http://www.codeaffine.com/2012/06/25/how-do-you-test-equals-and-hashcode/#comments</comments>
		<pubDate>Mon, 25 Jun 2012 06:51:13 +0000</pubDate>
		<dc:creator>Rüdiger Herrmann</dc:creator>
				<category><![CDATA[Common]]></category>

		<guid isPermaLink="false">http://www.codeaffine.com/?p=1733</guid>
		<description><![CDATA[Just recently I had to implement equals() and hashCode() for several classes. With the aid of your favourite IDE, generating these methods is a matter of seconds. Well, if there weren&#8217;t the missing tests. As I tend to be obsessive when it comes to tests, I also want to have tests for equals() and hashCode(). [...]]]></description>
			<content:encoded><![CDATA[<p>Just recently I had to implement <code>equals()</code> and <code>hashCode()</code> for several classes. With the aid of your favourite IDE, generating these methods is a matter of seconds.<br />
Well, if there weren&#8217;t the missing tests. As I tend to be obsessive when it comes to tests, I also want to have tests for equals() and hashCode(). Only then I am able to refactor the generated code to align it with the projects code style and make sure not to break it thereby.</p>
<p>After being half-way through writing tests for the second class that implemented equals() and hashCode(), and more and more copying code from the existing tests and getting more and more impatient with the progress I made, I decided to write a helper class that would hopefully reduce the redundant test code.</p>
<p>A few iterations later I had an <a href="https://gist.github.com/2970842">EqualsTester</a> class. The general idea is to feed its assert-methods with probes that are known to be equal or unequal. They will then ensure that equals() and hashCode() work like <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)">specified</a> as far as possible. If testing a probe fails, an <code>AssertionError</code> will be thrown. This is best illustrated with a code snippet:</p>
<pre class="brush:java">  @Test
  public void testEqualsAndHashCode() {
    EqualsTester&lt;Point&gt; equalsTester = newInstance( new Point( 1, 2 ) );
    equalsTester.assertEqual( new Point( 1, 2 ), new Point( 1, 2 ) );
    equalsTester.assertNotEqual( new Point( 1, 2 ), new Point( 3, 4 ) );
  }</pre>
<p>The factory method is just there to avoid yet another pair of angle brackets. With the given argument, a number of tests are executed to ensure that the argument</p>
<ul>
<li>is equal with itself (<code>object.equals( object ) == true</code>)</li>
<li>is not equal with <code>null</code></li>
<li>is not equal with <code>new Object()</code></li>
</ul>
<p>By passing two objects that are considered equal to <code>assertEqual()</code>, you can not only ensure that equals() is implemented correctly but also that their hash code is the same.</p>
<p><code>assertNotEqual()</code> makes sure that your equals() implementation does not accidentailly consider unequal objects as equal. By default assertNotEqual() will also check that hashCode() produces distinct results. This may improve the performance of hash tables. As it isn&#8217;t required by the general contract, it can of course be disabled.</p>
<p>This utility class helped me, thus I though it would be worth sharing. I hope that one or another will also find it useful. </p>
<hr />
]]></content:encoded>
			<wfw:commentRss>http://www.codeaffine.com/2012/06/25/how-do-you-test-equals-and-hashcode/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How do you import static in Eclipse?</title>
		<link>http://www.codeaffine.com/2012/03/28/how-do-you-import-static-in-eclipse/</link>
		<comments>http://www.codeaffine.com/2012/03/28/how-do-you-import-static-in-eclipse/#comments</comments>
		<pubDate>Wed, 28 Mar 2012 12:58:07 +0000</pubDate>
		<dc:creator>Rüdiger Herrmann</dc:creator>
				<category><![CDATA[Eclipse]]></category>

		<guid isPermaLink="false">http://www.codeaffine.com/?p=1651</guid>
		<description><![CDATA[Long story short, I just discovered that Ctrl+Shift+M (Source > Add Import) can not only be used to add missing imports. It can also help with static imports. Executed on a reference to a qualified member (read Class.member), the refactoring will add a static import for the defining class and remove the class-dot expression. For [...]]]></description>
			<content:encoded><![CDATA[<p>Long story short, I just discovered that <code>Ctrl+Shift+M</code> (<i>Source</i> > <i>Add Import</i>) can not only be used to add missing imports. It can also help with static imports. Executed on a reference to a qualified member (read Class.member), the refactoring will add a static import for the defining class and remove the <i>class-dot</i> expression.</p>
<p>For example, if you have</p>
<pre class="brush:java">
import java.lang.System;
class Example {
  void someMethod() {
    System.currentTimeMillis();
  }
}
</pre>
<p>Place the cursor on <code>currentTimeMillis()</code> and press <code>Ctrl+Shift+M</code>. This will transform the code to</p>
<pre class="brush:java">
import static java.lang.System.currentTimeMillis;
class Example {
  void someMethod() {
    currentTimeMillis();
  }
}
</pre>
<p>This feature has probably been here for a while and is documented and was announced in a New &amp; Noteworthy. I only just discovered it the other day and found that it highly improves working with static imports. Maybe you find it useful too&#8230;</p>
<h3>The long part of the story&#8230;</h3>
<p>Tired of often manually managing static imports and encouraged by the new <a href="http://blog.deepakazad.com/2012/03/contributing-quick-fix-and-quick-assist.html">API for quick assists</a> in <a href="http://eclipse.org/jdt">JDT</a>, I started writing a quick assist. This quick assist should replace qualified calls to static methods with static imports.<br />
Only after I already had a mostly working implementation, I searched the JDT bugzilla for such an enhancement request to eventually provide a patch. Thereby I <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=197850#c1">discovered</a> that <code>Ctrl+Shift+M</code> already does the job.<br />
In the end, I spent an evening coding and got to know new APIs of JDT, learned a new feature of an <i>old</i> shortcut and was remembered to &#8220;think first, then act&#8221;. <img src='http://www.codeaffine.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codeaffine.com/2012/03/28/how-do-you-import-static-in-eclipse/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Eclipse Archive Utilities</title>
		<link>http://www.codeaffine.com/2012/03/15/eclipse-archive-utilities/</link>
		<comments>http://www.codeaffine.com/2012/03/15/eclipse-archive-utilities/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 11:24:42 +0000</pubDate>
		<dc:creator>Rüdiger Herrmann</dc:creator>
				<category><![CDATA[Common]]></category>
		<category><![CDATA[Eclipse]]></category>

		<guid isPermaLink="false">http://www.codeaffine.com/?p=1413</guid>
		<description><![CDATA[A little while ago I started a side project to support working with (zip) archive files in the Eclipse IDE. Initially it was more meant to revive my plug-in development knowledge and play around with some technologies like Tycho or SWTBot. But in the end, I though it might be useful enough to share. The [...]]]></description>
			<content:encoded><![CDATA[<p>A little while ago I started a side project to support working with (zip) archive files in the <a href="http://eclipse.org/platform">Eclipse IDE</a>. Initially it was more meant to revive my plug-in development knowledge and play around with some technologies like <a href="http://eclipse.org/tycho">Tycho</a> or <a href="http://eclipse.org/swtbot">SWTBot</a>. But in the end, I though it might be useful enough to share.</p>
<p>The plug-in extends the <a href="http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Freference%2Fref-27.htm">Project Explorer</a> in that archive files can be expanded just like regular folders. Archive files can be extracted entirely or partially to locations in the workspace or file system. With the <em>Open</em> action, the content of an archive entry can be viewed in an editor.</p>
<p>From a Java developer&#8217;s point of view, the crux the the Project Explorer is, that it isn&#8217;t an equal replacement for the Package Explorer. When using working sets, the dynamic <em>Other Projects</em> working set is <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=266030">painfully missing</a>. Not sure though how IDEs for other languages deal with this.</p>
<p>If, for whatever reason, the Project Explorer isn&#8217;t your primary view to manage development artifacts you can always press <em>Alt+Shift+W</em> on the current selection to show it in the Project Explorer. And from there on you can delve into the content of the archive file.</p>
<p>The short movie below will give you an impression of how the archive utilities work.<br />
<iframe width="520" height="382" src="http://www.youtube.com/embed/Zln5QUyxTb0" frameborder="0" allowfullscreen></iframe></p>
<p>If you want to give it a try, the URL to the Eclipse software repository is<br />
<code>http://rherrmann.github.com/com.codeaffine.archive/repository</code><br />
To install, choose <em>Help</em> -> <em>Install New Software&#8230;</em> from the Eclipse main menu. In the upcoming dialog, enter the above URL in the <em>Work with</em> field.</p>
<p>There is even a small <a href="http://rherrmann.github.com/com.codeaffine.archive/index.html">homepage</a> with some more information. The code is open source and is hosted on <a href="https://github.com/rherrmann/com.codeaffine.archive">github</a>. </p>
<p><a href="https://github.com/rherrmann/com.codeaffine.archive/issues">Several things</a> like opening nested archive files, support for other archive- and compression-formats (e.g. tar, tar.gz), an <em>Open with&#8230;</em> action to open archive entries in a different editor are missing. Quite annoying is, that I haven&#8217;t brought key bindings for <em>Open</em> to work so far. If anyone knows how to reuse the <em>F3</em> key binding from the standard open command, please reply to <a href="http://www.eclipse.org/forums/index.php/t/294588/">this forum post</a> or leave a comment on this post.</p>
<p>Let me know what you think. If you are missing a feature or if you found a bug, please open an <a href="https://github.com/rherrmann/com.codeaffine.archive/issues/new">issue</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codeaffine.com/2012/03/15/eclipse-archive-utilities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Code Centric Trainings with the Samples Manager</title>
		<link>http://www.codeaffine.com/2011/12/01/code-centric-trainings-with-the-samples-manager/</link>
		<comments>http://www.codeaffine.com/2011/12/01/code-centric-trainings-with-the-samples-manager/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 16:36:26 +0000</pubDate>
		<dc:creator>Rüdiger Herrmann</dc:creator>
				<category><![CDATA[Common]]></category>
		<category><![CDATA[Eclipse]]></category>

		<guid isPermaLink="false">http://www.codeaffine.com/?p=766</guid>
		<description><![CDATA[I recently gave a training course on &#8211; you guess it &#8211; RAP/RWT. And as it seems natural for a training targeted at developers it was very code centric and had many hands-on exercises. For hands-on exercises I find it useful to give a starting point so that tedious steps like setting up a project [...]]]></description>
			<content:encoded><![CDATA[<p>I recently gave a training course on &#8211; you guess it &#8211; <a href="//eclipse.org/rap">RAP/RWT</a>.<br />
And as it seems natural for a training targeted at developers it was very code centric and had many hands-on exercises.</p>
<p>For hands-on exercises I find it useful to give a starting point so that tedious steps like setting up a project and the like haven&#8217;t to be done with each exercise.<br />
For any non-trivial exercise it makes also sense to provide the final solution. In case a training participant gets lost (s)he would certainly like to compare her/his current state with the final solution of the exercises.</p>
<p>All this and some more offers the <em><a href="http://sourceforge.net/projects/samplings">Eclipse Samples Manager</a></em>. Jean-Michel Lemieux and Jeff McAffer developed this Eclipse plug-in in 2007 for their <a href="http://eclipsercp.org">Eclipse RCP book</a>. It allows you to package and deploy code samples. The tool allows users to install a starting point, step between different iterations of samples, compare them, and reset when their changes don&#8217;t work.</p>
<p>If you haven&#8217;t seen the Samples Manager yet, try this short video. It will give you an overview of what the tool can be used for.<br />
<iframe width="480" height="360" src="http://www.youtube.com/embed/E-G44Gy5GrM" frameborder="0" allowfullscreen></iframe></p>
<p>I found the Samples Manager a useful tool and thought it was worth sharing. If there is enough interest, I will publish a small how-to on creating, packaging, and deploying samples with the Samples Manager in the near future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codeaffine.com/2011/12/01/code-centric-trainings-with-the-samples-manager/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The RAP Tooling has Offspring</title>
		<link>http://www.codeaffine.com/2011/10/28/the-rap-tooling-has-offspring/</link>
		<comments>http://www.codeaffine.com/2011/10/28/the-rap-tooling-has-offspring/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 17:00:34 +0000</pubDate>
		<dc:creator>Rüdiger Herrmann</dc:creator>
				<category><![CDATA[Common]]></category>
		<category><![CDATA[Eclipse]]></category>

		<guid isPermaLink="false">http://www.codeaffine.com/?p=764</guid>
		<description><![CDATA[I would like to use the opportunity to point you to a feature of RAP that is around for some time now but has gone almost unnoticed so far. The RAP tooling has grown by yet another launch configuration. With the RWT Application Launcher you can run RWT applications with just one shortcut. If you [...]]]></description>
			<content:encoded><![CDATA[<p>I would like to use the opportunity to point you to a feature of <a href="http://eclipse.org/rap">RAP</a> that is around for some time now but has gone almost unnoticed so far.</p>
<p>The RAP tooling has grown by yet another launch configuration. With the <em>RWT Application Launcher</em> you can run RWT applications with just one shortcut.</p>
<p>If you haven&#8217;t heard of RWT yet, think of it as SWT/Web. RWT is RAP without the workbench and OSGi, it plays the same role in RAP as SWT does for the Eclipse Platform/RCP. RWT lets you develop  lightweight, modular, and dynamic web applications.</p>
<p>The short movie shows how easy it is to launch your RWT code.<br />
<iframe width="520" height="415" src="http://youtube.com/embed/zIn_E8disk4" frameborder='0' allowfullscreen></iframe></p>
<p>From the package explorer, or from the Java editor, or any selection that points to an IEntryPoint you can choose Run As > RWT Application (or Alt+Shift+X 3 if you prefer). This command creates and runs an RWT launch configuration for the selected entry point. It starts a servlet engine, &#8220;deploys&#8221; the selected entry point, and opens a browser window with the application running therein.</p>
<p>As with all Eclipse launchers, the generated launch configuration can be edited and details such as the server port, the servlet path and if an internal, external or no browsers should be opened may be altered. Of course you are free to create a launch configuration from scratch if you prefer.</p>
<p>Apart from launching RWT applications you can also run an arbitrary web application. In the RWT launch configuration dialog you can choose a web.xml and specify the servlet path to be opened in the browser (if any).</p>
<p>The RWT application launcher is available since RAP 1.4. If you would like to try out this feature you can get it from the <a href="http://eclipse.org/rap/downloads">RAP downloads</a> page. Please let us know what you think. If you are missing feature or if you find a bug, please open a <a href="https://bugs.eclipse.org/bugs/enter_bug.cgi?product=RAP">bug report</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codeaffine.com/2011/10/28/the-rap-tooling-has-offspring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
