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    *      http://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.lang3.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.apache.commons.lang3.AbstractLangTest;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Unit tests for {@link org.apache.commons.lang3.text.CompositeFormat}.
33   */
34  @Deprecated
35  public class CompositeFormatTest extends AbstractLangTest {
36  
37      /**
38       * Ensures that the parse/format separation is correctly maintained.
39       */
40      @Test
41      public void testCompositeFormat() {
42  
43          final Format parser = new Format() {
44              private static final long serialVersionUID = 1L;
45  
46              @Override
47              public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) {
48                  throw new UnsupportedOperationException("Not implemented");
49              }
50  
51              @Override
52              public Object parseObject(final String source, final ParsePosition pos) {
53                  return null;    // do nothing
54              }
55          };
56  
57          final Format formatter = new Format() {
58              private static final long serialVersionUID = 1L;
59  
60              @Override
61              public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) {
62                  return null;    // do nothing
63              }
64  
65              @Override
66              public Object parseObject(final String source, final ParsePosition pos) {
67                  throw new UnsupportedOperationException("Not implemented");
68              }
69          };
70  
71          final CompositeFormat composite = new CompositeFormat(parser, formatter);
72  
73          composite.parseObject("", null);
74          composite.format(new Object(), new StringBuffer(), null);
75          assertEquals(parser, composite.getParser(), "Parser get method incorrectly implemented");
76          assertEquals(formatter, composite.getFormatter(), "Formatter get method incorrectly implemented");
77      }
78  
79      @Test
80      public void testUsage() throws Exception {
81          final Format f1 = new SimpleDateFormat("MMddyyyy", Locale.ENGLISH);
82          final Format f2 = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
83          final CompositeFormat c = new CompositeFormat(f1, f2);
84          final String testString = "January 3, 2005";
85          assertEquals(testString, c.format(c.parseObject("01032005")));
86          assertEquals(testString, c.reformat("01032005"));
87      }
88  
89  }