tag:blogger.com,1999:blog-87793148962216255112024-03-18T02:48:11.305-07:00Stash's Seriously Named Computing BlogJohn Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.comBlogger127125tag:blogger.com,1999:blog-8779314896221625511.post-28747608238778634632016-03-24T07:08:00.003-07:002016-03-24T07:08:58.657-07:00GohuFont and IntelliJ<p><a href="http://font.gohu.org/">GohuFont</a> is my new favorite font. It looks great in a terminal, but I was unable to get IntelliJ to recognize it.</p>
<p>After installing the TTF variant by Guilherme Maeda, linked from the <b>GohuFont</b> homepage (also <a href="https://github.com/koemaeda/gohufont-ttf">here</a>), it did show up in the list of available fonts:</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgITgS8h94iqj_tX_07lWrg3vffZ-pNle1ulFRCD_TfZciYzRmdLQYlHKlEwPpH4QrjJn6SAt2aSSJj-qW3UFWpg3IlaV5nP3EuXVwStMVBpxa1J6_kpOmkeRJu_zIEcT7jdzHCG7wRsKk/s1600/font.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgITgS8h94iqj_tX_07lWrg3vffZ-pNle1ulFRCD_TfZciYzRmdLQYlHKlEwPpH4QrjJn6SAt2aSSJj-qW3UFWpg3IlaV5nP3EuXVwStMVBpxa1J6_kpOmkeRJu_zIEcT7jdzHCG7wRsKk/s400/font.png" /></a></div>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-7507535464184980322015-10-03T09:01:00.000-07:002015-10-03T09:01:27.458-07:00Validating XML against an XSD with xmllintQuick reference for how to validate an XML document against a schema using <a href="http://infohost.nmt.edu/tcc/help/xml/lint.html">xmlint</a>:
<pre> $ xmllint --noout --schema stats.xsd example-stat.xml
example-stat.xml validates</pre>
Here is what you might see for an invalid document:
<pre> $ xmllint --noout --schema stats.xsd junk.xml
junk.xml:1: element event: Schemas validity error : Element 'event': No matching global declaration available for the validation root.
junk.xml fails to validate</pre>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com2tag:blogger.com,1999:blog-8779314896221625511.post-51387740851820522862015-10-01T07:50:00.000-07:002015-10-01T07:50:35.202-07:00Groovy: Closure Currying ExampleReally simple example of closure currying so I do not forget it and hope that it helps someone else:
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #333399; font-weight: bold">def</span> addTo <span style="color: #333333">=</span> <span style="color: #333333">{</span> Map m<span style="color: #333333">,</span> String k<span style="color: #333333">,</span> Object v <span style="color: #333333">-></span>
m<span style="color: #333333">[</span>k<span style="color: #333333">]</span> <span style="color: #333333">=</span> v
<span style="color: #333333">}</span>
Map elements <span style="color: #333333">=</span> <span style="color: #333333">[:]</span>
addTo elements<span style="color: #333333">,</span> <span style="background-color: #fff0f0">'a'</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">1</span>
<span style="color: #333399; font-weight: bold">def</span> addTo2 <span style="color: #333333">=</span> addTo<span style="color: #333333">.</span><span style="color: #0000CC">curry</span><span style="color: #333333">(</span>elements<span style="color: #333333">)</span>
addTo2 <span style="background-color: #fff0f0">'c'</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">3</span>
<span style="color: #008800; font-weight: bold">assert</span> elements <span style="color: #333333">==</span> <span style="color: #333333">[</span><span style="background-color: #fff0f0">'a'</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">,</span> <span style="background-color: #fff0f0">'c'</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">3</span><span style="color: #333333">]</span>
</pre></div>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-83028025153529446592015-03-08T17:32:00.001-07:002015-03-08T17:32:41.582-07:00Comparing JSON Strings with Jackson<p>Here's a quick way to compare two JSON String using <b>Jackson</b>. While we could always do a simple String compare, this method allows us to keep the expected JSON in a readable format, such as pretty-printed in a test resource:</p>
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">com.fasterxml.jackson.databind.ObjectMapper</span>
<span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">JsonAssert</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">static</span> ObjectMapper mapper <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> ObjectMapper()
<span style="color: #008800; font-weight: bold">static</span> <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">areEqual</span><span style="color: #333333">(</span>String json1<span style="color: #333333">,</span> String json2<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #333399; font-weight: bold">def</span> tree1 <span style="color: #333333">=</span> mapper<span style="color: #333333">.</span><span style="color: #0000CC">readTree</span> json1
<span style="color: #333399; font-weight: bold">def</span> tree2 <span style="color: #333333">=</span> mapper<span style="color: #333333">.</span><span style="color: #0000CC">readTree</span> json2
<span style="color: #008800; font-weight: bold">assert</span> tree1<span style="color: #333333">.</span><span style="color: #0000CC">equals</span><span style="color: #333333">(</span>tree2<span style="color: #333333">)</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
</pre></div>
<p>The above can be called from an <code>expect:</code> or <code>then:</code> block in a <b>Spock</b> specification.John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com1tag:blogger.com,1999:blog-8779314896221625511.post-85437672459666807312015-02-14T10:23:00.000-08:002015-02-14T10:24:13.304-08:00Simple Shell Script for Maven Integration Tests<p>Here's a simple shell script it took me forever to add to make it easier for me to run an integration test with <b>Maven</b>:
<pre>
$ cat ~/bin/it
#!/bin/bash
mvn clean verify -Dit -Dit.test=$1
</pre>
I just have to remember the test name now instead of repeatedly typing the above:
<pre>
$ it MyIntegrationSpec
</pre>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com1tag:blogger.com,1999:blog-8779314896221625511.post-63517464434589799532014-11-30T08:45:00.002-08:002014-11-30T08:45:53.619-08:00Subversion: Recovering Password from Local Cache<html>
<title>Subversion: Recovering Password from Local Cache</title>
<body>
<p>
If you've forgotten your <a href="https://subversion.apache.org/">Subversion</a> password, you should be able to retrieve it from another machine where you've succesfully made a commit before.</p>
<p>If you look in the local cache for your server's authentication type, you should see a series a files with hexadecimal names.</p>
<p>For example, for a server using simple or basic authentication:
<pre>
$ ls ~/.subversion/auth/svn.simple
2af94d688c4073220b0a6af1b5884861 887652dff29c33e3f09394ea7379fac9
</pre>
</p>
<p>
If you then look in one of the file, you should see your password:
<pre> $ cat ~/.subversion/auth/svn.simple/2af94d688c4073220b0a6af1b5884861 | grep -A 2 password
password
V 9
mypassword</pre>
</p>
<p>The above commands and locations are for a <b>Linux</b> box, but something similar should work on other OSes.</p>
</body>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com1tag:blogger.com,1999:blog-8779314896221625511.post-39420613156390473492014-11-05T07:28:00.000-08:002014-11-09T18:53:03.689-08:00Spock and Objenesis: Resolving IllegalAccessError<html>
<title>Spock and Objenesis: Resolving IllegalAccessError</title>
<body>
<p>When mocking some classes with <a href="http://spock-framework.readthedocs.org/en/latest/">Spock</a> in one of the usual ways, we often may need to include <b>cglib</b>. We also have to add <b>objensis</b> as well.</p>
<p>In one our modules, the latter was removed, leading to the below type of error that did not directly point out the cause:<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">java<span style="color: #333333">.</span><span style="color: #0000CC">lang</span><span style="color: #333333">.</span><span style="color: #0000CC">IllegalAccessError</span><span style="color: #333333">:</span> tried to access method org<span style="color: #333333">.</span><span style="color: #0000CC">company</span><span style="color: #333333">.</span><span style="color: #0000CC">service</span><span style="color: #333333">.</span><span style="color: #0000CC">export</span><span style="color: #333333">.</span><span style="color: #0000CC">ExportRequest</span><span style="color: #333333">.<</span>init<span style="color: #333333">>(</span>Lorg/company<span style="background-color: #fff0f0">/service/</span>export<span style="color: #333333">/</span>ExportRequest$Builder<span style="color: #333333">;)</span>V from <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">org</span><span style="color: #333333">.</span><span style="color: #0000CC">company</span><span style="color: #333333">.</span><span style="color: #0000CC">service</span><span style="color: #333333">.</span><span style="color: #0000CC">export</span><span style="color: #333333">.</span><span style="color: #0000CC">ExportRequest</span>$$EnhancerByCGLIB$$1969beb7
at org<span style="color: #333333">.</span><span style="color: #0000CC">spockframework</span><span style="color: #333333">.</span><span style="color: #0000CC">mock</span><span style="color: #333333">.</span><span style="color: #0000CC">runtime</span><span style="color: #333333">.</span><span style="color: #0000CC">MockInstantiator</span><span style="color: #333333">.</span><span style="color: #0000CC">instantiate</span><span style="color: #333333">(</span>MockInstantiator<span style="color: #333333">.</span><span style="color: #0000CC">java</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">33</span><span style="color: #333333">)</span>
at org<span style="color: #333333">.</span><span style="color: #0000CC">spockframework</span><span style="color: #333333">.</span><span style="color: #0000CC">mock</span><span style="color: #333333">.</span><span style="color: #0000CC">runtime</span><span style="color: #333333">.</span><span style="color: #0000CC">ProxyBasedMockFactory</span>$CglibMockFactory<span style="color: #333333">.</span><span style="color: #0000CC">createMock</span><span style="color: #333333">(</span>ProxyBasedMockFactory<span style="color: #333333">.</span><span style="color: #0000CC">java</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">92</span><span style="color: #333333">)</span>
at org<span style="color: #333333">.</span><span style="color: #0000CC">spockframework</span><span style="color: #333333">.</span><span style="color: #0000CC">mock</span><span style="color: #333333">.</span><span style="color: #0000CC">runtime</span><span style="color: #333333">.</span><span style="color: #0000CC">ProxyBasedMockFactory</span><span style="color: #333333">.</span><span style="color: #0000CC">create</span><span style="color: #333333">(</span>ProxyBasedMockFactory<span style="color: #333333">.</span><span style="color: #0000CC">java</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">49</span><span style="color: #333333">)</span>
at org<span style="color: #333333">.</span><span style="color: #0000CC">spockframework</span><span style="color: #333333">.</span><span style="color: #0000CC">mock</span><span style="color: #333333">.</span><span style="color: #0000CC">runtime</span><span style="color: #333333">.</span><span style="color: #0000CC">JavaMockFactory</span><span style="color: #333333">.</span><span style="color: #0000CC">create</span><span style="color: #333333">(</span>JavaMockFactory<span style="color: #333333">.</span><span style="color: #0000CC">java</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">51</span><span style="color: #333333">)</span>
at org<span style="color: #333333">.</span><span style="color: #0000CC">spockframework</span><span style="color: #333333">.</span><span style="color: #0000CC">mock</span><span style="color: #333333">.</span><span style="color: #0000CC">runtime</span><span style="color: #333333">.</span><span style="color: #0000CC">CompositeMockFactory</span><span style="color: #333333">.</span><span style="color: #0000CC">create</span><span style="color: #333333">(</span>CompositeMockFactory<span style="color: #333333">.</span><span style="color: #0000CC">java</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">44</span><span style="color: #333333">)</span>
at org<span style="color: #333333">.</span><span style="color: #0000CC">spockframework</span><span style="color: #333333">.</span><span style="color: #0000CC">lang</span><span style="color: #333333">.</span><span style="color: #0000CC">SpecInternals</span><span style="color: #333333">.</span><span style="color: #0000CC">createMock</span><span style="color: #333333">(</span>SpecInternals<span style="color: #333333">.</span><span style="color: #0000CC">java</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">47</span><span style="color: #333333">)</span>
at org<span style="color: #333333">.</span><span style="color: #0000CC">spockframework</span><span style="color: #333333">.</span><span style="color: #0000CC">lang</span><span style="color: #333333">.</span><span style="color: #0000CC">SpecInternals</span><span style="color: #333333">.</span><span style="color: #0000CC">createMockImpl</span><span style="color: #333333">(</span>SpecInternals<span style="color: #333333">.</span><span style="color: #0000CC">java</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">282</span><span style="color: #333333">)</span>
at org<span style="color: #333333">.</span><span style="color: #0000CC">spockframework</span><span style="color: #333333">.</span><span style="color: #0000CC">lang</span><span style="color: #333333">.</span><span style="color: #0000CC">SpecInternals</span><span style="color: #333333">.</span><span style="color: #0000CC">MockImpl</span><span style="color: #333333">(</span>SpecInternals<span style="color: #333333">.</span><span style="color: #0000CC">java</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">83</span><span style="color: #333333">)</span>
at org<span style="color: #333333">.</span><span style="color: #0000CC">company</span><span style="color: #333333">.</span><span style="color: #0000CC">service</span><span style="color: #333333">.</span><span style="color: #0000CC">export</span><span style="color: #333333">.</span><span style="color: #0000CC">RecordExporterSpec</span><span style="color: #333333">.</span>$spock_initializeFields<span style="color: #333333">(</span>RecordExporterSpec<span style="color: #333333">.</span><span style="color: #0000CC">groovy</span><span style="color: #333333">:</span><span style="color: #0000DD; font-weight: bold">141</span><span style="color: #333333">)</span>
</pre></div>
</p>
<p>For our mocking, we would define the mock at the field level like:
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"> ExportRequest request <span style="color: #333333">=</span> Mock<span style="color: #333333">()</span>
</pre></div>
</p>
<p>After adding <b>objenesis</b>, the error was resolved:
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"> <span style="color: #007700"><dependency></span>
<span style="color: #007700"><groupId></span>cglib<span style="color: #007700"></groupId></span>
<span style="color: #007700"><artifactId></span>cglib-nodep<span style="color: #007700"></artifactId></span>
<span style="color: #007700"><version></span>2.2<span style="color: #007700"></version></span>
<span style="color: #007700"></dependency></span>
<span style="color: #007700"><dependency></span>
<span style="color: #007700"><groupId></span>org.objenesis<span style="color: #007700"></groupId></span>
<span style="color: #007700"><artifactId></span>objenesis<span style="color: #007700"></artifactId></span>
<span style="color: #007700"><version></span>1.2<span style="color: #007700"></version></span>
<span style="color: #007700"></dependency></span>
</pre></div>
</p>
John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com2tag:blogger.com,1999:blog-8779314896221625511.post-77750679276826638082014-11-01T13:13:00.000-07:002014-11-01T13:13:20.566-07:00Sending Email with Spring<html>
<title>Sending Email with Spring</title>
<body>
<p>At work, we were using an in-house dependnecy to send text-based internal emails out the door of one our webapps for notifications. The solution worked will for us, but did more than just send emails. As a result, depending on it introduced a large dependency tree to our application.</p>
<p>Since the artifact simply appeared to be a wrapper around <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mail.html">Spring</a>, which we were using directly anyway, we thought it might be better to write our own little solution instead, and simplify our application's dependency graph.</p>
<p>Below I'll describe some "highlights". A full project example can be found here: <a href="https://github.com/prystasj/spring-mail-example">spring-mail-example</a></p>
<p>In our business processes, we send notifications to same email address and from the same sender. The only thing that may change is the subject of the email (success vs. failure) and the contents of the email. So we wanted a simple interface that just took both these parameters:</p>
<div class="highlight"><pre><span style="color: #A0A0A0; font-weight: bold">interface</span> Sender <span style="color: #666666">{</span>
<span style="color: #B00040">void</span> <span style="color: #0000FF">send</span><span style="color: #666666">(</span>String subject<span style="color: #666666">,</span> String text<span style="color: #666666">)</span>
<span style="color: #666666">}</span>
</pre></div>
<p>Below is an implementation of the interface that:
<ul>
<li>Sends a simple <code>MimeMessage</code> with type <code>text/html</code>.</li>
<li>Expects the sender and recipient email address to be injected.</li>
<li>Expects the name and port of the mail host to use to be injected.</li>
<li>Creates an instance of <code>JavaMailSender</code> to help simplify configuration for the user.</li>
</ul>
<p>The code:</p>
<div class="highlight"><pre><span style="color: #A0A0A0; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">EmailSender</span> <span style="color: #A0A0A0; font-weight: bold">implements</span> Sender<span style="color: #666666">,</span> InitializingBean <span style="color: #666666">{</span>
String mailHost
<span style="color: #B00040">int</span> mailPort
String fromAddress
String toAddress
<span style="color: #A0A0A0; font-weight: bold">protected</span> JavaMailSender mailSender
<span style="color: #AA22FF">@Override</span>
<span style="color: #B00040">void</span> <span style="color: #0000FF">send</span><span style="color: #666666">(</span>String subject<span style="color: #666666">,</span> String text<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #A0A0A0; font-weight: bold">try</span> <span style="color: #666666">{</span>
mailSender<span style="color: #666666">.</span><span style="color: #780000">send</span> <span style="color: #0000FF">htmlMessageWith</span><span style="color: #666666">(</span>subject<span style="color: #666666">,</span> text<span style="color: #666666">)</span>
<span style="color: #666666">}</span>
<span style="color: #A0A0A0; font-weight: bold">catch</span> <span style="color: #666666">(</span>e<span style="color: #666666">)</span> <span style="color: #666666">{</span>
logFailure e
<span style="color: #A0A0A0; font-weight: bold">throw</span> <span style="color: #A0A0A0; font-weight: bold">new</span> <span style="color: #0000FF">MessageException</span><span style="color: #666666">(</span>e<span style="color: #666666">)</span>
<span style="color: #666666">}</span>
logSent toAddress<span style="color: #666666">,</span> subject<span style="color: #666666">,</span> text
<span style="color: #666666">}</span>
<span style="color: #A0A0A0; font-weight: bold">private</span> MimeMessage <span style="color: #0000FF">htmlMessageWith</span><span style="color: #666666">(</span>subject<span style="color: #666666">,</span> text<span style="color: #666666">)</span> <span style="color: #666666">{</span>
def mimeMessage <span style="color: #666666">=</span> <span style="color: #A0A0A0; font-weight: bold">new</span> MimeMessage<span style="color: #666666">(</span><span style="color: #A0A0A0; font-weight: bold">null</span><span style="color: #666666">)</span>
def messageHelper <span style="color: #666666">=</span> <span style="color: #A0A0A0; font-weight: bold">new</span> MimeMessageHelper<span style="color: #666666">(</span>mimeMessage<span style="color: #666666">)</span>
messageHelper<span style="color: #666666">.</span><span style="color: #780000">with</span> <span style="color: #666666">{</span>
addTo toAddress
setFrom fromAddress
setSubject subject
setText text<span style="color: #666666">,</span> <span style="color: #A0A0A0; font-weight: bold">true</span>
<span style="color: #666666">}</span>
messageHelper<span style="color: #666666">.</span><span style="color: #780000">mimeMessage</span>
<span style="color: #666666">}</span>
<span style="color: #AA22FF">@Override</span>
<span style="color: #B00040">void</span> <span style="color: #0000FF">afterPropertiesSet</span><span style="color: #666666">()</span> <span style="color: #A0A0A0; font-weight: bold">throws</span> Exception <span style="color: #666666">{</span>
mailSender <span style="color: #666666">=</span> <span style="color: #A0A0A0; font-weight: bold">new</span> JavaMailSenderImpl<span style="color: #666666">()</span>
mailSender<span style="color: #666666">.</span><span style="color: #780000">setHost</span> mailHost
mailSender<span style="color: #666666">.</span><span style="color: #780000">setPort</span> mailPort
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>An example of how to configure the implementation with <b>Spring</b>:</p>
<pre> <span style="color: #A0A0A0; font-weight: bold"><bean</span> <span style="color: #780000">class=</span><span style="color: #000000">"prystasj.spring.mail.EmailSender"</span><span style="color: #A0A0A0; font-weight: bold">></span>
<span style="color: #A0A0A0; font-weight: bold"> <property</span> <span style="color: #780000">name=</span><span style="color: #000000">"fromAddress"</span> <span style="color: #780000">value=</span><span style="color: #000000">"prystasj@company.org"</span><span style="color: #A0A0A0; font-weight: bold">/></span>
<span style="color: #A0A0A0; font-weight: bold"> <property</span> <span style="color: #780000">name=</span><span style="color: #000000">"toAddress"</span> <span style="color: #780000">value=</span><span style="color: #000000">"friend@company.org"</span><span style="color: #A0A0A0; font-weight: bold">/></span>
<span style="color: #A0A0A0; font-weight: bold"> <property</span> <span style="color: #780000">name=</span><span style="color: #000000">"mailHost"</span> <span style="color: #780000">value=</span><span style="color: #000000">"mailhost.company.org"</span><span style="color: #A0A0A0; font-weight: bold">/></span>
<span style="color: #A0A0A0; font-weight: bold"> <property</span> <span style="color: #780000">name=</span><span style="color: #000000">"mailPort"</span> <span style="color: #780000">value=</span><span style="color: #000000">"25"</span><span style="color: #A0A0A0; font-weight: bold">/></span>
<span style="color: #A0A0A0; font-weight: bold"> </bean></span>
</pre></div>
<p>In the end, we decided to go with this solution for our emails. While we now have code of our own to maintain, the resulting dependency tree is smaller, and we have control over adding any features we want, so for now the trade-off is worth it.</p>
</body>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-23781002833644935542014-09-21T06:29:00.001-07:002014-09-21T06:29:17.573-07:00Groovy: Import Aliases<html>
<body>
<p>A feature I like in <a href="http://groovy.codehaus.org/">Groovy</a> is the ability to import class with aliases. I had forgotten about this feature, so I'm writing about it here to help get it to stick with me a bit.</p>
<p>Take this <a href="https://github.com/spockframework/spock">Spock</a> test class tha has to define a couple several values of a couple types:</p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">org.company.id.CorporateSymbol</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">org.company.id.CorporateName</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">CorporationMapperSpec</span> <span style="color: #008000; font-weight: bold">extends</span> Specification <span style="color: #666666">{</span>
<span style="color: #AA22FF">@Subject</span>
CorporationMapper mapper <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> CorporationMapper<span style="color: #666666">()</span>
def 'maps corporate symbols to names'<span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #A0A000">expect:</span>
symbolsToNames <span style="color: #666666">==</span> mapper<span style="color: #666666">.</span><span style="color: #7D9029">map</span><span style="color: #666666">(</span>symbols<span style="color: #666666">)</span>
<span style="color: #666666">}</span>
def symbol1 <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> CorporateSymbol<span style="color: #666666">(</span>'KRA'<span style="color: #666666">)</span>
def symbol2 <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> CorporateSymbol<span style="color: #666666">(</span>'VLY'<span style="color: #666666">),</span>
def symbll3 <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> CorporateSymbol<span style="color: #666666">(</span>'KRS'<span style="color: #666666">),</span>
def symbolsToNames <span style="color: #666666">=</span> <span style="color: #666666">[</span>
<span style="color: #666666">(</span>symbol1<span style="color: #666666">):</span> <span style="color: #008000; font-weight: bold">new</span> CorporateName<span style="color: #666666">(</span>'Kramerica'<span style="color: #666666">)</span>
<span style="color: #666666">(</span>symbol2<span style="color: #666666">):</span> <span style="color: #008000; font-weight: bold">new</span> CorporateName<span style="color: #666666">(</span>'Vandalay'<span style="color: #666666">)</span>
<span style="color: #666666">(</span>symbol3<span style="color: #666666">):</span> <span style="color: #008000; font-weight: bold">new</span> CorporateName<span style="color: #666666">(</span>'Kruger'<span style="color: #666666">)</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>We can use aliases with the import statements to make the code easier to work with:</p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">org.company.id.CorporateSymbol</span> as CS
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">org.company.id.CorporateName</span> as CN
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">CorporationMapperSpec</span> <span style="color: #008000; font-weight: bold">extends</span> Specification <span style="color: #666666">{</span>
<span style="color: #AA22FF">@Subject</span>
CorporationMapper mapper <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> CorporationMapper<span style="color: #666666">()</span>
def 'maps corporate symbols to names'<span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #A0A000">expect:</span>
symbolsToNames <span style="color: #666666">==</span> mapper<span style="color: #666666">.</span><span style="color: #7D9029">map</span><span style="color: #666666">(</span>symbols<span style="color: #666666">)</span>
<span style="color: #666666">}</span>
def symbol1 <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> CS<span style="color: #666666">(</span>'KRA'<span style="color: #666666">)</span>
def symbol2 <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> CS<span style="color: #666666">(</span>'VLY'<span style="color: #666666">),</span>
def symbll3 <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> CS<span style="color: #666666">(</span>'KRS'<span style="color: #666666">),</span>
def symbolsToNames <span style="color: #666666">=</span> <span style="color: #666666">[</span>
<span style="color: #666666">(</span>symbol1<span style="color: #666666">):</span> <span style="color: #008000; font-weight: bold">new</span> CN<span style="color: #666666">(</span>'Kramerica'<span style="color: #666666">)</span>
<span style="color: #666666">(</span>symbol2<span style="color: #666666">):</span> <span style="color: #008000; font-weight: bold">new</span> CN<span style="color: #666666">(</span>'Vandalay'<span style="color: #666666">)</span>
<span style="color: #666666">(</span>symbol3<span style="color: #666666">):</span> <span style="color: #008000; font-weight: bold">new</span> CN<span style="color: #666666">(</span>'Kruger'<span style="color: #666666">)</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>I would be a little wary about using it in main source, as it does add a little bit of indirection to the readability of the code, but for test source, I think it can definitely help making the code easier to read and work with.</p>
</body>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-67616012903167198972014-09-07T07:29:00.002-07:002014-09-07T07:31:52.040-07:00Jackrabbit and XPath Queries: Escaping Paths<html>
<title>Jackrabbit and XPath Queries: Escaping Paths</title>
<body>
<p>We have a service that manages a <a href="http://jackrabbit.apache.org/">Apache Jackrabbit</a> repository. The main client of the service builds lists of records of the form: <code>/<institution>/<userId>/<listType></code>, where user's are represented by a UUID. For example:
<pre> /JCP/feeadeaf-1dae-427f-bf4e-842b07965a93/label/</pre>
</p>
<p>Now we started to build a web endpoint to the service that can be used to view the contents of the repository at any time. This endpoint may want to create a query into the repository along the lines of "show me all lists for institution <code>JCP</code> and user <code>feeadeaf-1dae-427f-bf4e-842b07965a93</code>". When we create lists in the repository, we increment a property named <code>sequence</code> (more on this on a later date), so an <b>XPath</b> query that proved to work for us given the above example proved to be:
<pre> /*/JCP/feeadeaf-1dae-427f-bf4e-842b07965a93/label//*[@sequence]</pre>
<p>This was working well at first until we started to execute queries where the UUID had a leading digit. We would see an exception in our logs of the form:
<pre> Encountered "-" at line 1, column 26.
Was expecting one of:
<IntegerLiteral> ...
<DecimalLiteral> ...
<DoubleLiteral> ...
<StringLiteral> ...
...
for statement: for $v in /*/JCP/2eeadeaf-1dae-427f-bf4e-842b07965a93/label//*[@sequence] return $v</pre>
</p>
<p>Since the hypen was indicated as the culprit by the exception message, and given the fact we only ran into this when the UUID node began with a digit, we thought that both conditions were required to create the invalid query. Our first attempt at a solution simply invovled prefixing all nodes that fit the pattern of a UUID with a string like <code>uuid_</code>.</p>
<p>In reality, it was simply the leading digit that caused the problem. The above solution would definitely not work for all future use cases of the service. The problem can simply be stated that the query was invalid as XML nodes cannot start with digits. We could still however create paths into the repository in the above manner as long as we escaped the leading digit in the query:
<pre> /*/JCP/_x0032_eeadeaf-1dae-427f-bf4e-842b07965a93//*[@sequence]</pre>
<p>The code below demonstrates our modeling of a path into the repository and the method by which we perform the escaping. Before that though it is worth noting that we had to encode the individual steps when building the path included in the query. If we included the whole path in the escaping logic, the delimiting slashes would be escaped, and the query would not work. If we included the query as a whole into the escaping logic, the asterisks would be escaped, and the query would not also work.</p>
<div class="highlight"><pre><span style="color: #008000; font-weight: bold"> import</span> <span style="color: #0000FF; font-weight: bold">org.apache.commons.lang3.StringUtils</span><span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">org.apache.jackrabbit.util.ISO9075</span><span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Path</span> <span style="color: #666666">{</span>
List<span style="color: #666666"><</span>String<span style="color: #666666">></span> steps<span style="color: #666666">;</span> <span style="color: #408080; font-style: italic">//...</span>
<span style="color: #008000; font-weight: bold">public</span> String <span style="color: #0000FF">asQuery</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">return</span> steps<span style="color: #666666">.</span><span style="color: #7D9029">size</span><span style="color: #666666">()</span> <span style="color: #666666">></span> <span style="color: #666666">0</span> <span style="color: #666666">?</span> <span style="color: #BA2121">"/*"</span> <span style="color: #666666">+</span> asPathString<span style="color: #666666">(</span>encodedSteps<span style="color: #666666">())</span> <span style="color: #666666">+</span> <span style="color: #BA2121">"//*"</span> <span style="color: #666666">:</span> <span style="color: #BA2121">"//*"</span><span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">private</span> String <span style="color: #0000FF">asPathString</span><span style="color: #666666">(</span>List<span style="color: #666666"><</span>String<span style="color: #666666">></span> steps<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #BA2121">'/'</span> <span style="color: #666666">+</span> StringUtils<span style="color: #666666">.</span><span style="color: #7D9029">join</span><span style="color: #666666">(</span>steps<span style="color: #666666">,</span> <span style="color: #BA2121">'/'</span><span style="color: #666666">);</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">private</span> List<span style="color: #666666"><</span>String<span style="color: #666666">></span> encodedSteps<span style="color: #666666">()</span> <span style="color: #666666">{</span>
List<span style="color: #666666"><</span>String<span style="color: #666666">></span> encodedSteps <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> ArrayList<span style="color: #666666"><>();</span>
<span style="color: #008000; font-weight: bold">for</span> <span style="color: #666666">(</span>String step <span style="color: #666666">:</span> steps<span style="color: #666666">)</span> <span style="color: #666666">{</span>
encodedSteps<span style="color: #666666">.</span><span style="color: #7D9029">add</span><span style="color: #666666">(</span>ISO9075<span style="color: #666666">.</span><span style="color: #7D9029">encode</span><span style="color: #666666">(</span>step<span style="color: #666666">));</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">return</span> encodedSteps<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>The <code>ISO9075</code> class can be found in <code>org.apache.jackrabbit:jackrabbit-jcr-commons</code>.</p>
<p>My initial search for answers started on <a href="http://stackoverflow.com">Stack Overflow</a>, and of course the good folks answering questions there didn't let me down!</p>
</body>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-52866805300721713312014-08-30T06:46:00.002-07:002014-08-30T06:46:53.585-07:00Using Maven to Detect Duplicate Classes<html>
<title>Using Maven to Detect Duplicate Classes</title>
<body>
<p>As the number of dependencies from some our corporate project grew, we ran into a couple cases where we would be transitively pulling into different versions of the same classes. For a long time, we were lucky, as this did not cause any issues, until someday a developer new to our team introduced trying to run our application using a different operating system. The JVM on his machine loaded up the classes in a different order, and his application instance used an older version of a class that resulted in errors running the application.</p>
<p>This developer thankfully pointed out we can detect when we were pulling in different versions of classes using the <a href="http://maven.apache.org/enforcer/maven-enforcer-plugin/">Maven Enforcer plugin</a> with some help of some <a href="http://mojo.codehaus.org/extra-enforcer-rules/">extra rules</a>. We added something like this to our parent POM:</p>
<p>
<div class="highlight"><pre><span style="color: #000080; font-weight: normal"> <plugin></span>
<span style="color: #000080; font-weight: normal"><groupId></span>org.apache.maven.plugins<span style="color: #000080; font-weight: normal"></groupId></span>
<span style="color: #000080; font-weight: normal"><artifactId></span>maven-enforcer-plugin<span style="color: #000080; font-weight: normal"></artifactId></span>
<span style="color: #000080; font-weight: normal"><version></span>1.3.1<span style="color: #000080; font-weight: normal"></version></span>
<span style="color: #000080; font-weight: normal"><executions></span>
<span style="color: #000080; font-weight: normal"><execution></span>
<span style="color: #000080; font-weight: normal"><id></span>enforce<span style="color: #000080; font-weight: normal"></id></span>
<span style="color: #000080; font-weight: normal"><phase></span>validate<span style="color: #000080; font-weight: normal"></phase></span>
<span style="color: #000080; font-weight: normal"><configuration></span>
<span style="color: #000080; font-weight: normal"><rules></span>
<span style="color: #000080; font-weight: normal"><banDuplicateClasses></span>
<span style="color: #000080; font-weight: normal"><ignoreClasses></span>
<span style="color: #000080; font-weight: normal"><ignoreClass></span>javax.*<span style="color: #000080; font-weight: normal"></ignoreClass></span>
<span style="color: #000080; font-weight: normal"><ignoreClass></span>org.junit.*<span style="color: #000080; font-weight: normal"></ignoreClass></span>
<span style="color: #000080; font-weight: normal"><ignoreClass></span>org.hamcrest.*<span style="color: #000080; font-weight: normal"></ignoreClass></span>
<span style="color: #000080; font-weight: normal"><ignoreClass></span>org.slf4j.*<span style="color: #000080; font-weight: normal"></ignoreClass></span>
<span style="color: #000080; font-weight: normal"></ignoreClasses></span>
<span style="color: #000080; font-weight: normal"><findAllDuplicates></span>true<span style="color: #000080; font-weight: normal"></findAllDuplicates></span>
<span style="color: #000080; font-weight: normal"></banDuplicateClasses></span>
<span style="color: #000080; font-weight: normal"></rules></span>
<span style="color: #000080; font-weight: normal"><fail></span>false<span style="color: #000080; font-weight: normal"></fail></span>
<span style="color: #000080; font-weight: normal"></configuration></span>
<span style="color: #000080; font-weight: normal"><goals></span>
<span style="color: #000080; font-weight: normal"><goal></span>enforce<span style="color: #000080; font-weight: normal"></goal></span>
<span style="color: #000080; font-weight: normal"></goals></span>
<span style="color: #000080; font-weight: normal"></execution></span>
<span style="color: #000080; font-weight: normal"></executions></span>
<span style="color: #000080; font-weight: normal"><dependencies></span>
<span style="color: #000080; font-weight: normal"><dependency></span>
<span style="color: #000080; font-weight: normal"><groupId></span>org.codehaus.mojo<span style="color: #000080; font-weight: normal"></groupId></span>
<span style="color: #000080; font-weight: normal"><artifactId></span>extra-enforcer-rules<span style="color: #000080; font-weight: normal"></artifactId></span>
<span style="color: #000080; font-weight: normal"><version></span>1.0-beta-2<span style="color: #000080; font-weight: normal"></version></span>
<span style="color: #000080; font-weight: normal"></dependency></span>
<span style="color: #000080; font-weight: normal"></dependencies></span>
<span style="color: #000080; font-weight: normal"></plugin></span>
</pre></div>
</p>
<p>Example output from the <code>validate</code> phase of a build:
<pre> [INFO] --- maven-enforcer-plugin:1.1:enforce (enforce) @ cataloging-api ---
[INFO] Adding ignore: javax.*
[INFO] Adding ignore: org.junit.*
[INFO] Adding ignore: org.hamcrest.*
[INFO] Adding ignore: org.slf4j.*
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.BanDuplicateClasses failed with message:
Duplicate classes found:
Found in:
org.company.id.api:id-api:jar:1.28.0:compile
org.compnay.legacy.id-api:id-common-api:jar:2.8.0:compile
Duplicate classes:
org/company/service/id/SAMLEntityId.class
org/company/service/id/crypto/ServiceKey.class</pre></p>
<p>We were surprised at the amount of duplicate classes we were bringing in. We thought our dependency management was in pretty good shape, but adding the new rules showed we still had some work to do.</p>
<p>It is also worth noting, while our project is large, adding the plugin execution to the build added an insignifcat amount of time to the build overall, so there should be no performance concerns if you were considering adding something similar to your project.</p>
</body>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com1tag:blogger.com,1999:blog-8779314896221625511.post-83991446303912785132014-08-09T07:34:00.000-07:002014-08-09T07:34:02.089-07:00Spring AOP: Excluding Methods in Pointcuts<html>
<p>We use <a href="http://docs.spring.io/spring/docs/3.0.5.RELEASE/reference/aop.html">Spring AOP</a> to make use of aspects in our applications for the purpose of recording statistics about requests they receive. This approach has worked out well because it helps separate the concern of tracking statistics from our business logic. Previous iterations of our applications mixed the two, leaing to code that was generally harder to maintain.</p>
<p>Below is an example of how we might configure an aspect to target all entry points into our business application with the same aspect:</p>
<div class="highlight"><pre><span style="color: #FF00FF; font-weight: bold"> <aop:config></span>
<span style="color: #FF00FF; font-weight: bold"><aop:pointcut</span> <span style="color: #006600">id=</span><span style="color: #0000FF">"requestPointcut"</span>
<span style="color: #006600">expression=</span><span style="color: #0000FF">"execution(public * org.company.service.*.*(..))"</span><span style="color: #FF00FF; font-weight: bold">/></span>
<span style="color: #FF00FF; font-weight: bold"><aop:aspect</span> <span style="color: #006600">id=</span><span style="color: #0000FF">"requestAspect"</span> <span style="color: #006600">ref=</span><span style="color: #0000FF">"requestStatAspect"</span><span style="color: #FF00FF; font-weight: bold">></span>
<span style="color: #FF00FF; font-weight: bold"><aop:around</span> <span style="color: #006600">method=</span><span style="color: #0000FF">"around"</span> <span style="color: #006600">pointcut-ref=</span><span style="color: #0000FF">"requestPointcut"</span><span style="color: #FF00FF; font-weight: bold">/></span>
<span style="color: #FF00FF; font-weight: bold"></aop:aspect></span>
<span style="color: #FF00FF; font-weight: bold"></aop:config></span>
<span style="color: #FF00FF; font-weight: bold"><bean</span> <span style="color: #006600">id=</span><span style="color: #0000FF">"requestAspect"</span> <span style="color: #006600">class=</span><span style="color: #0000FF">"org.company.service.aop.RequestIdentifierAspect"</span> <span style="color: #006600">lazy-init=</span><span style="color: #0000FF">"true"</span><span style="color: #FF00FF; font-weight: bold">/></span>
</pre></div>
<p>With the above, we are saying we want request identifiers managed for every request to our interfaces. With the wildcards in the expression, we are saying target every class in the <code>org.mycompany.myapp.service</code> package and every method in those classes.</p>
<p>This approach worked greatly until we added an interface that provided health reporting for system monitoring purposes. This method lived in a new interface. Let's suppose added the interface method is defined as:
<ul>
<li><div class="highlight"><pre><span style="color: #FF00FF; font-weight: bold">public</span> <span style="color: #B00040">boolean</span> org<span style="color: #666666">.</span><span style="color: #006600">company</span><span style="color: #666666">.</span><span style="color: #006600">HealthService</span><span style="color: #666666">.</span><span style="color: #006600">isHealthy</span><span style="color: #666666">()</span>
</pre></div></li>
</ul>
</p>
<p>Since the method signature is different (it takes no arguments), running the advice in the aspect would lead to errors as the aspect evaluated the method arguments. We could change the code in the aspect to avoid processing if the arguments contained within the join point do not match what we are expecting. This approach is not that attractive because it one, involves changing code for special cases, and two, could lead to the aspect siltently not invoking its main logic at times we might not expect. If we target a method with the aspect unintentionally, we want to be informed by errors in our log.</p>
<p>An alternative we found was to exclude the new method from the pointcut expression:
<ul>
<li><div class="highlight"><pre>execution<span style="color: #666666">(</span><span style="color: #FF00FF; font-weight: bold">public</span> <span style="color: #666666">*</span> org<span style="color: #666666">.</span><span style="color: #006600">company</span><span style="color: #666666">.</span><span style="color: #006600">service</span><span style="color: #666666">.*.*(..))</span> <span style="color: #666666">&</span>amp<span style="color: #666666">;&</span>amp<span style="color: #666666">;</span> <span style="color: #666666">!</span>execution<span style="color: #666666">(*</span> org<span style="color: #666666">.</span><span style="color: #006600">company</span><span style="color: #666666">.</span><span style="color: #006600">service</span><span style="color: #666666">.</span><span style="color: #006600">HealthService</span><span style="color: #666666">.*(..))</span>
</pre></div></li>
</ul>
</p>
<p>Just with adding special case logic to an aspect or code in general to avoid exception cases, we have to be careful that the exceptional cases we avoid targeting with the aspect does not get out of hand as we are tempted to add more in the future.</p>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-42732142289227871912014-06-28T08:16:00.000-07:002014-06-28T08:16:34.227-07:00Sending data to a server with Groovy
<p>The <a href="groovy.codehaus.org/">Groovy</a> language provides a lot of additions to existing <a href="http://www.java.com">Java</a> classes that can make our jobs easier.</p>
<p>A couple of them we were able to take advantage of lately that use closures to make our coding simpler were:
<ul>
<li><a href="http://groovy.codehaus.org/groovy-jdk/java/net/ServerSocket.html#accept(groovy.lang.Closure)"><code>ServerSocket.start(Closure closure)</code></a></li>
<li><a href="http://groovy.codehaus.org/groovy-jdk/java/lang/Thread.html#start(groovy.lang.Closure)"><code>Thread.start(Closure closure)</code></a></li>
</ul>
</p>
<p>Our use case: We have a service whose responsibility is to send "records" over a <b>TCP/IP</b> connection to a remote server.</p>
<p>Of course we want to include a test for this in our integration tests, but how can we do it? We cannot expect a client to leave a server available open to receive records sent by our tests. We could create our test server to accept and output the sent record.</p>
<p>We'll write a test server using <b>Groovy</b> that can be started at the beginning of the test run and shutdown at the end:</p>
<div class="highlight"><pre><span style="color: #660066; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">TestServer</span> <span style="color: #666666">{</span>
<span style="color: #660066; font-weight: bold">static</span> defaultPort <span style="color: #666666">=</span> <span style="color: #666666">6666</span>
<span style="color: #660066; font-weight: bold">static</span> ServerSocket <span style="color: #0000FF">start_on_default_port</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
start_on defaultPort
<span style="color: #666666">}</span>
<span style="color: #660066; font-weight: bold">static</span> ServerSocket <span style="color: #0000FF">start_on</span><span style="color: #666666">(</span>port<span style="color: #666666">)</span> <span style="color: #666666">{</span>
def server <span style="color: #666666">=</span> <span style="color: #660066; font-weight: bold">new</span> ServerSocket<span style="color: #666666">(</span>port<span style="color: #666666">)</span>
String receivedRecord ''
def done <span style="color: #666666">=</span> <span style="color: #660066; font-weight: bold">false</span>
Thread<span style="color: #666666">.</span><span style="color: #660000">start</span> <span style="color: #666666">{</span> t <span style="color: #666666">-></span>
<span style="color: #660066; font-weight: bold">while</span> <span style="color: #666666">(!</span>done<span style="color: #666666">)</span> <span style="color: #666666">{</span>
server<span style="color: #666666">.</span><span style="color: #660000">accept</span> <span style="color: #666666">{</span> socket <span style="color: #666666">-></span>
socket<span style="color: #666666">.</span><span style="color: #660000">withStreams</span> <span style="color: #666666">{</span> input<span style="color: #666666">,</span> output <span style="color: #666666">-></span>
done <span style="color: #666666">=</span> <span style="color: #660066; font-weight: bold">false</span>
<span style="color: #660066; font-weight: bold">while</span> <span style="color: #666666">(!</span>done<span style="color: #666666">)</span> <span style="color: #666666">{</span>
def block <span style="color: #666666">=</span> <span style="color: #660066; font-weight: bold">new</span> <span style="color: #B00040">byte</span><span style="color: #666666">[128]</span>
<span style="color: #B00040">int</span> size <span style="color: #666666">=</span> input<span style="color: #666666">.</span><span style="color: #660000">read</span> block
<span style="color: #660066; font-weight: bold">if</span> <span style="color: #666666">(</span>size <span style="color: #666666">==</span> <span style="color: #666666">-1)</span> done <span style="color: #666666">=</span> <span style="color: #660066; font-weight: bold">true</span> <span style="color: #408080; font-style: italic">// there is nothing left to have been received</span>
block <span style="color: #666666">=</span> trim<span style="color: #666666">(</span>block as List<span style="color: #666666">)</span>
receivedRecord <span style="color: #666666">+=</span> <span style="color: #660066; font-weight: bold">new</span> String<span style="color: #666666">(</span>block<span style="color: #666666">)</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
server
<span style="color: #666666">}</span>
<span style="color: #408080; font-style: italic">/** Remove any empty or non-set bytes at the end of the array. */</span>
<span style="color: #660066; font-weight: bold">private</span> <span style="color: #660066; font-weight: bold">static</span> def <span style="color: #0000FF">trim</span><span style="color: #666666">(</span>List block<span style="color: #666666">)</span> <span style="color: #666666">{</span>
block<span style="color: #666666">.</span><span style="color: #660000">reverse</span><span style="color: #666666">().</span><span style="color: #660000">findAll</span> <span style="color: #666666">{</span> b <span style="color: #666666">-></span> b <span style="color: #666666">!=</span> <span style="color: #666666">0</span> <span style="color: #666666">}.</span><span style="color: #660000">reverse</span><span style="color: #666666">()</span> as <span style="color: #B00040">byte</span><span style="color: #666666">[]</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>In our integration tests class, written with <a href="http://spock-framework.readthedocs.org/en/latest/">Spock</a>, we start and stop the server at the beginning of the run:</p>
<div class="highlight"><pre><span style="color: #AA22FF">@ContextConfiguration</span>
<span style="color: #660066; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">RecordDeliveryServiceIntegrationSpec</span> <span style="color: #660066; font-weight: bold">extends</span> Specification <span style="color: #666666">{</span>
<span style="color: #AA22FF">@Autowired</span>
RecordDeliveryService service
<span style="color: #660066; font-weight: bold">static</span> def server
def <span style="color: #0000FF">setupSpec</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
server <span style="color: #666666">=</span> TestServer<span style="color: #666666">.</span><span style="color: #660000">start_on_default_port</span><span style="color: #666666">()</span>
<span style="color: #666666">}</span>
def <span style="color: #0000FF">cleanupSpec</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
server<span style="color: #666666">.</span><span style="color: #660000">close</span><span style="color: #666666">()</span>
<span style="color: #666666">}</span>
def 'delivers a record to a server'<span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #A0A000">expect:</span>
expectedResponse <span style="color: #666666">==</span> service<span style="color: #666666">.</span><span style="color: #660000">send</span> 'localhost'<span style="color: #666666">,</span> TestServier<span style="color: #666666">.</span><span style="color: #660000">defaultPort</span><span style="color: #666666">,</span> record
<span style="color: #666666">}</span>
<span style="color: #408080; font-style: italic">//...</span>
<span style="color: #666666">}</span>
</pre></div>
<p>For reference to complete the example, here's what a simplified service implementation could look like, using a methodology similar to that the test server uses to accept the record:</p>
<div class="highlight"><pre>d<span style="color: #660066; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">RecordDeliveryService</span> <span style="color: #660066; font-weight: bold">implements</span> DeliveryService <span style="color: #666666">{</span>
<span style="color: #660066; font-weight: bold">public</span> def <span style="color: #0000FF">send</span><span style="color: #666666">(</span>String host<span style="color: #666666">,</span> <span style="color: #B00040">long</span> port<span style="color: #666666">,</span> Record record<span style="color: #666666">)</span> <span style="color: #666666">{</span>
def socket <span style="color: #666666">=</span> <span style="color: #660066; font-weight: bold">new</span> Socket<span style="color: #666666">(</span>host<span style="color: #666666">,</span> port<span style="color: #666666">)</span>
socket<span style="color: #666666">.</span><span style="color: #660000">withStreams</span> <span style="color: #666666">{</span> input<span style="color: #666666">,</span> output <span style="color: #666666">-&</span>gt<span style="color: #666666">;</span>
output <span style="color: #666666">&</span>lt<span style="color: #666666">;&</span>lt<span style="color: #666666">;</span> record<span style="color: #666666">.</span><span style="color: #660000">toString</span><span style="color: #666666">()</span>
output<span style="color: #666666">.</span><span style="color: #660000">flush</span><span style="color: #666666">()</span>
output<span style="color: #666666">.</span><span style="color: #660000">close</span><span style="color: #666666">()</span>
<span style="color: #666666">}</span>
socket<span style="color: #666666">.</span><span style="color: #660000">close</span><span style="color: #666666">()</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span> <span style="color: #408080; font-style: italic">//...</span>
<span style="color: #666666">}</span>
</pre></div>
<p>If you can think of any improvements, or a better approach to the problem, I would be glad to hear from you!</p>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-49063226286608471052014-06-28T07:14:00.001-07:002014-06-28T07:14:19.126-07:00Maven: Failing the Build After Running All Modules<html>
<title>Maven: Failing the Build After Running All Modules</title>
<p>Here's a quick one to help me feel a better about not posting anything for quite a while.</p>
<p>In our large project at work that contains several deployed services, we have several parent integration testing <a href="http://maven.apache.org/">Maven</a> modules that run after our nightly development install. Each has several child modules that each run tests against a particular endpoint in each service.</p>
<p>So naturally, we want our build server to kick off each parent testing module after the install. The problem we ran into is when that one of the earlier child modules failed, the build will stop, without running the rest of the modules (Maven's default behavior).</p>
<p>This would often lead to us making changes to get the tests in the failing module to pass, then re-running the testing build, only to have a later child module to fail. It would be nice to have had all the modules to run, reporting all failures, so that all the required changes and fixes could be made up front, saving us time.</p>
<p>Fortunately, <b>Maven</b> provides an option we learned about do just that:
<pre>-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to continue</pre></p>
<p>There are also a couple more failure related options as well, but we have yet to find any use for them (yet):
<pre>-ff,--fail-fast Stop at first failure in reactorized builds
-fn,--fail-never NEVER fail the build, regardless of project result</pre></p>
<p>When working on the tests locally, we can pick up the build from within the parent from the point of the failed chilid module after making changes to our running service to fix the test with:
<pre>-rf,--resume-from Resume reactor from specified project</pre></p>
<p>For example:<pre>$ mvn verify -rf :service-integration-testing-records</pre></p>
<p>For more on working with multiple modules, see: <a href="http://maven.apache.org/guides/mini/guide-multiple-modules.html">Guide to Working with Multiple Modules</a>.</p>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com1tag:blogger.com,1999:blog-8779314896221625511.post-72538537438754750642013-05-11T09:21:00.000-07:002013-05-11T09:21:18.100-07:00Spring AOP: Ordering Aspects<html>
<body>
<p>Sometimes we may want to apply multiple aspects to the same pointcut when using <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html">Spring AOP</a>, keeping each aspect responsible for a single activity or concern. Below we'll discuss a business example of why we may want to order more than one aspects and how to ensure that ordering using <b>Spring</b>. Again, we'll be writing our code in <a href="http://groovy.codehaus.org/">Groovy</a> to keep things simple.</p>
<p>To start our example, let's say we have a service method that takes a <code>Purchase</code> and returns a <code>Shipment</code>:</p>
<div class="highlight"><pre><span style="color: #008000; font-weight: bold">interface</span> PurchaseProcessor <span style="color: #666666">{</span>
Shipment <span style="color: #0000FF">process</span><span style="color: #666666">(</span>Purchase<span style="color: #666666">)</span>
<span style="color: #666666">}</span>
</pre></div>
<p>Our first use of AOP may be recording some information about the <code>Purchase</code> for statistics purpose. Since statistics keeping is a separate concern from actually processing the <code>Purchase</code>, it serves as a perfect candidate for an aspect. For the purposes of this example, we will use an aspect that provides <i>around</i> advice instead of an alternative like <i>before</i> advice:</p>
<div class="highlight"><pre><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">StatRecordingAspect</span> <span style="color: #666666">{</span>
StatRecorder statRecorder
Object <span style="color: #0000FF">around</span><span style="color: #666666">(</span>ProceedingJoinPoint joinPoint<span style="color: #666666">)</span> <span style="color: #666666">{</span>
Purchase purchase <span style="color: #666666">=</span> joinPoint<span style="color: #666666">.</span><span style="color: #7D9029">args</span><span style="color: #666666">[0]</span>
statRecorder<span style="color: #666666">.</span><span style="color: #7D9029">record</span> purchase
joinPoint<span style="color: #666666">.</span><span style="color: #7D9029">proceed</span><span style="color: #666666">()</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>Later on, we decide we want to inform an internal process via <b>JMS</b> when the processed <code>Shipment</code> is on its way out the door. As the sending of the information could be considered another concern separate from the actual processing of the <code>Purchase</code> we decide it to should be done in an aspect. Additionally, this notification requires information from the statistics from the <code>Purchase</code>:
<div class="highlight"><pre><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">NotificationAspect</span> <span style="color: #666666">{</span>
JmsShipmentNotifier notifier
StatRecorder statRecorder
Object <span style="color: #0000FF">around</span><span style="color: #666666">(</span>ProceedingJoinPoint joinPoint<span style="color: #666666">)</span> <span style="color: #666666">{</span>
Purchase purchase <span style="color: #666666">=</span> joinPoint<span style="color: #666666">.</span><span style="color: #7D9029">args</span><span style="color: #666666">[0]</span>
Shipment shipment <span style="color: #666666">=</span> <span style="color: #666666">(</span>Shipment<span style="color: #666666">)</span>joinPoint<span style="color: #666666">.</span><span style="color: #7D9029">proceed</span><span style="color: #666666">()</span>
notifier<span style="color: #666666">.</span><span style="color: #7D9029">notifyAbout</span> shipment<span style="color: #666666">,</span> statRecord<span style="color: #666666">.</span><span style="color: #7D9029">statsFor</span><span style="color: #666666">(</span>purchase<span style="color: #666666">)</span>
shipment
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>The above business case requires that the stats about the <code>Purchase</code> be recorded before the notification. If we target both aspects at the <code>PurchaseProcessor</code> method via the same pointcut, how do we guarantee that the stats are recorded first?</p>
<p>We could opt to combine the two aspects into one, but it could be argued then the combined aspect would have one more than responsibility, record stats and sending out notifications, and have more than one concern.</p>
<p>A solution provided by <b>Spring AOP</b> is to have each aspect implement the <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/core/Ordered.html"><code>Ordered</code></a> interface provided by <b>spring-core</b> (<b>Spring</b> also provides an <code>@Order</code> annotation as an alternative to implemented the <code>Ordered</code> interface.):</p>.
<div class="highlight"><pre><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">StatRecordingAspect</span> <span style="color: #008000; font-weight: bold">implements</span> Ordered <span style="color: #666666">{</span> <span style="color: #408080; font-style: italic">//...</span>
Object <span style="color: #0000FF">around</span><span style="color: #666666">(</span>ProceedingJoinPoint joinPoint<span style="color: #666666">)</span> <span style="color: #666666">{</span> <span style="color: #408080; font-style: italic">//... }</span>
<span style="color: #AA22FF">@Override</span>
<span style="color: #B00040">int</span> <span style="color: #0000FF">getOrder</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #666666">0</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">NotificationAspect</span> <span style="color: #008000; font-weight: bold">implements</span> Ordered <span style="color: #666666">{</span> <span style="color: #408080; font-style: italic">//...</span>
Object <span style="color: #0000FF">around</span><span style="color: #666666">(</span>ProceedingJoinPoint joinPoint<span style="color: #666666">)</span> <span style="color: #666666">{</span> <span style="color: #408080; font-style: italic">//... }</span>
<span style="color: #AA22FF">@Override</span>
<span style="color: #B00040">int</span> <span style="color: #0000FF">getOrder</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #666666">1</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>With the above implementations, the <code>StatRecordingAspect</code> would be executed first as it is the aspect with the lowest order.</p>
<p>If the above makes you a little uneasy because the values returned are not linked to each other in any way, the values seem arbitrary, you could relate the orders to each other with an enumeration:</p>
<div class="highlight"><pre><span style="color: #008000; font-weight: bold">enum</span> AspectOrder <span style="color: #666666">{</span>
STAT_RECORDING<span style="color: #666666">(0),</span>
NOTIFICATION<span style="color: #666666">(1)</span>
<span style="color: #008000; font-weight: bold">final</span> <span style="color: #B00040">int</span> value
<span style="color: #0000FF">AspectOrder</span><span style="color: #666666">(</span><span style="color: #B00040">int</span> value<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">this</span><span style="color: #666666">.</span><span style="color: #7D9029">value</span> <span style="color: #666666">=</span> value
<span style="color: #666666">}</span>
<span style="color: #B00040">int</span> <span style="color: #0000FF">value</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
value
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">StatRecordingAspect</span> <span style="color: #008000; font-weight: bold">implements</span> Ordered <span style="color: #666666">{</span> <span style="color: #408080; font-style: italic">//...</span>
Object <span style="color: #0000FF">around</span><span style="color: #666666">(</span>ProceedingJoinPoint joinPoint<span style="color: #666666">)</span> <span style="color: #666666">{</span> <span style="color: #408080; font-style: italic">//... }</span>
<span style="color: #AA22FF">@Override</span>
<span style="color: #B00040">int</span> <span style="color: #0000FF">getOrder</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
AspectOrder<span style="color: #666666">.</span><span style="color: #7D9029">STAT_RECORDING</span><span style="color: #666666">.</span><span style="color: #7D9029">value</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">NotificationAspect</span> <span style="color: #008000; font-weight: bold">implements</span> Ordered <span style="color: #666666">{</span> <span style="color: #408080; font-style: italic">//...</span>
Object <span style="color: #0000FF">around</span><span style="color: #666666">(</span>ProceedingJoinPoint joinPoint<span style="color: #666666">)</span> <span style="color: #666666">{</span> <span style="color: #408080; font-style: italic">//... }</span>
<span style="color: #AA22FF">@Override</span>
<span style="color: #B00040">int</span> <span style="color: #0000FF">getOrder</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
AspectOrder<span style="color: #666666">.</span><span style="color: #7D9029">NOTIFICATION</span><span style="color: #666666">.</span><span style="color: #7D9029">value</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>Now in your unit tests you can compare the order value of one aspect to the others when testing the <code>getOrder()</code> method, ensuring they would in practice be ordered as you would expect.</p>
</body>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com1tag:blogger.com,1999:blog-8779314896221625511.post-29777148517302852632013-05-11T08:26:00.001-07:002013-05-11T08:26:32.629-07:00Loading Classpath Resources from a Static Context<html>
<body>
<p>Recently, I had to figure out how to load classpath resources from within a static method in a test class. I've been doing it from a non-static method forever, but had to do some searching to figure out how to do it from a static method.</p>
<p>For example to build a scenario where one might want to do this, let's take a generic <code>Record</code> class that is used by many of our classes under test. As <b>JSON</b> is the input method for records from the user of our system, it might make sense to store our records to use during our tests as JSON test resources.</p>
<p>During our tests, we can load the record JSON from a test resource on the classpath with a test helper class like the below. The <a href="http://groovy.codehaus.org">Groovy</a> test class uses the <code>ObjectMapper</code> class from <a href="http://jackson.codehaus.org/">Jackson</a>:</p>
<div class="highlight"><pre><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">RecordBuilder</span> <span style="color: #666666">{</span>
def <span style="color: #0000FF">recordFor</span><span style="color: #666666">(</span>resource<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">new</span> <span style="color: #0000FF">ObjectMapper</span><span style="color: #666666">().</span><span style="color: #7D9029">readValue</span> textOf<span style="color: #666666">(</span>resource<span style="color: #666666">),</span> Record<span style="color: #666666">.</span><span style="color: #7D9029">class</span>
<span style="color: #666666">}</span>
def <span style="color: #0000FF">textOf</span><span style="color: #666666">(</span>resource<span style="color: #666666">)</span> <span style="color: #666666">{</span>
getClass<span style="color: #666666">().</span><span style="color: #7D9029">getClassLoader</span><span style="color: #666666">().</span><span style="color: #7D9029">getResourceAsStream</span><span style="color: #666666">(</span>resource<span style="color: #666666">).</span><span style="color: #7D9029">text</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>One annoyance with using the above class in our test classes is that each test class would need to create or be given an instance of RecordBuilder to do its work. An alternative is to make the record-building method static, which should be ok since the normal concerns regarding static methods, pro or con, should not apply in a testing scenario as tests are usually executed one at at time.</p>
<p>The problem with making the <code>recordFor</code> method static is the approach to reading the resource from the classpath no longer works, and the solution may not be apparent to those who have not tried to do something similar before. Below includes the answer I was able to find, referring to the <code>RecordBuilder</code> class itself:</p>
<div class="highlight"><pre><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">RecordBuilder</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">static</span> def <span style="color: #0000FF">recordFor</span><span style="color: #666666">(</span>resource<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">new</span> <span style="color: #0000FF">ObjectMapper</span><span style="color: #666666">().</span><span style="color: #7D9029">readValue</span> textOf<span style="color: #666666">(</span>resource<span style="color: #666666">),</span> Record<span style="color: #666666">.</span><span style="color: #7D9029">class</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">static</span> def <span style="color: #0000FF">textOf</span><span style="color: #666666">(</span>resource<span style="color: #666666">)</span> <span style="color: #666666">{</span>
RecordBuilder<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">.</span><span style="color: #7D9029">getClassLoader</span><span style="color: #666666">().</span><span style="color: #7D9029">getResourceAsStream</span><span style="color: #666666">(</span>resource<span style="color: #666666">).</span><span style="color: #7D9029">text</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>I guess this was a rather long-winded way to show how to load classpath resources from a static context, but I hope it will do. Thanks.</p>
</body>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-3370119323727312172013-01-23T09:34:00.002-08:002013-01-23T09:34:08.354-08:00Spring & RestTemplate: Dealing with Double Escaped Entities<html>
<p>We've been using Spring's <b><code>RestTemplate</code></b> pretty extensively to perform various operations. Several have escaped entities in the query. For example, one query requires its parameters be surrounded by quotes:</p>
<pre>http://myhost.com/records?query=no+"1"+owner="John"</pre>
<p>We could inject that URL into a bean containing a <code>RestTemplate</code> instance, representing the URL with a <code>String</code> and escaping the quotes with <code>%22</code> to make the parsing of the bean file easier:</p>
<p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold"><bean</span> <span style="color: #7D9029">id=</span><span style="color: #BA2121">"recordUrl"</span> <span style="color: #7D9029">class=</span><span style="color: #BA2121">"java.lang.String"</span><span style="color: #008000; font-weight: bold">></span>
<span style="color: #008000; font-weight: bold"><constructor-arg</span> <span style="color: #7D9029">value=</span><span style="color: #BA2121">"http://myhost.com/records?query=no+%221%22+owner=%22John%22"</span><span style="color: #008000; font-weight: bold">/></span>
<span style="color: #008000; font-weight: bold"></bean></span>
</pre></div>
</p>
<p>Then executing the query:</p>
<p><pre> restTemplate.getForObject(recordUrl, String.class, urlVariables)</pre></p>
<p>However, our query would not work as each <code>%22</code> would be escaped again as %2522. For example:</p>
<p>
<pre> DEBUG main org.springframework.web.client.RestTemplate - Created GET request for: "http://myhost.com/records?query=no+%25221%2522+owner=%2522John%2522"</pre>
<p>How do we avoid the double escaping?. The <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html">RestTemplate Javadoc</a> contains the answer. To avoid the double-escaping, we need to use the version of the RestTemplate method that takes a <code>java.net.URI</code> as the first parameters instead of a <code>String</code> representing the URL:</p>
<p><pre> restTemplate.getForObject(new URI(recordUrl), String.class, urlVariables)</pre></p>
<p>Or instead of creating a <code>URI</code>, inject an instance of one like what was done with the <code>String</code> originally.
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com3tag:blogger.com,1999:blog-8779314896221625511.post-75916009663206358922012-10-28T14:53:00.001-07:002012-10-28T14:55:24.835-07:00Lombok and Spring: IntelliJ Plugin<html>
<p>As indicated in my last post, I've been playing around with <a href="http://projectlombok.org/">Project Lombok</a> a little. One of the easier to absorb features is the <a href="http://projectlombok.org/features/GetterSetter.html"><code>@Getter/@Setter</code></a> annotations, which removes the need to explicitly define getters and setters in your Java classes.</p>
<p>I use <a href="http://www.jetbrains.com/idea/">IntelliJ</a> as my main IDE. However, when I started to use Lombok, I would get error messages from the IDE when viewing a <a href="http://springframework.org">Spring</a> configuration file that used setter injection.</p>
<p>The answer to this problem was to simply install the <a href="http://code.google.com/p/lombok-intellij-plugin/">lombok-intellij-plugin</a>, which fixed the error rather nicely.</p>
<p>We'll setup the scenario withe simplest of objects that I can think of. If you now have the answer you're looking for (and you probably do), feel free to skip the example below:</p>
<p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Person</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">private</span> String name<span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #0000FF">Person</span><span style="color: #666666">()</span> <span style="color: #666666">{}</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #0000FF">Person</span><span style="color: #666666">(</span>String name<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">this</span><span style="color: #666666">.</span><span style="color: #7D9029">name</span> <span style="color: #666666">=</span> name<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">public</span> String <span style="color: #0000FF">getName</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">return</span> name<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">setName</span><span style="color: #666666">(</span>String name<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">this</span><span style="color: #666666">.</span><span style="color: #7D9029">name</span> <span style="color: #666666">=</span> name<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
</p>
<p>A very simplisitc <a href="http://code.google.com/p/spock/">Spock</a> specification that uses <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html">Spring testing</a> to inject an instance of a <code>Person</code>:</p>
<div class="highlight"><pre> <span style="color: #AA22FF">@ContextConfiguration</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">PersonSpec</span> <span style="color: #008000; font-weight: bold">extends</span> Specification <span style="color: #666666">{</span>
<span style="color: #AA22FF">@Autowired</span>
Person person
def <span style="color: #BA2121">"has a name"</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #A0A000">expect:</span> person<span style="color: #666666">.</span><span style="color: #7D9029">name</span> <span style="color: #666666">==</span> <span style="border: 1px solid #FF0000">'</span>Rudiger<span style="border: 1px solid #FF0000">'</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>And the bean that is autowired in from <code>PersonSpec-context.xml</code>:</p>
<p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold"><bean</span> <span style="color: #7D9029">id=</span><span style="color: #BA2121">"rudiger"</span> <span style="color: #7D9029">class=</span><span style="color: #BA2121">"prystasj.lombok.plugin.example.Person"</span><span style="color: #008000; font-weight: bold">></span>
<span style="color: #008000; font-weight: bold"><property</span> <span style="color: #7D9029">name=</span><span style="color: #BA2121">"name"</span> <span style="color: #7D9029">value=</span><span style="color: #BA2121">"Rudiger"</span><span style="color: #008000; font-weight: bold">/></span>
<span style="color: #008000; font-weight: bold"></bean></span>
</pre></div>
</p>
<p>Here the test passes and there is no "red" for errors in our IDE.</p>
<p>Now if we replace the getter and setter in the <code>Person</code> with annotations from <b>Lombok</b>:</p>
<p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Person</span> <span style="color: #666666">{</span>
<span style="color: #AA22FF">@Getter</span> <span style="color: #AA22FF">@Setter</span>
<span style="color: #008000; font-weight: bold">private</span> String name<span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #0000FF">Person</span><span style="color: #666666">()</span> <span style="color: #666666">{}</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #0000FF">Person</span><span style="color: #666666">(</span>String name<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">this</span><span style="color: #666666">.</span><span style="color: #7D9029">name</span> <span style="color: #666666">=</span> name<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
</p>
<p>The tests still passes, but our IDE is showing us some red when viewing the Spring configuration file:</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYjn4PodcjRHrFg4RPVbqUSci0ajfZ4TvvPVRksJbgmADPe24MU12obH4r_N2DZpJo99KMUhSCqT1ClHo7hWnpIZVicqWYyVSr_no5zVE2W47QTwI1Ox-gc6fox7OhWK2SVu3_I4e0-Dk/s1600/lombok-plugin.png" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="141" width="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYjn4PodcjRHrFg4RPVbqUSci0ajfZ4TvvPVRksJbgmADPe24MU12obH4r_N2DZpJo99KMUhSCqT1ClHo7hWnpIZVicqWYyVSr_no5zVE2W47QTwI1Ox-gc6fox7OhWK2SVu3_I4e0-Dk/s400/lombok-plugin.png" /></a></div>
<p>At this point, installing the <a href="http://code.google.com/p/lombok-intellij-plugin/">lombok-intellij-plugin</a> sends the error away.</p>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-51811149930538800342012-10-24T16:04:00.000-07:002012-10-24T16:04:30.020-07:00Lombok: Using @Getter/@Setter and @Delegate<html>
<p>I've been working on a project for the majority of the past year that's part of a larger architecture where the powers that be have not approved the use of a language like <a href="http://groovy.codehaus.org">Groovy</a> due to strong feelings about things like type safety, among other fears. I don't want to get into a debate about whether such fears have any foundation (maybe at a later date). I do however want to try to introduce <a href="http://projectlombok.org/">Project Lombok</a> here, which has helped simplify the Java we've been writing, helping to remove some of the boilerplate code that we've been writing, bringing our code a little closer to what it might look like with Groovy.</p>
<p><b>Lombok</b> has many features to help simplify your Java. For now, I'll present just <a href="http://projectlombok.org/features/GetterSetter.html"><code>@Getter/@Setter</code></a> and <a href="http://projectlombok.org/features/Delegate.html"><code>@Delegate</code></a>. We'll compare a plain Java version of some object and present both a Lombok and Groovy version for comparison.</p>
<p>Our example is pretty straightforward, a nameable <code>Rocket</code> that uses a <code>Booster</code> to take off and record if the rocket has been started:</p>
<p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Rocket</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">final</span> Booster booster<span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">private</span> String name<span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #0000FF">Rocket</span><span style="color: #666666">(</span>String name<span style="color: #666666">,</span> Booster booster<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">this</span><span style="color: #666666">.</span><span style="color: #7D9029">name</span> <span style="color: #666666">=</span> name<span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">this</span><span style="color: #666666">.</span><span style="color: #7D9029">booster</span> <span style="color: #666666">=</span> booster<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">takeoff</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
booster<span style="color: #666666">.</span><span style="color: #7D9029">takeoff</span><span style="color: #666666">();</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">boolean</span> <span style="color: #0000FF">isStarted</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">return</span> booster<span style="color: #666666">.</span><span style="color: #7D9029">isStarted</span><span style="color: #666666">();</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">public</span> String <span style="color: #0000FF">getName</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">return</span> name<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">setName</span><span style="color: #666666">(</span>String name<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">this</span><span style="color: #666666">.</span><span style="color: #7D9029">name</span> <span style="color: #666666">=</span> name<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Booster</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">private</span> <span style="color: #B00040">boolean</span> started <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">takeoff</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
System<span style="color: #666666">.</span><span style="color: #7D9029">out</span><span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">(</span><span style="color: #BA2121">"3.. 2.. 1.. Java Rocket go now!"</span><span style="color: #666666">);</span>
started <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">boolean</span> <span style="color: #0000FF">isStarted</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">return</span> started<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
</p>
<p>The above code has some classic Java boilerplate in getters and setters, along with a couple one line methods that acts as simple pass-throughs from <code>Rocket</code> to its <code>Booster</code> collaborator.</p>
<p>Now we can introduce the <code>@Getter/@Setter</code> and <code>@Delegate</code> annotations to simplify the code (with the <code>import</code> statements ommitted for brevity):</p>
<p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Rocket</span> <span style="color: #666666">{</span>
<span style="color: #AA22FF">@Delegate</span> <span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">final</span> Booster booster<span style="color: #666666">;</span>
<span style="color: #AA22FF">@Getter</span> <span style="color: #AA22FF">@Setter</span> <span style="color: #008000; font-weight: bold">private</span> String name<span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #0000FF">Rocket</span><span style="color: #666666">(</span>String name<span style="color: #666666">,</span> Booster booster<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #008000; font-weight: bold">this</span><span style="color: #666666">.</span><span style="color: #7D9029">name</span> <span style="color: #666666">=</span> name<span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">this</span><span style="color: #666666">.</span><span style="color: #7D9029">booster</span> <span style="color: #666666">=</span> booster<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Booster</span> <span style="color: #666666">{</span>
<span style="color: #AA22FF">@Getter</span> <span style="color: #008000; font-weight: bold">private</span> <span style="color: #B00040">boolean</span> started <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">takeoff</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
System<span style="color: #666666">.</span><span style="color: #7D9029">out</span><span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">(</span><span style="color: #BA2121">"3.. 2.. 1.. Lombok Rocket go now!"</span><span style="color: #666666">);</span>
started <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
</p>
<p>This code is simpler as the getters and setters have been removed, along with the pass-throughs. What would a more Groovy example look like?</p>
<p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Rocket</span> <span style="color: #666666">{</span>
<span style="color: #AA22FF">@Delegate</span> <span style="color: #008000; font-weight: bold">private</span> Booster booster
String name
<span style="color: #666666">}</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Booster</span> <span style="color: #666666">{</span>
<span style="color: #B00040">boolean</span> started <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">;</span>
def <span style="color: #0000FF">takeoff</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
println <span style="color: #BA2121">"3.. 2.. 1.. Groovy Rocket go now!"</span>
started <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">true</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
</p>
<p>This code is extremely similar to the Lombok example. The <code>@Getter</code> and <code>@Setter</code> annotations are missing because with Groovy we get them by default, however, now we could conceivably find a way to directly set the <code>started</code> property of the <code>Booster</code> class. Another somewhat significant difference is the <code>Rocket</code> constructor is missing, which we would have to reinstate if we were using this code from Java.</p>
<p>The same <a href="http://code.google.com/p/spock/">Spock</a> specification can be used to test all 3 classes to verify they provide the same behavior:</p>
<p>
<div class="highlight"><pre> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">RocketSpec</span> <span style="color: #008000; font-weight: bold">extends</span> Specification <span style="color: #666666">{</span>
def name <span style="color: #666666">=</span> <span style="color: #BA2121">"LombokRocket"</span>
def booster <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Booster<span style="color: #666666">()</span>
def rocket <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Rocket<span style="color: #666666">(</span>name<span style="color: #666666">,</span> booster<span style="color: #666666">)</span>
def <span style="color: #BA2121">"initially has not taken off"</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #A0A000">expect:</span> <span style="color: #666666">!</span>rocket<span style="color: #666666">.</span><span style="color: #7D9029">isStarted</span><span style="color: #666666">()</span>
<span style="color: #666666">}</span>
def <span style="color: #BA2121">"can takeoff"</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #A0A000">when:</span> rocket<span style="color: #666666">.</span><span style="color: #7D9029">takeoff</span><span style="color: #666666">()</span>
<span style="color: #A0A000">then:</span> rocket<span style="color: #666666">.</span><span style="color: #7D9029">isStarted</span><span style="color: #666666">()</span>
<span style="color: #666666">}</span>
def <span style="color: #BA2121">"has a name that can be changed"</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #A0A000">when:</span> rocket<span style="color: #666666">.</span><span style="color: #7D9029">name</span> <span style="color: #666666">=</span> name<span style="color: #666666">.</span><span style="color: #7D9029">reverse</span><span style="color: #666666">()</span>
<span style="color: #A0A000">then:</span> rocket<span style="color: #666666">.</span><span style="color: #7D9029">name</span> <span style="color: #666666">==</span> name<span style="color: #666666">.</span><span style="color: #7D9029">reverse</span><span style="color: #666666">()</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
</p>
<p>When looking at the Lombok version, it is definitely closer to the Groovy version, showing that if you are stuck using Java, there are some features you can still have with a little work.</p>
<p>Another point of interest is getting both Lomboked and Groovy code to compile alongside our Java code using <a href="http://maven.apache.org/">Maven</a>. I've uploaded this project to <b>GitHub</b> if you would like to view the POM and the rest of the source:
<ul>
<li><a href="https://github.com/prystasj/lombok-groovy-example">lombok-groovy-example</a></li>
</ul>
</p>
<p>I'll look to add to the above project in the future to demonstrate some more Lombok features.</p>
<p>Comment away, and thanks!</p>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-818268481559839602012-07-26T17:31:00.002-07:002012-07-26T17:32:58.812-07:00Setting the Location of the Maven Local Repository at the Command Line<p>If you ever need to temporarily override the directory <a href="http://maven.apache.org">Maven</a> uses for your local repository when running Maven from the command line, you can do so by setting the property <code>maven.repo.local</code>:<p>
<p><pre> $ mvn -Dmaven.repo.local=/tmp/repository install</pre></p>
<p>Why would you want to do this? I found a useful situation while logged into a server at work where my login had a form of "read-only" access, and therefore no home directory, but where I had access to <b>Maven</b> and needed to test something out. I'm sure there would be other reasons out there as well or the property wouldn't be available?</p>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-51812288761457981722012-07-26T17:25:00.003-07:002012-07-26T17:25:23.562-07:00Resolving Font Preferences Between Openbox and Gnome<p>While working on setting a new workstation with <a href="http://linuxmint.com/">Linux Mint 13 Maya</a> and <a href="http://openbox.org">Openbox</a>, I had quite a difficult time getting any adjustments to the fonts to take hold while working in Openbox.</p>
<p>The two approaches I took was adjusting the fonts through <a href="https://live.gnome.org/GnomeTweakTool/">Gnome Tweak Tool</a> and <a href="http://openbox.org/wiki/ObConf:About">ObConf</a>. Changes with the tweak tool took effect when logged in to <b>Gnome</b>, but not when in <b>Openbox</b>, which I found surprising in part because <b>Gtk</b> was still being used to render the windows. Adjustments in <b>ObConf</b> only affect items like the font rendering in the window title bar.</p>
<p>The difference however is when in <b>Openbox</b>, <b>gnome-settings-daemon</b> was not running. A good hint is found in the system wide <b>autostart</b> script at <code>/etc/xdg/openbox/autostart</code>:
<p><pre> # If you want to use GNOME config tools...
#
#if test -x /usr/lib/openbox/gnome-settings-daemon >/dev/null; then
# /usr/lib/openbox/gnome-settings-daemon &
#elif which gnome-settings-daemon >/dev/null; then
# gnome-settings-daemon &
#fi</pre></p>
<p>Running <code>/usr/bin/gnome-settings-daemon</code> from a terminal did the trick and the changes made to the fonts did take hold.</p>
<p>To make the changes permanent, you could uncomment the above section in the system-wide <b>autostart</b> script, but I chose for no particular reason to adjust the user-level script, <code>~/.config/openbox/autostart</code> by simply adding:</p>
<p><pre> gnome-settings-daemon &</pre></p>
<p>Either way should work if anyone is running into the same issue.</p>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-36004633462058985652012-05-14T04:11:00.000-07:002012-05-14T04:11:04.632-07:00Spock: Using @Unroll with Parameterized Data<p>With <a href="http://code.google.com/p/spock/">Spock</a>, a testing framework for <b>Java</b> and <a href="http://codehaus.org">Groovy</a>, we can easily write tests using parameterized data. One feature I'd like to share is the <code>@Unroll</code> annotation which makes working with parameterized data easier as each entry in the table will be treated as separate test.</p>
<p>Take this example that tests the addition of two numbers:</p>
<div class="highlight"><pre><span style="color: #008000; font-weight: bold"> package</span> org<span style="color: #666666">.</span><span style="color: #7D9029">stash</span><span style="color: #666666">.</span><span style="color: #7D9029">example</span>
<span style="color: #008000; font-weight: bold"> import</span> <span style="color: #0000FF; font-weight: bold">spock.lang.Specification</span>
<span style="color: #008000; font-weight: bold"> import</span> <span style="color: #0000FF; font-weight: bold">spock.lang.Unroll</span>
<span style="color: #008000; font-weight: bold"> class</span> <span style="color: #0000FF; font-weight: bold">AdderSpec</span> <span style="color: #008000; font-weight: bold">extends</span> Specification <span style="color: #666666">{</span>
Adder adder <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Adder<span style="color: #666666">()</span>
<span style="color: #AA22FF"> @Unroll</span><span style="color: #666666">(</span><span style="color: #BA2121">"#i + #j == #sum?"</span><span style="color: #666666">)</span>
def 'can add two numbers together to produce a sum'<span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #A0A000"> expect:</span>
sum <span style="color: #666666">==</span> adder<span style="color: #666666">.</span><span style="color: #7D9029">add</span><span style="color: #666666">(</span>i<span style="color: #666666">,</span> j<span style="color: #666666">)</span>
<span style="color: #A0A000"> where:</span>
i <span style="color: #666666">|</span> j <span style="color: #666666">|</span> sum
<span style="color: #666666">1</span> <span style="color: #666666">|</span> <span style="color: #666666">2</span> <span style="color: #666666">|</span> <span style="color: #666666">3</span>
<span style="color: #666666">2</span> <span style="color: #666666">|</span> <span style="color: #666666">2</span> <span style="color: #666666">|</span> <span style="color: #666666">4</span>
<span style="color: #666666">-1</span> <span style="color: #666666">|</span> <span style="color: #666666">2</span> <span style="color: #666666">|</span> <span style="color: #666666">0</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>
<p>Prior to adding the <code>@Unroll</code> annotation, our test output in <b>Maven</b> would look like this:</p>
<p><pre> Running org.stash.example.AdderSpec
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.374 sec</pre></p>
<p>The three separate test are treated as a whole. With the annotation added:</p>
<p><pre> Running org.stash.example.AdderSpec
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.388 sec</pre></p>
<p>Now each entry for the data table is treated as a separate test</p>
<p>If we were to force a failure, by say expecting <code>-1 + 2 == 0</code>...
<p><pre> Running org.stash.example.AdderSpec
Tests run: 3, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.501 sec <<< FAILURE!
Results :
Failed tests: -1 + 2 == 0?(org.stash.example.AdderSpec): Condition not satisfied:(..)</pre></p>
<p>We now see in the output the message attached to the annotation with the values used in the test from the data table filtered in!</p>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com2tag:blogger.com,1999:blog-8779314896221625511.post-5579049379720390792012-05-14T03:08:00.000-07:002012-05-14T03:08:09.786-07:00Hessian: Expected "End of Map" Exception<p>We've been working with <a href="http://hessian.caucho.com/">Hessian</a> services lately to send Java objects over the wire. So far the work has been trouble free, but from time to time, we run into some cryptic error messages. I'll document one here in case someone runs into a same problem.</p>
<p>Take the following error:</p>
<p><pre> Caused by: com.caucho.hessian.io.HessianProtocolException: expected end of map ('Z') at 'end of file'</pre></p>
<p>This appear simply means, at least in our case, that the object being received by the caller of a Hessian service cannot be deserialized, either the object being transferred directly, or object contained with the transferred object's graph.</p>
<p>To resolve make sure every object in the graph implements <a href="http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html">java.io.Serializable</a>.</p>
<p><b>Note:</b> The above error came from <code>com.caucho:hessian:jar:4.0.7</code>.</p>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com0tag:blogger.com,1999:blog-8779314896221625511.post-75952317608117195682012-04-21T10:05:00.001-07:002012-04-21T10:05:23.764-07:00Spring Expression Langauge and Simple Bean References<html>
<p>In this post I'll demonstrate a simple use of <a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/expressions.html">Spring Expression Language (SpEL)</a>. First, I'll give a little background of the "problem" I used to solve, but feel free to skip ahead to the Spring configuration below.</p>
<p>The project in question was required to make two separate RESTful calls to the same service, for example:</p>
<p><ol>
<li><pre>http://service.mycompany.com/business/record</pre></li>
<li><pre>http://service.mycompany.com/business/order</pre></li>
</ol></p>
<p>The URLs contain the same base path, <code>http://service.mycompany.com/business</code>, and two separate paths, <code>record</code> and <code>order</code>. The URLs are to be looked up in a service directory, whose entries we have control of.</p>
<p>At first I considered placing both URLs in the directory to avoid having to deal with adding each path to the base URL in code. This sounded like a misuse of the directory however as each URL would not represent a separate service deployment. Then I remembered reading about <b>SpEL</b> and thought this could be help me add the paths to the URL in good ol' Spring configuration.</p>
<p>Here is a simplification of the defintion of the bean for the URL (<code>java.net.URL</code>) we will reference for defining the two service URLs:</p>
<div class="highlight"><pre> <span style="color: #996699; font-weight: bold"><bean</span> <span style="color: #003366">id=</span><span style="color: #BA2121">"serviceUrl"</span> <span style="color: #003366">class=</span><span style="color: #BA2121">"org.mycompany.service.entry.ServiceEntryProxy"</span><span style="color: #996699; font-weight: bold">></span>
<span style="color: #996699; font-weight: bold"><property</span> <span style="color: #003366">name=</span><span style="color: #BA2121">"serviceName"</span> <span style="color: #003366">value=</span><span style="color: #BA2121">"businessService"</span><span style="color: #996699; font-weight: bold">/></span>
<span style="color: #996699; font-weight: bold"><property</span> <span style="color: #003366">name=</span><span style="color: #BA2121">"serviceDirectory"</span> <span style="color: #003366">ref=</span><span style="color: #BA2121">"serviceDirectory"</span><span style="color: #996699; font-weight: bold">/></span>
<span style="color: #996699; font-weight: bold"></bean></span>
</pre></div>
<p>As we planned on using Spring's <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/client/RestTemplate.html">RestTemplate</a> to invoke the remote service, we can convert the <code>java.net.URL</code> returned by the service lookup and translate them to <code>Strings</code> to be injected in the calling class.</p>
<div class="highlight"><pre> <span style="color: #996699; font-weight: bold"><bean</span> <span style="color: #003366">id=</span><span style="color: #BA2121">"recordServiceUrl"</span> <span style="color: #003366">class=</span><span style="color: #BA2121">"java.lang.String"</span><span style="color: #996699; font-weight: bold">></span>
<span style="color: #996699; font-weight: bold"><constructor-arg</span> <span style="color: #003366">value=</span><span style="color: #BA2121">"#{serviceUrl.toExternalForm()}/record"</span><span style="color: #996699; font-weight: bold">/></span>
<span style="color: #996699; font-weight: bold"></bean></span>
<span style="color: #996699; font-weight: bold"><bean</span> <span style="color: #003366">id=</span><span style="color: #BA2121">"orderServiceUrl"</span> <span style="color: #003366">class=</span><span style="color: #BA2121">"java.lang.String"</span><span style="color: #996699; font-weight: bold">></span>
<span style="color: #996699; font-weight: bold"><constructor-arg</span> <span style="color: #003366">value=</span><span style="color: #BA2121">"#{serviceUrl.toExternalForm()}/order"</span><span style="color: #996699; font-weight: bold">/></span>
<span style="color: #996699; font-weight: bold"></bean></span>
</pre></div>
<p>We create the expressions above using <code>#{...}</code>, which is similar to syntax used to resolve properies in Spring, <code>${...}</code>. Inside the expression, we can reference the <code>serviceUrl</code> bean defined earlier and call a method on it, <code>#{serviceUrl.toExternalForm()}</code>, which returns a <code>String</code> representation of the URL. From there we can add the remaining paths, <code>/record</code> and <code>/order</code>.</p>
<p>Given the following property signature of the class required to invoke the service:</p>
<div class="highlight"><pre> <span style="color: #996699; font-weight: bold">public</span> <span style="color: #996699; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">ServiceController</span> <span style="color: #666666">{</span>
<span style="color: #996699; font-weight: bold">private</span> RestTemplate restTemplate<span style="color: #666666">;</span>
<span style="color: #996699; font-weight: bold">private</span> String recordUrl<span style="color: #666666">;</span>
<span style="color: #996699; font-weight: bold">private</span> String orderUrl<span style="color: #666666">;</span>
<span style="color: #408080; font-style: italic">// ...</span>
<span style="color: #666666">}</span>
</pre></div>
<p>We can inject the URL string defined earlier:</p>
<div class="highlight"><pre> <span style="color: #996699; font-weight: bold"><bean</span> <span style="color: #003366">id=</span><span style="color: #BA2121">"serviceController"</span> <span style="color: #003366">class=</span><span style="color: #BA2121">"org.mycompany.service.business.ServiceController"</span><span style="color: #996699; font-weight: bold">></span>
<span style="color: #996699; font-weight: bold"><property</span> <span style="color: #003366">name=</span><span style="color: #BA2121">"restTemplate"</span> <span style="color: #003366">ref=</span><span style="color: #BA2121">"restTemplate"</span><span style="color: #996699; font-weight: bold">/></span>
<span style="color: #996699; font-weight: bold"><property</span> <span style="color: #003366">name=</span><span style="color: #BA2121">"recordUrl"</span> <span style="color: #003366">ref=</span><span style="color: #BA2121">"recordUrl"</span><span style="color: #996699; font-weight: bold">/></span>
<span style="color: #996699; font-weight: bold"><property</span> <span style="color: #003366">name=</span><span style="color: #BA2121">"orderUrl"</span> <span style="color: #003366">ref=</span><span style="color: #BA2121">"orderUrl"</span><span style="color: #996699; font-weight: bold">/></span>
<span style="color: #996699; font-weight: bold"></bean></span>
<span style="color: #996699; font-weight: bold"><bean</span> <span style="color: #003366">id=</span><span style="color: #BA2121">"restTemplate"</span> <span style="color: #003366">class=</span><span style="color: #BA2121">"org.springframework.web.client.RestTemplate"</span><span style="color: #996699; font-weight: bold">/></span>
</pre></div>
<p>Admittedly, this is a very simple example of what we can do with <b>SpEL</b>. The feature overview in the <a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/expressions.html">documentation</a> does a much better job of enumerating them than I could try to do. Hopefully, now that I've used in once, I'll look to <b>SpEL</b> again for other uses to help get out of some potentially tricky situations.</p>
</html>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com18tag:blogger.com,1999:blog-8779314896221625511.post-58968829183838351992012-03-10T13:45:00.004-08:002012-03-10T13:49:58.948-08:00Groovy: Regular Expressions and Multiple Lines<p>Regular expressions are sometimes used on text that spans multiple lines, which <a href="http://groovy.codehaus.org/">Groovy</a> has support for. I had to search for how to enable multi-line searching, so I thought it would be good to post here.</p><p>To allow the expression to span multiple lines we can add <b><code>(?ms)</code></b> to the beginning of the expression.</p><p>Take the following example, where want to grab the entire <code>book</code> element from a single-line XML document:</p><div class="highlight"><pre> def xml <span style="color: #666666">=</span> <span style="color: #BA2121">"<library><book><title>Effective Java</title><author>Bloch</author></book></library>"</span><br /> def matcher <span style="color: #666666">=</span> xml <span style="color: #666666">=~</span> <span style="color: #666666">/<</span>book<span style="color: #666666">>.*<</span><span style="border: 1px solid #FF0000">\</span><span style="color: #666666">/</span>book<span style="color: #666666">>/</span><br /> matcher<span style="color: #666666">.</span><span style="color: #7D9029">size</span><span style="color: #666666">()</span> <span style="color: #666666">></span> <span style="color: #666666">0</span> <span style="color: #666666">?</span> matcher<span style="color: #666666">[0]</span> <span style="color: #666666">:</span> <span style="color: #BA2121">"NOTHING"</span><br /></pre></div><p>This gives us an entire <code>book</code>:</p><p><pre><code> <book><title>Effective Java<title><author>Bloch<author><book></code></pre></p><p>If the XML was to span multiple lines however, we end up with <code>NOTHING</code>:</p><div class="highlight"><pre> def xml <span style="color: #666666">=</span> <span style="color: #BA2121">"""<library></span><br /><span style="color: #BA2121"> <book></span><br /><span style="color: #BA2121"> <title>Effective Java</title></span><br /><span style="color: #BA2121"> <author>Bloch</author></span><br /><span style="color: #BA2121"> </book></span><br /><span style="color: #BA2121"> </library>"""</span><br /> def matcher <span style="color: #666666">=</span> xml <span style="color: #666666">=~</span> <span style="color: #666666">/<</span>book<span style="color: #666666">>.*<</span><span style="border: 1px solid #FF0000">\</span><span style="color: #666666">/</span>book<span style="color: #666666">>/</span><br /> matcher<span style="color: #666666">.</span><span style="color: #7D9029">size</span><span style="color: #666666">()</span> <span style="color: #666666">></span> <span style="color: #666666">0</span> <span style="color: #666666">?</span> matcher<span style="color: #666666">[0]</span> <span style="color: #666666">:</span> <span style="color: #BA2121">"NOTHING"</span><br /></pre></div><p>Now if we add the <b><code>(?ms)</code></b>, we will still get an entire <code>book</code> entry:</p><div class="highlight"><pre> def xml <span style="color: #666666">=</span> <span style="color: #BA2121">"""<library></span><br /><span style="color: #BA2121"> <book></span><br /><span style="color: #BA2121"> <title>Effective Java</title></span><br /><span style="color: #BA2121"> <author>Bloch</author></span><br /><span style="color: #BA2121"> </book></span><br /><span style="color: #BA2121"> </library>"""</span><br /> def matcher <span style="color: #666666">=</span> xml <span style="color: #666666">=~</span> <span style="color: #666666">/(?</span>ms<span style="color: #666666">)<</span>book<span style="color: #666666">>.*<</span><span style="border: 1px solid #FF0000">\</span><span style="color: #666666">/</span>book<span style="color: #666666">>/</span><br /> matcher<span style="color: #666666">.</span><span style="color: #7D9029">size</span><span style="color: #666666">()</span> <span style="color: #666666">></span> <span style="color: #666666">0</span> <span style="color: #666666">?</span> matcher<span style="color: #666666">[0]</span> <span style="color: #666666">:</span> <span style="color: #BA2121">"NOTHING"</span><br /></pre></div><p>The above results in:</p><p><pre> <book><br /> <title>Effective Java<title><br /> <author>Bloch<author><br /> <book></pre></p><p>If you have any other tips or another way to accomplish the same thing, please feel free to leave a comment.</p>John Prystashhttp://www.blogger.com/profile/16707064951271867131noreply@blogger.com31