Archive for August, 2009

Aug 31 2009

JUnit 4 error: reference to assertEquals is ambiguous

Published by michael under JUnit, Java, Testing

This is a somewhat confusing compilation failure that sometimes happen when you write unit tests using JUnit 4. This is an example code snippet that produces this error (result.getValue() returns an Integer object):

assertEquals(12345, result.getValue());

And when you try to compile your project it produces a compilation error like this:

/projects/myapp/src/test/java/org/myapp/MyTest.java:[88,8] reference to assertEquals is ambiguous, both method assertEquals(double,double) in org.junit.Assert and method assertEquals(java.lang.Object,java.lang.Object) in org.junit.Assert match

Since JUnit offer several methods that are very similar the compiler can not always determine which one to use. In our case we have the number 12345 which is an int and result.getValue() which is an Integer. You’d think that the compiler could figure out to convert the number 12345 to an Integer object which would give us a match on “assertEquals(java.lang.Object, java.lang.Object) method, but instead it throws the above error.

The reason for this is that Java uses best match to try to determine the method that will require the least conversion of the parameters. In this case one method requires boxing, and the other method would require un-boxing. Both of these methods have equal priority and that’s why there is ambiguity since none of the methods are more specific than the other.

The solution is to help the compiler determine what it need to do. We can do that using this:

assertEquals(12345, (int) result.getValue());

or this:

assertEquals((Integer)12345, result.getValue());

to resolve this problem. The second solution that converts 12345 to an Integer object is most likely the better once since Java will auto-box a null value to the number 0 which may not be the correct behavior for the class you’re testing.

3 responses so far

Aug 26 2009

Spring XML and defining end of line characters in a bean property

Published by michael under Java, Spring, XML

If you need to define and end of line character (EOL) like \n in your Spring XML files in a bean property you can’t do this

<bean id="some-bean-id" class="some-class">
    <property name="my-property-name" value="\n"/>
</bean>

This simply wont work. You’ll end up with a backslash followed by the letter n. What you have to do is to use an escape character like this

<bean id="some-bean-id">
    <property name="my-property-name" value="&#10;"/>
</bean>

No responses yet

Aug 26 2009

Maven, Log4j and javax.jms

Published by michael under Java, Logging, Maven

Log4j version 1.2.15 added features which has new dependencies on sun and javax packages. When you try to build your project using Maven and log4j 1.2.15 you will see this:

[INFO] Unable to find resource 'com.sun.jmx:jmxri:jar:1.2.1' in repository central (http://repo1.maven.org/maven2)
[INFO] Unable to find resource 'com.sun.jdmk:jmxtools:jar:1.2.1' in repository central (http://repo1.maven.org/maven2)
[INFO] Unable to find resource 'javax.jms:jms:jar:1.1' in repository central (http://repo1.maven.org/maven2)

These packages are not in the Maven repositories due to licensing issues. There are two simple solutions to this problem. You can either modify your pom.xml file to use the previous version of log4j (log4j 1.2.14) which doesn’t depend on the JMX and JMS packages:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
</dependency>

Or you can exclude the dependencies with the caveat that some of the lo4gj appenders  that depend on these packages wont be available

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.15</version>
    <exclusions>
        <!--
        These packages are not in the Maven repository
        If you do this certain log4j appenders will not be available
        -->
        <exclusion>
            <groupId>javax.jms</groupId>
            <artifactId>jms</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jdmk</groupId>
            <artifactId>jmxtools</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jmx</groupId>
            <artifactId>jmxri</artifactId>
        </exclusion>
    </exclusions>
</dependency>

No responses yet

Aug 24 2009

svn: Inconsistent line ending style when trying to commit a binary file

Published by michael under SCM, Subversion / SVN

If you get this error message “svn: Inconsistent line ending style” when trying to commit a binary file into your SVN repository then Subversion probably thinks that your file is a text file.

First verify your file with:

svn proplist <your-file>

If the result contains svn:eol-style but you know that your file is a binary file then you have a problem where Subversion thinks the file is a text file even though it’s not. We fix this by first deleting the erroneous properties and then adding the ones we want.

svn propdel svn:eol-style <your-file>
svn propset svn:mime-type application/octet-stream <your-file>

Verify your file again with the command below

svn proplist <your-file>

If everything worked it should say svn:mime-type

No responses yet

Aug 21 2009

How to read environment variables in Java

Published by michael under Java

You can set environment variables or pass them in using the -Dvariablename syntax

SET logfile=/location/to/my/log/file.log

or

java -Dlogfile=/location/to/my/log/file.log HelloWorld

When you want to access these environment variables from your Java source file you can use

Java 1.5 and newer:

String logfileLocation = System.getenv("logfile");

Java 1.4 and older:

String logfileLocation = System.getProperty("logfile");

No responses yet

Aug 19 2009

What jar file does a Java class belong to?

Published by michael under Java

I found this very useful web site that allows you to search what JAR file a specific Java class belongs to. This has proven quite useful to me over the last couple of days when I had to deal with NoClassDefFoundError and ClassNotFoundException problems.

You can just enter the name of the class or JAR file your looking for and the site will display the result.

http://www.findjar.com/index.jsp

One response so far

Aug 04 2009

Mockito an alternative to JMock

Published by michael under Java, Testing

I’ve used JMock for quite some time and I’ve found it to be a great and very useful mocking framework for unit testing. I was never quite happy with the syntax though; especially the part where you specify expectations.  Here’s a JMock snippet illustrating an expectation and a return value from a mocked object

context.checking(new Expectations() {{
    oneOf (department).employees(); will(returnIterator(employees));
}});

In Mockito the equivalent code would look like this

when(department.employees()).thenReturn(employees);

I find the Mockito syntax to be easier to write and understand. I’ve seen fellow developers implement JMock expectations in a way where they actually didn’t perform a useful test, and I think the sometimes confusing syntax of JMock was the reason. I’m not dismissing JMock as an inferior mocking framework, but I do think that the learning curve is a bit steeper. If you haven’t used Mockito yet I encourage you to take a look at it.

2 responses so far

Aug 03 2009

Entourage taking over my harddrive

Published by michael under OS X / Apple OS

I noticed that I had almost no space left on my MacBook and I couldn’t see a good reason why I would have so little space left. In Windows I used an excellent application called WinDirStat to find out what was wasting space on my harddrive, and I was glad to see that there’s a similar app for OS X called Disk Inventory X. Both of these free and excellent applications  shows you a graphical tree map that makes it easy to see what is using up your disk space. In my case it turned out to be Entourage which due to frequent crashes had led to multiple copies of the Entourage database. I deleted several copies of the database and recovered a lot of space.

Disk Inventory X: http://www.derlien.com/

Location of Entourage files: http://www.entourage.mvps.org/path/index.html#ent_db

No responses yet

Aug 03 2009

Java and the current work directory

Published by michael under Java

I spent some time looking this up and hopefully I will save someone else time on this. The current work directory for an application in Java is stored in the system property “user.dir”. This is the default directory that you application will write files to if you don’t specify an absolute file location. You can access it using this Java syntax:

String userDir = System.getProperty("user.dir");

No responses yet

Aug 02 2009

Maven pom.xml structure

Published by michael under Maven

I wanted to see the full Maven pom.xml structure and I found the following page on the Maven web site that contains all the elements and descriptions.

http://maven.apache.org/ref/2.2.0/maven-model/maven.html

No responses yet

Next »