Archive for February, 2008

PostgreSQL database backup and restore

Sunday, February 24th, 2008

In the last couple of weeks I have been using PostgreSQL quite a lot. I have used PostgreSQL before but this time it was different as the development as well as the deployment environment were Linux based, as a result I was denied of the use of pgAdminIII, the GUI based administration tool for PostgreSQL database. My only option was to use the command prompt.

The need of the hour was to backup the live database and restore it on the test environment, which incidentally was also a Linux box. With pgAdminIII, it would have been really simple, take a backup of the database in a binary format and restore the test database from the binary file. So, off I went, digging into the PostgreSQL documentation looking for the commands which will do the same for me and wolla, I found pg_dump and pg_restore.

pg_dump is the utility for backing up a PostgreSQL database. Following is the command with it’s options that will create a binary backup of an entire database;
pg_dump -i -h host-name -p port-number -U username -F c -b -v -f "backup-filename.backup" dbname

pg_restore is the utility for restoring a PostgreSQL database from an archive created by pg_dump in one of the non-plain-text formats. Following is the command with it’s options that will restore the database from the binary backup file;
pg_restore -i -h host-name -p port-number -U username -d dbname -a -v "backup-filename.backup"
I noticed an interesting point while using pg_restore. pg_restore doesn’t work properly if there are referential integrity among the tables on the target database as tables are restored sequentially. So, the best restoring approach is to first create the tables in the database without enforcing their referential integrity, then run pg_restore to perform the magic. Once restoring is complete you can easily run another script to enforce all the referential integrities at one go.

On a completely different note, it is always considered as a best practice to test your application with a real data-set. This will give you the opportunity to write off those final few bugs which are only exposed when the application is used with a real set of data. What else could be better than the replica of the live database! More over, now you can backup and restore entire PostgreSQL database, even without the help of pgAdminIII.

Unit testing

Saturday, February 23rd, 2008

Mockery is a new JMock class. It allows you to create mock implementations of your interfaces in your unit tests.

In the days before Mockery, you could still use JMock’s Mock class and then use human-readable style of configuring the expectations. For example, to mock the Sender interface and its send(String s) method, you’d write

Mock sender = new Mock(Sender.class);
PostOffice postOffice = new PostOffice();
postOffice.setSender((Sender)sender.proxy());

sender.expects(once()).method("send").with(eq("Hello")).isVoid();
postOffice.post("Hello");

Now, assuming the PostOffice.post(String s) uses the Sender implementation to ultimately send the message, this test would work. However, if you refactored the Sender.send(String s) method, your IDE would not be able to refactor the JMock expectation.

Enter Mockery

The same code using JMock’s new Mockery class would look like this:

Mockery context = new Mockery();
final PostOffice postOffice = new PostOffice();
final Sender sender = context.mock(Sender.class);
postOffice.setSender(sender);

final String message = "Hello";
context.checking(new Expectations() { {
    one(sender).send(message);
} });

postOffice.post(message);

Here you can see that the test still reads very well, but most IDEs will automatically refactor the unit test expectations whenever you refactor the Sender interface. Also notice that you do not need to explicitly state with(eq("Hello")) or with(eq(message)). Instead, you “call” the mocked method directly, passing it the values to be checked.

There is a lot more to Mockery, we recommend that you give it a fair try in your unit tests.

Clean & tidy

Saturday, February 23rd, 2008

I am sure you remember your mother telling you to clean your room when you were little. This usually involved taking out the hoover (or the vacuum cleaner for the Yahoos) and getting on with the job.

Fast forward a few years and switch to PostgreSQL. It is very good idea to turn on the auto_vacuum parameter or vacuum the tables manually every few days. If you do not do that, you may end up with a database with just a few tens of thousands of rows occupying over 4.5 GB on the hard drive.

Planning & ToC

Friday, February 15th, 2008

We are going to start using the theory of constraints to control our projects; I believe we have more resources than we need. I am hoping to prove that we can either all finish at four o’clock every day or take on more work and earn more money.

If you are interested, check out Wikipedia. I will send charts that display the buffer consumption and the feeder buffer consumption in a few weeks’ time. You will see just how easy it is to control all your projects.

JBoss and Spring JMS

Monday, February 11th, 2008

We are playing around with the JBoss application server 4.2.2GA and Spring JMS. The whole setup is very simple; assuming you want to use a JMS queue, you need to configure it in JBoss and then use the new Spring 2.5 jms namespace.

