KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > widgets > TestSelectWidgetOption


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/widgets/TestSelectWidgetOption.java,v $
3  * Date : $Date: 2006/03/27 14:52:50 $
4  * Version: $Revision: 1.8 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31  
32 package org.opencms.widgets;
33
34 import java.util.Collections JavaDoc;
35 import java.util.List JavaDoc;
36
37 import junit.framework.TestCase;
38
39 /**
40  * Test cases for the parsing of select widget options.<p>
41  *
42  * @author Alexander Kandzior
43  * @version $Revision: 1.8 $
44  */

45 public class TestSelectWidgetOption extends TestCase {
46
47     /**
48      * Default JUnit constructor.<p>
49      *
50      * @param arg0 JUnit parameters
51      */

52     public TestSelectWidgetOption(String JavaDoc arg0) {
53         
54         super(arg0);
55     }
56     
57     /**
58      * Tests parsing of select widget options.<p>
59      *
60      * @throws Exception if the test fails
61      */

62     public void testOptionParser() throws Exception JavaDoc {
63
64         List JavaDoc res = CmsSelectWidgetOption.parseOptions(null);
65         assertSame(Collections.EMPTY_LIST, res);
66
67         res = CmsSelectWidgetOption.parseOptions("");
68         assertSame(Collections.EMPTY_LIST, res);
69
70         res = CmsSelectWidgetOption.parseOptions(" ");
71         assertSame(Collections.EMPTY_LIST, res);
72         
73         res = CmsSelectWidgetOption.parseOptions("one");
74         assertNotNull(res);
75         assertEquals(1, res.size());
76         
77         CmsSelectWidgetOption opt = (CmsSelectWidgetOption)res.get(0);
78         assertFalse(opt.isDefault());
79         assertEquals("one", opt.getValue());
80         assertNull(opt.getHelp());
81         assertSame(opt.getValue(), opt.getOption());
82
83         // some checks with malformed option values - these are silently ignored
84
assertEquals(Collections.EMPTY_LIST, CmsSelectWidgetOption.parseOptions("default='true'"));
85         assertEquals(Collections.EMPTY_LIST, CmsSelectWidgetOption.parseOptions("option='some'"));
86         assertEquals(Collections.EMPTY_LIST, CmsSelectWidgetOption.parseOptions("help='many'"));
87         assertEquals(Collections.EMPTY_LIST, CmsSelectWidgetOption.parseOptions("option='some' default='true'"));
88         assertEquals(Collections.EMPTY_LIST, CmsSelectWidgetOption.parseOptions("help='many' option='some' default='true'"));
89                 
90         // check the examples frm the JavaDoc to make sure they really work as advertised
91
assertEquals(CmsSelectWidgetOption.parseOptions("value='some value' default='true'"), CmsSelectWidgetOption.parseOptions("some value default='true'"));
92         assertEquals(CmsSelectWidgetOption.parseOptions("value='some value' default='true'"), CmsSelectWidgetOption.parseOptions("some value*"));
93         assertEquals(CmsSelectWidgetOption.parseOptions("value='some value' option='some option'"), CmsSelectWidgetOption.parseOptions("some value:some option"));
94         assertEquals(CmsSelectWidgetOption.parseOptions("value='some value' default='true' option='some option'"), CmsSelectWidgetOption.parseOptions("some value*:some option"));
95
96         assertEquals(CmsSelectWidgetOption.parseOptions(""), CmsSelectWidgetOption.parseOptions(CmsSelectWidgetOption.createConfigurationString(null)));
97         assertEquals(CmsSelectWidgetOption.parseOptions(null), CmsSelectWidgetOption.parseOptions(CmsSelectWidgetOption.createConfigurationString(Collections.EMPTY_LIST)));
98         
99         // check a first list with "full" syntax
100
List JavaDoc result1 = CmsSelectWidgetOption.parseOptions("value='1' default='true'|value='2'|value='3'|value='4'|value='5'|value='6'|value='7'|value='8'|value='9'|value='10'");
101         assertNotNull(result1);
102         assertEquals(10, result1.size());
103
104         opt = (CmsSelectWidgetOption)result1.get(0);
105         assertTrue(opt.isDefault());
106         assertEquals("1", opt.getValue());
107         assertNull(opt.getHelp());
108         assertSame(opt.getValue(), opt.getOption());
109         
110         opt = (CmsSelectWidgetOption)result1.get(4);
111         assertFalse(opt.isDefault());
112         assertEquals("5", opt.getValue());
113         assertNull(opt.getHelp());
114         assertSame(opt.getValue(), opt.getOption());
115         
116         opt = (CmsSelectWidgetOption)result1.get(9);
117         assertFalse(opt.isDefault());
118         assertEquals("10", opt.getValue());
119         assertNull(opt.getHelp());
120         assertSame(opt.getValue(), opt.getOption());
121         
122         // check "round trip" creation if a String from the parsed options
123
assertEquals(result1, CmsSelectWidgetOption.parseOptions(CmsSelectWidgetOption.createConfigurationString(result1)));
124         
125         // check a second list with "shortcut" syntax
126
List JavaDoc result2 = CmsSelectWidgetOption.parseOptions("1 default='true'|2|3|4|5|6|7|8|9|10");
127         assertNotNull(result2);
128         assertEquals(result1.size(), result2.size());
129         
130         for (int i=0; i<result1.size(); i++) {
131             assertEquals(result1.get(i), result2.get(i));
132         }
133
134         // check a third list with "legacy" syntax
135
result2 = CmsSelectWidgetOption.parseOptions("1*|2|3|4|5|6|7|8|9|10");
136         assertNotNull(result2);
137         assertEquals(result1.size(), result2.size());
138         
139         for (int i=0; i<result1.size(); i++) {
140             assertEquals(result1.get(i), result2.get(i));
141         }
142         
143         // now a different input list
144
result1 = CmsSelectWidgetOption.parseOptions("value='accessible' default='true' option='${key.layout.version.accessible}'|value='common' option='${key.layout.version.classic}'");
145         assertNotNull(result1);
146         assertEquals(2, result1.size());
147         
148         opt = (CmsSelectWidgetOption)result1.get(0);
149         assertTrue(opt.isDefault());
150         assertEquals("accessible", opt.getValue());
151         assertEquals("${key.layout.version.accessible}", opt.getOption());
152         assertNull(opt.getHelp());
153         
154         opt = (CmsSelectWidgetOption)result1.get(1);
155         assertFalse(opt.isDefault());
156         assertEquals("common", opt.getValue());
157         assertEquals("${key.layout.version.classic}", opt.getOption());
158         assertNull(opt.getHelp());
159         
160         // check "round trip" creation if a String from the parsed options
161
assertEquals(result1, CmsSelectWidgetOption.parseOptions(CmsSelectWidgetOption.createConfigurationString(result1)));
162         
163         // variation of the element order
164
result2 = CmsSelectWidgetOption.parseOptions("default='true' value='accessible' option='${key.layout.version.accessible}'|option='${key.layout.version.classic}' value='common'");
165         assertNotNull(result2);
166         assertEquals(result1.size(), result2.size());
167         
168         for (int i=0; i<result1.size(); i++) {
169             assertEquals(result1.get(i), result2.get(i));
170         }
171         
172         // shortcut syntax
173
result2 = CmsSelectWidgetOption.parseOptions("accessible default='true' option='${key.layout.version.accessible}'|common option='${key.layout.version.classic}'");
174         assertNotNull(result2);
175         assertEquals(result1.size(), result2.size());
176         
177         for (int i=0; i<result1.size(); i++) {
178             assertEquals(result1.get(i), result2.get(i));
179         }
180         
181         // shortcut syntax 2
182
result2 = CmsSelectWidgetOption.parseOptions("accessible* option='${key.layout.version.accessible}'|common option='${key.layout.version.classic}'");
183         assertNotNull(result2);
184         assertEquals(result1.size(), result2.size());
185         
186         for (int i=0; i<result1.size(); i++) {
187             assertEquals(result1.get(i), result2.get(i));
188         }
189         
190         // legacy syntax
191
result2 = CmsSelectWidgetOption.parseOptions("accessible*:${key.layout.version.accessible}|common:${key.layout.version.classic}");
192         assertNotNull(result2);
193         assertEquals(result1.size(), result2.size());
194         
195         for (int i=0; i<result1.size(); i++) {
196             assertEquals(result1.get(i), result2.get(i));
197         }
198     }
199 }
Popular Tags