KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import nextapp.echo2.app.MutableStyle;
37 import junit.framework.TestCase;
38
39 /**
40  * Unit test(s) for the <code>nextapp.echo2.app.MutableStyle</code>.
41  */

42 public class MutableStyleTest extends TestCase {
43     
44     public void testAddStyle() {
45         MutableStyle style1 = new MutableStyle();
46         style1.setIndexedProperty("alpha", 0, "tango");
47         style1.setIndexedProperty("alpha", 2, "uniform");
48         style1.setIndexedProperty("alpha", 3, "victor");
49         style1.setProperty("charlie", "delta");
50         style1.setProperty("echo", "foxtrot");
51         
52         MutableStyle style2 = new MutableStyle();
53         style2.setProperty("charlie", "golf");
54         assertEquals("golf", style2.getProperty("charlie"));
55         
56         style2.addStyleContent(style1);
57         
58         assertEquals("tango", style2.getIndexedProperty("alpha", 0));
59         assertEquals("uniform", style2.getIndexedProperty("alpha", 2));
60         assertEquals("victor", style2.getIndexedProperty("alpha", 3));
61         assertEquals("delta", style2.getProperty("charlie"));
62         assertEquals("foxtrot", style2.getProperty("echo"));
63         
64         style2.setIndexedProperty("alpha", 3, "whiskey");
65         assertEquals("whiskey", style2.getIndexedProperty("alpha", 3));
66         assertEquals("victor", style1.getIndexedProperty("alpha", 3));
67         
68         style2.setProperty("echo", "hotel");
69         assertEquals("hotel", style2.getProperty("echo"));
70         assertEquals("foxtrot", style1.getProperty("echo"));
71     }
72     
73     public void testBasic() {
74         MutableStyle style = new MutableStyle();
75         assertEquals(0, style.size());
76         assertFalse(style.isPropertySet("alpha"));
77         assertNull(style.getProperty("alpha"));
78         
79         style.setProperty("alpha", "bravo");
80         assertEquals(1, style.size());
81         assertTrue(style.isPropertySet("alpha"));
82         assertFalse(style.isPropertySet("bravo"));
83         
84         style.setProperty("bravo", "charlie");
85         assertEquals(2, style.size());
86         assertEquals("bravo", style.getProperty("alpha"));
87         assertEquals("charlie", style.getProperty("bravo"));
88         
89         style.setProperty("bravo", "delta");
90         assertEquals(2, style.size());
91         assertEquals("bravo", style.getProperty("alpha"));
92         assertEquals("delta", style.getProperty("bravo"));
93         
94         style.setProperty("echo", "foxtrot");
95         style.setProperty("golf", "hotel");
96         style.setProperty("india", "juliet");
97         style.setProperty("kilo", "lima");
98         assertEquals(6, style.size());
99         assertEquals("juliet", style.getProperty("india"));
100         
101         style.removeProperty("kilo");
102         assertEquals(5, style.size());
103         assertNull(style.getProperty("kilo"));
104
105         style.removeProperty("alpha");
106         assertEquals(4, style.size());
107         assertNull(style.getProperty("alpha"));
108         
109         style.removeProperty("mike");
110         assertEquals(4, style.size());
111         
112         style.removeProperty("echo");
113         style.removeProperty("golf");
114         style.removeProperty("india");
115         assertEquals(1, style.size());
116         
117         style.removeProperty("bravo");
118         assertEquals(0, style.size());
119     }
120     
121     public void testGetPropertyIndices() {
122         MutableStyle style = new MutableStyle();
123         style.setIndexedProperty("alpha", 0, "zero");
124         style.setIndexedProperty("alpha", 1, "one");
125         style.setIndexedProperty("alpha", 5, "five");
126         Iterator JavaDoc it = style.getPropertyIndices("alpha");
127         assertNotNull(it);
128         Set JavaDoc indices = new HashSet JavaDoc();
129         while (it.hasNext()) {
130             indices.add(it.next());
131         }
132         assertTrue(indices.contains(new Integer JavaDoc(0)));
133         assertTrue(indices.contains(new Integer JavaDoc(1)));
134         assertFalse(indices.contains(new Integer JavaDoc(2)));
135         assertTrue(indices.contains(new Integer JavaDoc(5)));
136     }
137     
138     public void testGetPropertyNames() {
139         MutableStyle style = new MutableStyle();
140         style.setProperty("alpha", "bravo");
141         style.setProperty("charlie", "delta");
142         style.setProperty("echo", "foxtrot");
143         Iterator JavaDoc it = style.getPropertyNames();
144         assertNotNull(it);
145         Set JavaDoc names = new HashSet JavaDoc();
146         while (it.hasNext()) {
147             names.add(it.next());
148         }
149         assertTrue(names.contains("alpha"));
150         assertTrue(names.contains("charlie"));
151         assertTrue(names.contains("echo"));
152         assertFalse(names.contains("bravo"));
153         assertFalse(names.contains("delta"));
154         assertFalse(names.contains("foxtrot"));
155     }
156     
157     public void testIndexedProperty() {
158         MutableStyle style = new MutableStyle();
159         style.setIndexedProperty("alpha", 0, "0");
160         style.setIndexedProperty("alpha", 1, "1");
161         style.setIndexedProperty("alpha", 2, "2");
162         style.setIndexedProperty("alpha", 0, "3");
163         
164         assertFalse(style.isIndexedPropertySet("alpha", -1));
165         assertTrue(style.isIndexedPropertySet("alpha", 0));
166         assertTrue(style.isIndexedPropertySet("alpha", 1));
167         assertTrue(style.isIndexedPropertySet("alpha", 2));
168         assertFalse(style.isIndexedPropertySet("alpha", 3));
169         assertEquals("3", style.getIndexedProperty("alpha", 0));
170         assertEquals("1", style.getIndexedProperty("alpha", 1));
171         assertEquals("2", style.getIndexedProperty("alpha", 2));
172         
173         style.removeIndexedProperty("alpha", 1);
174         assertFalse(style.isIndexedPropertySet("alpha", 1));
175     }
176     
177     public void testSet1Set2Remove2Set2() {
178         MutableStyle style = new MutableStyle();
179         style.setProperty("golf", "hotel");
180         style.setProperty("alpha", "bravo");
181         assertEquals("hotel", style.getProperty("golf"));
182         assertEquals("bravo", style.getProperty("alpha"));
183         style.setProperty("alpha", null);
184         assertEquals("hotel", style.getProperty("golf"));
185         assertEquals(null, style.getProperty("alpha"));
186         style.setProperty("alpha", "bravo");
187         assertEquals("hotel", style.getProperty("golf"));
188         assertEquals("bravo", style.getProperty("alpha"));
189     }
190 }
Popular Tags