ant and junit5 – simulating sysproperty

In JUnit 5, you use the junitlauncher Ant task rather than the junit Ant task. Unfortunately, this isn’t a drop in replacement. For example, junitlauncher doesn’t offer the option to fork and run the JUnit tests. As a result, it also doesn’t have the nested sysproperty tag so you can pass system properties. This is a problem.

For the CodeRanch, we set a system property for the default file encoding. Since developers are around the world, we can’t assume everyone “just has” the encoding set.

Disclaimer

Since JUnit 5 functionality for Ant was introduced this year, I’m hoping what I did in this post is a short term workaround.

Option 1 – pass to Ant

You can pass the the properties to Ant itself as described on Stack Overflow. For example:

__JAVA_OPTIONS=-Dfile.encoding=ISO8859_1

pros:

  • simple

cons:

  • subpar – all the developers need to remember to do this. The reason we had it in Ant in the first place is so folks wouldn’t need to remember
  • for some use cases, the desired system properties could be derived and not know when calling Ant

Option 2- Nashorn Code

Since JUnit 5 is being run in the same process as Ant itself, you need to set the system property in memory. Luckily, Ant allows you to run scripting in various languages. I chose Nashorn because it is built into Java. (There are other variants of this; see below)

<script language="javascript">
  <![CDATA[
    var imports = new JavaImporter(java.lang.System);
    imports.System.setProperty('file.encoding', 'ISO8859_1')
  ]]>
</script>

