<?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>Alex Ruiz&#039;s Blog</title>
	<atom:link href="http://alexruiz.developerblogs.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://alexruiz.developerblogs.com</link>
	<description>Having Fun with Java!</description>
	<lastBuildDate>Sat, 28 Apr 2012 00:11:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Testing formatting in Eclipse editors, take 2: real unit tests</title>
		<link>http://alexruiz.developerblogs.com/?p=2306</link>
		<comments>http://alexruiz.developerblogs.com/?p=2306#comments</comments>
		<pubDate>Wed, 07 Mar 2012 18:14:38 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[protobuf-dt]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Xtext]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=2306</guid>
		<description><![CDATA[In my previous post I described how I tested formatting in a Eclipse editor. The decision to write a functional test made sense back then because I didn&#8217;t know how formatting internally works but I knew what to expect from formatting, from the user&#8217;s perspective. In addition, I was itchy to try this cool tool, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://alexruiz.developerblogs.com/files/2012/03/taketwo-300x199.jpg" alt="" width="300" height="199" class="aligncenter size-medium wp-image-2307" /></p>
<p>In my <a href="http://alexruiz.developerblogs.com/?p=2271" title="Testing formatting in Eclipse editors">previous post</a> I described how I tested formatting in a Eclipse editor. The decision to write a functional test made sense back then because I didn&#8217;t know how formatting internally works but I knew what to expect from formatting, from the user&#8217;s perspective. In addition, I was itchy to try this cool tool, <a href="http://www.eclipse.org/swtbot/">SWTBot</a> :)</p>
<p>Luckily, <a href="http://blog.moritz.eysholdt.de/" target="_blank">Moritz Eysholdt</a>, a <a href="http://xtext.org" target="_blank">Xtext</a> committer, was kind enough to leave a <a href="http://alexruiz.developerblogs.com/?p=2271#comment-54018">comment</a> in my previous post explaining how he unit-tests formatting in a Xtext-based editor (take a look at his great presentation: &#8220;<a href="http://www.slideshare.net/meysholdt/testdriven-development-of-xtext-dsls" target="_blank">Test-Driven Development of Xtext DSLs</a>&#8220;.)</p>
<p>Moritz&#8217;s approach to unit testing is similar to mine in the sense that he separates test data from test code. The difference is that in his approach, test data is not in the test class itself, but in separate files. I personally prefer to have everything, test code and test data, in one place. Having test data as comments is, so far, my favorite way to separate it from test code.</p>
<p>Thanks to Moritz now I know that I can use <code>INodeModelFormatter</code> to unit-test formatting. My new formatting test looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProtobufFormatter_Test <span style="color: #009900;">&#123;</span>
  @Rule <span style="color: #000000; font-weight: bold;">public</span> CommentReaderRule commentReader <span style="color: #339933;">=</span> overrideRuntimeModuleWith<span style="color: #009900;">&#40;</span>unitTestModule<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  @Inject <span style="color: #000000; font-weight: bold;">private</span> INodeModelFormatter formatter<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// import 'dummy.proto';import 'google/protobuf/descriptor.proto';</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// import 'dummy.proto';</span>
  <span style="color: #666666; font-style: italic;">// import 'google/protobuf/descriptor.proto';</span>
  @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> should_format_normal_import<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ICompositeNode rootNode <span style="color: #339933;">=</span> commentReader.<span style="color: #006633;">rootNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    IFormattedRegion region <span style="color: #339933;">=</span> formatter.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>rootNode, <span style="color: #cc66cc;">0</span>, rootNode.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> formatted <span style="color: #339933;">=</span> region.<span style="color: #006633;">getFormattedText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertThat<span style="color: #009900;">&#40;</span>formatted, equalTo<span style="color: #009900;">&#40;</span>commentReader.<span style="color: #006633;">expectedText</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>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The most interesting part in the code above is line 2. The JUnit rule <code><a href="http://code.google.com/p/protobuf-dt/source/browse/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/formatting/CommentReaderRule.java" target="_blank">CommentReaderRule</a></code> is the one doing the heavy lifting:</p>
<ol>
<li>Reads the comments in each test method</li>
<li>Parses the first comment (the text <em>before</em> formatting)</li>
<li>Makes the second comment (the formatted text) available via the method <code>expectedText</code></li>
</ol>
<p>Another interesting aspect of this testing approach is that each test method has exactly the same code, all the context is in the comments. After removing some duplication, my test class looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// package com.google.proto.test;import 'google/protobuf/descriptor.proto';</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// package com.google.proto.test;</span>
  <span style="color: #666666; font-style: italic;">//</span>
  <span style="color: #666666; font-style: italic;">// import 'google/protobuf/descriptor.proto';</span>
  @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> should_format_package<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    assertThatFormattingWorksCorrectly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// import 'dummy.proto';import 'google/protobuf/descriptor.proto';</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// import 'dummy.proto';</span>
  <span style="color: #666666; font-style: italic;">// import 'google/protobuf/descriptor.proto';</span>
  @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> should_format_normal_import<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    assertThatFormattingWorksCorrectly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> assertThatFormattingWorksCorrectly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ICompositeNode rootNode <span style="color: #339933;">=</span> commentReader.<span style="color: #006633;">rootNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    IFormattedRegion region <span style="color: #339933;">=</span> formatter.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>rootNode, <span style="color: #cc66cc;">0</span>, rootNode.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> formatted <span style="color: #339933;">=</span> region.<span style="color: #006633;">getFormattedText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertThat<span style="color: #009900;">&#40;</span>formatted, equalTo<span style="color: #009900;">&#40;</span>commentReader.<span style="color: #006633;">expectedText</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>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now my tests run super fast (no UI and no OSGi) and are reliable. Sweet!</p>
<p><strong>Note:</strong> This new approach works with Xtext-based editors only. The <a href="http://alexruiz.developerblogs.com/?p=2271" title="Testing formatting in Eclipse editors">previous one</a> did not rely on Xtext internals and could be used to test formatting on any Eclipse editor.</p>
<p>Feedback is always welcome :)</p>
<div style="font-size: xx-small">
(Image taken from <a href="http://www.flickr.com/photos/m4dgroup/" target="_blank"> M4D GROUP&#8217;s flickr stream</a> under the creative commons license. Some modifications were made.)
</div>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=2306</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Testing formatting in Eclipse editors</title>
		<link>http://alexruiz.developerblogs.com/?p=2271</link>
		<comments>http://alexruiz.developerblogs.com/?p=2271#comments</comments>
		<pubDate>Mon, 05 Mar 2012 13:45:35 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[protobuf-dt]]></category>
		<category><![CDATA[UI Testing]]></category>
		<category><![CDATA[Xtext]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=2271</guid>
		<description><![CDATA[Starting with the next version of protobuf-dt, we are going to focus on a better editing experience, starting with a good code formatter. Even though Xtext provides a nice infrastructure for implementing editor formatters, it is also important to test that the formatter is doing what I think it is doing. I wanted my tests [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://alexruiz.developerblogs.com/files/2012/03/testing-300x225.jpg" alt="" width="300" height="225" class="aligncenter size-medium wp-image-2301" /></p>
<p>Starting with the next version of <a href="http://code.google.com/p/protobuf-dt/" target="_blank">protobuf-dt</a>, we are going to focus on a better editing experience, starting with a good code formatter. </p>
<p>Even though <a href="http://xtext.org" target="_blank">Xtext</a> provides a <a href="http://www.eclipse.org/Xtext/documentation/2_0_0/105-formatting.php" target="_blank">nice infrastructure</a> for implementing editor formatters, it is also important to test that the formatter is doing what I think it is doing. I wanted my tests to be reliable, short and readable. Well&#8230;who doesn&#8217;t? :)</p>
<p>I could write a unit test that verifies that I&#8217;m using Xtext&#8217;s infrastructure correctly. Unfortunately this approach is unreliable: the test may pass and my assumptions about the framework could still be wrong. Functional testing is the best approach I could think of. </p>
<p>Testing a formatter would involve creating a .proto file, inserting some text in the editor, triggering formatting and verifying that the text was formatted correctly. The best tool for this type of automation? <a href="http://www.eclipse.org/swtbot/" target="_blank">SWTBot</a> of course!</p>
<p>To write short and readable tests, I used the technique I described in the post &#8220;<a href="http://alexruiz.developerblogs.com/?p=1962">A simple way to test Eclipse-based editors</a>,&#8221; with a few tweaks. Each of my test methods contain two comments with the same text. The first comment contains the text to insert in the editor while the second comment contains the formatted text. I access these comments through a JUnit rule.</p>
<p>Here is a example of a test method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// import &quot;dummy.proto&quot;;import &quot;google/protobuf/descriptor.proto&quot;;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// import &quot;dummy.proto&quot;;</span>
  <span style="color: #666666; font-style: italic;">// import &quot;google/protobuf/descriptor.proto&quot;;</span>
  @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> should_format_normal_import<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    SWTBotEclipseEditor editor <span style="color: #339933;">=</span> robot.<span style="color: #006633;">createFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;formatNormalImport.proto&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Comments comments <span style="color: #339933;">=</span> commentsAbove<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    editor.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>comments.<span style="color: #006633;">beforeFormatting</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    formatAndSave<span style="color: #009900;">&#40;</span>editor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertThat<span style="color: #009900;">&#40;</span>editor.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, equalTo<span style="color: #009900;">&#40;</span>comments.<span style="color: #006633;">expected</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>What I like the most about this approach is the separation of test code and data (Protocol Buffer code.) The benefits are, IMHO:</p>
<ol>
<li>We reduce noise in test code by not having setup of test data (e.g. string concatenations, <code>StringBuilder</code>s, etc.)</li>
<li>We don&#8217;t have to escape characters in test data (e.g. double quotes,) improving its readability</li>
<li>Copying/pasting text between tests and a real editor is as simple as selecting text, then pressing &#8220;Ctrl+C&#8221;, &#8220;Ctrl+V&#8221; and &#8220;Ctrl+/&#8221;</li>
</ol>
<p>If you&#8217;d like to know more details about this test, please take a look at these classes:</p>
<ul>
<li><code><a href="http://code.google.com/p/protobuf-dt/source/browse/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/Formatter_Test.java" target="_blank">Formatter_Test</a></code>: the whole test class.</li>
<li><code><a href="http://code.google.com/p/protobuf-dt/source/browse/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/swtbot/ProtobufBot.java" target="_blank">ProtobufBot</a></code>: subclasses <code>SWTWorkbenchBot</code> and adds useful utilities (e.g. delete all projects in a workspace, create a file in a project, etc.)</li>
<li><code><a href="http://code.google.com/p/protobuf-dt/source/browse/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/junit/core/CommentReaderRule.java" target="_blank">CommentReaderRule</a></code>: JUnit rule that reads the comments of test methods (using a <code><a href="http://code.google.com/p/protobuf-dt/source/browse/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/CommentReader.java" target="_blank">CommentReader</a></code>) and makes them accessible from test code.</li>
</ul>
<p>Feedback is always welcome :)</p>
<div style="font-size: xx-small">
(Image taken from <a href="http://www.flickr.com/photos/cybergibbons/" target="_blank"> cybergibbons&#8217;s flickr stream</a> under the creative commons license)
</div>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=2271</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>My contribution to Eclipse CDT: external-tool-based code checkers</title>
		<link>http://alexruiz.developerblogs.com/?p=2231</link>
		<comments>http://alexruiz.developerblogs.com/?p=2231#comments</comments>
		<pubDate>Fri, 24 Feb 2012 12:30:34 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[CDT]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=2231</guid>
		<description><![CDATA[I&#8217;m very proud that my contribution, infrastructure for creating external-tool-based code checkers, is in the Eclipse CDT code repository! Background (Adapted from my EclipseCon Europe 2011 session) Eclipse CDT 8.0, part of the Indigo release, includes a nice code checking framework: Codan. Out of the box, Codan provides both the infrastructure needed to perform code [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-2244" src="http://alexruiz.developerblogs.com/files/2012/02/Cdt_logo_with_name.jpg" alt="" width="279" height="228" /></p>
<p>I&#8217;m very proud that my contribution, infrastructure for creating external-tool-based code checkers, is in the <a href="http://www.eclipse.org/cdt/" target="_blank">Eclipse CDT</a> code <a href="http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/log/?qt=author&amp;q=Alex+Ruiz" target="_blank">repository</a>!</p>
<h3>Background</h3>
<p>(Adapted from my EclipseCon Europe 2011 <a href="http://www.eclipsecon.org/sessions/cdt-and-external-static-code-analysis-tools-match-made-heaven" target="_blank">session</a>)</p>
<p>Eclipse CDT 8.0, part of the <a href="http://www.eclipse.org/indigo/" target="_blank">Indigo release</a>, includes a nice code checking framework: Codan. Out of the box, Codan provides both the infrastructure needed to perform code checks and some useful, ready-to-use checkers.</p>
<p>Even though the provided checkers are a great addition to a developer’s toolbox, many more are needed for Codan to have feature parity with existing tools like <a href="http://cppcheck.sourceforge.net/" target="_blank">Cppcheck</a>. In addition, Codan&#8217;s infrastructure provides good support for writing AST-based checkers, but support for integrating external tools was missing.</p>
<h3>Why?</h3>
<p>The main reason for integrating Codan with external tools is to enjoy all the code checks from mature tools without leaving our beloved Eclipse. With the new infrastructure:</p>
<ul>
<li>External tools can be configured using Codan&#8217;s preference page</li>
<li>External tools are invoked automatically when a C/C++ file is saved</li>
<li>The output of these tools can be displayed as editor markers</li>
</ul>
<h3>Example: Cppcheck</h3>
<p>My contribution includes a Cppchecker-based checker. This checker is disabled by default but it can be easily enabled and configured in the Codan preference page.</p>
<p><a href="http://alexruiz.developerblogs.com/files/2012/02/Screen-shot-2012-02-24-at-3.31.06-AM.png"><img class="aligncenter size-medium wp-image-2248" src="http://alexruiz.developerblogs.com/files/2012/02/Screen-shot-2012-02-24-at-3.31.06-AM-300x272.png" alt="" width="300" height="272" /></a></p>
<div style="text-align: center">(Click to enlarge)</div>
<p></p>
<p>A more detailed configuration dialog can be found by pressing the &#8220;Customize Selected&#8230;&#8221; button:</p>
<p><a href="http://alexruiz.developerblogs.com/files/2012/02/Screen-shot-2012-02-24-at-3.41.02-AM.png"><img src="http://alexruiz.developerblogs.com/files/2012/02/Screen-shot-2012-02-24-at-3.41.02-AM-300x222.png" alt="" width="300" height="222" class="aligncenter size-medium wp-image-2252" /></a></p>
<div style="text-align: center">(Click to enlarge)</div>
<p></p>
<p>As you can see from the screenshot above, you can specify the path of the Cppcheck executable, the arguments to pass and whether Cppcheck&#8217;s output should be displayed in the console. </p>
<p>Now, let&#8217;s see it in action!</p>
<div style="text-align: center">
<p><iframe width="500" height="375" src="http://www.youtube.com/embed/_OKpQ-W09MU?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
</div>
<h3>Writing your own external-tool-based checker</h3>
<p>The new infrastructure makes it very easy to write your own external-tool-based checker. In the simplest case, you will need to:</p>
<ol>
<li>Extend the abstract class <code> AbstractCxxExternalToolBasedChecker</code>.</li>
<li>Provide the name of your tool (e.g. &#8220;Cppcheck&#8221;.)</li>
<li>Provide default values for the path of the executable, arguments to pass and whether the output of the tool should be displayed in the console.</li>
<li>Provide one or more implementations of <code>AbstractOutputParser</code>. They will parse the output of the external tool, line by line. It&#8217;s up to you to decide what to do with the output (e.g. create error markers.)</li>
</ol>
<p>You can take a look at the <code>CppcheckChecker</code> class <a href="https://github.com/eclipse/cdt/blob/master/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/CppcheckChecker.java" target="_blank">here</a>.</p>
<p>In the case of tools that are complex to set up, the new infrastructure is extremely flexible and configurable. It allows you to pretty much to configure every single aspect of the checker, from the files that the tool can check to the way to feed arguments to the tool.</p>
<h3>Special thanks</h3>
<p>Many, many thanks to my teammate and mentor, Sergey Prigogin, for his guidance and for reviewing and improving this work.</p>
<p>Sergey is one of the best engineers I have ever met in my career (in fact, he is in my top-3 list,) a nice gentleman, and the toughest code reviewer&#8230;ever. His attention to detail and his high standards for quality are just out of this world.</p>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=2231</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sneak preview: Navigating from C++ code to Protocol Buffer declaration &#8211; in Eclipse</title>
		<link>http://alexruiz.developerblogs.com/?p=2151</link>
		<comments>http://alexruiz.developerblogs.com/?p=2151#comments</comments>
		<pubDate>Mon, 06 Feb 2012 13:26:58 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Xtext]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[protobuf-dt]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=2151</guid>
		<description><![CDATA[One of the most popular features of protobuf-dt is its integration with protoc, the Protocol Buffer compiler. When this feature is enabled, protobuf-dt invokes protoc to generate Java, C++ or Python code when a .proto file is saved. A fairly common feature request is navigation from generated code to its declaration in the .proto file. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://alexruiz.developerblogs.com/files/2012/02/compass-300x192.jpg" alt="" width="300" height="192" class="aligncenter size-medium wp-image-2152" /></p>
<p>One of the most popular features of <a href="http://code.google.com/p/protobuf-dt/" target="_blank">protobuf-dt</a> is its <a href="http://code.google.com/p/protobuf-dt/wiki/IntegrationWithProtoc" target="_blank">integration with protoc</a>, the <a href="http://code.google.com/p/protobuf/" target="_blank">Protocol Buffer</a> compiler. When this feature is enabled, protobuf-dt invokes protoc to generate Java, C++ or Python code when a .proto file is saved.</p>
<p>A fairly common feature request is navigation from generated code to its declaration in the .proto file. For example, navigating from a generated C++ class that extends <code>::google::protobuf::Message</code> to the actual declaration of the <code>Message</code> element in a .proto file. </p>
<p>I started working on this feature a week ago and now I&#8217;m happy to show off some progress. The following movie demonstrates the following:</p>
<ol>
<li>How to enable and configure protobuf-dt&#8217;s integration with protoc</li>
<li>How C++ code gets generated after saving a .proto file</li>
<li>How to navigate from the generated code to the protocol buffer declaration</li>
</ol>
<div align="center">
<p><iframe width="500" height="375" src="http://www.youtube.com/embed/s2c2F5_nyW0?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
</div>
<h3>Under the covers</h3>
<p>To accomplish this code navigation, I created an Eclipse plug-in that acts as a bridge between <a href="http://eclipse.org/cdt/" target="_blank">CDT</a> and protobuf-dt. Navigation from generated C++ code to its declaration in a .proto file is as easy as right-clicking on a C++ element and selecting the menu &#8220;Open Declaration in .proto File.&#8221;</p>
<p>Under the covers, the bridge plug-in does the following:</p>
<ol>
<li>Finds the C++ AST node enclosed in the current cursor position in the active <code>CEditor</code></li>
<li>Derives the qualified name and type of the proto element from the qualified name and type of the C++ element found in the AST</li>
<li>Asks the <a href="http://xtext.org" target="_blank">Xtext</a> index for the URI of the proto element that has an equal qualified name and type</li>
<li>Asks the Xtext index for the closest match, if the previous step failed</li>
<li>Asks Xtext to open and select the element under the found URI</li>
</ol>
<p>Of course, this a over-simplified explanation. For more details, the source code of the bridge can be found <a href="http://code.google.com/p/protobuf-dt/source/browse/#git%2Fcom.google.eclipse.protobuf.cdt" target="_blank">here</a>.</p>
<h3>Challenges</h3>
<p>Creating this plug-in was not too difficult. I think the biggest challenge is reverse-engineering protoc to figure out how navigation from generated C++ code to .proto files should be done. A good example is nested Protocol Buffer messages. When a nested message is compiled to C++, protoc creates a top-level class using underscores in the name to concatenate the names of its ancestors.</p>
<p>For example:</p>
<p><img src="http://alexruiz.developerblogs.com/files/2012/02/nesting.png" alt="" width="560" height="199" class="aligncenter size-full wp-image-2169" /></p>
<p>Both the messages <code>Outer_Message.Inner</code> and <code>Outer.Message.Inner</code> will generate a C++ class with name <code>Outer_Message_Inner</code>. In this case, there is no single path for navigating from C++ to Protocol Buffer. Currently we identify this ambiguity and select the first match only. I&#8217;m thinking about displaying a dialog showing all the possible options and let the user chose the Protocol Buffer element to navigate to.</p>
<p>Another thing I haven&#8217;t figured out is how to enable this menu only for files with names ending with &#8220;.pb.h.&#8221; Here is how I define the menu in the plug.xml file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extension</span> <span style="color: #000066;">point</span>=<span style="color: #ff0000;">&quot;org.eclipse.ui.popupMenus&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;viewerContribution</span></span>
<span style="color: #009900;">      <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;com.google.eclipse.protobuf.cdt.cEditorPopup&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">targetID</span>=<span style="color: #ff0000;">&quot;#CEditorContext&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span></span>
<span style="color: #009900;">        <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.google.eclipse.protobuf.cdt.ProtobufCdtExecutableExtensionFactory:com.google.eclipse.protobuf.cdt.actions.OpenProtoDeclarationAction&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">icon</span>=<span style="color: #ff0000;">&quot;icons/pb.gif&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;com.google.eclipse.protobuf.cdt.openProtoDeclaration&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Open Declaration in .proto File&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">menubarPath</span>=<span style="color: #ff0000;">&quot;group.open&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;push&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/viewerContribution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/extension<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Any suggestions or hints are appreciated! :) </p>
<h3>Road ahead</h3>
<p>I only scratched the surface of what can be done to integrate C++ and Protocol Buffers in Eclipse. There are still a lot of things to do. In the case of C++-to-Protocol-Buffer navigation, I still need to:</p>
<ul>
<li>Figure out how protoc handles letter cases and C++ keywords in Protocol Buffer element names</li>
<li>Implement message field matching by both qualified name and type (currently matching is done by qualified name only)</li>
<li>Support navigation for the rest of Protocol Buffer elements: enum literals, groups, rpcs, services and streams</li>
</ul>
<p>There is also other functionality that would be really useful to have:</p>
<ul>
<li>Cross-language refactoring: update all references in generated code when a Protocol Buffer element is renamed.</li>
<li>Find usages of Protocol Buffer elements in generated code. This functionality can also have other interesting uses. Eclipse can warn a user, before deleting a .proto file, that there are dependencies on the code generated from that .proto file.</li>
</ul>
<p>For now I&#8217;m concentrating on C++. Java support will come next.</p>
<h3>Summary</h3>
<p>In this post I have show you some of the upcoming functionality in protobuf-dt. Navigation from generated C++ code to its declaration in .proto files in Eclipse is a handy feature that is finally being implemented. It is not complete yet, but I expect to have a &#8220;beta&#8221; (or &#8220;testing&#8221;) release pretty soon.</p>
<p>Feedback is always welcome :)</p>
<div style="font-size: xx-small">
(Image taken from <a href="http://www.flickr.com/photos/calsidyrose/" target="_blank"> Calsidyrose&#8217;s flickr stream</a> under the creative commons license)
</div>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=2151</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Associating Xtext editors to file names, instead of file extensions</title>
		<link>http://alexruiz.developerblogs.com/?p=2080</link>
		<comments>http://alexruiz.developerblogs.com/?p=2080#comments</comments>
		<pubDate>Tue, 10 Jan 2012 12:49:19 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Xtext]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=2080</guid>
		<description><![CDATA[I&#8217;m currently working on a Xtext-based editor for Google&#8217;s BUILD language, as described in our Google Engineering Blog. BUILD files do not contain an extension, they are simply named &#8220;BUILD.&#8221; This naming convention made working with Xtext quite challenging. I needed to change Xtext&#8217;s default behavior: instead of associating Xtext editors to file extensions, I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on a <a href="http://xtext.org" target="_blank">Xtext</a>-based editor for Google&#8217;s BUILD language, as described in our <a href="http://google-engtools.blogspot.com/2011/08/build-in-cloud-how-build-system-works.html" target="_blank">Google Engineering Blog</a>. BUILD files do not contain an extension, they are simply named &#8220;BUILD.&#8221; This naming convention made working with Xtext quite challenging. I needed to change Xtext&#8217;s default behavior: instead of associating Xtext editors to file extensions, I needed to associate them to file names. In this blog post I&#8217;m describing how I was able to accomplish this.</p>
<p>In this post I&#8217;ll be using a brand-new Xtext project. To keep things simple, I&#8217;ll use the default file extension (mydsl) and grammar. You can get the source code of this post from <a href="http://code.google.com/p/xtext-samples/" target="_blank">Xtext Samples</a>, a new open source project where I plan to store the code I use in Xtext-related posts. All code is released under the <a href="http://www.eclipse.org/legal/epl-v10.html" target="_blank">Eclipse Public License (EPL) 1.0</a>.</p>
<p>The following steps assume we are going to associate our Xtext editor with the file name &#8220;MyDsl.&#8221;</p>
<ol>
<li>Modify the plug-in&#8217;s <code>XtextEditor</code> to make it understand file names, instead of file extensions.</li>
<li>Create a <code>ContentHandler</code> that describes the content of files with name &#8220;MyDsl.&#8221; As part of this step we need to create a new content type for &#8220;MyDsl&#8221; files.</li>
<li>Create an <code>IResourceServiceProvider</code> that provides services (e.g. validation, content description, encoding) to files with name &#8220;MyDsl.&#8221;</li>
<li>Register the classes created in the previous steps, to make them visible to <a href="http://www.eclipse.org/modeling/emf/" target="_blank">EMF</a> and Xtext.</li>
</ol>
<h3>1. Modify the plug-in&#8217;s <code>XtextEditor</code> to make it understand file names</h3>
<p>This is the easiest of all steps:</p>
<ol>
<li>Open the file plugin.xml in the &#8220;ui&#8221; project.</li>
<li>Select the &#8220;plugin.xml&#8221; tab at the bottom of the editor, to edit the XML code directly.</li>
<li>In the <code>editor</code> element, replace <code>extensions="mydsl"</code> with <code>filenames="MyDsl"</code></li>
</ol>
<h3>2. Create a <code>ContentHandler</code> that describes the content of files with name &#8220;MyDsl&#8221;</h3>
<p>Here is a simplified version of our <code>ContentHandler</code>. You can find the complete file <a href="http://code.google.com/p/xtext-samples/source/browse/work-with-file-names/com.google.eclipse.xtext.filename/src/com/google/eclipse/xtext/filename/resource/MyDslContentHandler.java" target="_blank">here</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyDslContentHandler <span style="color: #000000; font-weight: bold;">extends</span> ContentHandlerImpl <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> MY_DSL_FILE_CONTENT_TYPE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.google.eclipse.MyDsl&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  @Override <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> canHandle<span style="color: #009900;">&#40;</span>URI uri<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> isMyDslFile<span style="color: #009900;">&#40;</span>uri<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override <span style="color: #000000; font-weight: bold;">public</span> Map<span style="color: #339933;">&lt;</span>String, Object<span style="color: #339933;">&gt;</span> contentDescription<span style="color: #009900;">&#40;</span>URI uri, <span style="color: #003399;">InputStream</span> inputStream,
      Map<span style="color: #339933;">&lt;?</span>, <span style="color: #339933;">?&gt;</span> options, <span style="color: #003399;">Map</span> context<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>
    Map<span style="color: #339933;">&lt;</span>String, Object<span style="color: #339933;">&gt;</span> description <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">contentDescription</span><span style="color: #009900;">&#40;</span>uri, inputStream, options, context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>canHandle<span style="color: #009900;">&#40;</span>uri<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      description.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>VALIDITY_PROPERTY, VALID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      description.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>CONTENT_TYPE_PROPERTY, MY_DSL_FILE_CONTENT_TYPE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> description<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Line 2: We define a new content type for &#8220;MyDsl&#8221; files. We keep the constant <code>public</code> since we will be using it later.<br />
Line 4: We specify that file names with name &#8220;MyDsl&#8221; can be handled by the <code>ContentHandler</code>. We call the utility method <code>isMyDslFile(URI)</code> from the class <code><a href="http://code.google.com/p/xtext-samples/source/browse/work-with-file-names/com.google.eclipse.xtext.filename/src/com/google/eclipse/xtext/filename/resource/URIs.java" target="_blank">URIs</a></code>.<br />
Line 8: We declare the content of &#8220;MyDsl&#8221; files as <em>valid</em> and associate its type to the one defined in line 2.</p>
<h3>3. Create an <code>IResourceServiceProvider</code> that provides services to files with name &#8220;MyDsl&#8221;</h3>
<p>Here is a simplified version of our <code>IResourceServiceProvider</code>. You can find the complete file <a href="http://code.google.com/p/xtext-samples/source/browse/work-with-file-names/com.google.eclipse.xtext.filename/src/com/google/eclipse/xtext/filename/resource/MyDslResourceServiceProvider.java" target="_blank">here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyDslResourceServiceProvider <span style="color: #000000; font-weight: bold;">extends</span> DefaultResourceServiceProvider <span style="color: #009900;">&#123;</span>
  @Override <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> canHandle<span style="color: #009900;">&#40;</span>URI uri<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> isMyDslFile<span style="color: #009900;">&#40;</span>uri<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We call the utility method <code>isMyDslFile(URI)</code> from the class <code><a href="http://code.google.com/p/xtext-samples/source/browse/work-with-file-names/com.google.eclipse.xtext.filename/src/com/google/eclipse/xtext/filename/resource/URIs.java" target="_blank">URIs</a></code> to specify that this <code>IResourceServiceProvider</code> can handle files with names &#8220;MyDsl.&#8221;</p>
<h3>4. Register the classes created in the previous steps, to make them visible to EMF and Xtext</h3>
<p>Now it&#8217;s time to wire up everything together. First, we bind <code>IResourceServiceProvider</code> to our <code>MyDslResourceServiceProvider</code> in the plug-in <a href="http://code.google.com/p/xtext-samples/source/browse/work-with-file-names/com.google.eclipse.xtext.filename/src/com/google/eclipse/xtext/filename/MyDslRuntimeModule.java" target="_blank">runtime module</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> Class<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> IResourceServiceProvider<span style="color: #339933;">&gt;</span> bindIResourceServiceProvider<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> MyDslResourceServiceProvider.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we tell Xtext and EMF, in <code><a href="http://code.google.com/p/xtext-samples/source/browse/work-with-file-names/com.google.eclipse.xtext.filename.ui/src/com/google/eclipse/xtext/filename/ui/MyDslUiModule.java" target="_blank">MyDslUiModule</a></code>&#8216;s constructor, to understand files named &#8220;MyDsl.&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyDslUiModule <span style="color: #000000; font-weight: bold;">extends</span> AbstractMyDslUiModule <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> MyDslUiModule<span style="color: #009900;">&#40;</span>AbstractUIPlugin plugin<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>plugin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    configureXtextToWorkWithFileNames<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> InjectorProvider<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We pass an <code><a href="http://code.google.com/p/xtext-samples/source/browse/work-with-file-names/com.google.eclipse.xtext.filename.ui/src/com/google/eclipse/xtext/filename/ui/InjectorProvider.java" target="_blank">InjectorProvider</a></code> that obtains the plugin&#8217;s <code>Injector</code> only at the moment when it is needed. At this point we cannot pass the <code>Injector</code> directly, since it has not been fully constructed when <code>configureXtextToWorkWithFileNames</code> is called.</p>
<p>The method <code>configureXtextToWorkWithFileNames(InjectorProvider)</code> is defined in <a href="http://code.google.com/p/xtext-samples/source/browse/work-with-file-names/com.google.eclipse.xtext.filename.ui/src/com/google/eclipse/xtext/filename/ui/XtextSetup.java" target="_blank">XtextSetup</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> XtextSetup <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> configureXtextToWorkWithFileNames<span style="color: #009900;">&#40;</span>InjectorProvider injectorProvider<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    register<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MyDslContentHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    register<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ResourceFactoryDescriptor<span style="color: #009900;">&#40;</span>injectorProvider<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    register<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ResourceServiceProvider<span style="color: #009900;">&#40;</span>injectorProvider<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> register<span style="color: #009900;">&#40;</span><span style="color: #003399;">ContentHandler</span> h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">ContentHandler</span>.<span style="color: #003399;">Registry</span> registry <span style="color: #339933;">=</span> <span style="color: #003399;">ContentHandler</span>.<span style="color: #003399;">Registry</span>.<span style="color: #006633;">INSTANCE</span><span style="color: #339933;">;</span>
    registry.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>HIGH_PRIORITY, h<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> register<span style="color: #009900;">&#40;</span>Resource.<span style="color: #006633;">Factory</span>.<span style="color: #006633;">Descriptor</span> d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Resource.<span style="color: #006633;">Factory</span>.<span style="color: #003399;">Registry</span> registry <span style="color: #339933;">=</span> Resource.<span style="color: #006633;">Factory</span>.<span style="color: #003399;">Registry</span>.<span style="color: #006633;">INSTANCE</span><span style="color: #339933;">;</span>
    registry.<span style="color: #006633;">getContentTypeToFactoryMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>MY_DSL_FILE_CONTENT_TYPE, d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> register<span style="color: #009900;">&#40;</span>Provider<span style="color: #339933;">&lt;</span>IResourceServiceProvider<span style="color: #339933;">&gt;</span> p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    IResourceServiceProvider.<span style="color: #003399;">Registry</span> registry <span style="color: #339933;">=</span> IResourceServiceProvider.<span style="color: #003399;">Registry</span>.<span style="color: #006633;">INSTANCE</span><span style="color: #339933;">;</span>
    registry.<span style="color: #006633;">getContentTypeToFactoryMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>MY_DSL_FILE_CONTENT_TYPE, p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Line 4: We register the <code>ContentHandler</code> we created in step 2.<br />
Line 5: We associate the <code>IResourceFactory</code> in the plug-in&#8217;s Guice module with the content type defined in step 2.<br />
Line 6: We associate the <code>IResourceServiceProvider</code> we created in step 3 (and later on registered in the plug-in&#8217;s Guice module) with the content type defined in step 2.</p>
<h3>Testing the editor</h3>
<p>To run the editor, right-click any of the projects and select &#8220;Run As&#8221; &gt; &#8220;Eclipse Application&#8221; from the context menu.</p>
<p>Here is a screenshot of the editor opening a file named &#8220;MyDsl&#8221;</p>
<p><img src="http://alexruiz.developerblogs.com/files/2012/01/open-mydsl-file.png" alt="" width="613" height="462" class="aligncenter size-full wp-image-2141" /></p>
<h3>Summary</h3>
<p>In this post I have shown how to make Xtext understand file names in four easy steps. You can find the code for this post in the project <a href="http://code.google.com/p/xtext-samples/" target="_blank">Xtext Samples</a>. You can <a href="http://code.google.com/p/xtext-samples/source/checkout" target="_blank">checkout</a> the code, <a href="http://code.google.com/p/xtext-samples/source/browse/" target="_blank">browse</a> it or <a href="http://code.google.com/p/xtext-samples/downloads/detail?name=work-with-file-names.zip&amp;can=2&amp;q=">download</a> it.</p>
<p>Even though I spent a good amount of time reading Xtext and EMF code to figure this out, I cannot claim my solution is the best one. Please let me know if you know a better way to do what I described in this post :)</p>
<p>Feedback is always welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=2080</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upgraded to Xtext 2.1: first impressions</title>
		<link>http://alexruiz.developerblogs.com/?p=2048</link>
		<comments>http://alexruiz.developerblogs.com/?p=2048#comments</comments>
		<pubDate>Tue, 22 Nov 2011 19:30:38 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Xtext]]></category>
		<category><![CDATA[protobuf-dt]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=2048</guid>
		<description><![CDATA[During this past weekend I upgraded protobuf-dt to the new and shiny Xtext 2.1.1. I didn&#8217;t have a real need for the upgrade, the protocol buffer editor has been working well with version 2.0. It was mostly curiosity. Once again, the Xtext team did a great job. We, Xtext users, got an early Xmas gift. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://alexruiz.developerblogs.com/files/2011/11/gift-300x199.jpg" alt="" width="300" height="199" class="aligncenter size-medium wp-image-2059" /></p>
<p>During this past weekend I upgraded <a href="http://code.google.com/p/protobuf-dt/" target="_blank">protobuf-dt</a> to the new and shiny <a href="http://www.eclipse.org/Xtext/" target="_blank">Xtext</a> <a href="http://www.eclipse.org/Xtext/releasenotes/2.1.0/new_and_noteworthy.php" target="_blank">2.1.1</a>. I didn&#8217;t have a real need for the upgrade, the protocol buffer editor has been working well with version 2.0. It was mostly curiosity.</p>
<p>Once again, the Xtext team did a great job. We, Xtext users, got an early Xmas gift. Soon after upgrading I was pleasantly surprised by some pretty good improvements. These are my favorite ones:</p>
<ol>
<li><strong>Removing grammar ambiguity is easier.</strong> The soon-to-be-released version 1.1 of the protocol buffer editor aims at supporting 100% of the language syntax and scoping rules. Unfortunately the grammar for the protocol buffer is ambiguous, and I was having a hard time cleaning it up (I&#8217;m still a Xtext newbie.) The <em>syntactic predicates</em> I added to remove ambiguity in the grammar did not work in 2.0, but they worked fine in 2.1!</li>
<li><strong>Speed, speed, speed.</strong> My tests now execute considerably faster. Now I can run them constantly (even the integration tests,) without losing context. Note: I don&#8217;t have any numbers to back up this claim (I wasn&#8217;t even expecting any speed improvements.) I was just amazed to see how fast my test suite now runs.</li>
<li><strong>Rename refactoring now works.</strong> In Xtext 2.0 I had a few use cases where rename refactoring did not work well. It is all fixed in Xtext 2.1.</li>
<li><strong>Marker customization.</strong> Xtext comes with a really nice framework for adding semantic checks to your editor. Unfortunately, in Xtext 2.0 all editor markers created by this framework were of type &#8220;Xtext Check.&#8221; There was no way to change such type (e.g. &#8220;Protocol Buffer Problem.&#8221;) Now this is fixed in 2.1 (tracked as bug <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=351963" target="_blank">351963</a>.)</li>
</ol>
<p>Please note that these are just my first impressions after upgrading, I&#8217;m sure there are more goodies in Xtext 2.1 :)</p>
<p>As a side note, one of my sessions proposals, &#8220;<a href="http://www.eclipsecon.org/2012/sessions/xtext-success-story-google" target="_blank">Xtext Success Story at Google</a>,&#8221; got accepted for <a href="http://www.eclipsecon.org/2012/" target="_blank">EclipseCon 2012</a> as an <a href="http://www.eclipsecon.org/2012/early-bird-picks" target="_blank">early-bird pick</a>. I hope to see you there!</p>
<div style="font-size: xx-small">
(Image taken from <a href="http://www.flickr.com/photos/allerleirau/" target="_blank">allerleirau&#8217;s flickr stream</a> under the creative commons license)
</div>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=2048</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>A simple way to test Eclipse-based editors</title>
		<link>http://alexruiz.developerblogs.com/?p=1962</link>
		<comments>http://alexruiz.developerblogs.com/?p=1962#comments</comments>
		<pubDate>Mon, 17 Oct 2011 16:07:55 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[Google]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[protobuf-dt]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=1962</guid>
		<description><![CDATA[After two releases of the open source version of protobuf-dt and ten Google-internal ones, code complexity grow to a point that more comprehensive testing is needed. Even though the practices in this post can be used to test different aspects of an Eclipse editor, we&#8217;ll focus on testing scoping. Verifying that scoping works correctly involves: [...]]]></description>
			<content:encoded><![CDATA[<p>After two releases of the open source version of <a href="http://code.google.com/p/protobuf-dt/" target="_blank">protobuf-dt</a> and ten Google-internal ones, code complexity grow to a point that more comprehensive testing is needed. Even though the practices in this post can be used to test different aspects of an Eclipse editor, we&#8217;ll focus on testing <em>scoping</em>.</p>
<p>Verifying that scoping works correctly involves:</p>
<ol>
<li>Specifying and parsing protocol buffer-content to use for testing</li>
<li>Find a specific element in the parsed file</li>
<li>Verify that scoping returns all the possible elements that can match the element found in step #2</li>
</ol>
<p>An additional requirement is to make tests as lightweight as possible (e.g. do not require to instantiate a full Eclipse for running tests.)</p>
<h2>1. Specifying and parsing protocol buffer-contents to use</h2>
<p>My first attempt was to put all the protocol buffer code in a <code>StringBuilder</code> and then passed it to my <code>XtextRule</code> to get an AST back, as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Rule <span style="color: #000000; font-weight: bold;">public</span> XtextRule xtext <span style="color: #339933;">=</span> createWith<span style="color: #009900;">&#40;</span>integrationTestSetup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
@Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> should_provide_Property_fields_for_custom_field_option<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  StringBuilder proto <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  proto.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;package com.google.proto;                   &quot;</span><span style="color: #009900;">&#41;</span>
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;import 'google/protobuf/descriptor.proto';  &quot;</span><span style="color: #009900;">&#41;</span>
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;                                            &quot;</span><span style="color: #009900;">&#41;</span>
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;extend google.protobuf.FieldOptions {       &quot;</span><span style="color: #009900;">&#41;</span>
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;  optional int32 code = 1000;               &quot;</span><span style="color: #009900;">&#41;</span>
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;  optional int32 info = 1001;               &quot;</span><span style="color: #009900;">&#41;</span>
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;}                                           &quot;</span><span style="color: #009900;">&#41;</span>
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;                                            &quot;</span><span style="color: #009900;">&#41;</span>
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message Person {                            &quot;</span><span style="color: #009900;">&#41;</span> 
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;  optional boolean active = 1 [(code) = 68];&quot;</span><span style="color: #009900;">&#41;</span>
       .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;}                                           &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Protobuf root <span style="color: #339933;">=</span> xtext.<span style="color: #006633;">parseText</span><span style="color: #009900;">&#40;</span>proto<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// test implementation</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Needless to say, writing and maintaining this is a real PITA. To make it readable I have to add extra whitespace to each line so the whole thing looks square. In addition, copy/paste code from the protocol buffer editor requires also adding <code>append</code> and quotes to each line, plus formatting. On top of that, it makes the test method too long.</p>
<p>An alternative approach was to have .proto files in the file system. I was not happy with this approach since anybody reading the code will need to go to two places to understand what the test is doing.</p>
<p>Finally, I found a better approach: specify the protocol buffer text as a comment! </p>
<p>I didn&#8217;t come up with this idea myself, I&#8217;m just borrowing it from <a href="http://www.eclipse.org/cdt/" target="_blank">CDT</a>. After implementing a simpler, but similar approach, my test looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// package com.google.proto;</span>
<span style="color: #666666; font-style: italic;">// import 'google/protobuf/descriptor.proto';</span>
<span style="color: #666666; font-style: italic;">//  </span>
<span style="color: #666666; font-style: italic;">// extend google.protobuf.FieldOptions {</span>
<span style="color: #666666; font-style: italic;">//   optional int32 code = 1000;</span>
<span style="color: #666666; font-style: italic;">//   optional int32 info = 1001;</span>
<span style="color: #666666; font-style: italic;">// }</span>
<span style="color: #666666; font-style: italic;">//  </span>
<span style="color: #666666; font-style: italic;">// message Person {</span>
<span style="color: #666666; font-style: italic;">//   optional boolean active = 1 [(code) = 68];</span>
<span style="color: #666666; font-style: italic;">// }</span>
@Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> should_provide_Property_fields_for_custom_field_option<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  Protobuf root <span style="color: #339933;">=</span> xtext.<span style="color: #006633;">root</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// test implementation</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a big improvement! First of all, the test method ends up being shorter and easier to read. Second, it is easy to type something in the protocol buffer editor, copy it, and paste it above a test method. Then I just have to highlight it and press <em>Ctrl+/</em> to have the text converted to a comment.</p>
<p>In this new scenario, my custom <code>XtextRule</code> does the following,:</p>
<ol>
<li>extracts the comment of each test method and creates a <em>method name &gt; comment</em> mapping</li>
<li>parses the comment of a test method and creates an AST just <em>before</em> executing the test method, not earlier</li>
<li>keeps a reference to the root node of the AST, to be used by the test method itself</li>
</ol>
<h4>It looks good, but what about imported .proto files?</h4>
<p>When testing scoping, it is absolutely necessary to verify that imported types are included correctly. </p>
<p>I originally had .proto files to be imported in the file system and stored in Git, which is ugly for the reason I mentioned before.</p>
<p>To make things better, I borrowed another idea from CDT: specify the text and file name of the .proto file to be imported in comments, <code>XtextRule</code> will create the file in the file system just before executing a test method.</p>
<p>Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// // Create file custom-options.proto</span>
<span style="color: #666666; font-style: italic;">// </span>
<span style="color: #666666; font-style: italic;">// package com.google.proto;</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">// import &quot;google/protobuf/descriptor.proto&quot;;</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">// extend google.protobuf.FileOptions {</span>
<span style="color: #666666; font-style: italic;">//   optional int32 code = 1000;</span>
<span style="color: #666666; font-style: italic;">//   optional int32 info = 1002;</span>
<span style="color: #666666; font-style: italic;">// }  </span>
&nbsp;
<span style="color: #666666; font-style: italic;">// package com.google.proto;</span>
<span style="color: #666666; font-style: italic;">//  </span>
<span style="color: #666666; font-style: italic;">// import 'custom-options.proto';</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">// option (code) = 68;</span>
@Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> should_provide_imported_Property_fields_for_custom_option<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// test implementation</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the example above we have two comments for a test method. The first one tells my <code>XtextRule</code> to create a file named &#8220;custom-options.proto&#8221; before executing the test method. The second comment will be the one parsed and whose AST will be stored by the <code>XtextRule</code> (just like in the previous example.)</p>
<p>Neat, isn&#8217;t it? :)</p>
<h2>2. Find a specific element in the parsed file</h2>
<p>This step is necessary to ensure that scoping returns all the possible types that a given reference may be pointing to. </p>
<p>In this example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// package com.google.proto;</span>
<span style="color: #666666; font-style: italic;">// import 'google/protobuf/descriptor.proto';</span>
<span style="color: #666666; font-style: italic;">//  </span>
<span style="color: #666666; font-style: italic;">// extend google.protobuf.FieldOptions {</span>
<span style="color: #666666; font-style: italic;">//   optional int32 code = 1000;</span>
<span style="color: #666666; font-style: italic;">//   optional int32 info = 1001;</span>
<span style="color: #666666; font-style: italic;">// }</span>
<span style="color: #666666; font-style: italic;">//  </span>
<span style="color: #666666; font-style: italic;">// message Person {</span>
<span style="color: #666666; font-style: italic;">//   optional boolean active = 1 [(info) = 68];</span>
<span style="color: #666666; font-style: italic;">// }</span>
@Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> should_provide_Property_fields_for_custom_field_option<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>my test is going to verify that scoping returns the field options <code>code</code> and <code>info</code> as potential matches for <code>(code)</code>. To do so, I first need to find the proper AST element that <code>(code)</code> represents.</p>
<p>Let&#8217;s say I want to search for the custom field option <code>info</code>. My original approach would require the following:</p>
<ol>
<li>Find all the elements in the AST of type <code>CustomFieldOption</code></li>
<li>If any result is returned from #1, find the one whose name is &#8220;info&#8221;</li>
</ol>
<p>Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Protobuf root <span style="color: #339933;">=</span> xtext.<span style="color: #006633;">root</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// statically imported from CustomFieldOptionFinder</span>
CustomFieldOption option <span style="color: #339933;">=</span> findCustomFieldOption<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;info&quot;</span><span style="color: #009900;">&#41;</span>, in<span style="color: #009900;">&#40;</span>root<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This solution by itself is not too bad, and it works! Unfortunately it requires one specialized finder per type in the AST. For a relatively simple language like Protocol Buffers, it would require more than 10 finders, which is too much code to maintain just for testing.</p>
<p>Enter CDT with a better approach, which requires <strong>only one finder</strong> for all the types in the AST. This finder looks for elements this way:</p>
<ol>
<li>Find the first element matching some text</li>
<li>Verify that the found element is of the the specified type and has the specified name</li>
</ol>
<p>For example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">CustomFieldOption option <span style="color: #339933;">=</span> xtext.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;info&quot;</span>, <span style="color: #0000ff;">&quot;)&quot;</span>, CustomFieldOption.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To find the custom option &#8220;info&#8221; this finder will:</p>
<ol>
<li>concatenates the <code>String</code>s passed as arguments and find the first element that matches the text &#8220;info)&#8221;</li>
<li>verify that the found element is a <code>CustomFieldOption</code> and has name &#8220;info&#8221; (the first <code>String</code> argument only)</li>
</ol>
<p>As you can see, this approach is the complete opposite of my original one.</p>
<p>Since only one finder is needed, I can have my <code>XtextRule</code> create it and pass the root of the AST to it. This way, I don&#8217;t have to pass it to the finder every time I perform a lookup.</p>
<h2>3. Verifying that scoping returns the correct types</h2>
<p>This is actually DSL-specific. My only recommendation here is, if you are using JUnit, write your own Hamcrest matchers to keep test code short and readable, and without duplication.</p>
<h2>Putting it all together</h2>
<p>Here is how one of my tests for protobuf-dt looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// message Person {</span>
<span style="color: #666666; font-style: italic;">//   optional Type type = 1 [ctype = STRING];</span>
<span style="color: #666666; font-style: italic;">// }</span>
@Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> should_provide_Property_fields_for_native_field_option<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// We have an overloaded version of &quot;find&quot; that takes only one &lt;code&gt;String&lt;/code&gt;, </span>
  <span style="color: #666666; font-style: italic;">// to be used when the text to find and the name to match are the same.</span>
  NativeFieldOption option <span style="color: #339933;">=</span> xtext.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ctype&quot;</span>, NativeFieldOption.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  IScope scope <span style="color: #339933;">=</span> provider.<span style="color: #006633;">scope_PropertyRef_property</span><span style="color: #009900;">&#40;</span>option.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, reference<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Collection<span style="color: #339933;">&lt;</span>Property<span style="color: #339933;">&gt;</span> fieldOptions <span style="color: #339933;">=</span> descriptor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">optionsOfType</span><span style="color: #009900;">&#40;</span>FIELD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  assertThat<span style="color: #009900;">&#40;</span>descriptionsIn<span style="color: #009900;">&#40;</span>scope<span style="color: #009900;">&#41;</span>, containAll<span style="color: #009900;">&#40;</span>fieldOptions<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Please feel free to click these links to find the code of:</p>
<ul>
<li><a href="http://code.google.com/p/protobuf-dt/source/browse/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/XtextRule.java" target="_blank"><code>XtextRule</code></a></li>
<li><a href="http://code.google.com/p/protobuf-dt/source/browse/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/ModelFinder.java" target="_blank"><code>ModelFinder</code></a></li>
<li>or just get the project&#8217;s source code, as described <a href="http://code.google.com/p/protobuf-dt/source/checkout" target="_blank">here</a></li>
</ul>
<p>Feedback is always welcome! :)</p>
<h2>Future posts</h2>
<p>There are a couple of useful things I have done with <a href="http://www.eclipse.org/Xtext/" target="_blank">Xtext</a> that I&#8217;d like to blog about in the near future:</p>
<ul>
<li>Adding spell checking to comments and strings</li>
<li>Opening files outside an Eclipse workspace</li>
<li>Making Xtext work with file names, instead of file extensions</li>
</ul>
<p>Now it is a matter of finding the time and energy!</p>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=1962</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>What I am doing at Google</title>
		<link>http://alexruiz.developerblogs.com/?p=1921</link>
		<comments>http://alexruiz.developerblogs.com/?p=1921#comments</comments>
		<pubDate>Mon, 12 Sep 2011 16:06:28 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[Google]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Career]]></category>
		<category><![CDATA[Eclipse]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=1921</guid>
		<description><![CDATA[It has been eight months since I joined Google. Honestly, I have never, ever been this happy in my entire career. Google is a great place, for many reasons. The free food and the perks are nice indeed, but it is not what makes Google such a special place. What matters to me is that [...]]]></description>
			<content:encoded><![CDATA[<p>It has been eight months since I joined Google. Honestly, I have never, ever been this happy in my entire career. Google is a great place, for many reasons. The free food and the perks are nice indeed, but it is not what makes Google such a special place.</p>
<p>What matters to me is that I&#8217;m enjoying what I&#8217;m doing. Did I say &#8220;enjoying&#8221;? I <strong>love</strong> what I&#8217;m doing. I work on development tools, all <a href="http://eclipse.org" target="_blank">Eclipse</a>-based. This is what I asked to work on when I joined. For me, development tools is one of the most interesting domains in Software Engineering. I could go on and on explaining why, but let&#8217;s just say that I feel I&#8217;m developing for myself: I am part of the target audience of the software I write. I spend most of the time in Eclipse, and I like to build stuff that makes my experience better.</p>
<p>I was lucky enough to land in the Engineering Tools organization. My team is just great: super nice, talented and humble folks. Our job is to make Eclipse able to handle Google&#8217;s massive code base and to build support for Google-specific tools and languages. The following are the projects I&#8217;m currently working on:</p>
<ul>
<li>CDT integration with static code analysis tools (which BTW it is the topic for my <a href="http://www.eclipsecon.org/europe2011/sessions/cdt-and-external-static-code-analysis-tools-match-made-heaven" target="_blank">talk</a> at <a href="http://eclipsecon.org/" target="_blank">EclipseCon Europe 2011</a>)</li>
<li><a href="http://code.google.com/p/protobuf-dt/" target="_blank">protobuf-dt</a>, an editor for <a href="http://code.google.com/p/protobuf/" target="_blank">Protocol Buffers</a> (<a href="http://google-opensource.blogspot.com/2011/08/introducing-protobuf-dt-eclipse-editor.html" target="_blank">released as an open source project</A> in August)</li>
<li>support for our build language (described in our <a href="http://google-engtools.blogspot.com/2011/08/build-in-cloud-how-build-system-works.html" target="_blank">official blog</a>)</li>
<li>help organize &#8220;EclipseDay at Googleplex&#8221; with <a href="http://ianskerrett.wordpress.com/" target="_blank">Ian Skerrett</a></li>
</ul>
<p>I&#8217;ve also been visiting other Google offices (Zurich, New York, Seattle and Kirkland) co-presenting a tech talk. We gave this presentation the first time at Google&#8217;s headquarters. It was our Director that not only suggested us to take the tech talk outside of Mountain View, he actually encouraged us to do so. Needless to say, it was an awesome, mind-opener experience.</p>
<p>I will never forget how protobuf-dt became one of my main projects. It started as a toy I worked on for a few nights, on my own spare time (it was not even a 20% project.) I was so excited about the progress I made in such short period of time that I showed it to my Director during a 1:1. </p>
<p>His response was the following: </p>
<blockquote><p>
&#8220;Alex, this is great but I don&#8217;t want you to work on this at home anymore. When you are home, you enjoy your time with you family. From now on you work on this here, at the office.&#8221;
</p></blockquote>
<p>At the end of the conversation he added: </p>
<blockquote><p>
&#8220;I want you to work on what you like. I want you to be happy. When you are happy, you will be successful. Just let me know if there is anything I can do to help.&#8221;
</p></blockquote>
<p>I was completely shocked. I have never heard anything like that before. From that point on, I knew I was at the right place.</p>
<p>Probably you heard many stories about why working at Google is great. I just wanted to share mine :)</p>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=1921</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Not All Singletons are Evil</title>
		<link>http://alexruiz.developerblogs.com/?p=1832</link>
		<comments>http://alexruiz.developerblogs.com/?p=1832#comments</comments>
		<pubDate>Mon, 14 Mar 2011 14:28:24 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=1832</guid>
		<description><![CDATA[Among all the object-oriented design patterns, I think Singleton has the worst reputation. They have been called &#8220;evil,&#8221; &#8220;liars&#8221; and even &#8220;stupid,&#8221; and for good reasons: singletons are global access points, making client APIs hide their dependencies on them singletons make testing harder, mainly because they are hard to mock and inject This is true, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://alexruiz.developerblogs.com/files/2011/03/evil.png" alt="" width="158" height="193" class="aligncenter size-full wp-image-1863" /></p>
<p>
Among all the object-oriented design patterns, I think <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank">Singleton</a> has the worst reputation. They have been called &#8220;<a href="http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx" target="_blank">evil</a>,&#8221; &#8220;<a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/" target="_blank">liars</a>&#8221; and even &#8220;<a href="http://sites.google.com/site/steveyegge2/singleton-considered-stupid" target="_blank">stupid</a>,&#8221; and for good reasons:</p>
<ul>
<li>singletons are global access points, making client APIs hide their dependencies on them</li>
<li>singletons make testing harder, mainly because they are hard to mock and inject</li>
</ul>
<p>
This is true, but only when Singletons are implemented and used in the &#8220;traditional&#8221; way:</p>
<ol>
<li>
Singletons cannot be extended (note that the class is <code>final</code> and the constructor is <code>private</code>)</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> MySingleton <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> MySingleton INSTANCE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MySingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> MySingleton instance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> INSTANCE<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> MySingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// something here</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>
Singletons are called anywhere in your code</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> someMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  MySingleton.<span style="color: #006633;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// keep doing stuff</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<h2>Nothing is completely evil</h2>
<p>Just like everything else, nothing is completely good or bad. I&#8217;ve been using Singletons in the next version of <a href="http://fest.easytesting.org" target="_blank">FEST-Assert</a> in a way that avoids the problems mentioned earlier:</p>
<ol>
<li>
<strong>Do not make the Singleton final,</strong> making creation of mocks from a Singleton possible (BTW, <a href="http://mockito.org/" target="_blank">Mockito</a> can mock non-final classes with private constructors.)
</li>
<li>
<strong>Have the Singleton&#8217;s constructor package-protected.</strong> This is handy if your Singleton depends on other classes that need to be mocked during testing of the Singleton. This way, in your test, you can change the state of a new instance of the Singleton, without changing <strong>static</strong> state (that is, the state of the Singleton instance of a class.)
</li>
<li>
<strong>Inject Singletons just like any other dependency,</strong> completely avoiding the &#8220;global access point&#8221; problem.
</li>
</ol>
<h2>Sounds good. Show me the code!</h2>
<p>The following code is similar to what we have done in FEST, but a lot shorter and simpler. You can also take a look at the code in the <a href="https://github.com/alexruiz/fest-assert-2.x" target="_blank">github</a> repository.</p>
<p>Here is the Singleton:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySingleton <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> MySingleton INSTANCE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MySingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> MySingleton instance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> INSTANCE<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @VisibleForTesting MySingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// something here</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Currently, I&#8217;m doing manual dependency injection, because I consider the project to be pretty small and I&#8217;m trying to keep external dependencies to a minimum. Of course, you can (and probably should) use <a href="http://code.google.com/p/google-guice/" target="_blank">Google Guice</a> :)</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeClient <span style="color: #009900;">&#123;</span>
&nbsp;
  @VisibleForTesting <span style="color: #000000; font-weight: bold;">final</span> MySingleton mySingleton<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> SomeClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span>MySingleton.<span style="color: #006633;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @VisibleForTesting SomeClient<span style="color: #009900;">&#40;</span>MySingleton mySingleton<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">mySingleton</span> <span style="color: #339933;">=</span> mySingleton<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you may guessed by now, the constructor <code>SomeClient(MySingleton)</code> is used in tests, passing a mock <code>MySingleton</code> (and of course I also test that the default constructor uses the Singleton instance.) Any non-test code just calls the default constructor <code>SomeClient()</code>.</p>
<h2>Conclusion</h2>
<p>Despite the bad reputation that Singletons have, they can still be pretty useful. As long as Singletons are:</p>
<ol>
<li>extensible</li>
<li>injected, and</li>
<li>do not have <strong>static mutable</strong> state (state by itself is not bad)</li>
</ol>
<p>they are as good as any other tool.</p>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=1832</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>FEST-Assert 1.4: Fluent Interface for Assertions</title>
		<link>http://alexruiz.developerblogs.com/?p=1837</link>
		<comments>http://alexruiz.developerblogs.com/?p=1837#comments</comments>
		<pubDate>Mon, 28 Feb 2011 22:52:33 +0000</pubDate>
		<dc:creator>Alex Ruiz</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[FEST]]></category>
		<category><![CDATA[Release]]></category>

		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=1837</guid>
		<description><![CDATA[We are proud to announce that FEST-Assert 1.4 is out! FEST-Assert is an &#8220;assertThat&#8221; library that provides a fluent interface for writing assertions. Its main goal is to improve test code readability and make maintenance of tests easier. Example: int removed = employees.removeFired&#40;&#41;; assertThat&#40;removed&#41;.isZero&#40;&#41;; &#160; List newEmployees = employees.hired&#40;TODAY&#41;; assertThat&#40;newEmployees&#41;.hasSize&#40;6&#41; .contains&#40;frodo, sam&#41;; &#160; String&#91;&#93; newHires [...]]]></description>
			<content:encoded><![CDATA[<p>We are proud to announce that <a href="http://fest.googlecode.com/files/fest-assert-1.4.zip">FEST-Assert 1.4</a> is out!</p>
<p><a href="http://fest.easytesting.org/assert" target="_blank">FEST-Assert</a> is an &#8220;assertThat&#8221; library that provides a <a href="http://www.infoq.com/articles/internal-dsls-java" target="_blank">fluent interface</a> for writing assertions. Its main goal is to improve test code readability and make maintenance of tests easier.</p>
<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> removed <span style="color: #339933;">=</span> employees.<span style="color: #006633;">removeFired</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertThat<span style="color: #009900;">&#40;</span>removed<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isZero</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">List</span> newEmployees <span style="color: #339933;">=</span> employees.<span style="color: #006633;">hired</span><span style="color: #009900;">&#40;</span>TODAY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertThat<span style="color: #009900;">&#40;</span>newEmployees<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">hasSize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>frodo, sam<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> newHires <span style="color: #339933;">=</span> employees.<span style="color: #006633;">newHiresNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertThat<span style="color: #009900;">&#40;</span>newHires<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">containsOnly</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gandalf&quot;</span>, <span style="color: #0000ff;">&quot;Arwen&quot;</span>, <span style="color: #0000ff;">&quot;Gimli&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
assertThat<span style="color: #009900;">&#40;</span>yoda<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isInstanceOf</span><span style="color: #009900;">&#40;</span>Jedi.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">isEqualTo</span><span style="color: #009900;">&#40;</span>foundJedi<span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">isNotEqualTo</span><span style="color: #009900;">&#40;</span>foundSith<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>One of the biggest changes in this release is the implementation of Ansgar&#8217;s &quot;<a href="http://passion.forco.de/content/emulating-self-types-using-java-generics-simplify-fluent-api-implementation" target="_blank">Self Types</a>.&quot; By using this brilliant technique we ended up with a smaller, cleaner code base that is easier to maintain. </p>
<p>Here are some numbers, comparing this release with the previous one:</p>
<table class="data">
<tr>
<th>Release</th>
<th>Lines of code</th>
<th>Tests</th>
<th>Code coverage</th>
<p></tt></p>
<tr>
<td>1.3</td>
<td>3,132</td>
<td>3,707</td>
<td>98.7%</td>
</tr>
<tr>
<td>1.4</td>
<td>2,051</td>
<td>1,849</td>
<td>100%</td>
</tr>
</table>
<p>Please note that 1.4 has more features than 1.3 and yet the code base is <strong>35%</strong> smaller!</p>
<h3>Release notes</h3>
<h4>        Bug<br />
</h4>
<ul>
<li>[<a href='http://jira.codehaus.org/browse/FEST-378' target="_blank">FEST-378</a>] -         Newly added <code>assertThat(Iterable&lt;?&gt; actual)</code> eagerly calls <code>iterator()</code> and can throw an <code>NPE</code>
</li>
<li>[<a href='http://jira.codehaus.org/browse/FEST-402' target="_blank">FEST-402</a>] -         <code>onProperty</code> is unable to access properties from <code>Object</code> class
</li>
<li>[<a href='http://jira.codehaus.org/browse/FEST-414' target="_blank">FEST-414</a>] -         Impossible to use Fest Assert 1.3 with Ivy
</li>
<li>[<a href='http://jira.codehaus.org/browse/FEST-415' target="_blank">FEST-415</a>] -         <code>IteratorAssert</code> should delay <code>Iterator</code> comsumption as much as possible
</li>
<li>[<a href='http://jira.codehaus.org/browse/FEST-416' target="_blank">FEST-416</a>] -         <code>NPE</code> in <code>MapAssert</code> due to <a href="http://jira.codehaus.org/browse/FEST-111" target="_blank">FEST-329</a>
</li>
</ul>
<h4>        Improvement<br />
</h4>
<ul>
<li>[<a href='http://jira.codehaus.org/browse/FEST-105' target="_blank">FEST-105</a>] -         Add <code>isEither</code>, or <code>isOneOf</code>
</li>
<li>[<a href='http://jira.codehaus.org/browse/FEST-413' target="_blank">FEST-413</a>] -         Implement Ansgar&#039;s Self Types
</li>
<li>[<a href='http://jira.codehaus.org/browse/FEST-423' target="_blank">FEST-423</a>] -         User-friendly date and calendar formatting
</li>
</ul>
<h4>        New Feature<br />
</h4>
<ul>
<li>[<a href='http://jira.codehaus.org/browse/FEST-111' target="_blank">FEST-111</a>] -         Add support for regular expression matching to <code>StringAssert</code>
</li>
<li>[<a href='http://jira.codehaus.org/browse/FEST-381' target="_blank">FEST-381</a>] -         collection <code>onProperty()</code> assert might give nicer exceptions
</li>
<li>[<a href='http://jira.codehaus.org/browse/FEST-400' target="_blank">FEST-400</a>] -         Add generic assertions <code>isIn</code> / <code>isNotIn</code>
</li>
</ul>
<p>You can download the latest release <a href="http://code.google.com/p/fest/downloads/list" target="_blank">here</a> (file <a href="http://fest.googlecode.com/files/fest-assert-1.4.zip">fest-assert-1.4.zip</a>.) FEST-Assert requires <a href="http://java.sun.com/javase/downloads/index_jdk5.jsp" target="_blank">Java SE 5.0</a> or later.</p>
<p>Here are some useful links:</p>
<ul>
<li><a href="http://fest.easytesting.org/assert/" target="_blank">Home Page</a></li>
<li><a href="http://docs.codehaus.org/display/FEST/Fluent+Assertions+Module" target="_blank">Documentation (wiki)</a></li>
<li><a href="http://fest.easytesting.org/assert/apidocs/index.html" target="_blank">Javadocs</a></li>
<li><a href="http://jira.codehaus.org/browse/FEST" target="_blank">Issues</a></li>
<li><a href="http://groups.google.com/group/easytesting" target="_blank">Users' Group</a></li>
</ul>
<p>Feedback is always appreciated :)</p>
]]></content:encoded>
			<wfw:commentRss>http://alexruiz.developerblogs.com/?feed=rss2&#038;p=1837</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

