Wednesday, 26 August 2015

Updates to Java For Testers ebook and Notes on File Path construction in Java

Print Book Proof Review

When I released the e-book version of Java For Testers, I did not immediately release it as a print book. I needed it to settle and take comments from readers first.
Last week I started on the path of creating the print version. I used leanpub to create a print ready copy and for the last few days I’ve been reviewing Java For Testers as a print ready proof. As a result I’ve made some text and code changes.
The new version has minor text and formatting changes throughout. Little things like making some class names look like code in the text. Removing a few words hear and there, adding some words hear and there. Nothing substantial for most of the chapters.
The main block of work was around the File chapter, where I rewrote all the code to avoid creating custom paths.

Use File and Paths.get and Path

I previously wrote about creating a build on travis-ci, and how some of the tests failed. And since I hadn’t investigated properly, I initially assumed that was down to file permissions on travis-ci, so I did what all good teams do, and commented out those tests on that environment because I didn’t have time to fix them.
During the proof reading it became apparent to me that the File chapter didn’t represent how I build up paths in the real world. And perhaps, what I was really seeing was a cross platform issue. I hadn’t really made all the code cross platform, so I started to do that.
The first thing was to make sure that I only had hard coded paths in tests which didn’t write to disk. Those that did write to disk needed to use System property "".
I also needed to make sure that I build up paths in a cross platform way. Since the Java file operations on Windows will happily work when your separator is ‘/’, I naively assumed that this would generate cross-platform test code. And since the tests were running on Mac, I naively assumed that it as a travis-ci problem.
However looking at the output from the tests, it was clear that on Linux, the "" needed additional ‘/’ handling to check for it on the end of the string or not. And using File or Path means we don’t need to do that.
So I converted all the path construction code and File chapter to use a combination of File, Path and Paths.get. And lo, the code passed on Windows, Mac and travis-ci Linux.

Java For Testers supports Mac, Windows and Linux

A win-win result, and now the code runs cleanly cross platform, and the travis-ci build uses the pom.xml with no exclusions necessary.

So now, I run the code on Windows and Mac locally. Then use travis-ci to run on Linux, using JDK7, JDK8 and Open JDK 7.
Also the book now supports use on a Mac consistently throughout, since I’ve updated the install instructions to include Mac, and describe Mac specific IntelliJ shortcuts and folders etc.
The e-book changes have been released on, and I’ve uploaded a new version of the kindle epub to Amazon as well.

Updates to Java For Testers

A summary of the changes:
  • Amended the cover slightly.
  • Added some installation instructions to support maven on Mac
  • Updated new project process for Intellij 14
  • Updated the install page on the site
  • fixed code sample in chapter 1 to be in chapter 1 code base
  • fixed the description of ++ and – prefix incrementing
  • Chapter 14 - added some extra code in the text to show the hamcrest assertThat more clearly
  • Chapter 15 - rewrote the description of regionMatches
  • Chapter 17 Made Dates and Time exercise cross platform - added extra code to exercise answer
  • changed the Properties chapter and File chapter (chapters 18 and 19)
    • The initial examples show building a path by string concatenation
    • Changed this to use File constructors
    • Where path is long, I have added in Paths.get and Path
    • Added information about $TMPDIR on Mac to match %TEMP% on Windows
    • All file code now uses File and Paths.get to build paths rather than concatenate strings
  • Added some extra IntelliJ Hints and tips.
(I should receive new paper proof copies over the next few days, so hopefully a paperback version of Java For Testers won’t take long to rear its head.)