Unstable Terrain

Software development in the real world

Posts Tagged ‘Java

Efficient Singletons in legacy code

leave a comment »

The app I’m maintaining is not Spring-enabled so I am faced with the possibility of writing code for singleton objects. To my mild surprise, I discovered that Java 5 has made singleton creation considerably less bug-prone. There’s also better practices for double-checked locking, if your code (attempts to) swing that way.

Advertisements

Written by Trent

August 30, 2011 at 11:25 am

Posted in Software Development

Tagged with , ,

Testing JUnit 4 Exception Messages

leave a comment »

In previous years, I have tested exceptions in JUnit tests using variations on the

try {
  doSomething();
  fail();
} catch (Exception expected) {
  assertEquals("Invalid Number of Foos", expected.getMessage());
}

pattern. Various upgrades such as codifying it into an AssertThrows class and checking the correct exception type have helped but it was a pretty ordinary solution.

Changing to JUnit 4 allowed for the annotation below:

@Test(expected=IllegalArgumentException.class)

… but testing the exception message remained ugly.

JUnit 4.7 has a neat upgrade: testing that thrown exceptions have particular messages.

For example:

public class BuilderTest {
  @Rule
  public ExpectedException thrown = ExpectedException.none();

  @Test
  public void invalidMessagesShouldThrowAutoPopulateException() {
    thrown.expect(InternalAssertionError.class);
    thrown.expectMessage("too many autopopulate errors");

    builder.build(createGroupWithTooManyAutoPopulateAnnotations());
  }
}

It also accepts a Hamcrest-style matcher if you want to do something more complicated.

Written by Trent

February 18, 2011 at 4:19 pm

Posted in Software Development, Tools

Tagged with , ,

Ant Junit task Addendum: forkmode

leave a comment »

I mentioned previously about using the ant junit task with fork=false.

However, some tools such as EMMA insert shutdown hooks, so require forking. In addition, classloader issues may require you to fork to save your own sanity.

Turns out there’s a more robust solution.

<junit  haltonfailure="yes"
        printsummary="yes"
        fork="true"
        forkmode="once">
    <classpath refid="test.class.path.with.test.classes" />
    <formatter type="xml" />
    <batchtest toDir="${test.reports}">
        <fileset dir="${test.classes.dir}">
            <include name="**/*Test*.class" />
        </fileset>
    </batchtest>
</junit>

A new JVM will be forked, but all tests will be run inside that single forked JVM, increasing the speed of your tests substantially. If you have some tests that have side-effects, you can put them into a separate batch and change the forkmode to ‘perBatch‘, effectively isolating them while retaining the speed of the rest of your tests.

So, try it and see how much faster your build goes.

Written by Trent

July 26, 2010 at 6:44 pm

Eclipse Helios Released

leave a comment »

Eclipse 3.6 (Helios) has been released!

Without, it appears, a coherent set of release notes. If you like, you can read the list I cobbled together myself.

Update: I found a nice list of the top ten best features of Eclipse Helios.

Written by Trent

June 24, 2010 at 12:14 pm

Posted in Software Development, Tools

Tagged with , , ,

Eclipse Helios Milestone Release Notes

with one comment

Eclipse is my Java IDE of vaguely-choice, but keeping track of the milestones of its latest release is a pain; missing or broken links, invisibility on Google — the whole shebang.

So, here’s the result of my searching. I present to you, working Eclipse Helios milestone release notes.

… after which there are release candidates before the General Availability release, available here.

Written by Trent

June 16, 2010 at 11:51 am

Posted in Software Development, Tools

Tagged with , , ,

PayFlowAPI Proxy Issues

leave a comment »

At work, we have an old Java 1.4 application that has been experiencing connection issues.

We were experiencing intermittent problems connecting to remote servers. HttpClient logging showed strange connection attempts, like so:

HttpConnectionManager.getConnection:  config = HostConfiguration[host=https://external.site.example.com, proxyHost=http://:0]

These errors persisted even if the proxy details were set programmatically in the Axis2 client. Even setting the http.proxyHost system property didn’t work.

After much investigation, it turns out that the Payflow API jar was (re)setting the system properties for the proxy details based on the input parameters of its constructor — a very unfriendly thing to do. Further investigation showed that this issue was not new. Upgrading jars (or Java version) at this late stage of development was out of the question, so the interim solution was to re-set the proxy details using System.setProperty() each time the API wrecks them. Not ideal, but sufficient.

Written by Trent

April 21, 2010 at 12:35 pm

Tomcat 6.0.26 released

leave a comment »

Changelog says that Bug 41059 has been fixed, meaning that tomcat won’t hang inconveniently when it tries to shut down. Neat.

Written by Trent

April 16, 2010 at 8:12 pm

Posted in Software Development, Tools

Tagged with , ,