PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/SerializationManagerTest.java

http://datanucleus-appengine.googlecode.com/
Java | 101 lines | 67 code | 11 blank | 23 comment | 0 complexity | 4f452026df44e877f385b7eaf1d538d3 MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus;
 17
 18import com.google.appengine.api.datastore.Blob;
 19import com.google.appengine.datanucleus.test.jdo.HasSerializableJDO;
 20
 21import junit.framework.TestCase;
 22
 23import org.datanucleus.ClassLoaderResolverImpl;
 24import org.datanucleus.exceptions.NucleusException;
 25import org.datanucleus.metadata.AbstractMemberMetaData;
 26import org.datanucleus.metadata.ExtensionMetaData;
 27
 28import java.io.Serializable;
 29
 30/**
 31 * SerializationManager tests.
 32 *
 33 * @author Max Ross <maxr@google.com>
 34 */
 35public class SerializationManagerTest extends TestCase {
 36
 37  public void testDeserialize_NonObjectBytes() {
 38    try {
 39      SerializationManager.DEFAULT_SERIALIZATION_STRATEGY.deserialize(new Blob("yar".getBytes()), getClass());
 40      fail("Expcted NucleusException");
 41    } catch (NucleusException ne) {
 42      // good
 43    }
 44  }
 45
 46  public static final class MySerializable1 implements Serializable {}
 47  public static final class MySerializable2 implements Serializable {}
 48
 49  public void testDeserialize_BytesAreOfWrongType() {
 50    MySerializable1 ser1 = new MySerializable1();
 51    Blob ser1Blob = SerializationManager.DEFAULT_SERIALIZATION_STRATEGY.serialize(ser1);
 52    try {
 53      SerializationManager.DEFAULT_SERIALIZATION_STRATEGY.deserialize(ser1Blob, MySerializable2.class);
 54      fail("Expcted NucleusException");
 55    } catch (NucleusException ne) {
 56      // good
 57    }
 58  }
 59
 60  public void testGetDefaultSerializer() {
 61    SerializationManager mgr = new SerializationManager();
 62    AbstractMemberMetaData ammd = new AbstractMemberMetaData(null, "yar") {};
 63    assertSame(SerializationManager.DEFAULT_SERIALIZATION_STRATEGY, mgr.getSerializationStrategy(new ClassLoaderResolverImpl(), ammd));
 64  }
 65
 66  public void testGetCustomSerializer() {
 67    SerializationManager mgr = new SerializationManager();
 68    AbstractMemberMetaData ammd = new AbstractMemberMetaData(null, "yar") {
 69      @Override
 70      public ExtensionMetaData[] getExtensions() {
 71        ExtensionMetaData emd = new ExtensionMetaData(
 72            "datanucleus",
 73            SerializationManager.SERIALIZATION_STRATEGY_KEY,
 74            HasSerializableJDO.ProtocolBufferSerializationStrategy.class.getName());
 75        return new ExtensionMetaData[] {emd};
 76      }
 77    };
 78    SerializationStrategy serializationStrategy = mgr.getSerializationStrategy(new ClassLoaderResolverImpl(), ammd);
 79    assertTrue(serializationStrategy instanceof HasSerializableJDO.ProtocolBufferSerializationStrategy);
 80  }
 81
 82  public void testGetCustomSerializer_BadClass() {
 83    SerializationManager mgr = new SerializationManager();
 84    AbstractMemberMetaData ammd = new AbstractMemberMetaData(null, "yar") {
 85      @Override
 86      public ExtensionMetaData[] getExtensions() {
 87        ExtensionMetaData emd = new ExtensionMetaData(
 88            "datanucleus",
 89            SerializationManager.SERIALIZATION_STRATEGY_KEY,
 90            getClass().getName());
 91        return new ExtensionMetaData[] {emd};
 92      }
 93    };
 94    try {
 95      mgr.getSerializationStrategy(new ClassLoaderResolverImpl(), ammd);
 96      fail("Expectd NucleusException");
 97    } catch (NucleusException ne) {
 98      // good
 99    }
100  }
101}