Tag Archives: Development

EclEmma: the free Java code coverage tool for Eclipse

Unit testing has a lot of aspects which are more or less equally important but code coverage is something on what you really have to pay attention. Depending on the complexity of our methods, more combinations are possible on arbitrary test inputs. Considering every permutation in some not-so-obvious cases takes some time and effort, and the chance to make a mistake – which will result in an uncovered piece of code – is even higher.

EclEmma is the right tool to measure the coverage of your code and to make sure that every single line is executed properly. It has plenty of useful features from which I’ll present only the Coverage tool. In this post you will:

  • see how to run your unit tests through EclEmma
  • how to interpret the output of the Coverage tool

I’m using EclEmma with JUnit4 but it will run with JUnit3 too. You will find any neccessary information regarding to the installation on EclEmma site: http://eclemma.org/.

So lets take a look at an example from the source of Qt Jambi. QPair of the com.trolltech.qt package is used to hold two generic values. There are 3 methods to test: public boolean equals(Object o), public String toString() and public QPair clone().

I won’t give any detailed explanations about the test methods since the main goal of this post is to introduce EclEmma. However if you are new in unit testing too you could take a look at one of my old posts Unit testing with Qt Jambi. There are a lot of resources about unit testing but that post is directly related with the currently studied examples.

Let’s take a look at public boolean equals(Object o) which is a method to check the equality of two objects (beware, it could be very tricky!). If you can’t access the resource above, don’t worry the code is reproduced here:

    /**
     * Returns true if this pair is the same as the other pair. If any
     * of the first or second members are null the result is false regardless.
     * @param The other parameter
     * @return True if they are equal.
     */
    @Override
    public boolean equals(Object o) {
        QPair<?, ?> other = o instanceof QPair ? (QPair<?, ?>) o : null;
        if (other == null || first == null || second == null || other.first == null || other.second == null)
            return false;
        return first.equals(other.first) && second.equals(other.second);
    }

Our test case for this particular method is:

	@org.junit.Test
	public void testEquals() {
		assertTrue("qp1", qp1.equals(qp1));
		assertFalse("qp2", qp1.equals(qp2));
		assertFalse("qp3", qp1.equals(qp3));
	}

       @org.junit.Before
       public void setUp() {

               qp1 = new QPair<Integer, Integer>(3, 5);
               qp2 = new QPair<Integer, Integer>(5, 3);
               qp3 = new QPair<Integer, Boolean>(1, null);
               qp4 = new QPair<Integer, Boolean>(null, true);	
       }

While the test case is opened, from the Run menu select Coverage As > JUnit Test. The JUnit test starts and EclEmma is calculating some measurable properties of your code. Now you should see something like this:

As EclEmma finished its job you can see the coverage result of your unit test code:

and the code of the tested class too:

But some of the lines appeared in yellow instead of green, what happened? If you take a closer look to these lines EclEmma will tell you that there are some uncovered branches in your code. In this particular case we have 5, 3 on line 79 and 2 on line 81:

Lets take a look again at our test and study it carefully:

	@org.junit.Test
	public void testEquals() {
		assertTrue("qp1", qp1.equals(qp1));
		assertFalse("qp2", qp1.equals(qp2));
		assertFalse("qp3", qp1.equals(qp3));
	}

       @org.junit.Before
       public void setUp() {

               qp1 = new QPair<Integer, Integer>(3, 5);
               qp2 = new QPair<Integer, Integer>(5, 3);
               qp3 = new QPair<Integer, Boolean>(1, null);
               qp4 = new QPair<Integer, Boolean>(null, true);	
       }

Is there any assertion in our test which is running through this if:

               if (other == null || first == null || second == null || other.first == null || other.second == null)

Lets see: the expression inside if will be true iff at least one of those conditions between || is true. Our only assertion which involves null is this one:

               assertFalse("qp3", qp1.equals(qp3));

which is, obviously, won’t cover all the possible candidates in which the above if will be executed. This assertion covers exactly one case. Remember QPair(T, S), and because qp3 was constructed with (1, null) it covers:

               if (other == null || first == null || second == null || other.first == null || other.second == null)

Thanks EclEmma for pointing out this obvious mistake! After adding more assertions to cover all the possible cases you will see something like this:

All branches are covered properly! \o/

Of course this is just one from EclEmmma’s useful features. In case of big projects it is good to see how your overall coverage is forwarding, you can check that too:

As you can see we have a lots of work to do.

This was a very short but fair introduction about EclEmma’s main feature, using it on regular basis to check your unit tests functionality will save you lots of time.

Thank you for your time, comments are welcome, as always!

Advertisements

Qt Jambi Unit Testing progress

