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.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   * Unit tests for {@link org.apache.commons.lang.text.CompositeFormat}.
33   */
34  public class CompositeFormatTest extends TestCase {
35  
36      /**
37       * Main method.
38       * 
39       * @param args  command line arguments, ignored
40       */
41      public static void main(String[] args) {
42          TestRunner.run(suite());
43      }
44  
45      /**
46       * Return a new test suite containing this test case.
47       * 
48       * @return a new test suite containing this test case
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       * Create a new test case with the specified name.
58       * 
59       * @param name
60       *            name
61       */
62      public CompositeFormatTest(String name) {
63          super(name);
64      }
65  
66  
67      /**
68       * Ensures that the parse/format separation is correctly maintained. 
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;    // do nothing
79              }
80          };
81  
82          Format formatter = new Format() {
83              public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
84                  return null;    // do nothing
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 }