Constructors don’t get better with age!

The main goal of the article is to make you remember 2 things:

  1. Constructors aren’t like cheese i.e. they don’t get better with age
  2. An “ad litteram” iterative approach on writing constructors is BAD

How bad you wonder? Well… this bad:

new Admin(code,
 name,
 propId,
 locale,
 description,
 serviceNo,
 emailAddress1,
 emailAddress2,
 rfiEmailAddress,
 generalReports,
 vatNumber,
 partnerId,
 junk,
 dateJoining,
 appSessionId,
 legalType,
 legalTypeOther,
 contractRelationshipId,
 keyAccountTypeId,
 crmId,
 virtualTerminal,
 acquirerCountryConfirmed,
 acquirerUsesRegistration,
 sanction,
 timestamp,
 passwordExpiryDays,
 concurrentLogins,
 nonUseSuspensionDays,
 contractVersionId,
 calculateVAT,
 version);

Usually when you add a new filed Continue reading

My first open source project: Selenium on Steroids

The projects targets QA automation engineers that want a clean and simple way to create Selenium tests allowing them to focus more on designing the tests rather than dealing with technical difficulties. Using a simple property file and Maven profiles the instantiation of a WebDriver objects with different kind of settings reduces to just setting some predefined properties. The project enhances the functionality of the WebDriver API by offering additional methods for common tasks, having as a long run target to implement most of the Selenium IDE functions. SoS also offers different utility methods to interact with simple files, properties files, Excel documents, XML documents, Databases, post XML calls over HTTP, etc. Basically everything you need for your data driven testing scenarios.

Features

Using Google Guice in Web Applications

Introduction

As promised in the previous article I’ll continue presenting Google Guice also for web applications. In order to do this you’ll need to get the servlet extension – part of the standard distribution, along with other extensions like JMX, JNDI, Persist, Struts or Spring. Using Guice, the web.xml will be reduced at minimum – just make the Guice container start. The rest of the configurations will be easily done in Java in the same type-safe manner presented in the previous article.

The servlets will benefit from:

Code Review Guidelines – Part 2

Please read Part 1 first.

Automating the Code Review process

You can split the Code Review process into 2 parts: the Low Level part (styling, code duplicate, naming conventions, etc) and the High Level part (architecture, design, business).

Starting from a Coding Standards document, you can easily automate the Low level part of the Code Review process. The automation tools for Code Review will eliminate most of the styling issues, naming conventions, cyclomatic complexity, duplicate code, code coverage, etc. They can’t though detect major design issues, architectural flaws or project specific functionality and this is where the reviewer skills are the most important. I’ll present below the most used tools for Continue reading

Code review guidelines – Part 1

Last updated on 27th of January.

Please also read Code Review Guidelines Part 2.

What is a Code Review?

Code review is systematic examination (often known as peer review) of computer source code. It is intended to find and fix mistakes overlooked in the initial development phase, improving both the overall quality of Continue reading

The Generic DAO pattern in Java with Spring 3 and JPA 2.0

One thing that annoys me the most is code duplication. The DAO layer is the perfect candidate for this kind of situation. Often, developers forget about OOP, polymorphism and design patterns and just copy&paste code, change the name of the class and voila, we have a brand “new” BankDao class. I'll present you how to implement a generic DAO pattern to avoid code duplication and preserve type safety in the same time. Why would you care about type safety and just don't go use the EntityManager's generic methods. Well, for various reasons:

  1. You know for sure which entity objects can be persisted
  2. You avoid a lot of explicit casts which are error prone
  3. The code is cleaner and  very easy to follow
  4.  You actually apply OOP principles like inheritance and polymorphism. ;)

The purpose of this article is not to get you familiar with the DAO pattern, but to show you a better way of using it. You can find a complete reference about DAO here: DataAccessobject.

The Generic DAO interface

Let's get started. First of all this article assumes you are using Spring 3 (although this can be easily adapted to Spring 2.5) and JPA 2.0 in you project and the initial configuration is in place:  you already have a data sources declared, an entity manager factory, etc.  The application is basically up&running.

The foundation of using a Generic DAO is the CRUD operations that you can perform on each entity. And this is the least you can have. Additional generic methods can be defined like: count all objects of a specific type; execute generic queries based on some parameters, etc. You'll see a sample bellow for countAll().

The core of this pattern will be an interface called, surprisingly, GenericDao and its implementation Continue reading

Creating a URL JLabel in Swing

So, you need a simple clickable URL JLabel in your Swing application? You can, of course, use third party libraries to do this, but why add another dependency for a single component when can easily write one in 5 minutes.

So what do we want for this JLabel:

  • to click on it and go to an URL
  • to have a hand cursor on mouse over
  • the text to be underlined just like a real link

We’ll write a class that extends JLabel and a mouse adapter that will take care of opening the link. As the mouse adapter will be used only for this label, we’ll add it as a private member class. Here is the code:

package insidecoding.swing

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import javax.swing.JLabel;

/**
 *
 * @author ludovicianul
 */
public class URLLabel extends JLabel {

    private String url;

    public URLLabel() {
        this("","");
    }

    public URLLabel(String label, String url) {
        super(label);

        this.url = url;
        setForeground(Color.BLUE.darker());
        setCursor(
                new Cursor(Cursor.HAND_CURSOR));
        addMouseListener(
                new URLOpenAdapter());
    }

    public void setURL(String url) {
        this.url = url;
    }

    //this is used to underline the text
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.blue);

        Insets insets = getInsets();

        int left = insets.left;
        if (getIcon() != null) {
            left += getIcon().getIconWidth() + getIconTextGap();
        }

        g.drawLine(left, getHeight() - 1 - insets.bottom, (int) getPreferredSize().getWidth()
                - insets.right, getHeight() - 1 - insets.bottom);
    }

    private class URLOpenAdapter extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().browse(new URI(url));
                } catch (Throwable t) {
                    //
                }
            }
        }
    }
}

I think the code is pretty self-explanatory:

  • in the constructor we set the label color to a darker blue, we set the mouse over cursor to Hand Cursor and we register the mouse click listener
  • we use the paint() method to underline the label's text
  • the mouse adapter uses the Continue reading