|
Easy unit testing of File operations
[
vmassol
]
The experience that I'm relating here is part of an exploratory refactoring that I'm currently doing on the Cargo code base. Till now we were using Java File objects for representing J2EE archives or container installation and configuration directories. This is ok but it makes unit testing a little bit complex when it comes to unit testing File operations. The reason is that you need to define a location on your local file system where you're going to read/write files to, clean up the files, etc. Here's a method we had (it expands a JAR file):
Here's how I've transformed the method by removing all
The interesting part comes now. Because it was a bit hard to create a unit test for the original
Notice the use of the "ram:" URI scheme. This one of the many filesystems supported by VFS and it means that all file operations will happen in a virtual file system in memory.
Also note that VFS doesn't currently support creating Zip files so we're using the JDK's The other nice thing is that by introducing VFS to this Interesting. I know several codebases that could reuse something like your FileHandler to ease testing :) It would be interesting to see how the FileHandler interface would evolve if used by various projects. In your case it seems like it was designed to fit in your current implementation. Another interesting aspect is that this tends to prove that, as many other Java APIs, the initial java.io. classes were designed the old way: written for a client, but not for testing. --Jerome Lacoste, July 17, 2006 02:37 PM
Jerome, I've only a FileHandler interface because we're still not sure we want to use VFS yet. This is currently exploratory. However if you're ok to use it then the common File operations API is the VFS one which is a very good API and which works for all types of filesystems. --Vincent Massol, July 17, 2006 02:57 PM
Vincent, I just came across your posting and I find it quite interesting. Could you explain what makes you hesitating using the VFS or did you in the meantime decide to use it? Regards, Andreas --Andreas Guther, September 11, 2006 06:44 AM
Hi Andreas, Yes we're already using VFS in Cargof or your tests. We have a FileHandler interface and 2 implementations: - DefaultFileHandler which uses File operations Right now we're only using it for tests. We might also use it for our production code later on if we can provide value to our users (like the ability to use other file systems). We haven't gone to that level yet. VFS is working nicely for us right now. However I'd like to see a 1.0 release ASAP. We're using a snapshot version. -Vincent --Vincent Massol, September 11, 2006 10:00 AM
Post a comment
|