1 18 package net.sf.uitags.util; 19 20 import junit.framework.TestCase; 21 22 28 public class UiStringTest extends TestCase { 29 32 private UiString boundUiString; 33 34 37 private UiString mappedUiString; 38 39 44 public static void main(String [] args) { 45 junit.textui.TestRunner.run(UiStringTest.class); 46 } 47 48 49 protected void setUp() throws Exception { 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 57 protected void tearDown() throws Exception { 58 super.tearDown(); 59 this.boundUiString = null; 60 this.mappedUiString = null; 61 } 62 63 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 e) { 73 } 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 e) { 81 } 83 } 84 85 88 public void testBind() { 89 this.boundUiString.bind("Tom"); 90 this.boundUiString.bind("Jerry"); 91 assertEquals("\"Tom\" & \"Jerry\"", this.boundUiString.construct()); 93 94 String tempString = UiString.simpleConstruct( 95 "\"{0}\" {1} {2}", new String [] { "Tom", "&", "\"Jerry\"" }); 96 assertEquals(tempString, this.boundUiString.construct()); 97 98 bindTestWithoutOption(); 99 bindTestWithAutoSurround(); 100 bindTestWithHtmlEncoding(); 101 bindTestWithJsEscape(); 102 } 103 104 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 assertEquals("Tom & Jerry", this.boundUiString.construct()); 115 } 116 117 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 assertEquals("'Tom' & <cat>", this.boundUiString.construct()); 128 129 this.boundUiString.clearBindParameters(); 130 this.boundUiString.bind("Tom"); 131 132 assertEquals("Tom & {1}", this.boundUiString.construct()); 134 } 135 136 139 private void bindTestWithHtmlEncoding() { 140 this.boundUiString.setDefaultOptions(UiString.OPTION_HTML_ENCODING); 141 this.boundUiString.bind("<cat>"); 142 this.boundUiString.bind("others"); 144 145 assertEquals("Tom & <cat>", this.boundUiString.construct()); 147 } 148 149 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 165 public void testMap() { 166 this.mappedUiString.map("first", "Tom"); 167 168 assertEquals("\"Tom\" & {second}", this.mappedUiString.construct()); 170 171 this.mappedUiString.map("second", "Jerry"); 172 173 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 e) { 181 } 183 184 this.mappedUiString.map("second", "Jerry", UiString.OPTION_NONE); 185 186 assertEquals("\"Tom\" & Jerry", this.mappedUiString.construct()); 188 189 mapTestSetOptions(); 190 } 191 192 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 & friends\"", 214 this.mappedUiString.construct()); 215 216 this.mappedUiString.clearOptions(); 217 this.mappedUiString.map("first", "{second}"); 218 this.mappedUiString.map("second", "Jerry"); 219 220 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 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 |