Testing Keys and Values for Distributed Java Caching

Posted: July 23rd, 2011 | Author: | Filed under: Distributed Caching

In distributed caching, cache keys and cached values routinely travel across the network. That’s why it is critical to write proper unit tests for keys and values in order to avoid unpleasant production surprises. This post tells how to test keys and values for distributed caching.

Distributed caching imposes an additional requirement towards unit tests for cache keys and cached values. Particularly, you must ensure that the object that was received at another end is the object that was sent. Here is how to tests it:

First, serialize an object to a byte array and then deserialize the byte array back to an object

      // Serialize the object
      ByteArrayOutputStream baos = new ByteArrayOutputStream(100);
      ObjectOutputStream oos = new ObjectOutputStream(baos);

      // Deserialize the object in serialized form
      final byte[] serializedInvoiceKey = baos.toByteArray();
      ByteArrayInputStream bais = new ByteArrayInputStream(serializedInvoiceKey);
      ObjectInputStream ois = new ObjectInputStream(bais);
      final InvoiceKey deserializedInvoiceKey = (InvoiceKey) ois.readObject();

Second, assert that the deserialized object and the original object are equal:

      assertEquals(originalInvoiceKey, deserializedInvoiceKey);

While you have already implemented methods equals() and hashCode() for the key, cached values need to have that methods added. For value objects, equals() and hashCode() need to satisfy a requirement for ‘literal equality’. In other words, to test that a value object can be serialized and deserialized, its equals() and hashCode() should include all non-transient fields:

public final class Invoice implements Externalizable {

   private int invoiceID;
   private Date invoiceDate = null;
   private int invoiceNumber;
   private int customerID;


   public boolean equals(final Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
      final Invoice invoice = (Invoice) o;
      if (invoiceID != invoice.invoiceID) return false;
      if (invoiceNumber != invoice.invoiceNumber) return false;
      if (customerID != invoice.customerID) return false;
      if (invoiceDate != null ? !invoiceDate.equals(invoice.invoiceDate) : invoice.invoiceDate != null) return false;
      return true;

   public int hashCode() {
      int result = invoiceID;
      result = 31 * result + (invoiceDate != null ? invoiceDate.hashCode() : 0);
      result = 31 * result + invoiceNumber;
      result = 31 * result + customerID;
      return result;

If adding equals() and hashCode() to the cached value is impossible, use explicit assertEquals() for object’s fields:

      assertEquals(invoiceID, deserializedInvoiceKey.getInvoiceID());


Slava Imeshev

(3) Comments: Post a response

Freeing Desk Space with Docking Stand

Posted: June 13th, 2011 | Author: | Filed under: General

My laptop takes quite some space on my  desk. The good news, there is a neat solution that allows to reduce its footprint almost 10 times.

I am using Mac Pro and MacBook Pro as development machines while working on Cacheonix. The Mac Pro is tucked under my desk and is practically invisible. Yet, the MacBook has been occupying a noticeable chunk of the desk surface, which is not nice:

Macbook Taking Space on the Table

The obvious solution was to set it upright, but how? My requirements to the solution were: simple; stable; good-looking.  An extensive research fetched a winner – Power Support’s Docking Stand for MacBook. This neat device satisfies all my requirements. The assembly includes pads to prevent scratching. The aluminum color that matches my MacBook’s body is a nice bonus. $40 price tag is a steal.

Assembly has taken 2 minutes and after getting it all together the MacBook has practically disappeared from the surface of the desk freeing about 50 sq. in:

Macbook with Docking Stand

Too bad PowerSupport stopped making those.  Henge Docks now makes a more advanced and a bit more expensive model that allows to really dock your Macbook.


Slava Imeshev
Cacheonix: Reliable Distributed Java Cache

(No) Comments: Post a response