001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.io.*;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * ComponentSerialize
028: */
029: public class ComponentSerialize extends TestCase {
030:
031: @SuppressWarnings("serial")
032: static final class MyComponent extends Component {
033: }
034:
035: public void testAll() throws IOException, ClassNotFoundException {
036: writeAndRead(new MyComponent());
037: writeAndRead(new Container());
038: writeAndRead(new Canvas());
039: writeAndRead(new Panel());
040: writeAndRead(new Frame());
041: writeAndRead(new Dialog(new Frame()));
042: }
043:
044: public void testReadWriteComponent() throws IOException,
045: ClassNotFoundException {
046: MyComponent c = new MyComponent();
047: MyComponent t = (MyComponent) writeAndRead(c);
048: compareComponents(c, t);
049: }
050:
051: public void testReadWriteFrame() throws IOException,
052: ClassNotFoundException {
053: Frame c = new Frame("Frame");
054: Frame t = (Frame) writeAndRead(c);
055: compareComponents(c, t);
056:
057: assertEquals(c.getTitle(), t.getTitle());
058:
059: assertNotNull(t.getWindowListeners());
060: assertNotNull(t.getWindowStateListeners());
061: }
062:
063: private void compareComponents(Component original,
064: Component restored) {
065:
066: // assertEquals(t, c);
067: assertEquals(restored.getClass(), original.getClass());
068: assertEquals(restored.getBounds(), original.getBounds());
069:
070: // verify transient fields
071: assertEquals(restored.toolkit, original.toolkit);
072:
073: assertNotNull(restored.behaviour);
074: assertEquals(restored.behaviour.getClass(), original.behaviour
075: .getClass());
076:
077: assertNotNull(restored.getComponentListeners());
078: assertNotNull(restored.getFocusListeners());
079: assertNotNull(restored.getHierarchyListeners());
080: assertNotNull(restored.getHierarchyBoundsListeners());
081: assertNotNull(restored.getKeyListeners());
082: assertNotNull(restored.getMouseListeners());
083: assertNotNull(restored.getMouseMotionListeners());
084: assertNotNull(restored.getMouseWheelListeners());
085: assertNotNull(restored.getInputMethodListeners());
086:
087: }
088:
089: private Component writeAndRead(Component original)
090: throws IOException, ClassNotFoundException {
091: File tempFile = File.createTempFile("save", ".object");
092:
093: FileOutputStream fos = new FileOutputStream(tempFile);
094:
095: ObjectOutputStream oos = new ObjectOutputStream(fos);
096: oos.writeObject(original);
097: oos.close();
098:
099: FileInputStream fis = new FileInputStream(tempFile);
100: ObjectInputStream ois = new ObjectInputStream(fis);
101:
102: Component restored = (Component) ois.readObject();
103: tempFile.delete();
104: return restored;
105: }
106: }
|