1 15 package org.apache.hivemind.internal.ser; 16 17 import hivemind.test.FrameworkTestCase; 18 import hivemind.test.services.SimpleModule; 19 import hivemind.test.services.SimpleService; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 26 import org.apache.hivemind.ApplicationRuntimeException; 27 import org.apache.hivemind.Registry; 28 29 35 public class TestServiceSerializationHelper extends FrameworkTestCase 36 { 37 private ServiceSerializationSupport newSupport() 38 { 39 return (ServiceSerializationSupport) newMock(ServiceSerializationSupport.class); 40 } 41 42 public void testGetNoSupport() 43 { 44 try 45 { 46 ServiceSerializationHelper.getServiceSerializationSupport(); 47 unreachable(); 48 } 49 catch (ApplicationRuntimeException ex) 50 { 51 assertEquals(SerMessages.noSupportSet(), ex.getMessage()); 52 } 53 } 54 55 public void testStore() 56 { 57 ServiceSerializationSupport s = newSupport(); 58 59 replayControls(); 60 61 ServiceSerializationHelper.setServiceSerializationSupport(s); 62 63 assertSame(s, ServiceSerializationHelper.getServiceSerializationSupport()); 64 65 verifyControls(); 66 } 67 68 public void testIntegration() throws Exception 69 { 70 Registry r = buildFrameworkRegistry(new SimpleModule()); 71 72 SimpleService a = (SimpleService) r.getService(SimpleService.class); 73 74 AdderWrapper aw1 = new AdderWrapper(a); 75 76 byte[] data = serialize(aw1); 77 78 AdderWrapper aw2 = (AdderWrapper) deserialize(data); 79 80 assertEquals(17, aw2.add(9, 8)); 81 } 82 83 private byte[] serialize(Object o) throws Exception 84 { 85 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 86 ObjectOutputStream oos = new ObjectOutputStream (bos); 87 88 oos.writeObject(o); 89 90 oos.close(); 91 92 return bos.toByteArray(); 93 } 94 95 private Object deserialize(byte[] data) throws Exception 96 { 97 ByteArrayInputStream bis = new ByteArrayInputStream (data); 98 ObjectInputStream ois = new ObjectInputStream (bis); 99 100 return ois.readObject(); 101 } 102 } | Popular Tags |