Hi!
Today I finished with my last unit testing implementation in the com.trolltech.qt package. It’s still incomplete, but are some thread specific classes which requires better understanding of the Qt thread mechanism.
However there are a lot of tests left to implement, and the sources what I provided should be enough to get started!
My git feed was kinda dead in November (actually there are no commits from this month at all :)) due to my packaging experiments around Qt Jambi on Ubuntu – without any success :(. Now, If you are trying to get this running on Ubuntu you have to grab the Qt sources , compile it, and then you can build Qt Jambi either with the old-fashioned $QTDIR way or by using the buildpath.configuration file (yes, it’s fixed. It will read those paths right ;)).
Our development repository is merged with the master branch, what means that we are one step closer to the 4.7.x release. 🙂
So, stay tuned!

Getting started with Unit Testing

In this post I’ll talk about the concepts and theory of Unit Testing, shortly after the way to implement them. If you were thinking that the “algebra and such as” isn’t part of the practical programming then you were wrong! In this field the set of keyword contains only 1 important element – assert – which either generates an exception or not. Or, more precisely: you have a Groupoid defined on YourClass with (YourClass, assert).
…search engines and tutorials are not the key elements of this game, you will rather use your natural logic. 😉

So, what is Unit Testing about? I could paste here some Wikipedia definition, and after reading it you could ask yourself: why to test some parts of my program once it’s debugged and works correctly.

Imagine the following situation: you have an open-source project – because you are cool -. It’s source code is quite big, consists from 100 unique classes and 1000 definitions and holds 10000 lines of code. As I said, its open source, what means today you’ll hack something on the getFeedProperties() item, tomorrow I’ll and after a week a third person.

After a month when you are starting to package your software you discover that something is wrong with the program. It’s working, but in a little different way. Actually it’s showing the Feed’s URL instead of their desired name (yeah, it’s probably a feed reader).

What will you do? Of course debug it! You assume that the bug appears in the nth class. After checking the formatting parameters, the string output, etc. you discover that everything is fine in this class. No problem, backtrack it, look at the n-1th class and increment you personal wastedTime variable with 1 :P. Unfortunately in the n-1th everything is okay, so wastedTime++, take a look to n-2th class. After k steps you will reach the last class in your debugging, and you will found out that the getFeedProperties() returns the desired object, but the object’s name field is set to the feed’s URL.
Conclusion: problem solved with time requirement of O(n-k), it’s linear, not bad!
Could be it better?

What happens if your project has built with Unit Testing? When a third person submits his own patch for something what breaks something else, your automated Unit Test will report if some method is working different then it’s expected. So, if I should define: Unit Testing is about defining the behavior of different parts in your program between the given circumstances.

Take a look to this pseudo code:

// from the feed reader messed up by a 3rd person
FeedProperty getFeedProperties() {
...
}
...
// from FeedReaderTest test class
...
Feed f = new Feed("http://planet.ubuntu.com/rss20.xml", ....);

void testGetFeedProperties() {
...
FeedProperty fp = f.getFeedProperties();
...
assert fp.getFeedName == "Ubuntu"
//an exception will be generated here, because the getFeedProperties placed wrong string into the "name" field of your "f" object. Your class is inconsistent. The bug is obviously here.
}

Time requirement: O(1), the bug were found by running the top of your Unit Testing class.

Any thoughts, comments, spams are welcome!

Getting started with KDE development on openSUSE

Hi!

In this guide I’ll share my experiences and show you how to get started with KDE development. This article is based on the opensuse-wiki article on my personal solutions and on the helpful suggestions from the guys on #kde-devel channel.

If you are looking for something more official take a look to KDE TechBase.

Requirements:

    1) Space required for source files, executables, etc. is ~6.5GB (yes, pretty huge because building with full debugging symbols).
    2) Build dependencies: Here is the list for several distributions.
    3) an svn client

The openSUSE KDE team also provides two scripts kde4-run-env, kde4-build-env to set up the environment variables for the build dependencies (2).

  • Set up the environment

Some guides are suggesting the use of different startup scripts also. However I’ve created a new user for KDE development to protect my stable KDE configurations from my mistakes:

useradd -m -G users,audio,video -s /bin/bash kde-devel

you can do it without the -G option – like I did – If you want to create a user with default groups. And finally set a password for this user:

passwd kde-devel

Now you can continue your work on different ways, I did the following: my graphical session is running on tty6 and i started a new terminal session on tty1. To switch between terminal sessions use the <Ctrl>+<Alt>+<Fn>, n = 1,…,12 key combinations. This isn’t the best solution from the view of debugging because you can’t use clipboard between your terminal and graphical session and if something goes wrong and you need to copy+paste an output to the IRC channel/forums then this operation takes extra time.

So, Instead of switching to a new terminal session open your terminal emulator in your graphical session – Konsole – and log in with the kde-devel user and jump to the home directory:

su kde-devel
cd ~

Copy the scripts kde4-run-env, kde4-build-env into kde-devel‘s home directory. and add the following line to the~/.profile:

. ~/kde4-build-env

