KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > format > FormatterTest


1 package com.tonbeller.wcf.format;
2
3 import java.util.Calendar JavaDoc;
4 import java.util.GregorianCalendar JavaDoc;
5 import java.util.Locale JavaDoc;
6
7 import junit.framework.TestCase;
8
9
10 /**
11  * @author av
12  *
13  * To change this generated comment edit the template variable "typecomment":
14  * Window>Preferences>Java>Templates.
15  * To enable and disable the creation of type comments go to
16  * Window>Preferences>Java>Code Generation.
17  */

18 public class FormatterTest extends TestCase {
19
20   Formatter en;
21   Formatter de;
22
23   /**
24    * Constructor for FormatterTest.
25    * @param arg0
26    */

27   public FormatterTest(String JavaDoc arg0) {
28     super(arg0);
29   }
30
31   /**
32    * @see junit.framework.TestCase#setUp()
33    */

34   protected void setUp() throws Exception JavaDoc {
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 JavaDoc(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 JavaDoc(123.45), h.parse("123.45", null));
59     h = de.getHandler("double");
60     assertEquals(new Double JavaDoc(123.45), h.parse("123,45", null));
61     assertEquals("123,45", h.format(new Double JavaDoc(123.45), null));
62   }
63
64   public void testNaNDouble() {
65     FormatHandler h = en.getHandler("nandouble");
66     assertEquals(new Double JavaDoc(123.45), h.parse("123.45", null));
67     h = de.getHandler("nandouble");
68     assertEquals(new Double JavaDoc(123.45), h.parse("123,45", null));
69     assertEquals("123,45", h.format(new Double JavaDoc(123.45), null));
70     assertEquals(new Double JavaDoc(Double.NaN), h.parse("", null));
71     assertEquals("", h.format(new Double JavaDoc(Double.NaN), null));
72   }
73
74   public void testDate() {
75     GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc(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