Pros

  • Short and simple
  • Nashorn is deprecated for removal starting Java 11. This means at some point, it can be removed. (I’m hopeful that the Ant task itself will support system properties by then.

Cons

  • Requires Java 8 or higher
  • The System property is set for the remainder of the build (you could write another code block to null it out after the test if this is a problem)

Variants of option 2

If you are running Ant with a version of Java below Java 8, you could use this technique, but use Rhino instead. I didn’t test this, but I think the code is


importClass(java.lang.System);
System.setProperty('file.encoding', 'ISO8859_1')


JUnit 5 itself uses Java 8 so nobody would be in the situation of pre-Java 8 and trying to use this blog post.

If you are running Ant with a version of Java where Nashorn has been removed, you could use Groovy or Jython as the embedded language. The code is simpler. I didn’t chose this because it requires adding another jar to the Ant directory. I prefer to minimize these set up extensions. Especially for a feature like this which is likely to be temporary.

System.setProperty('file.encoding', 'ISO8859_1')

my nine month experiment with safari books online

I’ve completed an experiment of using Safari online for nine months. I learned it’s not for me. I also learned specifically why. So keep reading. You may find that one or more of these don’t apply to you.

My bias

I love paper books. We spend so much time with screens; I like to read on paper. It’s relaxing and uses the eyes differently. Also for computer books, I learn tactically. I remember where things are on pages. I use page numbers to see where I am in the chapter and create mental anchors. I read mainly on the subway while commuting.

I do read books electronically on occasion; usually when editing/proofing books that aren’t in print yet. These are in the form of Word docs or PDFs.

What I tried to read (and what I thought)

  1. Core Java SE 9 for the Impatient” – I picked this as my first book to attempt to read. I love Cay Horstmann’s work. And I’m pretty familiar with what is in Java 9 so I figured this wouldn’t be a terribly hard read. Learning something new while getting used to the Safari reader seemed overly ambitious.
    • I read two chapters before I got too fed up with the O’Reilly “Queue” reader.
    • My biggest problem was that it was hard to figure out where I was within a chapter. There are scroll bars, but I want to see the mapping to chapter subheadings like in the table of contents. I can view these subheadings and go to them, but not see where I am. Pagination matters!
  2. Effective Java” – Ok so maybe the problem was that the previous book was too easy and I couldn’t get into it. I did want to read the revamped Effective Java book. And it has very short sections – there are 90 tips in about 400 pages. So I figured I wouldn’t be impacted with the problems I had with Core Java. Well, I had two new ones.
    • The code blocks don’t fit on the  screen so you have to use two finger horizontal scroll to read the code. Alternatively, you can click a link to view the code as an image. The image is all the code in the book in various fonts. It’s just as hard to follow. Besides either of these approaches break flow and make it harder to read/understand the text and how it relates to code.
    • I also learned that even with the short sections for each item, I was losing the spatial memory of being in a specific place on the page for reading. (This problem isn’t as bad with e-book PDFs when I review because there are page numbers and page break so you still have some level of anchoring.)
    • Oh and you can highlight text for notes in the iPad app, but the only way I can find to view the highlights/notes in one place is on the web site. Grumble.
    • I still want to read this book, but I’ve given up on reading the Safari version and ordered a paper version. And I paid for the paper version. I might have been able to get a review copy in December from CodeRanch, but I didn’t ask then because I thought I’d read it on Safari.
  3. My books – I flipped through parts of my books. I know the contents of these books well having written them. And I was just checking out the format so the anchoring/pagination problems weren’t a big deal. Or so I thought.
    • First of all, publishers report errata by *page number*.  Getting rid of the page numbers does the reader a disservice. Especially on certification study guides where errata matter a lot.
    • The rendering is bad. I’ve seen the official e-book of my titles. The official e-books look nice. The Safari book is nothing like them. On the assessment, the answers have numbers instead of letters. (The answer key of course uses letters so this is annoying.)
    • The code doesn’t fit on the screen without scrolling despite the fact that there over an inch of whitespace on the side of each code block in the rendered version.
    • The excessive whitespace margins are annoying in and of themselves. It means less text appears on the screen. Computer book authors put effort into making sure certain things are visible at the same time for comparison/reference when learning. All this is lost.
  4. Introduction to DevOps with Chocolate, Lego and Scrum Game” – Last try. This book is relatively short (140 pages), doesn’t have code and is light reading. I still had to horizontal scroll in a couple places for tables. At least this book I was able to get through in Safari online.

What I didn’t try

I didn’t watch any videos. I prefer to learn by reading vs video. (probably because I want to be reading when I’m not at my computer and use my computer time for actually coding or typing.) I could watch a video on my iPad, but then I’d have to juggle another item to take notes. Yuck.

I didn’t read the e-books in a browser on my computer. A quick scan says the code does fit on the screen there, but there still isn’t pagination.

Other opinions

During this experiment, I asked others what they thought of Safari Online. Two mentors at the robotics lab felt it was too expensive. I also asked at CodeRanch in this post. Highlights:

  • Bear (a very senior developer and successful author who reads e-books exclusively) said he wasn’t impressed during the free trial and PDFs are far superior.
  • Tim C (who has a free subscription at work) uses it occasionally for copy/paste but prefers paper book.
  • two people comment the material they need is already online for free

What is good about Safari Online

I imagine the videos are good. And being able to reference so many books or read as much as you want is a nice benefit.

Why Safari Online isn’t for me

For books the decision to not have pagination ruins it for me. As does the poor highlighting/note taking ability. DRM is all well and good but I find the iPad app to be far inferior to a PDF as far as e-books go.

The other problem is the cost. I typically read 10 computer books a year (cover to cover). Granted some are books I borrowed from others or are books I get for free for reviews. But even if I bought them all, that would cost roughly the same as a Safari online membership. ($39/month.)

Personally, I’d rather have the physical books to show to others, reference my notes in, etc. I’m going to request my license either be transferred to someone else or not renewed for next year.

Disclosure: my employer paid for the year of Safari Online so this experiment only cost my time.

 

how to install open jdk 11

Since I’m speaking at QCon about Java 11, I figure I should install it instead of just having read about it :).

Wait? Will this mess up my existing Java install?

No. Open JDK provides tar files. This means you can have as many versions installed as you want.

How to install

  1. Go to http://jdk.java.net/11/
  2. Download the Open JDK tar.gz file (It’s about 300 MB)
  3. Unzip
  4. Move to the directory you typically keep JDKs to make it easy to find. For example, on Mac:
    sudo mv jdk-11.jdk /Library/Java/JavaVirtualMachines
    cd /Library/Java/JavaVirtualMachines
  5. Optionally, add aliases. I like to create javacVersionNumber and javaVersionNumber so I can quickly compare multiple versions. For example, on Mac, I opened .bash_profile and added aliases for java 11 to the ones I already had.
    alias javac11=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home/bin/javac
    alias java11=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home/bin/java