-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed.xml
More file actions
567 lines (409 loc) · 106 KB
/
feed.xml
File metadata and controls
567 lines (409 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Software 'n stuff</title>
<description>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.
</description>
<link>http://yourdomain.com/</link>
<atom:link href="http://yourdomain.com/feed.xml" rel="self" type="application/rss+xml"/>
<pubDate>Tue, 24 Feb 2015 19:13:41 +0000</pubDate>
<lastBuildDate>Tue, 24 Feb 2015 19:13:41 +0000</lastBuildDate>
<generator>Jekyll v2.5.3</generator>
<item>
<title>Welcome to Jekyll!</title>
<description><p>You’ll find this post in your <code>_posts</code> directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run <code>jekyll serve</code>, which launches a web server and auto-regenerates your site when a file is updated.</p>
<p>To add new posts, simply add a file in the <code>_posts</code> directory that follows the convention <code>YYYY-MM-DD-name-of-post.ext</code> and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.</p>
<p>Jekyll also offers powerful support for code snippets:</p>
<div class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">def</span> <span class="nf">print_hi</span><span class="p">(</span><span class="nb">name</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">&quot;Hi, </span><span class="si">#{</span><span class="nb">name</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="k">end</span>
<span class="n">print_hi</span><span class="p">(</span><span class="s1">&#39;Tom&#39;</span><span class="p">)</span>
<span class="c1">#=&gt; prints &#39;Hi, Tom&#39; to STDOUT.</span></code></pre></div>
<p>Check out the <a href="http://jekyllrb.com">Jekyll docs</a> for more info on how to get the most out of Jekyll. File all bugs/feature requests at <a href="https://github.com/jekyll/jekyll">Jekyll’s GitHub repo</a>. If you have questions, you can ask them on <a href="https://github.com/jekyll/jekyll-help">Jekyll’s dedicated Help repository</a>.</p>
</description>
<pubDate>Tue, 24 Feb 2015 15:00:13 +0000</pubDate>
<link>http://yourdomain.com/jekyll/update/2015/02/24/welcome-to-jekyll.html</link>
<guid isPermaLink="true">http://yourdomain.com/jekyll/update/2015/02/24/welcome-to-jekyll.html</guid>
<category>jekyll</category>
<category>update</category>
</item>
<item>
<title>Configuring logback via groovy in an SBT project</title>
<description><p>I wanted to use logback for my tests’ logging on an <code>SBT</code> project recently, and it took a little while to work out how best to do it. The good news is that it turns out to be simple enough (once you know what to do).</p>
<p>In your <code>build.sbt</code> add dependencies for Groovy and Logback Classic (I have restricted them just to ‘test’ scope as I only want this to apply to my tests)</p>
<div class="highlight"><pre><code class="language-scala" data-lang="scala"><span class="n">libraryDependencies</span> <span class="o">+=</span> <span class="s">&quot;ch.qos.logback&quot;</span> <span class="o">%</span> <span class="s">&quot;logback-classic&quot;</span> <span class="o">%</span> <span class="s">&quot;1.1.2&quot;</span> <span class="o">%</span> <span class="s">&quot;test&quot;</span>
<span class="n">libraryDependencies</span> <span class="o">+=</span> <span class="s">&quot;org.codehaus.groovy&quot;</span> <span class="o">%</span> <span class="s">&quot;groovy-all&quot;</span> <span class="o">%</span> <span class="s">&quot;2.4.1&quot;</span> <span class="o">%</span> <span class="s">&quot;test&quot;</span></code></pre></div>
<p>Then put your <code>logback.groovy</code> in the root of your <code>/test/resources</code> folder.</p>
<p>That’s it - logging during test execution should now be under the control of your <code>logback.groovy</code> config.</p>
</description>
<pubDate>Tue, 24 Feb 2015 12:04:00 +0000</pubDate>
<link>http://yourdomain.com/programming/2015/02/24/configuring-logback-via-groovy-in-an-sbt-project.html</link>
<guid isPermaLink="true">http://yourdomain.com/programming/2015/02/24/configuring-logback-via-groovy-in-an-sbt-project.html</guid>
<category>programming</category>
</item>
<item>
<title>Watermelon Reporting</title>
<description><p>“Watermelon reporting” is a lovely phrase I heard for the first time the other day. </p>
<p>Everything’s red in the centre of the organisation, gradually changing colour as you go up through the layers until it’s nice and bright green on the outside. Don’t worry boss, the reports are all looking good…</p>
<p><a href="http://blog.scrumphony.com/2011/02/watermelon-reporting/">http://blog.scrumphony.com/2011/02/watermelon-reporting/</a></p>
</description>
<pubDate>Wed, 05 Dec 2012 20:47:00 +0000</pubDate>
<link>http://yourdomain.com/programming/2012/12/05/watermelon-reporting.html</link>
<guid isPermaLink="true">http://yourdomain.com/programming/2012/12/05/watermelon-reporting.html</guid>
<category>programming</category>
</item>
<item>
<title>testing jsr303 annotated beans</title>
<description><p>I have been looking at using <a href="http://jcp.org/en/jsr/detail?id=303">jsr303</a> annotations to mark up some of my beans. The good news is that both <a href="http://www.hibernate.org">Hibernate</a> and <a href="http://www.springsource.org">Spring</a> <a href="http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html/validator-integration.html#validator-checkconstraints-orm-hibernateevent">play</a> <a href="http://static.springsource.org/spring/docs/3.1.2.RELEASE/spring-framework-reference/html/validation.html#validation-beanvalidation">nicely</a> with these annotations, and will validate them at appropriate times. The bad news is that the <a href="http://blog.m1key.me/2010/07/bean-validation-with-jee-6-jsr-303.html">examples</a> <a href="http://www.xinotes.org/notes/note/1734/">of</a> <a href="http://musingsofaprogrammingaddict.blogspot.co.uk/2009/02/using-bean-validation-with-spring.html">testing</a> these annotations that I have found online so far look cumbersome and awkward.</p>
<p>Let’s take a simple example:</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">simple java bean with a single jsr303 annotation</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SimpleBean</span> <span class="o">&#x7b;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nd">@NotNull</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">private</span> <span class="n">Long</span> <span class="n">id</span><span class="o">;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">public</span> <span class="nf">SimpleBean</span><span class="o">(</span><span class="n">Long</span> <span class="n">id</span><span class="o">,</span> <span class="n">String</span> <span class="n">name</span><span class="o">)</span> <span class="o">&#x7b;</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">this</span><span class="o">.</span><span class="na">id</span> <span class="o">=</span> <span class="n">id</span><span class="o">;</span> <span class="k">this</span><span class="o">.</span><span class="na">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">;</span>
</div></div><div data-line="8" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="o">&#x7d;</span>
</div></div><div data-line="9" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="c1">// getters and setters...</span>
</div></div><div data-line="10" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="o">&#x7d;</span></div></div></pre></div></figure>
<p>This is fine so far and we have the possibility that the constructor could be passed a null id. So, let’s write a unit test that ensures our validation will barf.</p>
<figure class="code-highlight-figure"><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SimpleBeanTest</span> <span class="o">&#x7b;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">private</span> <span class="n">Validator</span> <span class="n">validator</span><span class="o">;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nd">@Before</span>
</div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="o">&#x7b;</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">validator</span> <span class="o">=</span> <span class="n">Validation</span><span class="o">.</span><span class="na">buildDefaultValidatorFactory</span><span class="o">().</span><span class="na">getValidator</span><span class="o">();</span>
</div></div><div data-line="8" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="o">&#x7d;</span>
</div></div><div data-line="9" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="10" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nd">@Test</span>
</div></div><div data-line="11" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">nullIdMustThrowValidationError</span><span class="o">()</span> <span class="o">&#x7b;</span>
</div></div><div data-line="12" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">SimpleBean</span> <span class="n">bean</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">SimpleBean</span><span class="o">(</span><span class="kc">null</span><span class="o">,</span> <span class="s">&quot;valid string&quot;</span><span class="o">);</span>
</div></div><div data-line="13" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">Set</span><span class="o">&lt;</span><span class="n">ConstraintViolation</span><span class="o">&lt;</span><span class="n">SimpleBean</span><span class="o">&gt;&gt;</span> <span class="n">violations</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="na">validate</span><span class="o">(</span><span class="n">bean</span><span class="o">);</span>
</div></div><div data-line="14" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">assertThat</span><span class="o">(</span><span class="n">violations</span><span class="o">.</span><span class="na">size</span><span class="o">(),</span> <span class="n">equalTo</span><span class="o">(</span><span class="mi">1</span><span class="o">));</span>
</div></div><div data-line="15" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="o">&#x7d;</span>
</div></div><div data-line="16" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="o">&#x7d;</span></div></div></pre></div></figure>
<p>If this test fails, we get something like this as the result:</p>
<p><code>
java.lang.AssertionError:
Expected: &lt;1&gt;
but: was &lt;0&gt;
...
</code></p>
<p>Not so pretty. This is where <a href="https://github.com/hamcrest">hamcrest</a>’s matcher toolkit comes in handy. What if we could write our test like this instead?</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">unit test with custom validations matcher</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SimpleBeanTest</span> <span class="o">&#x7b;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nd">@Test</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">nullIdMustThrowValdationError</span><span class="o">()</span> <span class="o">&#x7b;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">SimpleBean</span> <span class="n">bean</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">SimpleBean</span><span class="o">(</span><span class="kc">null</span><span class="o">,</span> <span class="s">&quot;valid string&quot;</span><span class="o">);</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">assertThat</span><span class="o">(</span><span class="n">bean</span><span class="o">,</span> <span class="n">hasNoViolations</span><span class="o">());</span>
</div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="o">&#x7d;</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="o">&#x7d;</span></div></div></pre></div></figure>
<p>A matcher implementation could look like this</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">JSR303NoViolationsMatcher</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="kd">extends</span> <span class="n">TypeSafeMatcher</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="o">{</span>
<span class="kd">private</span> <span class="n">Validator</span> <span class="n">validator</span><span class="o">;</span>
<span class="kd">private</span> <span class="n">Set</span><span class="o">&lt;</span><span class="n">ConstraintViolation</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&gt;</span> <span class="n">violations</span><span class="o">;</span>
<span class="kd">public</span> <span class="nf">JSR303NoViolationsMatcher</span><span class="o">()</span> <span class="o">{</span>
<span class="n">validator</span> <span class="o">=</span> <span class="n">Validation</span><span class="o">.</span><span class="na">buildDefaultValidatorFactory</span><span class="o">().</span><span class="na">getValidator</span><span class="o">();</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">protected</span> <span class="kt">boolean</span> <span class="nf">matchesSafely</span><span class="o">(</span><span class="n">T</span> <span class="n">t</span><span class="o">)</span> <span class="o">{</span>
<span class="n">violations</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="na">validate</span><span class="o">(</span><span class="n">t</span><span class="o">);</span>
<span class="k">return</span> <span class="n">violations</span><span class="o">.</span><span class="na">size</span><span class="o">()</span> <span class="o">==</span> <span class="mi">0</span><span class="o">;</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">describeTo</span><span class="o">(</span><span class="n">Description</span> <span class="n">description</span><span class="o">)</span> <span class="o">{</span>
<span class="n">description</span><span class="o">.</span><span class="na">appendText</span><span class="o">(</span><span class="s">&quot;no jsr303 validation violations&quot;</span><span class="o">);</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">protected</span> <span class="kt">void</span> <span class="nf">describeMismatchSafely</span><span class="o">(</span><span class="n">Object</span> <span class="n">item</span><span class="o">,</span> <span class="n">Description</span> <span class="n">mismatchDescription</span><span class="o">)</span> <span class="o">{</span>
<span class="n">mismatchDescription</span><span class="o">.</span><span class="na">appendText</span><span class="o">(</span><span class="s">&quot;was &quot;</span><span class="o">).</span><span class="na">appendValue</span><span class="o">(</span><span class="n">violations</span><span class="o">);</span>
<span class="o">}</span>
<span class="nd">@Factory</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="n">JSR303NoViolationsMatcher</span> <span class="nf">hasNoViolations</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nf">JSR303NoViolationsMatcher</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span></code></pre></div>
<p>And our new-look test spits out the following upon failure:</p>
<p><code>
java.lang.AssertionError:
Expected: no jsr303 validation violations
but: was &lt;[ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=id, rootBeanClass=class com.example.SimpleBean, messageTemplate='{javax.validation.constraints.NotNull.message}'}]&gt;
</code></p>
<p>There. Much nicer. Not only do we know that the test failed, but we are immediately told how it failed.</p>
<p>Thanks to <a href="http://www.platnetgeek.ch">planetgeek</a> for a <a href="http://www.planetgeek.ch/2012/03/07/create-your-own-matcher/">nice article about writing custom hamcrest matchers</a>.</p>
</description>
<pubDate>Thu, 12 Jul 2012 09:04:00 +0100</pubDate>
<link>http://yourdomain.com/programming/unit-testing/jsr303/2012/07/12/testing-jsr303-annotated-beans.html</link>
<guid isPermaLink="true">http://yourdomain.com/programming/unit-testing/jsr303/2012/07/12/testing-jsr303-annotated-beans.html</guid>
<category>programming</category>
<category>unit-testing</category>
<category>jsr303</category>
</item>
<item>
<title>Using typesafe Role enums with Spring Security</title>
<description><p><a href="http://www.springsource.org/spring-security">Spring Security</a> is a seriously useful tool but one of the things that has been nagging away at the back of my mind about it is that it is so heavily reliant on magic strings. Take a typical example:</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">spring security config fragment</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;http&gt;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;intercept-url</span> <span class="na">pattern=</span><span class="s">&quot;/**&quot;</span> <span class="na">access=</span><span class="s">&quot;ROLE_USER&quot;</span><span class="nt">/&gt;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> ...
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;/http&gt;</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;global-method-security</span> <span class="na">pre-post-annotations=</span><span class="s">&quot;enabled&quot;</span><span class="nt">/&gt;</span></div></div></pre></div></figure>
<p>This configures all URLs in the application to require that the user is logged in and has the privilege named <code>ROLE_USER</code>.</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">sensitive method</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nd">@PreAuthorize</span><span class="o">(</span><span class="s">&quot;hasRole(&#39;ROLE_SYSADMIN&#39;)&quot;</span><span class="o">)</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kd">public</span> <span class="n">String</span> <span class="nf">getSensitiveInformation</span><span class="o">()</span> <span class="o">&#x7b;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span> <span class="s">&quot;Only special people are allowed to see this information&quot;</span><span class="o">;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="o">&#x7d;</span></div></div></pre></div></figure>
<p>This ensures that anyone calling this method (either directly or indirectly) must be logged in and have the privilege named <code>ROLE_SYSADMIN</code>.</p>
<p>So far, so straightforward.</p>
<p>The problem is that these role names proliferate across the codebase. Chances are you will want to refer to the same role name in many different places in your application. If these role names were in code, you would typically refactor them out into a single representation as soon as you had two different references to them in the codebase. Unfortunately, this is not so simple with Spring Security.</p>
<p>The good news is that it is possible.</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">Authority enum</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kn">package</span> <span class="n">com</span><span class="o">.</span><span class="na">example</span><span class="o">;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kd">public</span> <span class="kd">enum</span> <span class="n">Authority</span> <span class="o">&#x7b;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">USER</span><span class="o">,</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">SYSADMIN</span>
</div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="o">&#x7d;</span></div></div></pre></div></figure>
<p>Which can be referred to in Spring’s expression language like this:</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">spring security config using role enum</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;http</span> <span class="na">use-expressions=</span><span class="s">&quot;true&quot;</span><span class="nt">&gt;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;intercept-url</span> <span class="na">pattern=</span><span class="s">&quot;/**&quot;</span> <span class="na">access=</span><span class="s">&quot;hasRole(T(com.example.Authority).USER.toString())&quot;</span><span class="nt">&gt;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> ...
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;/http&gt;</span></div></div></pre></div></figure>
<p>And similarly on method annotations</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">sensitive method with typesafe role enum</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nd">@PreAuthorize</span><span class="o">(</span><span class="s">&quot;hasRole(T(com.example.Authority).SYSADMIN.toString())&quot;</span><span class="o">)</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kd">public</span> <span class="n">String</span> <span class="nf">getSensitiveInformation</span><span class="o">()</span> <span class="o">&#x7b;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span> <span class="s">&quot;Still only special people are allowed to see this information&quot;</span><span class="o">;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="o">&#x7d;</span></div></div></pre></div></figure>
<p>Unfortunately IntelliJ Community Edition’s ‘Find Usages’ is not clever enough to return these Spring expression language references, but it does at least feel better than sprinkling identical magic strings across the codebase.</p>
</description>
<pubDate>Fri, 06 Jul 2012 11:54:00 +0100</pubDate>
<link>http://yourdomain.com/programming/spring/2012/07/06/using-typesafe-role-enums-with-spring-security.html</link>
<guid isPermaLink="true">http://yourdomain.com/programming/spring/2012/07/06/using-typesafe-role-enums-with-spring-security.html</guid>
<category>programming</category>
<category>spring</category>
</item>
<item>
<title>inline editing forms with javascript</title>
<description><p>I have picked up some contracting work to build a new web site. It is very early days and I am just putting together some initial workable web forms at the moment and want something for editing information that would be a little less ugly than just presenting a standard set of populated html form controls. Instead, I like the idea that the information will be written to the page as normal text, but clicking on it will allow you to change the value inline. After a little poking about, there appear to be two main contenders to help implement this in <a href="http://jquery.com">JQuery</a>, <a href="http://www.appelsiini.net/projects/jeditable">Jeditable</a> and <a href="http://www.arashkarimzadeh.com/component/content/article/7.html">Editable</a>.</p>
<table class="feature-comparison">
<caption>Feature comparison of Jeditable and Editable JQuery plugins</caption>
<thead>
<tr><td></td><th scope="col">Jeditable</th><th scope="col">Editable</th></tr>
</thead>
<tbody>
<tr><th scope="row">html 5 form elements</th><td>no</td><td>no</td></tr>
<tr><th scope="row">extendable input types</th><td>yes,<br />via inbuilt <code>addInputType</code> method</td><td>yes, <br />by modifying the source directly</td></tr>
<tr><th scope="row">default POST data format</th><td><code>id:element_id value:element_value</code></td><td>none</td></tr>
</tbody>
</table>
<h2 id="adding-html5-input-types">Adding html5 input types</h2>
<h3 id="editable">Editable</h3>
<p>Editable does at least provide an <code>editableFactory</code> in its source which is intended to be extended. Although easy to do, it still doesn’t get around the fundamental problem of having to modify the source itself.</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">modifying .editableFactory directly to handle 'url' input types</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nx">$</span><span class="p">.</span><span class="nx">editableFactory</span> <span class="o">=</span> <span class="p">&#x7b;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="s1">&#39;text&#39;</span><span class="o">:</span> <span class="p">&#x7b;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">toEditable</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$this</span><span class="p">,</span><span class="nx">options</span><span class="p">)&#x7b;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;&lt;input/&gt;&#39;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="nx">$this</span><span class="p">)</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">.</span><span class="nx">val</span><span class="p">(</span><span class="nx">$this</span><span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="s1">&#39;editable.current&#39;</span><span class="p">));</span>
</div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;,</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">getValue</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$this</span><span class="p">,</span><span class="nx">options</span><span class="p">)&#x7b;</span>
</div></div><div data-line="8" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span> <span class="nx">$this</span><span class="p">.</span><span class="nx">children</span><span class="p">().</span><span class="nx">val</span><span class="p">();</span>
</div></div><div data-line="9" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;</span>
</div></div><div data-line="10" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;,</span>
</div></div><div data-line="11" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="c1">// adding custom support for &lt;input type=&quot;url&quot;&gt;</span>
</div></div><div data-line="12" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="s1">&#39;url&#39;</span><span class="o">:</span> <span class="p">&#x7b;</span>
</div></div><div data-line="13" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">toEditable</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$this</span><span class="p">,</span><span class="nx">options</span><span class="p">)&#x7b;</span>
</div></div><div data-line="14" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;&lt;input type=&quot;url&quot;/&gt;&#39;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="nx">$this</span><span class="p">)</span>
</div></div><div data-line="15" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">.</span><span class="nx">val</span><span class="p">(</span><span class="nx">$this</span><span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="s1">&#39;editable.current&#39;</span><span class="p">));</span>
</div></div><div data-line="16" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;,</span>
</div></div><div data-line="17" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">getValue</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$this</span><span class="p">,</span><span class="nx">options</span><span class="p">)&#x7b;</span>
</div></div><div data-line="18" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span> <span class="nx">$this</span><span class="p">.</span><span class="nx">children</span><span class="p">().</span><span class="nx">val</span><span class="p">();</span>
</div></div><div data-line="19" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;</span>
</div></div><div data-line="20" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;,</span>
</div></div><div data-line="21" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="c1">// end of added block</span>
</div></div><div data-line="22" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">...</span></div></div></pre></div></figure>
<h3 id="jeditable">Jeditable</h3>
<p>There really is not much in it in terms of complexity or verboseness between the two implementations, but the fact that Jeditable allows extensions without modifying its source swings it for me.</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">Extending Jeditable's set of supported input types</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">.</span><span class="nx">editable</span><span class="p">.</span><span class="nx">addInputType</span><span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="p">&#x7b;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">element</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">settings</span><span class="p">,</span> <span class="nx">original</span><span class="p">)</span> <span class="p">&#x7b;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">var</span> <span class="nx">input</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;&lt;input type=&quot;url&quot;&gt;&#39;</span><span class="p">);</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="nx">input</span><span class="p">);</span>
</div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span><span class="p">(</span><span class="nx">input</span><span class="p">);</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;</span>
</div></div><div data-line="8" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;);</span>
</div></div><div data-line="9" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;/script&gt;</span></div></div></pre></div></figure>
<h2 id="posting-changes-back-to-the-server">Posting changes back to the server</h2>
<p>Both of the following examples work with the following template. You will also notice that this is not plain html, but a velocity template. The template is served from a spring/velocity application, more details of which will follow in later posts.</p>
<figure class="code-highlight-figure"><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line">#springBind(&quot;myDomainObject.url&quot;)
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;span</span> <span class="na">class=</span><span class="s">&quot;editableURL&quot;</span><span class="nt">&gt;</span>$status.value<span class="nt">&lt;/span&gt;</span></div></div></pre></div></figure>
<h3 id="editable-1">Editable</h3>
<p>Although Editable doesn’t support this out of the box, it is pretty straightforward.</p>
<figure class="code-highlight-figure"><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">&#x7b;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.editableURL&#39;</span><span class="p">).</span><span class="nx">editable</span><span class="p">(&#x7b;</span><span class="nx">type</span><span class="o">:</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="nx">submit</span><span class="o">:</span><span class="s1">&#39;save&#39;</span><span class="p">,</span> <span class="nx">cancel</span><span class="o">:</span><span class="s1">&#39;cancel&#39;</span><span class="p">,</span> <span class="nx">onSubmit</span><span class="o">:</span><span class="nx">end</span><span class="p">&#x7d;);</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;);</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">function</span> <span class="nx">end</span><span class="p">(</span><span class="nx">content</span><span class="p">)</span> <span class="p">&#x7b;</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">href</span><span class="p">,</span> <span class="p">&#x7b;</span><span class="nx">url</span><span class="o">:</span> <span class="nx">content</span><span class="p">.</span><span class="nx">current</span><span class="p">&#x7d;);</span>
</div></div><div data-line="8" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;</span>
</div></div><div data-line="9" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;/script&gt;</span></div></div></pre></div></figure>
<p>So we wait for the document to be loaded by the browser, then decorate all elements with the “editableURL” class with Editable’s behaviour. The <code>end</code> function is triggered when you submit your change, and is responsible for POSTing the data back to the server. Note that, in this example, data is POSTed back to the same URL from which the page was served.</p>
<p>All in all, this is rather nice, and we have complete control over the data passed back to our server.</p>
<h3 id="jeditable-1">Jeditable</h3>
<p>As mentioned in the comparison table above, Jeditable by default will POST data in the format <code>id: element_id, value: element_value</code>. This is fine for table-based data in which every field has an id, but a long way from OK for our domain-object-backed Spring application. The good news is that we get complete control over Jeditable’s POST behaviour, too.</p>
<figure class="code-highlight-figure"><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">&#x7b;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.editableURL&#39;</span><span class="p">).</span><span class="nx">editable</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">settings</span><span class="p">)</span> <span class="p">&#x7b;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">href</span><span class="p">,</span> <span class="p">&#x7b;</span><span class="nx">url</span><span class="o">:</span> <span class="nx">value</span><span class="p">&#x7d;);</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
</div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;,</span> <span class="p">&#x7b;</span><span class="nx">submit</span><span class="o">:</span> <span class="s1">&#39;save&#39;</span><span class="p">,</span> <span class="nx">type</span><span class="o">:</span> <span class="s1">&#39;url&#39;</span><span class="p">&#x7d;);</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;);</span>
</div></div><div data-line="8" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;/script&gt;</span></div></div></pre></div></figure>
<p>This snippet works broadly the same way, with the added control that the return value of the function is what is used as the new value displayed on the page. I can’t make up my mind whether this is a good thing. As the script stands, if the server encounters an error updating the field, then the new value will be displayed on the page and the user will be none the wiser that anything went wrong.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Although <a href="http://stackoverflow.com/questions/708801/whats-the-best-edit-in-place-plugin-for-jquery">Editable is arguably preferred by Stack Overflow users</a>, there is a little snag with both of them. Neither library seems to handle keyboard navigation particularly well (at least in Chrome on OS X). Editable’s submit and cancel buttons do nothing when they are triggered by receiving focus and hitting the spacebar, whereas Jeditable’s support is better in that the buttons do work, just that you have to be quick about it. If you leave the focus on the button for more than a second or so then it assumes you have given up and cancels the edit, taking away the form control again.</p>
<p>All in all, Jeditable seems to suit my preferences a little better. Its <a href="https://github.com/tuupola/jquery_jeditable">public revision control</a> and nicer web site also make it seem just that little bit more polished. The only negative point, and it is a small one, is that Jeditable doesn’t provide a labelled version number. Judging by the <a href="https://github.com/tuupola/jquery_jeditable#readme">readme on github</a> it is up to version 1.7.2 at the time of writing, so that is the number I inserted into the filename myself before using it.</p>
<figure class="code-highlight-figure"><figcaption class="code-highlight-caption"><span class="code-highlight-caption-title">A complete example of using Jeditable</span></figcaption><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="cp">&lt;!DOCTYPE html&gt;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;html&gt;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;head&gt;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;title&gt;</span>Inline editing forms with JQuery and Jeditable<span class="nt">&lt;/title&gt;</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js&quot;</span> <span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;&lt;/script&gt;</span>
</div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&quot;/&#x7b;your_path_here&#x7d;/jquery.jeditable-1.7.2.mini.js&quot;</span> <span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;&lt;/script&gt;</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;/head&gt;</span>
</div></div><div data-line="8" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;body&gt;</span>
</div></div><div data-line="9" class="code-highlight-row numbered"><div class="code-highlight-line"> #springBind(&quot;myDomainObject.url&quot;)
</div></div><div data-line="10" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;span</span> <span class="na">class=</span><span class="s">&quot;editableURL&quot;</span><span class="nt">&gt;</span>$status.value<span class="nt">&lt;/span&gt;</span>
</div></div><div data-line="11" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="12" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;</span>
</div></div><div data-line="13" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">.</span><span class="nx">editable</span><span class="p">.</span><span class="nx">addInputType</span><span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="p">&#x7b;</span>
</div></div><div data-line="14" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">element</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">settings</span><span class="p">,</span> <span class="nx">original</span><span class="p">)</span> <span class="p">&#x7b;</span>
</div></div><div data-line="15" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="kd">var</span> <span class="nx">input</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;&lt;input type=&quot;url&quot;&gt;&#39;</span><span class="p">);</span>
</div></div><div data-line="16" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="nx">input</span><span class="p">);</span>
</div></div><div data-line="17" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span><span class="p">(</span><span class="nx">input</span><span class="p">);</span>
</div></div><div data-line="18" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;</span>
</div></div><div data-line="19" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;);</span>
</div></div><div data-line="20" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;/script&gt;</span>
</div></div><div data-line="21" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="22" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;</span>
</div></div><div data-line="23" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">&#x7b;</span>
</div></div><div data-line="24" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.editableURL&#39;</span><span class="p">).</span><span class="nx">editable</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">settings</span><span class="p">)</span> <span class="p">&#x7b;</span>
</div></div><div data-line="25" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nx">$</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">href</span><span class="p">,</span> <span class="p">&#x7b;</span><span class="nx">url</span><span class="o">:</span> <span class="nx">value</span><span class="p">&#x7d;);</span>
</div></div><div data-line="26" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
</div></div><div data-line="27" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;,</span> <span class="p">&#x7b;</span><span class="nx">submit</span><span class="o">:</span> <span class="s1">&#39;save&#39;</span><span class="p">,</span> <span class="nx">type</span><span class="o">:</span> <span class="s1">&#39;url&#39;</span><span class="p">&#x7d;);</span>
</div></div><div data-line="28" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="p">&#x7d;);</span>
</div></div><div data-line="29" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;/script&gt;</span>
</div></div><div data-line="30" class="code-highlight-row numbered"><div class="code-highlight-line"> </div></div><div data-line="31" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="nt">&lt;/body&gt;</span>
</div></div><div data-line="32" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nt">&lt;/html&gt;</span></div></div></pre></div></figure>
</description>
<pubDate>Tue, 19 Jun 2012 15:09:00 +0100</pubDate>
<link>http://yourdomain.com/programming/javascript/2012/06/19/inline-editing-forms-with-javascript.html</link>
<guid isPermaLink="true">http://yourdomain.com/programming/javascript/2012/06/19/inline-editing-forms-with-javascript.html</guid>
<category>programming</category>
<category>javascript</category>
</item>
<item>
<title>How to change a headlamp bulb on an Alfa Romeo GTV</title>
<description><p>The dipped headlamp bulb blew on my Alfa Romeo GTV the other day and I spent way too long today working out how to replace it. What’s more, there didn’t seem to be clear instructions that I could find on the net, so I thought the decent thing to do would be to write it up here.</p>
<!-- more -->
<p>First off, there is <a href="http://www.alfaowner.com/Forum/alfa-gtv-and-916-spider/234333-headlight-bulbs-gtv.html">confusion about correct part numbers</a>. Several newsgroup posts say that Halfords’ part numbers are mixed up between full and dipped bulbs. Even my local Alfa dealership sold me the wrong one. I discovered that Halfords’ paper part finder in their store was correct, yet <a href="http://www.halfords.com/webapp/wcs/stores/servlet/GenericProductGuidedSalesCmd?action=getProducts&amp;storeId=10001&amp;categoryId=165538&amp;catalogId=10151&amp;langId=-1&amp;vrn=&amp;rememberMe=1&amp;storeId=10001&amp;catalogId=10151&amp;categoryId=165538&amp;langId=-1&amp;engineInd=0&amp;makeName=10&amp;modelName=1006&amp;dvlaYear=38&amp;subset=1006050&amp;findPartsMMYS=Find+parts">their systems were wrong</a>. Either way, you need bulb number 9005 for the dipped headlamp.</p>
<p><img src="/images/dipped_headlamp_bulb.jpg" alt="dipped headlamp bulb showing part number" /></p>
<p>Once you have a replacement bulb, you’ll need to remove the plastic cover over the side of the engine bay</p>
<p><img src="/images/engine_bay_cover.jpg" alt="engine bay cover" /></p>
<p>There are four large cross-head screws holding it in place</p>
<p><img src="/images/removing_cover_screws.jpg" alt="dipped removing first screw from cover" /></p>
<p>Once this cover is removed, the rear of the headlight assembly should be as accessible as it’s going to get</p>
<figure>
<img src="/images/rear_headlight_assembly.jpg" alt="the rear of the headlamp cluster" />
<figcaption>(sorry, the pictures are a little out of sequence - the bulb has already been removed in this shot)</figcaption>
</figure>
<p>Then remove the clip holding the cable on to the back of the bulb. There are two little prongs that hold the clip in place - they should lift easily then the connector pulls off.</p>
<p><img src="/images/rear_dipped_headlamp_bulb.jpg" alt="in-situ bulb closeup" /></p>
<p>Turn the bulb about 45 degrees anti-clockwise and then it should slide out without fuss.</p>
<p>Put your new bulb in, twisting it into place so the connector is facing downwards as the last picture above shows, carefully avoiding putting your fingers on the bulb itself. Do make sure to test that the new bulb is working, then reverse the steps above and you should be good to go.</p>
<p>I hope this helps.</p>
</description>
<pubDate>Sat, 04 Feb 2012 15:56:00 +0000</pubDate>
<link>http://yourdomain.com/alfa%20romeo/car%20repair/2012/02/04/how-to-change-a-headlamp-bulb-on-an-alfa-romeo-gtv.html</link>
<guid isPermaLink="true">http://yourdomain.com/alfa%20romeo/car%20repair/2012/02/04/how-to-change-a-headlamp-bulb-on-an-alfa-romeo-gtv.html</guid>
<category>alfa romeo</category>
<category>car repair</category>
</item>
<item>
<title>Why are all the good ideas already taken?</title>
<description><p>The other evening on the way home from work, I had a brilliant idea. It was one of those that had been churning away in the back of my mind for a while but suddenly popped out, fully formed, in a way that hadn’t occurred to me before. This was it, this was going to be the interesting side project that had real traction.</p>
<p>I had been <a href="http://www.codinghorror.com/blog/2011/04/working-with-the-chaos-monkey.html">reading</a> again about Netflix’s <a href="http://techblog.netflix.com/2010/12/5-lessons-weve-learned-using-aws.html">Chaos Monkey</a>. It is a brilliant idea - in order to be good at something, you need to do it often, and therefore the best way of handling failure is to fail often. The other half of the equation was an approach to code-reviewing unit tests that I have been using for a while. If you can delete a line in the class under test, and the unit test still passes, then you didn’t need that line in the first place (although unfortunately it is usually the other way around - that more scenarios need to be added to the test). What if you could combine the two ideas? What if you had a system that would, as part of the build, randomly modify your source files and verify that your tests failed? That would be brilliant. It would be a really useful indicator of how good your tests actually are (and, for good measure, fully automate the net output of some of the less useful developers I have worked with over the years!).</p>
<p>The first thing I needed was something that understands Java syntax. More than that, I wanted to make use of a tool that is already capable of modifying Java source files. Understanding syntax was important because my first use case was going to be finding a magic number and incrementing it by one. Let’s look at an example method:</p>
<figure class="code-highlight-figure"><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kd">public</span> <span class="n">String</span> <span class="nf">noddyExample</span><span class="o">()</span> <span class="o">&#x7b;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">String</span> <span class="n">dontDoThisAtHome</span> <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="o">;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">5</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">&#x7b;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">dontDoThisAtHome</span> <span class="o">+=</span> <span class="n">i</span><span class="o">;</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="o">&#x7d;</span>
</div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span> <span class="s">&quot;value: &quot;</span> <span class="o">+</span> <span class="n">dontDoThisAtHome</span><span class="o">;</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="o">&#x7d;</span></div></div></pre></div></figure>
<p>and its associated test:</p>
<figure class="code-highlight-figure"><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="nd">@Test</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kd">public</span> <span class="kt">void</span> <span class="nf">noddyValueIsCorrect</span><span class="o">()</span> <span class="o">&#x7b;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">assertThat</span><span class="o">(</span><span class="n">noddyExample</span><span class="o">(),</span> <span class="n">startsWith</span><span class="o">(</span><span class="s">&quot;value&quot;</span><span class="o">));</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="o">&#x7d;</span></div></div></pre></div></figure>
<p>So far, so trivial. It is a rather contrived example, but we have all seen more complicated examples of effectively the same issue in production code. The essential point is that we are all happy. The source compiles, it passes its tests, and the system behaves as expected. It is worth pointing out that a traditional test code coverage metric is no good here because the loop did execute when the test ran, so is considered to be covered. So, we run our test chaos monkey on our source code and let it increment a magic number by one.</p>
<figure class="code-highlight-figure"><div class="code-highlight"><pre class="code-highlight-pre"><div data-line="1" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="kd">public</span> <span class="n">String</span> <span class="nf">noddyExample</span><span class="o">()</span> <span class="o">&#x7b;</span>
</div></div><div data-line="2" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">String</span> <span class="n">dontDoThisAtHome</span> <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="o">;</span>
</div></div><div data-line="3" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">6</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">&#x7b;</span>
</div></div><div data-line="4" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="n">dontDoThisAtHome</span> <span class="o">+=</span> <span class="n">i</span><span class="o">;</span>
</div></div><div data-line="5" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="o">&#x7d;</span>
</div></div><div data-line="6" class="code-highlight-row numbered"><div class="code-highlight-line"> <span class="k">return</span> <span class="s">&quot;value: &quot;</span> <span class="o">+</span> <span class="n">dontDoThisAtHome</span><span class="o">;</span>
</div></div><div data-line="7" class="code-highlight-row numbered"><div class="code-highlight-line"><span class="o">&#x7d;</span></div></div></pre></div></figure>
<p>If we re-run our test now, we find that the test still passes. We have changed our source file in a meaningful way and yet our tests have not proven good enough to catch the changed behaviour. This, ladies and gentlemen, is what we in the trade call A Bad Thing™.</p>
<p>Also, this is where I get to the point. Five minutes of googling later I find out that, of course, this is already a known and understood way of testing. It is called <a href="http://en.wikipedia.org/wiki/Mutation_testing">Mutation Testing</a> and people smarter than me have been doing it for years. Then, like all these sorts of questions, once you know the name for it all sorts of useful information comes flooding out of search engines. There is a stackoverflow question on <a href="http://stackoverflow.com/questions/4916801/java-mutation-testing-integration-with-maven">how to integrate Java mutation testing with Maven</a> and an answer directing me to <a href="http://pitest.org/">PIT</a>, an open source mutation testing tool. The question itself lists a bunch of other Java mutation testing tools; one of which, <a href="http://jester.sourceforge.net/">Jester</a>, has been the subject of an <a href="http://www.ibm.com/developerworks/library/j-jester/">IBM Developer Works article</a> and a rather nice quote from <a href="http://www.objectmentor.com/omTeam/martin_r.html">Robert C. Martin</a> on how it guided him to a <a href="http://groups.yahoo.com/group/extremeprogramming/message/32277">more simple implementation</a> of a <a href="http://www.objectmentor.com/resources/articles/xpepisode.htm">test-driven coding exercise</a>. So this really is not a new, cute, extension to our now traditional unit test safety net, but just me independently stumbling upon an idea that is apparently an already solved problem. That is great news from the point of view of getting this idea quickly in to practice in the day job, but just doesn’t feel as much fun as if it were my own idea.</p>
<p>So why are all the good ideas already taken? People smarter than you or I thought of them years ago.</p>
</description>
<pubDate>Tue, 09 Aug 2011 20:06:00 +0100</pubDate>
<link>http://yourdomain.com/unit%20testing/programming/2011/08/09/why-are-all-the-good-ideas-already-taken.html</link>
<guid isPermaLink="true">http://yourdomain.com/unit%20testing/programming/2011/08/09/why-are-all-the-good-ideas-already-taken.html</guid>
<category>unit testing</category>
<category>programming</category>
</item>
<item>
<title>HDD v SSD compiling benchmarks</title>
<description><p>If there ever was a subject that caused more unfounded claims and counter-claims, it’s performance benchmarking. Given the dearth of useful info I’ve been able to find about whether SSDs improve compilation time or not, I thought I would put up the numbers that I have found during a small test I did today.</p>
<p>Program to be compiled:</p>
<ul>
<li>Java webapp of ~10,000 executable lines of code</li>
<li>Maven 3</li>
<li>Java 1.6</li>
</ul>
<p>New system:</p>
<ul>
<li>HP Compaq 8100 Elite Convertible Minitower PC</li>
<li>Intel Core i7</li>
<li>6GB ram</li>
<li>64-bit Windows 7</li>
<li>OCZ Vertex SATA SSD 60GB</li>
<li>Barracuda 7200.12 SATA 3Gb/s 500GB HDD</li>
</ul>
<p>Old system:</p>
<ul>
<li>Dell Dimension 9200</li>
<li>Intel Core 2 6300</li>
<li>4GB ram</li>
<li>SATA 260GB HDD</li>
</ul>
<h2 id="benchmark-results">Benchmark Results</h2>
<p>All times are as reported by Maven, not of the entire duration of the shell command.
Test command: <code>mvn clean install</code></p>
<p><img src="/images/ssd_benchmark_graph.png" alt="benchmark graph" /></p>
<p>So, across the three build runs I performed on each environment, the timings are pleasingly consistent. The good news is that my new PC significantly outperforms my old one. The somewhat surprising news is that the HDD is consistently marginally quicker than the SSD. This suggests that this maven goal is not bound by disk I/O, that it is something else about my new system’s spec that gives such a performance boost. This actually reinforces what Joel Spolsky found when he <a href="http://www.joelonsoftware.com/items/2009/03/27.html">ran a similar test</a> a couple of years ago, and also one of the golden rules about working on performance - your assumptions will be wrong so measurement is king. Amen.</p>
</description>
<pubDate>Fri, 01 Apr 2011 12:15:00 +0100</pubDate>
<link>http://yourdomain.com/ssd/performance%20testing/programming/2011/04/01/hdd-v-ssd-compiling-benchmarks.html</link>
<guid isPermaLink="true">http://yourdomain.com/ssd/performance%20testing/programming/2011/04/01/hdd-v-ssd-compiling-benchmarks.html</guid>
<category>ssd</category>
<category>performance testing</category>
<category>programming</category>
</item>
<item>
<title>Reading Good to Great by Jim Collins</title>
<description><p>This book was recommended to me over a year ago but I only now have found the time to get myself a copy and read it.</p>
<p>Good to Great claims to be a fact-based investigation into what factors made a previously average-performing company change into one that significantly outperformed both the market and its competitors. I have only read the first fifty pages of the book so far, so can’t claim to either agree or disagree with just how well the evidence supports the book’s claims, but I think there are a couple of points that are worth highlighting based upon what I have read so far.</p>
<p>Firstly, of the eleven companies that meet that author’s criteria for having transformed from “good” to “great”, <a href="http://www.fanniemae.com/">Fannie Mae</a> and Gillette are the only two I have heard of. This isn’t good news as we all know <a href="http://www.guardian.co.uk/business/2008/sep/07/freddiemacfanniemae">how well Fannie Mae turned out</a>. Given the book was written in 2001 and Fannie Mae’s spectacular collapse happened in 2008, it’s easy to snipe with the benefit of hindsight, but it can’t help tempering my view of just how “great” these selected companies really are. Jim Collins’s approach appears to have been to measure greatness by the value of a company’s stock returns, and I agree that a data-based metric of some kind is needed to make this comparative study worthwhile, but it is worth bearing in mind that there is much, much more to how great a company is than its performance on the stock market.</p>
<p>Secondly, one of the conclusions drawn is that they found no correlation between executive renumeration and company performance. That’s including performance and target-related incentives which is one of my own bug-bears. I have always found it mildly insulting to be offered a target-related bonus. They always seem to be used as a way modifying behaviour and, I think, entirely miss the point. Yes, money is the reason why we turn up for work but it’s my own job satisfaction that is the single most important goal once I’m there. Bonuses don’t typically make the difference between an income you’re happy with and one you’re not (would 10-15% of your salary really make that much difference, given that you can’t budget for it?). A bonus attached to a specific goal really doesn’t make me want to achieve that goal any more than I did already and, if that goal is in conflict with my perception of job satisfaction (doing the right thing for the job at hand), then there is something wrong here already. Either my understanding of what doing a good job looks like is wrong, or the incentivised target is wrong. Just setting the incentive doesn’t fix this - if I thought achieving it was part of doing a good job, then I would be doing everything I could to make it happen out of a need for job satisfaction alone. I have thought for a long time now that if you can pay people well to do a good job, then that’s exactly what you should do. You can then reasonably expect people to do a good job for you based upon mutual respect. It’s this mutual respect that I think is eroded by performance-based bonuses.</p>
<p>Given this is from the first fifty pages alone, there’s evidently quite a lot to digest in this one book. I’ll undoubtedly write more as I get through the book.</p>
<div class="book"><span class="author">Collins, Jim</span> <span class="title">Good to great: why some companies make the leap... and others don't</span> <span class="publisher">Random House Business Books</span> <span class="year">2001</span> <span class="isbn">ISBN 9780712676090</span></div>
</description>
<pubDate>Thu, 24 Feb 2011 08:36:00 +0000</pubDate>
<link>http://yourdomain.com/reading/2011/02/24/reading-good-to-great-by-jim-collins.html</link>
<guid isPermaLink="true">http://yourdomain.com/reading/2011/02/24/reading-good-to-great-by-jim-collins.html</guid>
<category>reading</category>
</item>
</channel>
</rss>