KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > test > ApplicationInstanceTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.test;
31
32 import nextapp.echo2.app.ApplicationInstance;
33 import nextapp.echo2.app.Component;
34 import nextapp.echo2.app.Label;
35 import nextapp.echo2.app.Column;
36 import nextapp.echo2.app.Window;
37 import junit.framework.TestCase;
38
39 /**
40  * Unit test(s) for the <code>nextapp.echo2.app.ApplicationInstance</code> object.
41  */

42 public class ApplicationInstanceTest extends TestCase {
43     
44     private class RegistrationTestComponent extends Component {
45         int initCount = 0;
46         int disposeCount = 0;
47         
48         public void dispose() {
49             super.dispose();
50             ++disposeCount;
51         }
52
53         public void init() {
54             super.init();
55             ++initCount;
56         }
57     }
58
59     private class ValidatingLabel extends Label {
60         
61         boolean valid = false;
62         
63         public void invalidate() {
64             valid = false;
65         }
66         
67         public void validate() {
68             super.validate();
69             valid = true;
70         }
71     }
72     
73     /**
74      * Test setting active (ThreadLocal) <code>ApplicationInstance</code>.
75      */

76     public void testActivation() {
77         assertNull(ApplicationInstance.getActive());
78         HelloWorldApp app = new HelloWorldApp();
79         assertNull(ApplicationInstance.getActive());
80         ApplicationInstance.setActive(app);
81         assertTrue(app == ApplicationInstance.getActive());
82         ApplicationInstance.setActive(null);
83         assertNull(ApplicationInstance.getActive());
84     }
85     
86     /**
87      * Test setting and retrieving contextual information.
88      */

89     public void testContext() {
90         HelloWorldApp app = new HelloWorldApp();
91         assertNull(app.getContextProperty("alpha"));
92         app.setContextProperty("alpha", "bravo");
93         assertEquals("bravo", app.getContextProperty("alpha"));
94         app.setContextProperty("alpha", null);
95         assertNull(app.getContextProperty("alpha"));
96     }
97
98     /**
99      * Test registration flag of components in hierarchies belong to the
100      * <code>ApplicationInstance</code>.
101      */

102     public void testRegistration() {
103         ColumnApp columnApp = new ColumnApp();
104         ApplicationInstance.setActive(columnApp);
105         
106         Window window = columnApp.doInit();
107         assertTrue(window.isRegistered());
108         assertTrue(columnApp.getColumn().isRegistered());
109         Label label = new Label();
110         assertFalse(label.isRegistered());
111         columnApp.getColumn().add(label);
112         assertTrue(label.isRegistered());
113         columnApp.getColumn().remove(label);
114         assertFalse(label.isRegistered());
115         columnApp.getColumn().add(label);
116         assertTrue(label.isRegistered());
117         columnApp.getContentPane().remove(columnApp.getColumn());
118         assertFalse(label.isRegistered());
119
120         ApplicationInstance.setActive(null);
121     }
122     
123     /**
124      * Test component-application registration life-cycle methods, i.e.,
125      * <code>Component.init()</code> / <code>Component.dispose()</code>.
126      */

127     public void testRegistrationLifecycle() {
128         ColumnApp columnApp = new ColumnApp();
129         ApplicationInstance.setActive(columnApp);
130         columnApp.doInit();
131         Column column = columnApp.getColumn();
132
133         RegistrationTestComponent rtc = new RegistrationTestComponent();
134         
135         assertEquals(0, rtc.initCount);
136         assertEquals(0, rtc.disposeCount);
137         
138         column.add(rtc);
139         
140         assertEquals(1, rtc.initCount);
141         assertEquals(0, rtc.disposeCount);
142         
143         column.remove(rtc);
144         
145         assertEquals(1, rtc.initCount);
146         assertEquals(1, rtc.disposeCount);
147         
148         ApplicationInstance.setActive(null);
149     }
150     
151     /**
152      * Test component-application registration life-cycle methods, i.e.,
153      * <code>Component.init()</code> / <code>Component.dispose()</code>
154      * with regard to initial hierarchy.
155      */

156     public void testRegistrationLifecycleInitialHierarchy() {
157         final RegistrationTestComponent rtc = new RegistrationTestComponent();
158
159         assertEquals(0, rtc.initCount);
160         assertEquals(0, rtc.disposeCount);
161         
162         ColumnApp columnApp = new ColumnApp(){
163         
164             public Window init() {
165                 Window window = super.init();
166                 getColumn().add(rtc);
167                 return window;
168             }
169         };
170         ApplicationInstance.setActive(columnApp);
171         columnApp.doInit();
172
173         assertEquals(1, rtc.initCount);
174         assertEquals(0, rtc.disposeCount);
175         
176         ApplicationInstance.setActive(null);
177     }
178     
179     /**
180      * Test <code>Component.validate()</code> being invoked at
181      * application initialization and after client update processing.
182      */

183     public void testValidation() {
184         final ValidatingLabel validatingLabel = new ValidatingLabel();
185         ColumnApp app = new ColumnApp() {
186             public Window init() {
187                 Window window = super.init();
188                 getColumn().add(validatingLabel);
189                 return window;
190             }
191         };
192         
193         assertFalse(validatingLabel.valid);
194         
195         ApplicationInstance.setActive(app);
196         
197         app.doInit();
198         
199         // Test for initial validation.
200
assertTrue(validatingLabel.valid);
201
202         validatingLabel.invalidate();
203         assertFalse(validatingLabel.valid);
204         
205         // test validation after client update processing.
206
app.getUpdateManager().processClientUpdates();
207         assertTrue(validatingLabel.valid);
208         
209         ApplicationInstance.setActive(null);
210     }
211 }
212
Popular Tags