Archive for May, 2009

Some words about unit testing – Junit, PhpUnit

Wednesday, May 27th, 2009

Unit Testing with Junit and PHPUnit

All the worlds speaks about testdriven development and unit testing. But most people finishing there exams don’t have a clue about what this means. To be honest, I was surprised that people could get out of a university just with one or two Junit tests implemented. Well, here are some words about testing.

What is a unit test?

A unit test is nothing else than some lines of test code which tries to check out if the lines of business logic you wrote actually works. To help you with this, tools like JUnit and PHPUnit (or SimpleTest in PHP enviroment) have been written. They are frameworks which give you some methods to work with. To be clear: a unit test is some kind of class which instantiates another class and checks if the parameters you put in bring the correct results.

What does testdriven development mean?

Hardcore people say, before you write the business logic, you’ll have to write a test case. This is test driven development: write your test, then write the actual business logic. For me it works usually like this: I write my business logic and as soon as I have something cool working, I write a testcase. Sometimes it behaves a bit different. Depends on. But thats a matter of taste. As usual I recommend not to be to extreme about everything and just try out what is best for you.

What and how much to test?

Everything. Well, not really. A good testing ratio is at 70%. More does mean that you even test your exceptions, less means you have forgotten some classes. There are tools available which help you with checking out how much you have tested. In Javaworld its Cobertura, in PHP its Spike PHP Testcoverage, for example.

How does a testcase look like?

Usually you have to extend your tester class from a class called TestCase or similar. In PHPUnit its PHPUnit_Framework_TestCase. They make your classes executable in the testing enviroment. Then implement methods – all methods which implement an actual test is prefixed with test.

An example from Log4PHP:

