1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.text;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.text.FieldPosition;
23 import java.text.Format;
24 import java.text.ParsePosition;
25 import java.text.SimpleDateFormat;
26 import java.util.Locale;
27
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 class CompositeFormatTest {
34
35
36
37
38 @Test
39 void testCompositeFormat() {
40
41 final Format parser = new Format() {
42 private static final long serialVersionUID = 1L;
43
44 @Override
45 public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) {
46 throw new UnsupportedOperationException("Not implemented");
47 }
48
49 @Override
50 public Object parseObject(final String source, final ParsePosition pos) {
51 return null;
52 }
53 };
54
55 final Format formatter = new Format() {
56 private static final long serialVersionUID = 1L;
57
58 @Override
59 public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) {
60 return null;
61 }
62
63 @Override
64 public Object parseObject(final String source, final ParsePosition pos) {
65 throw new UnsupportedOperationException("Not implemented");
66 }
67 };
68
69 final CompositeFormat composite = new CompositeFormat(parser, formatter);
70
71 composite.parseObject("", null);
72 composite.format(new Object(), new StringBuffer(), null);
73 assertEquals(parser, composite.getParser(), "Parser get method incorrectly implemented");
74 assertEquals(formatter, composite.getFormatter(), "Formatter get method incorrectly implemented");
75 }
76
77 @Test
78 void testUsage() throws Exception {
79 final Format f1 = new SimpleDateFormat("MMddyyyy", Locale.ENGLISH);
80 final Format f2 = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
81 final CompositeFormat c = new CompositeFormat(f1, f2);
82 final String testString = "January 3, 2005";
83 assertEquals(testString, c.format(c.parseObject("01032005")));
84 assertEquals(testString, c.reformat("01032005"));
85 }
86
87 }