Now execute the script kde4-build-env with

. kde4-build-env

– actually re-logging with kde-devel have the same effect.

  • Download the sources

We’ll store everything what is related to KDE sources/build in the directory kde. Under kde create a subdirectory for the sources, and finally jump into the sources directory:

mkdir ~/kde/sources
cd ~/kde/sources

Now get the directory structure of KDE svn trunk:

svn checkout -N svn://anonsvn.kde.org/home/kde/trunk

with the -N option you are getting only the root directories without its contents. This will be useful later, after every checkout take a look to the current directory structure – just to see what are you doing.
So, you’ve checked out the trunk directory, go there

cd trunk

and check out the remaining directories.

svn up -N KDE
cd KDE

KDE is the last directory on the svn before the three module directories: kdebase kdelibs kdepimlibs – containing: base runtime elements, Personal Information Management modules and the basic libraries. Check out the sources for every module with:

svn up kdelibs kdepimlibs kdebase.
*This may take some time depending on your network connection speed.

  • Build the sources

We have all the sources just need to build them. The command cs is provided by the script kde4-build-env – if you’ve added the script properly to the .profile. First we need to build the kdelibs directory. Change the source patch to kdelibs with:

cs KDE/kdelibs

and start to build kdeblis:

cmakekde
*This may take some time depending on several factors, like systems load, CPU speed, etc.

Repeat this for the remaining modules:

cs KDE/kdepimlibs
cmakekde
cs KDE/kdebase
cmakekde

Remember, you can pause the building/compiling with <Ctrl>+<Z>:
[1]+ Stopped cmakekde
and resume with:
fg 1.

  • Run KDE 4

You can start from the terminal or with KDM. I started from a terminal session the KDM way is seems to be more complicated to implement.
Create a script runKDE:


#!/bin/bash
echo Setting up the KDE runtime environment.
. kde4-run-env
echo Starting the DBus session.
eval `dbus-launch --auto-syntax`
echo Starting the X server.
export DISPLAY=:1
X -novtswitch $DISPLAY &
echo Starting KDE 4 on tty8
sleep 2
startkde

and copy into /home/kde-devel/. Log out with kde-devel in your Konsole session. Switching between terminal sessions explained under “Set up the environment”. Now switch to tty1 and login as kde-devel and run the script runKDE:


./runKDE

if the script doesn’t switch automatically to tty8 – where your KDE is starting – then switch manually. The first start takes more time because KDE is build the configuration from scratch.

  • SVN Changes

For updating the sources just do:

cs KDE
svn up kdelibs kdepimlibs kdebase

and repeat the steps under “Build the sources”.

It’s possbile that while trying to “svn up” your sources svn returns with an error like:
No such revision X. This happens because the synchronization between the mirrors providing the sources for the anonymous svn. Wait a few minutes and try again.

All comments and corrections are appreciated.

Happy hacking!

KDE 4 trunk

Plasmoids

KDE 4 turnk

Desktop

YAMI Neutral Builds!

I’ve just a found a great hosting service at http://www.heliohost.com and uploaded the first neutral build of YAMI. You can find the packages @ http://yami.heliohost.org/.
After downloading just start with:

java -jar yamiSVN181.jar

Remember:In software development, a neutral build is a build that reflects the current state of the source code checked into the source code version control system by the developers, but without any developer-specific changes.

return 0

First of all: this blog isn’t dead! I just made a little vacation. 🙂
…and I’m back! with new experiences what I want to share with YOU.
Lets get start with my project YAMI. On the site http://yami.googlecode.com you can see that the latest change is from 2009.02.06. :S Yes I was to lazy to touch this code, but today I committed the latest revision r181 with some minimalistic changes like new code formatting rules and such as, and I also added the latest version of the JDBC connector’s license. I’m still searching for contributors, please contact me if you have some experience in Java!
You can download the Nightly builds of YAMI – precompiled packages, just start them with:
java -jar yami_nightlyBuild.jar
(Nightly builds are unstable versions, use only for testing!)
The latest stable version is 0.1.3 download here: http://code.google.com/p/yami/downloads/list.
For the source code take a look to: http://code.google.com/p/yami/source/browse/#svn/trunk.

New topic.
Qt Jambi – i started to write a netlimiter-like application in Java using the Qt Jambi framework. I still agree that the framework is lightweight and looks pretty cool compared to the old Java metal style, but… deploying a pure Qt Jambi application is a nightmare.
Why? The documentation is still incomplete, they have no community site or forum (I’ve already mentioned this on qtlabs). The only thing what the Jambi community have is a mailing list, what is IMHO a bit uncomfortable. I want to finish this project (without Qt Jambi OFC) because there is no netlimiter-like application for Linux with graphical user interface – or at least I can’t find any of them. 🙂
If somebody is interested in this project, please drop and e-mail or add a comment!

Ow, new theme ^^, cool isn’t? 😉