<s> import cPickle as pickle <EOL> import unittest <EOL> from hazelcast . config import SerializationConfig <EOL> from hazelcast . serialization . api import StreamSerializer <EOL> from hazelcast . serialization . service import SerializationServiceV1 <EOL> class TestGlobalSerializer ( StreamSerializer ) : <EOL> def __init__ ( self ) : <EOL> super ( TestGlobalSerializer , self ) . __init__ ( ) <EOL> def read ( self , inp ) : <EOL> utf = inp . read_utf ( ) <EOL> obj = pickle . loads ( utf . encode ( ) ) <EOL> try : <EOL> obj . source = "<STR_LIT>" <EOL> except AttributeError : <EOL> pass <EOL> return obj <EOL> def write ( self , out , obj ) : <EOL> out . write_utf ( pickle . dumps ( obj ) ) <EOL> def get_type_id ( self ) : <EOL> return <NUM_LIT> <EOL> def destroy ( self ) : <EOL> pass <EOL> class CustomClass ( object ) : <EOL> def __init__ ( self , uid , name , text , source = None ) : <EOL> self . uid = uid <EOL> self . name = name <EOL> self . text = text <EOL> self . source = source <EOL> def __eq__ ( self , other ) : <EOL> if other : <EOL> return self . name == other . name and self . uid == other . uid and self . text == other . text <EOL> return False <EOL> class CustomSerializer ( StreamSerializer ) : <EOL> def write ( self , out , obj ) : <EOL> if isinstance ( obj , CustomClass ) : <EOL> out . write_utf ( obj . uid ) <EOL> out . write_utf ( obj . name ) <EOL> out . write_utf ( obj . text ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def read ( self , inp ) : <EOL> return CustomClass ( inp . read_utf ( ) , <EOL> inp . read_utf ( ) , <EOL> inp . read_utf ( ) , <EOL> "<STR_LIT>" ) <EOL> def get_type_id ( self ) : <EOL> return <NUM_LIT> <EOL> def destroy ( self ) : <EOL> pass <EOL> class TheOtherCustomSerializer ( StreamSerializer ) : <EOL> def write ( self , out , obj ) : <EOL> if isinstance ( obj , CustomClass ) : <EOL> out . write_utf ( obj . text ) <EOL> out . write_utf ( obj . name ) <EOL> out . write_utf ( obj . uid ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def read ( self , inp ) : <EOL> text_ = inp . read_utf ( ) <EOL> name_ = inp . read_utf ( ) <EOL> uid = inp . read_utf ( ) <EOL> return CustomClass ( uid , name_ , text_ , "<STR_LIT>" ) <EOL> def get_type_id ( self ) : <EOL> return <NUM_LIT> <EOL> def destroy ( self ) : <EOL> pass <EOL> class CustomSerializationTestCase ( unittest . TestCase ) : <EOL> def test_global_encode_decode ( self ) : <EOL> config = SerializationConfig ( ) <EOL> config . set_global_serializer ( TestGlobalSerializer ) <EOL> service = SerializationServiceV1 ( serialization_config = config ) <EOL> obj = CustomClass ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> data = service . to_data ( obj ) <EOL> obj2 = service . to_object ( data ) <EOL> self . assertEqual ( obj , obj2 ) <EOL> self . assertEqual ( "<STR_LIT>" , obj2 . source ) <EOL> def test_custom_serializer ( self ) : <EOL> config = SerializationConfig ( ) <EOL> config . set_custom_serializer ( CustomClass , CustomSerializer ) <EOL> service = SerializationServiceV1 ( serialization_config = config ) <EOL> obj = CustomClass ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> data = service . to_data ( obj ) <EOL> obj2 = service . to_object ( data ) <EOL> self . assertEqual ( obj , obj2 ) <EOL> self . assertEqual ( "<STR_LIT>" , obj2 . source ) <EOL> def test_global_custom_serializer ( self ) : <EOL> config = SerializationConfig ( ) <EOL> config . set_custom_serializer ( CustomClass , CustomSerializer ) <EOL> config . set_global_serializer ( TestGlobalSerializer ) <EOL> service = SerializationServiceV1 ( serialization_config = config ) <EOL> obj = CustomClass ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> data = service . to_data ( obj ) <EOL> obj2 = service . to_object ( data ) <EOL> self . assertEqual ( obj , obj2 ) <EOL> self . assertEqual ( "<STR_LIT>" , obj2 . source ) <EOL> def test_double_register_custom_serializer ( self ) : <EOL> config = SerializationConfig ( ) <EOL> config . set_custom_serializer ( CustomClass , CustomSerializer ) <EOL> service = SerializationServiceV1 ( serialization_config = config ) <EOL> with self . assertRaises ( ValueError ) : <EOL> service . _registry . safe_register_serializer ( TheOtherCustomSerializer , CustomClass ) </s>
