/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.marshall;

import org.jboss.cache.Fqn;
import org.jboss.cache.GlobalTransaction;
import org.jgroups.stack.IpAddress;

import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class TreeCacheMarshaller140Test extends TreeCacheMarshallerTestBase
{
   public TreeCacheMarshaller140Test()
   {
      currentVersion = "1.4.0.GA";
      currentVersionShort = 14;
      expectedMarshallerClass = TreeCacheMarshaller140.class;
   }

   public void testKnownCollectionMagicNumbers() throws Exception
   {
      doMagicNumberTest(new ArrayList(), TreeCacheMarshaller140.MAGICNUMBER_ARRAY_LIST);
      doMagicNumberTest(new LinkedList(), TreeCacheMarshaller140.MAGICNUMBER_LINKED_LIST);
      doMagicNumberTest(new HashSet(), TreeCacheMarshaller140.MAGICNUMBER_HASH_SET);
      doMagicNumberTest(new TreeSet(), TreeCacheMarshaller140.MAGICNUMBER_TREE_SET);
      doMagicNumberTest(new HashMap(), TreeCacheMarshaller140.MAGICNUMBER_HASH_MAP);
      doMagicNumberTest(new TreeMap(), TreeCacheMarshaller140.MAGICNUMBER_TREE_MAP);

      // custom collections
      doMagicNumberTest(new MyList(), TreeCacheMarshaller140.MAGICNUMBER_SERIALIZABLE);
      doMagicNumberTest(new MySet(), TreeCacheMarshaller140.MAGICNUMBER_SERIALIZABLE);
      doMagicNumberTest(new MyMap(), TreeCacheMarshaller140.MAGICNUMBER_SERIALIZABLE);

      // empty collections
      doMagicNumberTest(Collections.EMPTY_LIST, TreeCacheMarshaller140.MAGICNUMBER_SERIALIZABLE);
      doMagicNumberTest(Collections.EMPTY_SET, TreeCacheMarshaller140.MAGICNUMBER_SERIALIZABLE);
      doMagicNumberTest(Collections.EMPTY_MAP, TreeCacheMarshaller140.MAGICNUMBER_SERIALIZABLE);
   }

   public void testKnownTypeMagicNumbers() throws Exception
   {
      doMagicNumberTest(new Fqn(), TreeCacheMarshaller140.MAGICNUMBER_FQN);
      doMagicNumberTest(new GlobalTransaction(), TreeCacheMarshaller140.MAGICNUMBER_GTX);
      doMagicNumberTest(new IpAddress(), TreeCacheMarshaller140.MAGICNUMBER_IPADDRESS);
      doMagicNumberTest(new Integer(10), TreeCacheMarshaller140.MAGICNUMBER_INTEGER);
      doMagicNumberTest(new Long(10), TreeCacheMarshaller140.MAGICNUMBER_LONG);
      doMagicNumberTest(Boolean.TRUE, TreeCacheMarshaller140.MAGICNUMBER_BOOLEAN);
      doMagicNumberTest("HELLO", TreeCacheMarshaller140.MAGICNUMBER_STRING);
      doMagicNumberTest(null, TreeCacheMarshaller140.MAGICNUMBER_NULL);
   }

   private void doMagicNumberTest(Object o, int magicNumber) throws Exception
   {
      byte[] bytes = marshaller.objectToByteBuffer(o);
      ObjectInputStream is = ObjectSerializationFactory.createObjectInputStream(bytes);
      short versionShort = is.readShort();
      assertEquals(currentVersionShort, versionShort);
      byte magicNumberFound = is.readByte();
      assertEquals("Magic number mismatch", magicNumber, magicNumberFound);
   }
}
