1 package com.tonbeller.wcf.format; 2 3 import java.util.Calendar ; 4 import java.util.GregorianCalendar ; 5 import java.util.Locale ; 6 7 import junit.framework.TestCase; 8 9 10 18 public class FormatterTest extends TestCase { 19 20 Formatter en; 21 Formatter de; 22 23 27 public FormatterTest(String arg0) { 28 super(arg0); 29 } 30 31 34 protected void setUp() throws Exception { 35 en = FormatterFactory.instance(Locale.ENGLISH); 36 de = FormatterFactory.instance(Locale.GERMAN); 37 } 38 39 public void testString() { 40 assertEquals("123", de.parse("string", "123", null)); 41 assertEquals("123", de.format("string", "123", null)); 42 assertEquals("", de.format("string", null, null)); 43 } 44 45 public void testInteger() { 46 assertEquals(new Integer (123), de.parse("int", "123", null)); 47 try { 48 en.parse("int", "1x23", null); 49 assertEquals("should throw exception", true, false); 50 } 51 catch (FormatException e) { 52 } 53 } 54 55 56 public void testDouble() { 57 FormatHandler h = en.getHandler("double"); 58 assertEquals(new Double (123.45), h.parse("123.45", null)); 59 h = de.getHandler("double"); 60 assertEquals(new Double (123.45), h.parse("123,45", null)); 61 assertEquals("123,45", h.format(new Double (123.45), null)); 62 } 63 64 public void testNaNDouble() { 65 FormatHandler h = en.getHandler("nandouble"); 66 assertEquals(new Double (123.45), h.parse("123.45", null)); 67 h = de.getHandler("nandouble"); 68 assertEquals(new Double (123.45), h.parse("123,45", null)); 69 assertEquals("123,45", h.format(new Double (123.45), null)); 70 assertEquals(new Double (Double.NaN), h.parse("", null)); 71 assertEquals("", h.format(new Double (Double.NaN), null)); 72 } 73 74 public void testDate() { 75 GregorianCalendar gc = new GregorianCalendar (2000, Calendar.AUGUST, 12); 76 assertEquals("08/12/2000", en.format("date", gc.getTime(), null)); 77 } 78 79 80 public void testRegex() { 81 assertEquals("abc@de.com", de.parse("email", "abc@de.com", null)); 82 try { 83 en.parse("email", "abcde.com", null); 84 assertEquals("should throw exception", "abcde.com", null); 85 } 86 catch (FormatException e) { 87 } 88 } 89 90 91 } 92 | Popular Tags |