class LoggerLayoutHtmlTest extends PHPUnit_Framework_TestCase {
    public function testErrorLayout() {

The testing frameworks usually look for methods like this and execute them one by one. In newer JUnit Versions you have to annotate testing methods. OK, and this is a valid PHP testcase:

public function testErrorLayout() {
        $event = new LoggerLoggingEvent("LoggerLayoutHtmlTest", new Logger("TEST"), LoggerLevel::getLevelError(), "testmessage");
        $layout = new LoggerLayoutHtml();
        $v = $layout->format($event);
        $e = "blub";
        self::assertEquals($v, $e);
    }

Again, its Log4PHP. I instantiate a class which I am intending to test and call a method on it. The result is stored in $v. What I expect is stored in $e. All testing frameworks provide you so called assert methods, which enable you to compare or check otherwise if the expectation meets the actual result. In my case, “blub” is expected. If this assertion fails, my tool shows me that error.

Why is it necessary to keep old tests?

If you change business logic or refactor something you can simply execute old tests and recognize if something goes wrong. People say, before putting an echo or System.out.prinln somewhere, write an test. I agree here. A good testing ratio makes your software stable and you take care of side effects, even when software grows. And before you deliver you run all your tests. This way you make sure that everything is fine.

Test Data and dependend Tests

To make it short, test methods should never depend on the success of  test methods. Each test method can be executed stand alone. Otherwise your life will be hell – think on thousand methods depending on each other. Same goes to test data. A testcase must not depend on data another test method created. This will cause you hell.

I know that this is very difficult  with databases. Testframeworks usually give you “tearUp” or “tearDown” methods which are called before a test method starts. You can create your testdata in the databases in these methods. But this is very time consuming. Test executions of this kind can easily take hours. Best is, keep your tables short, make a SQL file for each package or even each test case. There are no best practices which fits on each project.

In my last project we have used Excel (=Manager aware) to take care of the data and then generated SQL files out of it. Time consuming, complex, but it worked. The relations in this tables where to strong, we couldn’t keep the data care up otherwise.

Testing helps coding

Having said the above, you can imagine that you should think about testability BEFORE you code. Spaghetthi code isn’t testable. Make short methods. Keep in mind what “Separation of Concerns” means. Same goes to databases. Make them plugable. Not at all time foreign keys are good. Think about it twice – they are mighty but can be evil too. I have worked in projects where it was very heavy duty or even impossible to delete any data. If you write your application testable, its going to be good designed in much cases.

This doesn’t mean you should put all methods to public scope! Test cases should be in the same package as their Test classes are, so you can try to work with package scoped methods aswell. However, whatever you do, testable code is good, but it never should break encapsulation just for beeing testable.

In most cases private methods can be tested with the test of public methods. Test coverage tools help to identify test lacks.

What to test? Again!

Try to test all public methods! If you need to create files, use the tempfolder. Set up databases for the test. Use Jetty and HSQL if need it embedded – in PHP its PDO and SQLite. Don’t waste your time with testing exceptions which usually never happen. Just use 20% of your time to make 80% of tests! Try to test business logic – if that means the creation of complex objects, so be it. Maybe you can share the logic. But don’t get bored with testing getter and setters. Those are called thousand of times within normal tests or procedures, no need to do that.

How much time can test development need??

Calculate the same time as implemeting the testcase. Too much? Not really! Imagine you are writing business code for 5 hours. Thousands of outputs on the console you read manually. You delete it, check it in and later want to fix again. Put those outputs in again? Have commented it out? Code is ugly? Put all your stuff in a testcase. Nobody claims if you need 5 hours for it, if its done properly. Sometimes testcases need 3 days but the fix just 1 hour. This can happen in EAI enviroments, where multiple systems communicate. No problem here – these is business critical, automatic tests are the best you can do.

Apache Commons Compress 1.0 released!

Friday, May 22nd, 2009

The Commons Compress team is pleased to announce the commons-compress-1.0 release!

Commons Compress is a component that contains Ar, Cpio, Jar, Tar, Zip and BZip2 packages

Source and binary distributions are available for download from the Apache Commons download site:

http://commons.apache.org/compress/download_compress.cgi

When downloading, please verify signatures using the KEYS file available at the above location when downloading the release.

For more information on Apache Commons Compress, visit the Commons Compress home page:

http://commons.apache.org/compress/

Changes in this version include:

New features:
o Initial release

Have fun!
-Commons Compress team

Mountpoint issue on OS X – and how to correct it

Sunday, May 10th, 2009

On my Mac Mini box running with OS X 10.4 aka Tiger I had once a Lacie harddisk mounted with the name ANDREW. I plugged it off and connected later another one under the same name. Unfortunatly the mountpoint of this disk wasn’t:

/Volumes/ANDREW

as expected, but rather:

/Volumes/ANDREW 1

which gave me hell when running my Junit Test for Apache Commons Compress due to the space in the harddisks name. I was wondering why OS X created the name ANDREW 1. Looking with my bash into /Volumes i recognized that there already was a folder with the name ANDREW. This happened cause when I removed the disk i started eclipse and the IDE couldn’t find a specific folder. Instead of telling me that, Eclipse simply created the folder. This could happen since the /Volumes folder has write access to everybody on OS X.

I deleted the unnecessary folder. After that I disconnected and reconnected my harddisk named ANDREW. The mountpoint was correct again and life is going normal :-)

Apple Logic 8

Saturday, May 9th, 2009

After long time I finally decided to buy Apple Logic 8 in favour of GarageBand and Apple Logic 7! It now has arrived!

Apple Logic 8

Apple Logic 8

Spotting it out showed me that everything looks more cool and nothing is missing. It runs smooth on a Mac Mini Intel Core Duo (not 2) with 2 Gig of RAM. Performance is not different to Logic 7.  Synthesizer Content is amazing and I really think that the time of creating a VST or AU plugin is near :-)

We’ll see – now I enjoy that cool piece of software!

The Singleton issue – an everlasting discussion

Friday, May 1st, 2009

Today it looks that everybody needs an very extreme opinion in each technical topic. I mean, Windows is evil, OS X too, but Linux is uncomfortable and Unix is dead. JavaScript is just a toy, without JavaScript you don’t have a professional website. Java is slowest, Java is coolest and so on and so on. Finally: Singletons are evil. By the way, sometimes several people say that using design patterns destroy the ability of thinking yourself.

