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);
      oos.writeObject(originalInvoiceKey);
      oos.close();

      // 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();
      ois.close();

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());

Regards,

Slava Imeshev

(3) Comments: Post a response
  1. yoel said at 11:15 am on July 26th, 2011:

    Nice post.

    Why do you use magic number 31 for the hashCode?

  2. simeshev said at 5:16 pm on July 26th, 2011:

    That’s a simple way to generate a simple hascode that IntelliJ uses. Cacheonix provides an API to generate better hashcodes, cacheonix.util.HashCode:

    final HashCode hashCodeCalculator = new HashCode();
    hashCodeCalculator.add(invoiceID);
    hashCodeCalculator.add(customerID);
    return hashCodeCalculator.getValue();

    Slava Imeshev
    Cacheonix: Distributed Java Cache

  3. Gaurav Chopra said at 1:34 pm on July 28th, 2011:

    It’s an excellent to test out the things. Thanks for sharing the information.

You must be logged in to post a comment.