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:
018: package org.apache.commons.lang.text;
019:
020: import java.text.Format;
021: import java.text.FieldPosition;
022: import java.text.ParsePosition;
023:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027: import junit.textui.TestRunner;
028:
029: import java.text.SimpleDateFormat;
030: import java.util.Locale;
031:
032: /**
033: * Unit tests for {@link org.apache.commons.lang.text.CompositeFormat}.
034: */
035: public class CompositeFormatTest extends TestCase {
036:
037: /**
038: * Main method.
039: *
040: * @param args command line arguments, ignored
041: */
042: public static void main(String[] args) {
043: TestRunner.run(suite());
044: }
045:
046: /**
047: * Return a new test suite containing this test case.
048: *
049: * @return a new test suite containing this test case
050: */
051: public static Test suite() {
052: TestSuite suite = new TestSuite(CompositeFormatTest.class);
053: suite.setName("CompositeFormat Tests");
054: return suite;
055: }
056:
057: /**
058: * Create a new test case with the specified name.
059: *
060: * @param name
061: * name
062: */
063: public CompositeFormatTest(String name) {
064: super (name);
065: }
066:
067: /**
068: * Ensures that the parse/format separation is correctly maintained.
069: */
070: public void testCompositeFormat() {
071:
072: Format parser = new Format() {
073: public StringBuffer format(Object obj,
074: StringBuffer toAppendTo, FieldPosition pos) {
075: throw new UnsupportedOperationException(
076: "Not implemented");
077: }
078:
079: public Object parseObject(String source, ParsePosition pos) {
080: return null; // do nothing
081: }
082: };
083:
084: Format formatter = new Format() {
085: public StringBuffer format(Object obj,
086: StringBuffer toAppendTo, FieldPosition pos) {
087: return null; // do nothing
088: }
089:
090: public Object parseObject(String source, ParsePosition pos) {
091: throw new UnsupportedOperationException(
092: "Not implemented");
093: }
094: };
095:
096: CompositeFormat composite = new CompositeFormat(parser,
097: formatter);
098:
099: composite.parseObject("", null);
100: composite.format(new Object(), new StringBuffer(), null);
101: assertEquals("Parser get method incorrectly implemented",
102: parser, composite.getParser());
103: assertEquals("Formatter get method incorrectly implemented",
104: formatter, composite.getFormatter());
105: }
106:
107: public void testUsage() throws Exception {
108: Format f1 = new SimpleDateFormat("MMddyyyy", Locale.ENGLISH);
109: Format f2 = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
110: CompositeFormat c = new CompositeFormat(f1, f2);
111: String testString = "January 3, 2005";
112: assertEquals(testString, c.format(c.parseObject("01032005")));
113: assertEquals(testString, c.reformat("01032005"));
114: }
115:
116: }
|