In fact, I strongly believe that every concept (ok, most :-) ) have benefits and drawbacks. The case of the singleton for me is easy: it depends.

So what’s wrong with that pattern?

Some people claim that the Singleton pattern is a sign of bad software design. They say the Singleton can be used as some kind of global variable. Well, that’s true, sometimes. But the Singleton can be used otherwise than a global variable holder. Actually you have to differ between your programming language before thinking about a good or bad usage of the Singleton Pattern.

How to implement the Singleton pattern in PHP

PHP is a scripting language, it’s actions are performed by an interpreter. It doesn’t support multiple threads like Java or Ruby does. This is very important later. But first, lets create a singleton in PHP.

class MySingleton {
    private static $instance;
    private function __construct() {}
    public static function getInstance() {
        if(self::instance == null) {
            self::instance = new MySingleton();
        }
        return self::instance;
    }
}

How does this work? You create a static variable in a class. If one calls getInstance() for the first time, a new object is created and stored in the static variable. If you call the method again, you’ll get exactly the same instance you created with the first call. This object – since it is static – will stay until your script has ended and the complete request is processed. You see, why the singleton has it’s name. It’s only one object created and used for all the time.

And to keep this behaviour, you just have to put your constructor private – nobody else than your getInstance method should be allowed to create this object.

How to implement a Singleton with Java

Now you have to think about some more issues. Since Java supports multithreading you have to think about more than just creating the object and returning it. First, we’ll see how a pure PHP programmer would create the singleton with Java syntax.

public class MySingleton {
    private static MySingleton instance;
    // Again - just MySingleton is allowed to construct this object
    private MySingleton() {}
    public static getInstance() {
        if(instance == null) {
            instance = new MySingleton();
        }
        return instance;
    }
}

OK, now think about two threads which call getInstance() at the same time, and imagine nobody has done so before. Both threads would enter getInstance(). Chances are good that Java helps you even with multiple processors (it should do, cause Suns Sales Managers sell Java as a simple to use language ;-) ). In that case, one thread would go on to create the MySingleton object, while the other one will jump over it and return the instance.

You have to ask the question, does this really work? Cause at which point of time is instance not longer null? The answer is, if you create the object headers, the instance variable is not longer null. That means, that a second thread could overtake thread one and work on an instance which is not really created (just the have of it)! Something unexpected will occure and even if your customer will claim about an error, you probably will never have the chance to find it.

So what now? We have to make sure that:

a) two threads are not creating a MySingleton object at the same time
b) one thread doesn’t work on the object while it is still under construction

Some programmers start something like this:

public class MySingleton {
    private static MySingleton instance;
    public static getInstance() {
        if(instance == null) {
            synchronized(this) {
                if(instance == null) {
                    instance = new MySingleton();
                }
            }
        }
        return instance;
    }
}

Please note the synchronized block above. It keeps care that only one thread can execute the code for creation at one time.

Is this the solution? No. Even in that case it can happen that Thread 1 locks everything correctly for creating the object. But if Thread 2 comes at a bad time, it will ask the outer instance == null check and again it could return an unfinished object. The synchronized block just helps with one problem.

Actually there is only one 100% solution. Create the necessary object while class loading.

public class MySingleton {
    private static MySingleton instance = new MySingleton();
    public static getInstance() {
        return instance;
    }
}

This is a solution PHP programmers cannot have since PHP doesn’t support to create objects like this. But for the Java programmer it’s the only way to make object creation work. And yes, sometimes it’s not possible to go this way. But that’s another story.

Singleton Lifetime

You should know about another difference between Java and PHP Singletons. In Java, the static keywords makes the variable available at a class level, not at object level. This means it follows you as long as the JVM is alive – from programm start to programm end, and in case of an enterprise or an eai server, this can take a long, long time.

For a PHP programm it’s the same – from programm start to programm end. But in PHP words this means the time when an HTTP request arrives and script execution begins till the end of the script has been reached. After a request, the singleton is gone, dead, and you’ll have to create it again, with the next request.

In human words, this means that a Java singleton probably lives for days, weeks or even months and the PHP singleton just for less milliseconds.

