001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.test;
031:
032: import nextapp.echo2.app.ApplicationInstance;
033: import nextapp.echo2.app.Component;
034: import nextapp.echo2.app.Label;
035: import nextapp.echo2.app.Column;
036: import nextapp.echo2.app.Window;
037: import junit.framework.TestCase;
038:
039: /**
040: * Unit test(s) for the <code>nextapp.echo2.app.ApplicationInstance</code> object.
041: */
042: public class ApplicationInstanceTest extends TestCase {
043:
044: private class RegistrationTestComponent extends Component {
045: int initCount = 0;
046: int disposeCount = 0;
047:
048: public void dispose() {
049: super .dispose();
050: ++disposeCount;
051: }
052:
053: public void init() {
054: super .init();
055: ++initCount;
056: }
057: }
058:
059: private class ValidatingLabel extends Label {
060:
061: boolean valid = false;
062:
063: public void invalidate() {
064: valid = false;
065: }
066:
067: public void validate() {
068: super .validate();
069: valid = true;
070: }
071: }
072:
073: /**
074: * Test setting active (ThreadLocal) <code>ApplicationInstance</code>.
075: */
076: public void testActivation() {
077: assertNull(ApplicationInstance.getActive());
078: HelloWorldApp app = new HelloWorldApp();
079: assertNull(ApplicationInstance.getActive());
080: ApplicationInstance.setActive(app);
081: assertTrue(app == ApplicationInstance.getActive());
082: ApplicationInstance.setActive(null);
083: assertNull(ApplicationInstance.getActive());
084: }
085:
086: /**
087: * Test setting and retrieving contextual information.
088: */
089: public void testContext() {
090: HelloWorldApp app = new HelloWorldApp();
091: assertNull(app.getContextProperty("alpha"));
092: app.setContextProperty("alpha", "bravo");
093: assertEquals("bravo", app.getContextProperty("alpha"));
094: app.setContextProperty("alpha", null);
095: assertNull(app.getContextProperty("alpha"));
096: }
097:
098: /**
099: * 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: }
|