01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.awt;
19:
20: import java.io.ByteArrayInputStream;
21: import java.io.ByteArrayOutputStream;
22: import java.io.IOException;
23: import java.io.ObjectInputStream;
24: import java.io.ObjectOutputStream;
25: import java.io.Serializable;
26: import java.util.EventListener;
27:
28: import junit.framework.TestCase;
29:
30: public class ListenerListTest extends TestCase {
31:
32: static class SerializableListener implements EventListener,
33: Serializable {
34: private static final long serialVersionUID = 1L;
35: }
36:
37: public void testSerialize() {
38: ListenerList<EventListener> list = new ListenerList<EventListener>();
39:
40: list.addUserListener(new EventListener() {
41: });
42: assertTrue(list.getUserIterator().hasNext());
43:
44: list.addSystemListener(new SerializableListener());
45: assertTrue(list.getSystemIterator().hasNext());
46:
47: ListenerList<EventListener> restored = writeAndRead(list);
48: assertFalse(restored.getUserIterator().hasNext());
49: assertTrue(restored.getSystemIterator().hasNext());
50:
51: }
52:
53: @SuppressWarnings("unchecked")
54: ListenerList<EventListener> writeAndRead(
55: ListenerList<EventListener> original) {
56: try {
57: ByteArrayOutputStream byteStream = new ByteArrayOutputStream(
58: 512);
59: ObjectOutputStream oos = new ObjectOutputStream(byteStream);
60: oos.writeObject(original);
61: oos.flush();
62: oos.close();
63:
64: ObjectInputStream ois = new ObjectInputStream(
65: new ByteArrayInputStream(byteStream.toByteArray()));
66:
67: ListenerList<EventListener> restored = (ListenerList<EventListener>) ois
68: .readObject();
69: return restored;
70:
71: } catch (IOException e) {
72: throw new AssertionError(e);
73: } catch (ClassNotFoundException e) {
74: throw new AssertionError(e);
75: }
76: }
77: }
|