View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link CompositeFormat}.
32   */
33  class CompositeFormatTest {
34  
35      /**
36       * Ensures that the parse/format separation is correctly maintained.
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;    // do nothing
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;    // do nothing
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  }