Archive for the 'Java' Category

Note to self; Mockito cannot stub final methods

I realize this is clearly stated on the Mockito website, but it caught myself and my pair up for a little while. we were trying to stub out a ResourceBundle using Mockito and found that the unit test kept trying to find a concrete implementation of the ResourceBundle. The reason, we figured out is that the ResourceBundle.getString() method is final and Mockito cannot mock or stub final methods. Something to keep in mind when working with this mocking framework.

Back to Java

I am on a new project located in Melbourne, Australia. This time I am back to the world of Java. Some of the new to me technology includes Hibernate Validations, JAXB, and JPA. It looks like an interesting project with lots of XP practices being followed. Everyone is pairing, which will be a nice change for me as I have yet to be on a project where pairing was being followed. I am going to attempt to post entries several times a week about the stuff that I am learning.

Capturing Java Stacktrace as a String

Here is a little code snippet for a method that will capture an Exception’s stacktrace as a string so you can output it on the command line or in a simple log statement.

Wicket Phone Book and Maven 2

This evening I tried to download and setup the wicket-phonebook example in Eclipse. The application uses Maven2, something I have not had a lot of experience working with. In the root directory of wicket-phonebook, there is a script called Eclipse.sh which configures everything as an Eclipse project. It also downloads into the Maven2 repository almost all the dependencies.

However, there are two steps that are not done:

  1. Set up the M2_REPO environment variable in Eclipse
  2. Download and install JTA

For the first, I found the following command:

mvn -Declipse.workspace=
 eclipse:add-maven-repo

And for the second problem, I found the answer on JUGPadova website. In short, the problem has to do with Sun’s licensing of some of their J2EE jar files. Once I downloaded the JTA.1.0.1B.jar file, the following command put it in place and took care of all remaining errors in my Eclipse project.

mvn install:install-file \
  -Dfile=./jta-1_0_1B-classes.zip \
  -DgroupId=javax.transaction \
  -DartifactId=jta -Dversion=1.0.1B \
  -Dpackaging=jar

Immutable Objects

I am back in Chicago for more training. This time it is Object Bootcamp. One thing that we touched on today was the idea of immutable objects. Put simply, this means that the state of an object cannot be changed. From a conceptual point of view, this makes a lot of sense. If you have a Money and you create a new object worth $5.00 there is no way that you should be able to change that into $2.00. If you want to have $2.00, you need to create a new object since in reality you would no longer have $5.00.

The trick comes when you need to scale this up to more complex domain objects which have more then one or two attributes. So for example, how does this translate when you have a Class that represents a person. If that person’s name changes, does that make them a new person? Probably not. However, if their address changes, and the address is represented by an object composited within Person, then you can make a very strong argument that it represents a new address. Therefore, a new Address object needs to be instantiated.

The same argument could be made for a person’s email address or phone number.

This idea becomes important when you think about creating multi-threaded systems. Having all of your domain objects immutable means that they are all read-only. This means that you really do not have any shared resources between threads and it reduces the complexity of the entire application.

I am still thinking all of this through and have yet to try any of it on a real life application. In theory though it makes a lot of sense, but I am not sure how well it will work in practice. I think there will always be some information in domain model objects, or any class for that matter, that it makes sense to have change and thus violates this idea of having all objects be immutable.

Problems with Log4j

The testing team has discovered an odd problem related to Log4j. While system testing our application, they decided to delete the log file. The result was that Log4j did not create a new log file the next time there was a log request. They had to restart the application in order for Log4j to create a new file and start logging to it.

I have scoured the web to find a solution to this and have come up empty handed. I basically need a solution that configures Log4j to, when the current log file is deleted, create a new file. Anyone have any ideas?

So far my best advice has been: don’t delete the log file :-P .

Eclipse and the hunt for soft tabs

If I want to change the default behaviour for using soft tabs–using spaces instead of tabs when I press the tab key–in NetBeans, I only need to make the change in one place. If I want to do the same in jEdit, I only need to set it once. If I want to set soft tabs in Eclipse, I need to change it all over the place.

I was working on an Ant build script today in Eclipse and wanted to change the default editor behaviour to use spaces instead of tabs. So I wandered into the Preferences section. I know the setting is not going to be under Java, so I try the General tab, no luck. Finally, I try the “Web and XML” tab. Sure enough, there is a tab for XML files under which I can set the “Indent using spaces” option and press apply.

I then go back to editing my build file and it is still using tabs. Apparently, to Eclipse, a build file is not an XML file. So back into the preferences I go. This time I see the Ant tab and am able to set it to use spaces there.

Something as fundamental to programming as the option of using Tabs or Spaces should be obvious and easy to set for every editor. This is one area that NetBeans has Eclipse beat hands down. It is very easy to set editor styles in NetBeans. Granted, Eclipse’s editor styles and setting are more powerful, but they are also incredibly complex and confusing.

Every time I go looking to set spaces instead of tabs for the Java editor, it takes me at least a half hour of searching and hunting. This is because it is not even an option. Instead, it needs to be configured via the Code Style Formatter by either creating a new style or modifying one of the default styles.

What is the saying, “Make the simple things simple and the complex things possible”? This is one area that Eclipse makes what should be an easy thing incredibly complex. i think this is Eclipse’s biggest downfall, it simply is not simple to use. Nothing is intuitive about the application. True, it is very powerful and configurable. I guess that it is why it is the number one IDE, but simple it is not.

I use Eclipse all the time, but for new programmers I always recommend NetBeans because it is simple to use and easy to start working with.

Next Page »