KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > internal > ser > TestServiceSerializationHelper


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

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 JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25
26 import org.apache.hivemind.ApplicationRuntimeException;
27 import org.apache.hivemind.Registry;
28
29 /**
30  * Tests for {@link org.apache.hivemind.internal.ser.ServiceSerializationHelper}.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 1.1
34  */

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 JavaDoc
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 JavaDoc o) throws Exception JavaDoc
84     {
85         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
86         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bos);
87
88         oos.writeObject(o);
89
90         oos.close();
91
92         return bos.toByteArray();
93     }
94
95     private Object JavaDoc deserialize(byte[] data) throws Exception JavaDoc
96     {
97         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(data);
98         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bis);
99
100         return ois.readObject();
101     }
102 }
Popular Tags