Tag

Posts tagged with ‘java’

Replacing a HashSet with a BitSet

kinow @ Oct 20, 2012 19:51:39

I always read the messages in the Apache dev mailing lists, including Apache Commons dev mailing list. And you should too. There are always interesting discussions. Sometimes you participate, other times you only watch what’s happening, but in the end you always learn something new.

A few days ago, I found an issue where it was being proposed to replace an unnecessary HashSet in ArrayUtils#removeElements() by a BitSet. Here’s how the code looked like:

HashSet<Integer> toRemove = new HashSet<Integer>();
for (Map.Entry<Character, MutableInt> e : occurrences.entrySet()) {
    Character v = e.getKey();
    int found = 0;
    for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) {
        found = indexOf(array, v.charValue(), found);
        if (found < 0) {
            break;
        }
        toRemove.add(found++);
    }
}
return (char[]) removeAll((Object)array, extractIndices(toRemove));

The HashSet created at line 1, in the code above, was used to store the array index of the elements that should be removed. And at line 13 there is a call to removeAll method, passing the indexes to be removed. And here’s how the new code looks like:

BitSet toRemove = new BitSet();
for (Map.Entry<Character, MutableInt> e : occurrences.entrySet()) {
    Character v = e.getKey();
    int found = 0;
    for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) {
        found = indexOf(array, v.charValue(), found);
        if (found < 0) {
            break;
        }
        toRemove.set(found++);
    }
}
return (char[]) removeAll(array, toRemove);

The first difference is at line 1. Instead of a HashSet, it is now using a BitSet. And at line 10, instead of adding a new element to the HashSet, now it “sets” a bit in the set (the bit at the specified position is now true). But there are important changes at line 13. The method removeAll was changed, and now the array doesn’t require a cast anymore. And the it is not necessary to cast the elements from HashSet anymore, as now the bit in the index position of the set is set to true. So the extractIndices method could be removed.

The code got simpler. But that’s not all. At Apache Software Foundation you can find a lot of talented developers - that’s why I got so excited after joining them. Besides simplifying the code, the developer responsible for these changes (sebb) also pointed out that the new code consumes less memory and is faster. Ah! And he also wrote unit tests

Invoking Testopia XML-RPC or JSON methods using Java

kinow @ Sep 09, 2012 16:08:09

Most TestLink [1] users are aware that there is an external API, maybe for the external API token being displayed in the user profile section. Today after a meeting with Peter Florijn [2], I realized that the same may not be true for Testopia [3] users.

I am quite new to Testopia, and there are many features that I haven’t used yet. But if I understand it correctly, the database is interfaced by several Perl scripts that are, by its turns, exposed as Web Service (most of them). The web services are available via a JSON and a XML-RPC API (what is very useful, TestLink supports supports only XML-RPC).

The communication between different programming languages and the external API’s is accomplished by a client API. In TestLink you have testlink-java-api [4] and testlink-api-java-client [5].

Testopia has a Java client too, available in Testopia source repository [6] and can be used to integrate your existing Java code with Testopia.

( Read more … )

Writing code to integrate Java projects to Testopia

kinow @ May 18, 2012 17:16:25

Peter Florijn and I are writing a Jenkins plug-in to integrate several test tools into Jenkins, something similar to what is done in TestLink plug-in. It’s still an idea being explored, and the whole project is subjected to changes without warning. The code is at https://github.com/kinow/testthemall.

The first tool that we are integrating is not TestLink, but Mozilla Testopia. As part of the process to integrate these tools, many Java API’s to interface the existing external APIs will be created, like it was done in TestLink with TestLink Java API.

Installing Testopia is very easy and straightforward. This was the best guide that I could find, and worked without errors at my Debian Squeeze. I only had to move the directories from /var/www to my home directory (I use my PHP Eclipse workspace as Apache home).

Testopia has a XML-RPC APi, just like TestLink, however it lacks an user friendly documentation and examples. I migrated the Java driver from Ant to Maven, for the sake of commodity. But the XML-RPC server is complaining that I have to log-in before listing the test cases of a test plan.

If you are interested in using Java and Testopia, here’s the link for the java project with Maven support: https://github.com/kinow/testopia-java-driver. I will update the project with examples, more tests and will try to clean up the code. Probably I will use either GitHub pages or a Wiki somewhere to document how to use Testopia and Java.

Stay tuned!

Cheers,

Maven site tips: Maven Fluido Skin and Javadoc class diagrams

kinow @ Apr 06, 2012 21:01:52

I have been using Maven sites for a while, and am very happy with it. I didn’t like to have to update my projects after Maven 3, but that was all right, Maven 3 brought many new cool things. However, there were two things that annoyed me: lack of a nice and modern skin, and browsing Javadoc of complex code. The thought of creating a custom Maven skin even crossed my mind, but I never had time to read about it.

But the world is full of good and talented people! Like the guys from 99soft. They created Maven Fluido Skin, and donated it to Apache Software Foundation. It’s built on top of Twitter’s Bootstrap¬†and¬†available from Maven central repository. In order to use it in your Maven project, all that you have to do is add the following settings into your src/site/site.xml:

<skin>
    <groupId>org.apache.maven.skins</groupId>
    <artifactId>maven-fluido-skin</artifactId>
    <version>1.2.1</version>
</skin>

Here’s a list of some projects using Maven Fluido Skin (hopefully, in the near future Apache Commons and other projects will adopt this skin as default too :-)):

( Read more … )

Practical test doubles: adding stubs to TestLink Java API

kinow @ Oct 23, 2011 22:10:02

Test Double is a generic term for any case where you replace a production object for testing purposes” [1]. There are different types of Test Doubles: Dummy, Fake, Stubs, Spies and Mocks. In this post we will see a practical example of adding stubs to TestLink Java API.

Stubs are objects that return canned answers to calls during tests [1]. This is useful specially when you have a system that communicates with different resources such as databases, web services, XML-RPC services and so on.

TestLink Java API is a small Java project created to act as an interface between TestLink XML-RPC API and a client program written in Java.

( Read more … )