The Benefits of Singleton

Well, benefit is: you have to create only one object of an class and can use it over and over again. This is cool, since creating an object takes usually a long time in the interpreter or in the virtual machine. Cause of this, old programmers often say object orientation is slow. Meanwhile everything is faster and in most cases you don’t have to care bout this anymore.
And this is where critic starts: people say, you could misuse the Singleton as some kind of global variable store. This comes often from PHP programmers, who think about that code:

class MySingleton {
    ...
    public $bla = "blubber";
    public static function getInstance() {
        ...
    }
}

In this case, you can have the variable $bla available all over your code. Just do a:

MySingleton::getInstance()->$bla;

Is this good? No. Of course not. If you just want to store something, go ahead, use $GLOBALS or a similar global available variable. Same goes to Java. There are no global fields, but you can create some similar with System properties.

But Singleton will have more the worth if the singleton object has some methods you can operate on. Imagine that singleton object trys to reload a configuration.

class MySingleton {
    ...
    public $bla = "blubber";
    public function loadConfiguration() {
        // code to load xml file
        $this->bla = getValueFromXML();
    }
    ...
}

This would make it possible to reload the config at runtime. Even date formatting etc. would be possible at runtime.

Basically said, an object which provides methods is worth something. It’s not to bad to do so. But a more cool benefit is if you have a stateless singleton.

class MySingleton {
    ...
    public function doSomething($bla) {
        $bla = $bla / 2;
        return $bla;
    }
    ...
}

Imagine doSomething() does some complex things with $bla. You don’t need to store the result in the MySingleton object. It will returned. Since there are no object variables available, this object is stateless. And there is really no need to create multiple objects just to perform some operations on $bla.

But why should one consider to create an object if he can do this in a static way, you ask. Static access to methods is not very flexible. Think on AOP, where objects wrap other objects and prevent you from calling a method directly. This wouldn’t work. That’s just one example. There are others: maybe you want to choose at runtime, which implementation you use to create this task. In a static context you would have to change your implementation, in an object context you could use a factory and a strategy pattern to switch. This is way more flexible.

Drawbacks of the Singleton pattern

Yes, of course there are several. Think on the global variable thing. It’s true, imagine you have one thousand of Singletons, all keeping care of their own little states, read within the application from a million of places. Not very good. Esspecially with PHP you cannot easily find out where those variables are called. In Java you have some good shortcuts in your IDE.

But we know now, that a Singleton is meant for reducing object creation time and not for state sharing. Next drawback is more significant, since the first one is a mistake just inexperienced programmers do.

Problem is, if you create a Singleton it will stay one forever. If you need to make multiple objects of your class, you have to write code for this. In small projects this is not such a big issue, but in a 100.000 lines of code project it is. There are other patterns like Prototype or Factory which could be useful suddenly and we would have to write code. This is a risk.

The solution is to create this Singleton not by code like we did above. Use a framework for creating your objects. In PHP you PIWI and the BeanFactory at your side, which helps you with constructing singletons (and injects the objects in other objects – Dependency Injection, see the previous article).
In Java you can use Spring for this. Spring is the big inspiration source for PIWI (regarding object creation and dependency injection) and widely used. Besides Spring there is Google Guice and PicoContainer too, which provide similar stuff.

Finally

There is no need to say Singleton is good or bad. It’s neither. It’s just a pattern. A Singleton is just as evil as you misuse it, and just as glorious as you identify it as fitting pattern to a specific problem.

In my projects I use a Singletons sometimes for configuration. I don’t think it’s bad behaviour. If I don’t have one thousand of this object in my System, it’s OK. More often Singletons are used as some kind of worker classes, just here for doing complex stuff with storing a state. They help me not to waste memory. And due Singletons are objects I easily can extends them via aspect oriented programming – perfect!

  • Recent Posts

  • Tags

  • Categories

  • Archives

  • Wave Notifications

    Download Adobe Wave now!

    This application requires Adobe® AIR™ to be installed for
    Mac OS or Windows.




Feeds