KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > windows > TopComponentTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.windows;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.ObjectInputStream JavaDoc;
25 import junit.framework.TestCase;
26 import org.openide.util.io.NbMarshalledObject;
27
28 /**
29  *
30  * @author mkleint
31  */

32 public class TopComponentTest extends TestCase {
33
34     public TopComponentTest(String JavaDoc testName) {
35         super(testName);
36     }
37
38     /**
39      * Test of readExternal method, of class org.openide.windows.TopComponent.
40      */

41     public void testReadExternal() throws Exception JavaDoc {
42         // first try tc with displayname
43
TopComponent tc = new TopComponent();
44         tc.setName("testName");
45         tc.setDisplayName("testdisplayName");
46         tc.setToolTipText("testTooltip");
47         NbMarshalledObject obj = new NbMarshalledObject(tc);
48         tc.close();
49         
50         tc = (TopComponent)obj.get();
51         assertNotNull("One again", tc);
52         assertEquals("testName", tc.getName());
53         assertEquals("testTooltip", tc.getToolTipText());
54         assertEquals("testdisplayName", tc.getDisplayName());
55         
56         // now try tc withOUT displayname
57
tc = new TopComponent();
58         tc.setName("testName");
59         tc.setToolTipText("testTooltip");
60         obj = new NbMarshalledObject(tc);
61         tc.close();
62         
63         tc = (TopComponent)obj.get();
64         assertNotNull("One again", tc);
65         assertEquals("testName", tc.getName());
66         assertEquals("testTooltip", tc.getToolTipText());
67         assertNull(tc.getDisplayName());
68         
69     }
70     
71     /**
72      * Test of readExternal method, of class org.openide.windows.TopComponent.
73      */

74     public void testOldReadExternal() throws Exception JavaDoc {
75         TopComponent tc = null;
76         try {
77             ObjectInputStream JavaDoc stream = new ObjectInputStream JavaDoc(
78                     getClass().getResourceAsStream("data/oldTcWithoutDisplayName.ser"));
79             tc = (TopComponent)stream.readObject();
80             stream.close();
81         } catch (Exception JavaDoc exc) {
82             exc.printStackTrace();
83             fail("Cannot read tc");
84         }
85         
86         
87         assertNotNull("One again", tc);
88         assertEquals("testName", tc.getName());
89         assertEquals("testTooltip", tc.getToolTipText());
90         assertEquals("If the old component does not have a display name, then keep it null", null, tc.getDisplayName());
91     }
92
93     TopComponent tcOpened = null;
94     TopComponent tcClosed = null;
95     
96     public void testOpenedClosed () throws Exception JavaDoc {
97         System.out.println("Testing property firing of TopComponent's registry");
98         tcOpened = null;
99         tcClosed = null;
100                 
101         TopComponent.getRegistry().addPropertyChangeListener(
102                 new PropertyChangeListener JavaDoc() {
103                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
104                         if (TopComponent.Registry.PROP_TC_OPENED.equals(evt.getPropertyName())) {
105                             tcOpened = (TopComponent) evt.getNewValue();
106                         }
107                         if (TopComponent.Registry.PROP_TC_CLOSED.equals(evt.getPropertyName())) {
108                             tcClosed = (TopComponent) evt.getNewValue();
109                         }
110                     }
111                 });
112                 
113         TopComponent tc = new TopComponent();
114         
115         tc.open();
116         assertNotNull("Property change was not fired, tcOpened is null", tcOpened);
117         assertEquals("New value in property change is wrong", tc, tcOpened);
118                 
119         tc.close();
120         assertNotNull("Property change was not fired, tcClosed is null", tcClosed);
121         assertEquals("New value in property change is wrong", tc, tcClosed);
122         
123     }
124     
125     
126     
127 }
128
Popular Tags