Let’s begin with the JBoss configuration. You need to drop the *-service.xml file into $SERVER_DIR/deploy/jms. The contents of the file define the destinations and queue names; the file we will show here includes two queues.


<server>
  <mbean code="org.jboss.mq.server.jmx.Queue"
	 name="jboss.mq.destination:service=Queue,name=invoice-data">
    <depends optional-attribute-name="DestinationManager">
        jboss.mq:service=DestinationManager
    </depends>
    <depends optional-attribute-name="SecurityManager">
        jboss.mq:service=SecurityManager
    </depends>
    <attribute name="MessageCounterHistoryDayLimit">-1</attribute>
    <attribute name="SecurityConf">
      <security>
        <role name="guest" read="true" write="true"/>
        <role name="publisher" read="true" write="true" create="false"/>
        <role name="noacc" read="false" write="false" create="false"/>
      </security>
    </attribute>
  </mbean>

Next, we define the Spring queueConnectionFactory and jmsTemplate beans, as shown in the following context file


<beans>
...
    <jee:jndi-lookup jndi-name="java:ConnectionFactory"
        id="queueConnectionFactory" expected-type="javax.jms.ConnectionFactory"/>
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate102">
        <property name="connectionFactory" ref="queueConnectionFactory"/>
    </bean>
    <jms:listener-container
        connection-factory="queueConnectionFactory"
        concurrency="10">
        <jms:listener destination="invoice-data"
             ref="mdb" method="mdbMethod"/>
    </jms:listener-container>
...
</beans>

The mdb bean refers to a standard POJO with the mdbMethod. The signature of the mdbMethod follows pattern void mdbMehtod(argument). Now, depending on the type of the message, the argument is either the object we send in ObjectMessage or String if we send TextMessages.

JUnit4’s advanced features

Monday, February 4th, 2008

In our Spring 2.5 flagship project, we have chosen to use JUnit 4 and its constraint-based asserts. This is similar to how you write mock objects using the jMock library.

In short, the assertions take form of assertThat(T t, Constraint<T>). The Constraint<T> is an interface that the framework calls to validate the value t.

To get started, download JUnit (4.4) and jMock, unzip the downloaded file and jump straight into writing a test.


import static org.junit.Assert.assertThat;
import org.junit.Test;

public class InvoiceTest {

    @Test public void testAddLine() {
        Invoice invoice = new Invoice();
        InvoiceLine line = new InvoiceLine();
        invoice.addLine(line);
        assertThat(line, org.hamcrest.collection.IsIn.isIn(invoice.getLines()));
    }

}

So here you have it. Your first test using JUnit jMock-style constraints rather than TestCase.assertXXX calls.

Cake’s numbers grow!

Friday, February 1st, 2008

The beginning of this week saw Ibrahim join Cake, bringing an additional two years Java application development experience to the team. Ibrahim has spent the week setting his machine up and getting use to the Cake systems and tools, on Monday he will begin work on one of our major projects. Monday also sees the arrival of Andrew Chalkley who adds new skills in to the Cake mix. Andrew adds Ajax and Ruby & Rails skills to our toolset as well as adding CSS, and OOP PHP expertise. Andrew will embark on a 4 week Cake internal Java training program before begging work on one of our current projects.

One of the next internal projects I intend to begin today is the updating and redesigning of the Cake website. Our current design has been with us for a couple of years now and although much of the website is current the portfolio section and our service section needs a rewrite. My intention is to update, add and delete some of the current content before looking at the design of the website. Watch this space.

One item on the website “The 30 Second Pitch” has been renamed to “Cake’s Value Proposition” and revised to “Cake Solutions helps companies achieve their business goals. We do this by taking time to understand your business and using our broad technical knowledge and specialist programming skills to produce software that truly helps your organisation.
Cake is made up of a team of talented J2EE and open source software developers and business savvy project management personnel. Members of the Cake team are published authors and Sun certified developers.
A recent system built for a central government agency is being used by 2,500 international users. The application began as a research project, which thanks to its success within the agency has now grown in to a mature, successful and high profile system.
Contact Cake if you know that your current systems are not performing as well as they should or you need an innovative solution to a business problem, on 0161 975 6110
.” Let me know what you think?

Finally Ani, Aleksa, Jan and I are all off to visit DeCare on Tuesday & Wednesday to help cement the newly formed partnership and to get a handover for some code that Cake will maintain recently written by DeCare. We are all looking forward to that trip immensely!

That’s it for now, have a great weekend.