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

import junit.framework.TestCase;
import org.jboss.cache.Version;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * Tests the enhanced treecache marshaller
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class VersionAwareMarshallerTest extends TestCase
{
   public void testMarshallerSelection()
   {

      VersionAwareMarshaller marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.4.0.GA");
      assertEquals(TreeCacheMarshaller140.class, marshaller.defaultMarshaller.getClass());

      marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.5.0.GA");
      assertEquals(LegacyTreeCacheMarshaller.class, marshaller.defaultMarshaller.getClass());

      marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.3.0.GA");
      assertEquals(LegacyTreeCacheMarshaller.class, marshaller.defaultMarshaller.getClass());

      marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.3.0.SP2");
      assertEquals(LegacyTreeCacheMarshaller.class, marshaller.defaultMarshaller.getClass());

      marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.3.1.GA");
      assertEquals(LegacyTreeCacheMarshaller.class, marshaller.defaultMarshaller.getClass());

      marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.2.4.SP2");
      assertEquals(LegacyTreeCacheMarshaller.class, marshaller.defaultMarshaller.getClass());

      marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.2.3");
      assertEquals(LegacyTreeCacheMarshaller.class, marshaller.defaultMarshaller.getClass());
   }

   public void testVersionHeaderDefaultCurrent() throws Exception
   {
      VersionAwareMarshaller marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, Version.getVersionString(Version.getVersionShort()));
      byte[] bytes = marshaller.objectToByteBuffer("Hello");

      // expect that this has been serialized using JBoss Serialization so use this to get an OIS.
      ObjectInputStream in = ObjectSerializationFactory.createObjectInputStream(bytes);
      assertEquals("Version header short should be '14'", 14, in.readShort());
   }

   public void testVersionHeader140() throws Exception
   {
      VersionAwareMarshaller marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.4.0.GA");
      byte[] bytes = marshaller.objectToByteBuffer("Hello");

      // expect that this has been serialized using JBoss Serialization so use this to get an OIS.
      ObjectInputStream in = ObjectSerializationFactory.createObjectInputStream(bytes);
      assertEquals("Version header short should be '14'", 14, in.readShort());
   }

   public void testVersionHeaderLegacy() throws Exception
   {
      VersionAwareMarshaller marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.3.0.GA");
      byte[] bytes = marshaller.objectToByteBuffer("Hello");

      // expect that this has been serialized traditional serialization
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));

      try
      {
         in.readShort();
         fail("Version header short should not exist.");
      }
      catch (IOException e)
      {
         assertTrue("Expected an exception trying to read the version short", true);
      }
   }
}
