001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.lang3.text;
019
020import org.junit.Test;
021import static org.junit.Assert.*;
022import java.text.FieldPosition;
023import java.text.Format;
024import java.text.ParsePosition;
025import java.text.SimpleDateFormat;
026import java.util.Locale;
027
028/**
029 * Unit tests for {@link org.apache.commons.lang3.text.CompositeFormat}.
030 */
031public class CompositeFormatTest {
032
033    /**
034     * Ensures that the parse/format separation is correctly maintained. 
035     */
036    @Test
037    public void testCompositeFormat() {
038
039        final Format parser = new Format() {
040            @Override
041            public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) {
042                throw new UnsupportedOperationException("Not implemented");
043            }
044
045            @Override
046            public Object parseObject(final String source, final ParsePosition pos) {
047                return null;    // do nothing
048            }
049        };
050
051        final Format formatter = new Format() {
052            @Override
053            public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) {
054                return null;    // do nothing
055            }
056
057            @Override
058            public Object parseObject(final String source, final ParsePosition pos) {
059                throw new UnsupportedOperationException("Not implemented");
060            }
061        };
062
063        final CompositeFormat composite = new CompositeFormat(parser, formatter);
064
065        composite.parseObject("", null);
066        composite.format(new Object(), new StringBuffer(), null);
067        assertEquals( "Parser get method incorrectly implemented", parser, composite.getParser() );
068        assertEquals( "Formatter get method incorrectly implemented", formatter, composite.getFormatter() );
069    }
070
071    @Test
072    public void testUsage() throws Exception {
073        final Format f1 = new SimpleDateFormat("MMddyyyy", Locale.ENGLISH);
074        final Format f2 = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
075        final CompositeFormat c = new CompositeFormat(f1, f2);
076        final String testString = "January 3, 2005";
077        assertEquals(testString, c.format(c.parseObject("01032005")));
078        assertEquals(testString, c.reformat("01032005"));
079    }
080
081}