KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > util > UiStringTest


1 /**
2  * Feb 23, 2005
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.util;
19
20 import junit.framework.TestCase;
21
22 /**
23  * Tests {@link net.sf.uitags.util.UiString}.
24  *
25  * @author hgani
26  * @version $Id: UiStringTest.java,v 1.1.1.1 2006/02/26 11:39:22 hgani Exp $
27  */

28 public class UiStringTest extends TestCase {
29   /**
30    * Instance of the class under test.
31    */

32   private UiString boundUiString;
33
34   /**
35    * Instance of the class under test.
36    */

37   private UiString mappedUiString;
38
39   /**
40    * Main method for the tests.
41    *
42    * @param args main arguments
43    */

44   public static void main(String JavaDoc[] args) {
45     junit.textui.TestRunner.run(UiStringTest.class);
46   }
47
48   /** {@inheritDoc} */
49   protected void setUp() throws Exception JavaDoc {
50     super.setUp();
51     this.boundUiString = new UiString("{0} & {1}", UiString.OPTION_ALL);
52     this.mappedUiString = new UiString(
53         "{first} & {second}", UiString.OPTION_ALL);
54   }
55
56   /** {@inheritDoc} */
57   protected void tearDown() throws Exception JavaDoc {
58     super.tearDown();
59     this.boundUiString = null;
60     this.mappedUiString = null;
61   }
62
63   /**
64    * Ensures that {@link UiString#setDefaultOptions(int)}
65    * does a proper validation for the options value.
66    */

67   public void testSetDefaultOptions() {
68     try {
69       this.boundUiString.setDefaultOptions(-1);
70       fail("-1 is not supposed to be a valid option.");
71     }
72     catch (IllegalArgumentException JavaDoc e) {
73       // as expected
74
}
75
76     try {
77       this.mappedUiString.setDefaultOptions(UiString.OPTION_ALL + 1);
78       fail("Options value is too big, should be invalid.");
79     }
80     catch (IllegalArgumentException JavaDoc e) {
81       // as expected
82
}
83   }
84
85   /**
86    * Ensures that the parameters binding technique is working properly.
87    */

88   public void testBind() {
89     this.boundUiString.bind("Tom");
90     this.boundUiString.bind("Jerry");
91     // both params are surrounded with quotes
92
assertEquals("\"Tom\" & \"Jerry\"", this.boundUiString.construct());
93
94     String JavaDoc tempString = UiString.simpleConstruct(
95         "\"{0}\" {1} {2}", new String JavaDoc[] { "Tom", "&", "\"Jerry\"" });
96     assertEquals(tempString, this.boundUiString.construct());
97
98     bindTestWithoutOption();
99     bindTestWithAutoSurround();
100     bindTestWithHtmlEncoding();
101     bindTestWithJsEscape();
102   }
103
104   /**
105    * Ensures that the non-option binding is working properly.
106    */

107   private void bindTestWithoutOption() {
108     this.boundUiString.setDefaultOptions(UiString.OPTION_NONE);
109     this.boundUiString.clearBindParameters();
110     this.boundUiString.bind("Tom");
111     this.boundUiString.bind("Jerry");
112
113     // no quotes
114
assertEquals("Tom & Jerry", this.boundUiString.construct());
115   }
116
117   /**
118    * Ensures that binding with <i>Auto Surround</i> is working properly.
119    */

120   private void bindTestWithAutoSurround() {
121     this.boundUiString.setSurroundString("'");
122     this.boundUiString.clearBindParameters();
123     this.boundUiString.bind("Tom", UiString.OPTION_AUTO_SURROUND);
124     this.boundUiString.bind("<cat>", UiString.OPTION_NONE);
125
126     // second param not encoded
127
assertEquals("'Tom' & <cat>", this.boundUiString.construct());
128
129     this.boundUiString.clearBindParameters();
130     this.boundUiString.bind("Tom");
131
132     // second holder not bound
133
assertEquals("Tom & {1}", this.boundUiString.construct());
134   }
135
136   /**
137    * Ensures that binding with <i>HTML Encode</i> is working properly.
138    */

139   private void bindTestWithHtmlEncoding() {
140     this.boundUiString.setDefaultOptions(UiString.OPTION_HTML_ENCODING);
141     this.boundUiString.bind("<cat>");
142     // bind more params than needed
143
this.boundUiString.bind("others");
144
145     // second param encoded
146
assertEquals("Tom & &lt;cat&gt;", this.boundUiString.construct());
147   }
148
149   /**
150    * Ensures that binding with <i>JS Escape</i> is working properly.
151    */

152   private void bindTestWithJsEscape() {
153     this.boundUiString.clearBindParameters();
154     this.boundUiString.setDefaultOptions(UiString.OPTION_JS_ESCAPE);
155     this.boundUiString.bind("\"Tom's cat\"");
156     this.boundUiString.bind("\\mouse\\");
157
158     assertEquals("\\\"Tom\\\'s cat\\\" & \\\\mouse\\\\",
159         this.boundUiString.construct());
160   }
161
162   /**
163    * Ensures that the parameters mapping technique is working properly.
164    */

165   public void testMap() {
166     this.mappedUiString.map("first", "Tom");
167
168     // second holder not mapped
169
assertEquals("\"Tom\" & {second}", this.mappedUiString.construct());
170
171     this.mappedUiString.map("second", "Jerry");
172
173     // both params quoted
174
assertEquals("\"Tom\" & \"Jerry\"", this.mappedUiString.construct());
175
176     try {
177       this.mappedUiString.bind("Tom");
178       fail("Binding and mapping technique cannot be used together.");
179     }
180     catch (IllegalStateException JavaDoc e) {
181       // as expected
182
}
183
184     this.mappedUiString.map("second", "Jerry", UiString.OPTION_NONE);
185
186     // second param not quoted
187
assertEquals("\"Tom\" & Jerry", this.mappedUiString.construct());
188
189     mapTestSetOptions();
190   }
191
192   /**
193    * Ensures that all option-related methods change the <code>String</code>
194    * construction behaviour correctly.
195    */

196   private void mapTestSetOptions() {
197     this.mappedUiString.map("first", "Tom");
198     this.mappedUiString.clearOptions();
199     this.mappedUiString.map("second", "Jerry");
200
201     assertEquals("\"Tom\" & Jerry", this.mappedUiString.construct());
202
203     this.mappedUiString.jsEscape();
204     this.mappedUiString.map("first", "\"Tom\"");
205     this.mappedUiString.autoSurround();
206     this.mappedUiString.map("second", "Jerry");
207
208     assertEquals("\\\"Tom\\\" & \"Jerry\"", this.mappedUiString.construct());
209
210     this.mappedUiString.htmlEncode();
211     this.mappedUiString.map("second", "Jerry & friends");
212
213     assertEquals("\\\"Tom\\\" & \"Jerry&nbsp;&amp;&nbsp;friends\"",
214         this.mappedUiString.construct());
215
216     this.mappedUiString.clearOptions();
217     this.mappedUiString.map("first", "{second}");
218     this.mappedUiString.map("second", "Jerry");
219
220     // this is the expected behaviour
221
assertEquals("Jerry & Jerry", this.mappedUiString.construct());
222
223     this.mappedUiString.map("first", "\\{second\\}");
224     this.mappedUiString.map("second", "Jerry");
225
226     assertEquals("{second} & Jerry", this.mappedUiString.construct());
227
228     this.mappedUiString = new UiString("\\{first\\} & {second}");
229
230     this.mappedUiString.map("first", "Tom");
231     this.mappedUiString.map("second", "\\\\{second\\\\}");
232
233     assertEquals("{first} & \\{second\\}", this.mappedUiString.construct());
234
235     // taken from UiString documentation
236
this.mappedUiString = new UiString("{a} and {b} and {c}");
237
238     this.mappedUiString.map("c", "{b}");
239     this.mappedUiString.map("b", "bValue");
240     this.mappedUiString.map("a", "{b}");
241     this.mappedUiString.map("second", "\\\\{second\\\\}");
242
243     assertEquals("{b} and bValue and bValue", this.mappedUiString.construct());
244   }
245 }
246
Popular Tags