KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Arrays JavaDoc;
33 import java.util.List JavaDoc;
34
35 import nextapp.echo2.app.Alignment;
36 import nextapp.echo2.app.ApplicationInstance;
37 import nextapp.echo2.app.Color;
38 import nextapp.echo2.app.Command;
39 import nextapp.echo2.app.Component;
40 import nextapp.echo2.app.Label;
41 import nextapp.echo2.app.Column;
42 import nextapp.echo2.app.TextField;
43 import nextapp.echo2.app.layout.ColumnLayoutData;
44 import nextapp.echo2.app.update.PropertyUpdate;
45 import nextapp.echo2.app.update.ServerComponentUpdate;
46 import nextapp.echo2.app.update.UpdateManager;
47 import junit.framework.TestCase;
48
49 /**
50  * Unit test(s) for update management subsystem.
51  */

52 public class UpdateManagerTest extends TestCase {
53     
54     /**
55      * Test <code>Command</code>.
56      */

57     private static class ExampleCommand implements Command { }
58     
59     /**
60      * Test <code>ApplicationInstance</code>.
61      */

62     private ColumnApp columnApp;
63     
64     /**
65      * <code>UpdateManager</code> for <code>columnApp</code>.
66      */

67     private UpdateManager manager;
68     
69     /**
70      * @see junit.framework.TestCase#setUp()
71      */

72     public void setUp() {
73         columnApp = new ColumnApp();
74         ApplicationInstance.setActive(columnApp);
75         columnApp.doInit();
76         manager = columnApp.getUpdateManager();
77     }
78     
79     /**
80      * @see junit.framework.TestCase#tearDown()
81      */

82     public void tearDown() {
83         ApplicationInstance.setActive(null);
84     }
85     
86     /**
87      * Test adding children to an parent that is being added in current
88      * transaction.
89      */

90     public void testAddChlidToAddedParent() {
91         ServerComponentUpdate[] componentUpdates;
92         Component[] addedChildren;
93         manager.purge();
94
95         // Add a column.
96
Column column1 = new Column();
97         columnApp.getColumn().add(column1);
98         
99         // Ensure only 1 update w/ 1 child added.
100
componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
101         assertEquals(1, componentUpdates.length);
102         addedChildren = componentUpdates[0].getAddedChildren();
103         assertEquals(1, addedChildren.length);
104         assertEquals(0, componentUpdates[0].getRemovedChildren().length);
105         assertEquals(column1, addedChildren[0]);
106         
107         // Add a label to column.
108
column1.add(new Label("A"));
109
110         // Ensure only 1 update w/ 1 child added.
111
componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
112         assertEquals(1, componentUpdates.length);
113         addedChildren = componentUpdates[0].getAddedChildren();
114         assertEquals(1, addedChildren.length);
115         assertEquals(0, componentUpdates[0].getRemovedChildren().length);
116         assertEquals(column1, addedChildren[0]);
117
118         // Add another column.
119
Column column2 = new Column();
120         Label labelB = new Label("B");
121         column2.add(labelB);
122         columnApp.getColumn().add(column2);
123         
124         // Ensure only 1 update w/ 2 children added.
125
componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
126         assertEquals(1, componentUpdates.length);
127         addedChildren = componentUpdates[0].getAddedChildren();
128         assertEquals(2, addedChildren.length);
129         assertEquals(0, componentUpdates[0].getRemovedChildren().length);
130         
131         List JavaDoc addedChildrenList = Arrays.asList(addedChildren);
132         assertTrue(addedChildrenList.contains(column1));
133         assertTrue(addedChildrenList.contains(column2));
134         assertFalse(addedChildrenList.contains(labelB));
135     }
136     
137     /**
138      * Ensure adding an invisible component does not add entries.
139      */

140     public void testAddInvisibleComponent() {
141         ServerComponentUpdate[] componentUpdates;
142  
143         manager.purge();
144         
145         Label label = new Label("Label1");
146         label.setVisible(false);
147         columnApp.getColumn().add(label);
148         
149         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
150         assertEquals(0, componentUpdates.length);
151     }
152     
153     /**
154      * Test storage/retrieval of application property update.
155      */

156     public void testApplicationPropertyUpdate() {
157         manager.purge();
158         columnApp.setFocusedComponent(columnApp.getLabel());
159         PropertyUpdate propertyUpdate = manager.getServerUpdateManager().getApplicationPropertyUpdate(
160                 ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY);
161         assertNotNull(propertyUpdate);
162         assertNull(propertyUpdate.getOldValue());
163         assertEquals(columnApp.getLabel(), propertyUpdate.getNewValue());
164     }
165
166     /**
167      * Ensure that an application property update is stored in the
168      * <code>ServerUpdateManager</code> even if an update to the same
169      * property was received from the <code>ClientUpdateManager</code> BUT
170      * the property value is now different.
171      */

172     public void testApplicationPropertyUpdateWithDifferentClientUpdate() {
173         manager.purge();
174         manager.getClientUpdateManager().setApplicationProperty(ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY,
175                 columnApp.getColumn());
176         manager.processClientUpdates();
177         assertEquals(columnApp.getColumn(), columnApp.getFocusedComponent());
178         
179         columnApp.setFocusedComponent(columnApp.getLabel());
180         PropertyUpdate propertyUpdate = manager.getServerUpdateManager().getApplicationPropertyUpdate(
181                 ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY);
182         assertNotNull(propertyUpdate);
183         assertEquals(columnApp.getColumn(), propertyUpdate.getOldValue());
184         assertEquals(columnApp.getLabel(), propertyUpdate.getNewValue());
185     }
186     
187     /**
188      * Ensure that an application property update is NOT stored in the
189      * <code>ServerUpdateManager</code> as a result of a property update
190      * received from the <code>ClientUpdateManager</code>.
191      */

192     public void testApplicationPropertyUpdateWithEquivalentClientUpdate() {
193         manager.purge();
194         manager.getClientUpdateManager().setApplicationProperty(ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY,
195                 columnApp.getLabel());
196         manager.processClientUpdates();
197         assertEquals(columnApp.getLabel(), columnApp.getFocusedComponent());
198         PropertyUpdate propertyUpdate = manager.getServerUpdateManager().getApplicationPropertyUpdate(
199                 ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY);
200         assertNull(propertyUpdate);
201     }
202
203     /**
204      * Ensure property handling of <code>Command</code>s.
205      */

206     public void testCommand() {
207         Command command = new ExampleCommand();
208
209         manager.purge();
210         columnApp.enqueueCommand(command);
211
212         // Test basic command queuing.
213
assertEquals(1, manager.getServerUpdateManager().getCommands().length);
214         assertEquals(command, manager.getServerUpdateManager().getCommands()[0]);
215         
216         manager.getServerUpdateManager().processFullRefresh();
217
218         // Ensure command survives full refresh.
219
assertEquals(1, manager.getServerUpdateManager().getCommands().length);
220         assertEquals(command, manager.getServerUpdateManager().getCommands()[0]);
221         
222         manager.purge();
223         
224         // Ensure command purged.
225
assertEquals(0, manager.getServerUpdateManager().getCommands().length);
226
227         manager.getServerUpdateManager().processFullRefresh();
228         columnApp.enqueueCommand(command);
229         
230         // Ensure commands can be enqueued even if a full refresh is present.
231
assertEquals(1, manager.getServerUpdateManager().getCommands().length);
232         assertEquals(command, manager.getServerUpdateManager().getCommands()[0]);
233     }
234     
235     /**
236      * Ensure updates to invisible hierarchy do not add entries.
237      */

238     public void testInvisibleHierarchyUpdate() {
239         ServerComponentUpdate[] componentUpdates;
240         columnApp.getColumn().setVisible(false);
241  
242         manager.purge();
243         
244         Column column = new Column();
245         columnApp.getColumn().add(column);
246         Label label1 = new Label("Label1");
247         column.add(label1);
248         Label label2 = new Label("Label2");
249         label2.setVisible(false);
250         column.add(label2);
251         
252         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
253         assertEquals(0, componentUpdates.length);
254     }
255     
256     /**
257      * Test updating of <code>LayoutData</code> properties, ensuring that
258      * these properties correctly register updates for the
259      * <strong>parent</strong> <code>Component</code>.
260      */

261     public void testLayoutDataUpdate() {
262         ServerComponentUpdate[] componentUpdates;
263         ColumnLayoutData columnLayoutData;
264  
265         manager.purge();
266         
267         // Setup.
268
Column column = new Column();
269         columnApp.getColumn().add(column);
270         Label label1 = new Label("Label1");
271         column.add(label1);
272         Label label2 = new Label("Label2");
273         column.add(label2);
274         label2.setLayoutData(new ColumnLayoutData());
275
276         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
277         assertEquals(1, componentUpdates.length);
278         assertEquals(false, componentUpdates[0].hasUpdatedLayoutDataChildren());
279         
280         manager.purge();
281         
282         columnLayoutData = new ColumnLayoutData();
283         columnLayoutData.setAlignment(new Alignment(Alignment.CENTER, Alignment.DEFAULT));
284         label1.setLayoutData(columnLayoutData);
285         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
286         assertEquals(1, componentUpdates.length);
287         assertEquals(column, componentUpdates[0].getParent());
288         assertFalse(componentUpdates[0].hasAddedChildren());
289         assertFalse(componentUpdates[0].hasRemovedChildren());
290         assertFalse(componentUpdates[0].hasUpdatedProperties());
291         assertTrue(componentUpdates[0].hasUpdatedLayoutDataChildren());
292         
293         Component[] components = componentUpdates[0].getUpdatedLayoutDataChildren();
294         assertEquals(1, components.length);
295         assertEquals(label1, components[0]);
296     }
297     
298     
299     /**
300      * Test recording of simple property updates, and their removal
301      * in the event that the updated <code>Component</code> is later
302      * removed.
303      */

304     public void testPropertyUpdate() {
305         ServerComponentUpdate[] componentUpdates;
306         // Remove previous updates.
307
manager.purge();
308         
309         // Update text property of label and verify.
310
columnApp.getLabel().setText("Hi there");
311         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
312         assertEquals(columnApp.getLabel(), componentUpdates[0].getParent());
313         assertEquals(1, componentUpdates.length);
314         assertEquals(0, componentUpdates[0].getAddedChildren().length);
315         assertEquals(0, componentUpdates[0].getRemovedChildren().length);
316         
317         String JavaDoc[] updatedPropertyNames = componentUpdates[0].getUpdatedPropertyNames();
318         assertEquals(1, updatedPropertyNames.length);
319         assertEquals(Label.PROPERTY_TEXT, updatedPropertyNames[0]);
320         PropertyUpdate propertyUpdate = componentUpdates[0].getUpdatedProperty(Label.PROPERTY_TEXT);
321         assertEquals("Label", propertyUpdate.getOldValue());
322         assertEquals("Hi there", propertyUpdate.getNewValue());
323         
324         // Remove label entirely and ensure property update disappears.
325
columnApp.getColumn().remove(columnApp.getLabel());
326         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
327         assertEquals(1, componentUpdates.length);
328         assertEquals(0, componentUpdates[0].getUpdatedPropertyNames().length);
329         assertEquals(0, componentUpdates[0].getAddedChildren().length);
330     }
331     
332     /**
333      * Ensure that a property update whose state is already reflected on the
334      * client (because the end-user made the property change) is properly
335      * canceled.
336      */

337     public void testPropertyUpdateCancellation1() {
338         TextField textField = new TextField();
339         columnApp.getColumn().add(textField);
340         
341         manager.purge();
342         manager.getClientUpdateManager().setComponentProperty(textField, TextField.TEXT_CHANGED_PROPERTY, "a user typed this.");
343         manager.processClientUpdates();
344         
345         ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
346         assertEquals(0, componentUpdates.length);
347     }
348     
349     /**
350      * Ensure that a property update whose state is already reflected on the
351      * client (because the end-user made the property change) is properly
352      * canceled. This test will ensure that if additional properties changed
353      * on a user-updated component, that only the non-user-updated properties
354      * are reflected by the <code>UpdateManager</code>.
355      */

356     public void testPropertyUpdateCancellation2() {
357         TextField textField = new TextField();
358         columnApp.getColumn().add(textField);
359         
360         manager.purge();
361         textField.setBackground(Color.BLUE);
362         manager.getClientUpdateManager().setComponentProperty(textField, TextField.TEXT_CHANGED_PROPERTY, "a user typed this.");
363         manager.processClientUpdates();
364         
365         ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
366         assertEquals(1, componentUpdates.length);
367         PropertyUpdate backgroundUpdate =
368                   componentUpdates[0].getUpdatedProperty(TextField.PROPERTY_BACKGROUND);
369         assertNotNull(backgroundUpdate);
370         assertEquals(Color.BLUE, backgroundUpdate.getNewValue());
371         assertNull(componentUpdates[0].getUpdatedProperty(TextField.TEXT_CHANGED_PROPERTY));
372     }
373     
374     /**
375      * Ensure that a property update whose state is already reflected on the
376      * client (because the end-user made the property change) is properly
377      * canceled. This test will ensure that user-changed properties are
378      * canceled even in the event that the application ALSO updated the
379      * property, though it was later overwritten by a user update.
380      */

381     public void testPropertyUpdateCancellation3() {
382         TextField textField = new TextField();
383         columnApp.getColumn().add(textField);
384         
385         manager.purge();
386         textField.setText("first the application set it to this.");
387         manager.getClientUpdateManager().setComponentProperty(textField, TextField.TEXT_CHANGED_PROPERTY, "a user typed this.");
388         manager.processClientUpdates();
389         
390         ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
391         assertEquals(1, componentUpdates.length);
392         assertFalse(componentUpdates[0].hasUpdatedProperties());
393     }
394     
395     /**
396      * Test basic operation <code>UpdateManager.purge()</code> method.
397      */

398     public void testPurge() {
399         assertFalse(manager.getServerUpdateManager().getComponentUpdates().length == 0);
400         manager.purge();
401         assertTrue(manager.getServerUpdateManager().getComponentUpdates().length == 0);
402     }
403
404     /**
405      * Ensure that component removes and descendant removes are properly stored.
406      */

407     public void testRemove1() {
408         Column column = new Column();
409         Label label = new Label();
410         column.add(label);
411         columnApp.getColumn().add(column);
412         manager.purge();
413         
414         columnApp.getColumn().remove(column);
415         
416         ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
417         assertEquals(1, componentUpdates.length);
418         
419         assertEquals(true, componentUpdates[0].hasRemovedChildren());
420         assertEquals(true, componentUpdates[0].hasRemovedDescendants());
421         
422         Component[] removedChildren = componentUpdates[0].getRemovedChildren();
423         assertEquals(1, removedChildren.length);
424         assertEquals(column, removedChildren[0]);
425         
426         Component[] removedDescendants = componentUpdates[0].getRemovedDescendants();
427         assertEquals(1, removedDescendants.length);
428         assertEquals(label, removedDescendants[0]);
429     }
430
431     /**
432      * Another slightly more complex test to ensure that component
433      * removes/descendant removes are properly stored.
434      *
435      * -- Initial State --
436      * [ColumnApp]
437      * * Window
438      * * ContentPane
439      * * Column
440      * * Column1
441      * * Column2
442      * * label
443      *
444      * -- New State --
445      * [ColumnApp]
446      * * Window
447      * * ContentPane
448      * * Column
449      * * Column1 [REMOVED]
450      * X Column2 [REMOVED DESCENDANT]
451      * X label [REMOVED DESCENDANT]
452      */

453     public void testRemove2() {
454         Column column1 = new Column();
455         Column column2 = new Column();
456         column1.add(column2);
457         Label label = new Label();
458         column2.add(label);
459         columnApp.getColumn().add(column1);
460         manager.purge();
461         
462         column1.remove(column2);
463         columnApp.getColumn().remove(column1);
464         
465         ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
466         assertEquals(1, componentUpdates.length);
467         
468         assertEquals(true, componentUpdates[0].hasRemovedChildren());
469         assertEquals(true, componentUpdates[0].hasRemovedDescendants());
470         
471         Component[] removedChildren = componentUpdates[0].getRemovedChildren();
472         assertEquals(1, removedChildren.length);
473         assertEquals(column1, removedChildren[0]);
474         
475         Component[] removedDescendants = componentUpdates[0].getRemovedDescendants();
476         assertEquals(2, removedDescendants.length);
477         assertTrue(removedDescendants[0].equals(column2) || removedDescendants[1].equals(column2));
478         assertTrue(removedDescendants[0].equals(label) || removedDescendants[1].equals(label));
479     }
480
481     /**
482      * Ensure updates are returned sorted by component depth.
483      */

484     public void testUpdateSorting1() {
485         ServerComponentUpdate[] componentUpdates;
486         manager.purge();
487
488         columnApp.getLabel().setBackground(Color.BLUE);
489         columnApp.getContentPane().setBackground(Color.RED);
490         columnApp.getColumn().setBackground(Color.GREEN);
491         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
492
493         assertEquals(3, componentUpdates.length);
494         assertEquals(columnApp.getContentPane(), componentUpdates[0].getParent());
495         assertEquals(columnApp.getColumn(), componentUpdates[1].getParent());
496         assertEquals(columnApp.getLabel(), componentUpdates[2].getParent());
497     }
498     
499     /**
500      * Ensure updates are returned sorted by component depth.
501      */

502     public void testUpdateSorting2() {
503         ServerComponentUpdate[] componentUpdates;
504         Column column2 = new Column();
505         columnApp.getColumn().add(column2);
506         Label label2 = new Label();
507         column2.add(label2);
508         manager.purge();
509
510         columnApp.getLabel().setBackground(Color.BLUE);
511         columnApp.getContentPane().setBackground(Color.RED);
512         columnApp.getColumn().setBackground(Color.GREEN);
513         label2.setBackground(Color.YELLOW);
514         column2.setBackground(Color.ORANGE);
515
516         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
517         assertEquals(5, componentUpdates.length);
518         assertEquals(columnApp.getContentPane(), componentUpdates[0].getParent());
519         assertEquals(columnApp.getColumn(), componentUpdates[1].getParent());
520         assertTrue(columnApp.getLabel().equals(componentUpdates[2].getParent())
521                 || columnApp.getLabel().equals(componentUpdates[3].getParent()));
522         assertTrue(column2.equals(componentUpdates[2].getParent())
523                 || column2.equals(componentUpdates[3].getParent()));
524         assertEquals(label2, componentUpdates[4].getParent());
525     }
526     
527     /**
528      * Ensure that visible updates are treated as adds/removes.
529      */

530     public void testVisibleUpdate() {
531         ServerComponentUpdate[] componentUpdates;
532  
533         manager.purge();
534         
535         // Setup.
536
Column column = new Column();
537         columnApp.getColumn().add(column);
538         Label label1 = new Label("Label1");
539         column.add(label1);
540         Label label2 = new Label("Label2");
541         label2.setVisible(false);
542         column.add(label2);
543         label2.setLayoutData(new ColumnLayoutData());
544
545         manager.purge();
546         
547         label1.setVisible(false);
548         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
549         assertEquals(1, componentUpdates.length);
550         assertEquals(column, componentUpdates[0].getParent());
551         assertFalse(componentUpdates[0].hasAddedChildren());
552         assertTrue(componentUpdates[0].hasRemovedChildren());
553         assertFalse(componentUpdates[0].hasUpdatedProperties());
554         assertFalse(componentUpdates[0].hasUpdatedLayoutDataChildren());
555         
556         Component[] components = componentUpdates[0].getRemovedChildren();
557         assertEquals(1, components.length);
558         assertEquals(label1, components[0]);
559         
560         manager.purge();
561         
562         label2.setVisible(true);
563         componentUpdates = manager.getServerUpdateManager().getComponentUpdates();
564         assertEquals(1, componentUpdates.length);
565         assertEquals(column, componentUpdates[0].getParent());
566         assertTrue(componentUpdates[0].hasAddedChildren());
567         assertFalse(componentUpdates[0].hasRemovedChildren());
568         assertFalse(componentUpdates[0].hasUpdatedProperties());
569         assertFalse(componentUpdates[0].hasUpdatedLayoutDataChildren());
570
571         components = componentUpdates[0].getAddedChildren();
572         assertEquals(1, components.length);
573         assertEquals(label2, components[0]);
574         
575         label1.setVisible(true);
576     }
577 }
578
Popular Tags