1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang.text;
19
20 import java.text.FieldPosition;
21 import java.text.Format;
22 import java.text.ParsePosition;
23 import java.text.SimpleDateFormat;
24 import java.util.Locale;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29 import junit.textui.TestRunner;
30
31
32
33
34 public class CompositeFormatTest extends TestCase {
35
36
37
38
39
40
41 public static void main(String[] args) {
42 TestRunner.run(suite());
43 }
44
45
46
47
48
49
50 public static Test suite() {
51 TestSuite suite = new TestSuite(CompositeFormatTest.class);
52 suite.setName("CompositeFormat Tests");
53 return suite;
54 }
55
56
57
58
59
60
61
62 public CompositeFormatTest(String name) {
63 super(name);
64 }
65
66
67
68
69
70 public void testCompositeFormat() {
71
72 Format parser = new Format() {
73 public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
74 throw new UnsupportedOperationException("Not implemented");
75 }
76
77 public Object parseObject(String source, ParsePosition pos) {
78 return null;
79 }
80 };
81
82 Format formatter = new Format() {
83 public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
84 return null;
85 }
86
87 public Object parseObject(String source, ParsePosition pos) {
88 throw new UnsupportedOperationException("Not implemented");
89 }
90 };
91
92 CompositeFormat composite = new CompositeFormat(parser, formatter);
93
94 composite.parseObject("", null);
95 composite.format(new Object(), new StringBuffer(), null);
96 assertEquals( "Parser get method incorrectly implemented", parser, composite.getParser() );
97 assertEquals( "Formatter get method incorrectly implemented", formatter, composite.getFormatter() );
98 }
99
100 public void testUsage() throws Exception {
101 Format f1 = new SimpleDateFormat("MMddyyyy", Locale.ENGLISH);
102 Format f2 = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
103 CompositeFormat c = new CompositeFormat(f1, f2);
104 String testString = "January 3, 2005";
105 assertEquals(testString, c.format(c.parseObject("01032005")));
106 assertEquals(testString, c.reformat("01032005"));
107 }
108
109 }