View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, Steve Meyfroidt 4 * All rights reserved. 5 * Email: smeyfroi@users.sourceforge.net 6 * 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Neither the name "Scope" nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 * 36 * 37 * $Id: TestStringConvertors.java,v 1.6 2002/08/05 13:16:45 ludovicc Exp $ 38 */ 39 40 41 package test.util.convertor; 42 43 44 import java.math.BigDecimal; 45 import java.math.BigInteger; 46 import java.text.DateFormat; 47 import java.util.Date; 48 import java.util.Enumeration; 49 import java.util.GregorianCalendar; 50 import java.util.Locale; 51 import java.util.TimeZone; 52 import junit.framework.TestCase; 53 import org.scopemvc.util.DateTime; 54 import org.scopemvc.util.ScopeConfig; 55 import org.scopemvc.util.Time; 56 import org.scopemvc.util.convertor.*; 57 58 59 /*** 60 * <P> 61 * </P> 62 * 63 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 64 * @author <A HREF="mailto:danmi@users.sourceforge.net">Daniel Michalik</A> 65 * @version $Revision: 1.6 $ $Date: 2002/08/05 13:16:45 $ 66 */ 67 public final class TestStringConvertors extends TestCase { 68 69 private Date jan1_1970; 70 71 static { 72 Locale.setDefault(Locale.UK); 73 TimeZone.setDefault(TimeZone.getTimeZone("GMT")); 74 } 75 76 77 public TestStringConvertors(String inName) { 78 super(inName); 79 } 80 81 82 protected void setUp() { 83 GregorianCalendar cal = new GregorianCalendar(); 84 cal.clear(); 85 cal.set(1970, 0, 1, 0, 0, 0); 86 jan1_1970 = cal.getTime(); 87 } 88 89 90 public void testStringStringConvertor() { 91 92 StringStringConvertor c = new StringStringConvertor(); 93 94 assertEquals("", c.valueAsString(null)); 95 assertEquals("abc", c.valueAsString("abc")); 96 try { 97 c.valueAsString(new Integer(1)); 98 fail("StringStringConvertor took an Integer"); 99 } catch(Exception e) { 100 // expected 101 } 102 103 assertTrue(c.stringAsValue("xyz") instanceof String); 104 assertEquals(null, c.stringAsValue("")); 105 assertEquals("xyz", c.stringAsValue("xyz")); 106 107 c.setNullAsString("(null)"); 108 assertNull(c.stringAsValue("(null)")); 109 assertEquals("(null)", c.valueAsString(null)); 110 } 111 112 113 public void testIntegerStringConvertor() { 114 115 IntegerStringConvertor c = new IntegerStringConvertor(); 116 117 assertEquals("", c.valueAsString(null)); 118 assertEquals("1", c.valueAsString(new Integer(1))); 119 try { 120 c.valueAsString(""); 121 fail("IntegerStringConvertor took a String"); 122 } catch(Exception e) { 123 // expected 124 } 125 126 assertNull(c.stringAsValue("")); 127 assertNull(c.stringAsValue(null)); 128 assertTrue(c.stringAsValue("1") instanceof Integer); 129 assertEquals(new Integer(1), c.stringAsValue("1")); 130 131 try { 132 c.stringAsValue("(null)"); 133 fail("IntegerStringConvertor parsed '(null)'"); 134 } catch(Exception e) { 135 // expected 136 } 137 c.setNullAsString("(null)"); 138 assertNull(c.stringAsValue("(null)")); 139 assertNull(c.stringAsValue("")); 140 assertEquals("(null)", c.valueAsString(null)); 141 try { 142 c.stringAsValue("xyz"); 143 fail("IntegerStringConvertor parsed 'xyz'"); 144 } catch(Exception e) { 145 // expected 146 } 147 } 148 149 150 public void testLongStringConvertor() { 151 152 LongStringConvertor c = new LongStringConvertor(); 153 154 assertEquals("", c.valueAsString(null)); 155 assertEquals("1", c.valueAsString(new Long(1))); 156 try { 157 c.valueAsString(""); 158 fail("LongStringConvertor took a String"); 159 } catch(Exception e) { 160 // expected 161 } 162 163 assertNull(c.stringAsValue("")); 164 assertNull(c.stringAsValue(null)); 165 assertTrue(c.stringAsValue("1") instanceof Long); 166 assertEquals(new Long(1), c.stringAsValue("1")); 167 168 try { 169 c.stringAsValue("(null)"); 170 fail("LongStringConvertor parsed '(null)'"); 171 } catch(Exception e) { 172 // expected 173 } 174 c.setNullAsString("(null)"); 175 assertNull(c.stringAsValue("(null)")); 176 assertNull(c.stringAsValue("")); 177 assertEquals("(null)", c.valueAsString(null)); 178 try { 179 c.stringAsValue("xyz"); 180 fail("LongStringConvertor parsed 'xyz'"); 181 } catch(Exception e) { 182 // expected 183 } 184 } 185 186 187 public void testDoubleStringConvertor() { 188 DoubleStringConvertor c = new DoubleStringConvertor(); 189 190 assertEquals("", c.valueAsString(null)); 191 assertEquals("1.1", c.valueAsString(new Double(1.1))); 192 try { 193 c.valueAsString(""); 194 fail("DoubleStringConvertor took a String"); 195 } catch(Exception e) { 196 // expected 197 } 198 199 assertNull(c.stringAsValue("")); 200 assertNull(c.stringAsValue(null)); 201 assertTrue(c.stringAsValue("1.1") instanceof Double); 202 assertEquals(new Double(1.1), c.stringAsValue("1.1")); 203 204 try { 205 c.stringAsValue("(null)"); 206 fail("DoubleStringConvertor parsed '(null)'"); 207 } catch(Exception e) { 208 // expected 209 } 210 c.setNullAsString("(null)"); 211 assertNull(c.stringAsValue("(null)")); 212 assertNull(c.stringAsValue("")); 213 assertEquals("(null)", c.valueAsString(null)); 214 try { 215 c.stringAsValue("xyz"); 216 fail("DoubleStringConvertor parsed 'xyz'"); 217 } catch(Exception e) { 218 // expected 219 } 220 } 221 222 223 public void testFloatStringConvertor() { 224 FloatStringConvertor c = new FloatStringConvertor(); 225 226 assertEquals("", c.valueAsString(null)); 227 assertEquals("1.1", c.valueAsString(new Float(1.1))); 228 try { 229 c.valueAsString(""); 230 fail("FloatStringConvertor took a String"); 231 } catch(Exception e) { 232 // expected 233 } 234 235 assertNull(c.stringAsValue("")); 236 assertNull(c.stringAsValue(null)); 237 assertTrue(c.stringAsValue("1.1") instanceof Float); 238 assertEquals(new Float(1.1), c.stringAsValue("1.1")); 239 240 try { 241 c.stringAsValue("(null)"); 242 fail("FloatStringConvertor parsed '(null)'"); 243 } catch(Exception e) { 244 // expected 245 } 246 c.setNullAsString("(null)"); 247 assertNull(c.stringAsValue("(null)")); 248 assertNull(c.stringAsValue("")); 249 assertEquals("(null)", c.valueAsString(null)); 250 try { 251 c.stringAsValue("xyz"); 252 fail("FloatStringConvertor parsed 'xyz'"); 253 } catch(Exception e) { 254 // expected 255 } 256 } 257 258 259 public void testDateStringConvertor() { 260 DateStringConvertor c = new DateStringConvertor(); 261 // don't use string constants as date formats depend on the JVM version. 262 String dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM).format(jan1_1970); 263 264 assertEquals("", c.valueAsString(null)); 265 assertEquals(dateFormat, c.valueAsString(jan1_1970)); 266 try { 267 c.valueAsString(""); 268 fail("DateStringConvertor took an empty String"); 269 } catch(Exception e) { 270 // expected 271 } 272 273 assertNull(c.stringAsValue("")); 274 assertNull(c.stringAsValue(null)); 275 276 assertTrue(c.stringAsValue(dateFormat) instanceof Date); 277 assertEquals(jan1_1970, c.stringAsValue(dateFormat)); 278 279 280 try { 281 c.stringAsValue("1x"); 282 fail("DateStringConvertor converted nonsense"); 283 } catch(Exception e) { 284 // expected 285 } 286 try { 287 c.stringAsValue("(null)"); 288 fail("DateStringConvertor parsed '(null)'"); 289 } catch(Exception e) { 290 // expected 291 } 292 293 c.setNullAsString("(null)"); 294 assertNull(c.stringAsValue("(null)")); 295 assertNull(c.stringAsValue("")); 296 assertEquals("(null)", c.valueAsString(null)); 297 try { 298 c.stringAsValue("xyz"); 299 fail("DateStringConvertor parsed 'xyz'"); 300 } catch(Exception e) { 301 // expected 302 } 303 } 304 305 306 public void testDateTimeStringConvertor() { 307 DateTimeStringConvertor c = new DateTimeStringConvertor(); 308 // don't use string constants as date formats depend on the JVM version. 309 String dateTimeFormat = DateFormat.getDateTimeInstance().format(jan1_1970); 310 311 assertEquals("", c.valueAsString(null)); 312 assertEquals(dateTimeFormat, c.valueAsString(jan1_1970)); 313 try { 314 c.valueAsString(""); 315 fail("DateTimeStringConvertor took a String"); 316 } catch(Exception e) { 317 // expected 318 } 319 320 assertNull(c.stringAsValue("")); 321 assertNull(c.stringAsValue(null)); 322 323 assertEquals(Date.class, c.stringAsValue(dateTimeFormat).getClass()); 324 assertEquals(jan1_1970, c.stringAsValue(dateTimeFormat)); 325 326 try { 327 c.stringAsValue("1x"); 328 fail("DateTimeStringConvertor converted nonsense"); 329 } catch(Exception e) { 330 // expected 331 } 332 try { 333 c.stringAsValue("(null)"); 334 fail("DateTimeStringConvertor parsed '(null)'"); 335 } catch(Exception e) { 336 // expected 337 } 338 339 c.setNullAsString("(null)"); 340 assertNull(c.stringAsValue("(null)")); 341 assertNull(c.stringAsValue("")); 342 assertEquals("(null)", c.valueAsString(null)); 343 try { 344 c.stringAsValue("xyz"); 345 fail("DateStringConvertor parsed 'xyz'"); 346 } catch(Exception e) { 347 // expected 348 } 349 } 350 351 352 public void testTimeStringConvertor() { 353 TimeStringConvertor c = new TimeStringConvertor(); 354 // don't use string constants as date formats depend on the JVM version. 355 String timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(jan1_1970); 356 357 assertEquals("", c.valueAsString(null)); 358 assertEquals(timeFormat, c.valueAsString(new Date(0))); 359 try { 360 c.valueAsString(""); 361 fail("DateTimeStringConvertor took a String"); 362 } catch(Exception e) { 363 // expected 364 } 365 366 assertNull(c.stringAsValue("")); 367 assertNull(c.stringAsValue(null)); 368 369 assertTrue(c.stringAsValue(timeFormat) instanceof Date); 370 assertEquals(jan1_1970, c.stringAsValue(timeFormat)); 371 372 try { 373 c.stringAsValue("1x"); 374 fail("DateTimeStringConvertor converted nonsense"); 375 } catch(Exception e) { 376 // expected 377 } 378 try { 379 c.stringAsValue("(null)"); 380 fail("DateTimeStringConvertor parsed '(null)'"); 381 } catch(Exception e) { 382 // expected 383 } 384 385 c.setNullAsString("(null)"); 386 assertNull(c.stringAsValue("(null)")); 387 assertNull(c.stringAsValue("")); 388 assertEquals("(null)", c.valueAsString(null)); 389 try { 390 c.stringAsValue("xyz"); 391 fail("DateStringConvertor parsed 'xyz'"); 392 } catch(Exception e) { 393 // expected 394 } 395 } 396 397 398 public void testBigIntegerStringConvertor() { 399 400 BigIntegerStringConvertor c = new BigIntegerStringConvertor(); 401 402 assertEquals("", c.valueAsString(null)); 403 assertEquals("1", c.valueAsString(BigInteger.valueOf(1L))); 404 try { 405 c.valueAsString(""); 406 fail("BigIntegerStringConvertor took a String"); 407 } catch(Exception e) { 408 // expected 409 } 410 411 assertNull(c.stringAsValue("")); 412 assertNull(c.stringAsValue(null)); 413 assertTrue(c.stringAsValue("1") instanceof BigInteger); 414 assertEquals(BigInteger.valueOf(1L), c.stringAsValue("1")); 415 416 try { 417 c.stringAsValue("(null)"); 418 fail("BigIntegerStringConvertor parsed '(null)'"); 419 } catch(Exception e) { 420 // expected 421 } 422 c.setNullAsString("(null)"); 423 assertNull(c.stringAsValue("(null)")); 424 assertNull(c.stringAsValue("")); 425 assertEquals("(null)", c.valueAsString(null)); 426 try { 427 c.stringAsValue("xyz"); 428 fail("BigIntegerStringConvertor parsed 'xyz'"); 429 } catch(Exception e) { 430 // expected 431 } 432 } 433 434 435 public void testBigDecimalStringConvertor() { 436 437 BigDecimalStringConvertor c = new BigDecimalStringConvertor(); 438 439 assertEquals("", c.valueAsString(null)); 440 assertTrue(c.valueAsString(new BigDecimal(1.1)).startsWith("1.1000")); // rounding errors! 441 try { 442 c.valueAsString(""); 443 fail("BigDecimalStringConvertor took a String"); 444 } catch(Exception e) { 445 // expected 446 } 447 448 assertNull(c.stringAsValue("")); 449 assertNull(c.stringAsValue(null)); 450 assertTrue(c.stringAsValue("1.1") instanceof BigDecimal); 451 assertTrue( ((BigDecimal)c.stringAsValue("1.1")).floatValue() - 1.1f < .05); 452 453 try { 454 c.stringAsValue("(null)"); 455 fail("BigDecimalStringConvertor parsed '(null)'"); 456 } catch(Exception e) { 457 // expected 458 } 459 c.setNullAsString("(null)"); 460 assertNull(c.stringAsValue("(null)")); 461 assertNull(c.stringAsValue("")); 462 assertEquals("(null)", c.valueAsString(null)); 463 try { 464 c.stringAsValue("xyz"); 465 fail("BigDecimalStringConvertor parsed 'xyz'"); 466 } catch(Exception e) { 467 // expected 468 } 469 } 470 471 472 public void testStringConvertorFactory() { 473 ScopeConfig.setPropertiesName("test.util.CustomScopeConfig"); 474 475 assertNull(StringConvertors.forClass(java.util.Enumeration.class)); 476 assertTrue(StringConvertors.forClass(java.lang.String.class) instanceof StringStringConvertor); 477 assertTrue(StringConvertors.forClass(java.lang.Integer.class) instanceof IntegerStringConvertor); 478 assertTrue(StringConvertors.forClass(java.lang.Integer.TYPE) instanceof IntegerStringConvertor); 479 assertTrue(StringConvertors.forClass(java.lang.Long.class) instanceof LongStringConvertor); 480 assertTrue(StringConvertors.forClass(java.lang.Long.TYPE) instanceof LongStringConvertor); 481 assertTrue(StringConvertors.forClass(java.lang.Double.class) instanceof DoubleStringConvertor); 482 assertTrue(StringConvertors.forClass(java.lang.Double.TYPE) instanceof DoubleStringConvertor); 483 assertTrue(StringConvertors.forClass(java.lang.Float.class) instanceof FloatStringConvertor); 484 assertTrue(StringConvertors.forClass(java.lang.Float.TYPE) instanceof FloatStringConvertor); 485 assertTrue(StringConvertors.forClass(java.util.Date.class) instanceof DateStringConvertor); 486 assertTrue(StringConvertors.forClass(org.scopemvc.util.Time.class) instanceof TimeStringConvertor); 487 assertTrue(StringConvertors.forClass(org.scopemvc.util.DateTime.class) instanceof DateTimeStringConvertor); 488 assertTrue(StringConvertors.forClass(java.math.BigInteger.class) instanceof BigIntegerStringConvertor); 489 assertTrue(StringConvertors.forClass(java.math.BigDecimal.class) instanceof BigDecimalStringConvertor); 490 } 491 }

This page was automatically generated by Maven