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 java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.Set;
035:
036: import nextapp.echo2.app.MutableStyle;
037: import junit.framework.TestCase;
038:
039: /**
040: * Unit test(s) for the <code>nextapp.echo2.app.MutableStyle</code>.
041: */
042: public class MutableStyleTest extends TestCase {
043:
044: public void testAddStyle() {
045: MutableStyle style1 = new MutableStyle();
046: style1.setIndexedProperty("alpha", 0, "tango");
047: style1.setIndexedProperty("alpha", 2, "uniform");
048: style1.setIndexedProperty("alpha", 3, "victor");
049: style1.setProperty("charlie", "delta");
050: style1.setProperty("echo", "foxtrot");
051:
052: MutableStyle style2 = new MutableStyle();
053: style2.setProperty("charlie", "golf");
054: assertEquals("golf", style2.getProperty("charlie"));
055:
056: style2.addStyleContent(style1);
057:
058: assertEquals("tango", style2.getIndexedProperty("alpha", 0));
059: assertEquals("uniform", style2.getIndexedProperty("alpha", 2));
060: assertEquals("victor", style2.getIndexedProperty("alpha", 3));
061: assertEquals("delta", style2.getProperty("charlie"));
062: assertEquals("foxtrot", style2.getProperty("echo"));
063:
064: style2.setIndexedProperty("alpha", 3, "whiskey");
065: assertEquals("whiskey", style2.getIndexedProperty("alpha", 3));
066: assertEquals("victor", style1.getIndexedProperty("alpha", 3));
067:
068: style2.setProperty("echo", "hotel");
069: assertEquals("hotel", style2.getProperty("echo"));
070: assertEquals("foxtrot", style1.getProperty("echo"));
071: }
072:
073: public void testBasic() {
074: MutableStyle style = new MutableStyle();
075: assertEquals(0, style.size());
076: assertFalse(style.isPropertySet("alpha"));
077: assertNull(style.getProperty("alpha"));
078:
079: style.setProperty("alpha", "bravo");
080: assertEquals(1, style.size());
081: assertTrue(style.isPropertySet("alpha"));
082: assertFalse(style.isPropertySet("bravo"));
083:
084: style.setProperty("bravo", "charlie");
085: assertEquals(2, style.size());
086: assertEquals("bravo", style.getProperty("alpha"));
087: assertEquals("charlie", style.getProperty("bravo"));
088:
089: style.setProperty("bravo", "delta");
090: assertEquals(2, style.size());
091: assertEquals("bravo", style.getProperty("alpha"));
092: assertEquals("delta", style.getProperty("bravo"));
093:
094: style.setProperty("echo", "foxtrot");
095: style.setProperty("golf", "hotel");
096: style.setProperty("india", "juliet");
097: style.setProperty("kilo", "lima");
098: assertEquals(6, style.size());
099: 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 it = style.getPropertyIndices("alpha");
127: assertNotNull(it);
128: Set indices = new HashSet();
129: while (it.hasNext()) {
130: indices.add(it.next());
131: }
132: assertTrue(indices.contains(new Integer(0)));
133: assertTrue(indices.contains(new Integer(1)));
134: assertFalse(indices.contains(new Integer(2)));
135: assertTrue(indices.contains(new Integer(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 it = style.getPropertyNames();
144: assertNotNull(it);
145: Set names = new HashSet();
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: }
|