001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.format;
017:
018: import junit.framework.TestCase;
019: import junit.framework.TestSuite;
020:
021: import org.joda.time.DateTime;
022: import org.joda.time.DateTimeFieldType;
023:
024: /**
025: * This class is a Junit unit test for DateTimeFormatterBuilder.
026: *
027: * @author Stephen Colebourne
028: * @author Brian S O'Neill
029: */
030: public class TestDateTimeFormatterBuilder extends TestCase {
031:
032: public static void main(String[] args) {
033: junit.textui.TestRunner.run(suite());
034: }
035:
036: public static TestSuite suite() {
037: return new TestSuite(TestDateTimeFormatterBuilder.class);
038: }
039:
040: public TestDateTimeFormatterBuilder(String name) {
041: super (name);
042: }
043:
044: protected void setUp() throws Exception {
045: }
046:
047: protected void tearDown() throws Exception {
048: }
049:
050: //-----------------------------------------------------------------------
051: public void test_toFormatter() {
052: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
053: try {
054: bld.toFormatter();
055: fail();
056: } catch (UnsupportedOperationException ex) {
057: }
058: bld.appendLiteral('X');
059: assertNotNull(bld.toFormatter());
060: }
061:
062: public void test_toPrinter() {
063: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
064: try {
065: bld.toPrinter();
066: fail();
067: } catch (UnsupportedOperationException ex) {
068: }
069: bld.appendLiteral('X');
070: assertNotNull(bld.toPrinter());
071: }
072:
073: public void test_toParser() {
074: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
075: try {
076: bld.toParser();
077: fail();
078: } catch (UnsupportedOperationException ex) {
079: }
080: bld.appendLiteral('X');
081: assertNotNull(bld.toParser());
082: }
083:
084: //-----------------------------------------------------------------------
085: public void test_canBuildFormatter() {
086: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
087: assertEquals(false, bld.canBuildFormatter());
088: bld.appendLiteral('X');
089: assertEquals(true, bld.canBuildFormatter());
090: }
091:
092: public void test_canBuildPrinter() {
093: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
094: assertEquals(false, bld.canBuildPrinter());
095: bld.appendLiteral('X');
096: assertEquals(true, bld.canBuildPrinter());
097: }
098:
099: public void test_canBuildParser() {
100: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
101: assertEquals(false, bld.canBuildParser());
102: bld.appendLiteral('X');
103: assertEquals(true, bld.canBuildParser());
104: }
105:
106: //-----------------------------------------------------------------------
107: public void test_append_Formatter() {
108: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
109: bld.appendLiteral('Y');
110: DateTimeFormatter f = bld.toFormatter();
111:
112: DateTimeFormatterBuilder bld2 = new DateTimeFormatterBuilder();
113: bld2.appendLiteral('X');
114: bld2.append(f);
115: bld2.appendLiteral('Z');
116: assertEquals("XYZ", bld2.toFormatter().print(0L));
117: }
118:
119: //-----------------------------------------------------------------------
120: public void test_append_Printer() {
121: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
122: bld.appendLiteral('Y');
123: DateTimePrinter p = bld.toPrinter();
124:
125: DateTimeFormatterBuilder bld2 = new DateTimeFormatterBuilder();
126: bld2.appendLiteral('X');
127: bld2.append(p);
128: bld2.appendLiteral('Z');
129: assertEquals("XYZ", bld2.toFormatter().print(0L));
130: }
131:
132: //-----------------------------------------------------------------------
133: public void test_appendFixedDecimal() {
134: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
135: bld.appendFixedDecimal(DateTimeFieldType.year(), 4);
136: DateTimeFormatter f = bld.toFormatter();
137:
138: assertEquals("2007", f.print(new DateTime("2007-01-01")));
139: assertEquals("0123", f.print(new DateTime("123-01-01")));
140: assertEquals("0001", f.print(new DateTime("1-2-3")));
141: assertEquals("99999", f.print(new DateTime("99999-2-3")));
142: assertEquals("-0099", f.print(new DateTime("-99-2-3")));
143: assertEquals("0000", f.print(new DateTime("0-2-3")));
144:
145: assertEquals(2001, f.parseDateTime("2001").getYear());
146: try {
147: f.parseDateTime("-2001");
148: fail();
149: } catch (IllegalArgumentException e) {
150: }
151: try {
152: f.parseDateTime("200");
153: fail();
154: } catch (IllegalArgumentException e) {
155: }
156: try {
157: f.parseDateTime("20016");
158: fail();
159: } catch (IllegalArgumentException e) {
160: }
161:
162: bld = new DateTimeFormatterBuilder();
163: bld.appendFixedDecimal(DateTimeFieldType.hourOfDay(), 2);
164: bld.appendLiteral(':');
165: bld.appendFixedDecimal(DateTimeFieldType.minuteOfHour(), 2);
166: bld.appendLiteral(':');
167: bld.appendFixedDecimal(DateTimeFieldType.secondOfMinute(), 2);
168: f = bld.toFormatter();
169:
170: assertEquals("01:02:34", f.print(new DateTime("T1:2:34")));
171:
172: DateTime dt = f.parseDateTime("01:02:34");
173: assertEquals(1, dt.getHourOfDay());
174: assertEquals(2, dt.getMinuteOfHour());
175: assertEquals(34, dt.getSecondOfMinute());
176:
177: try {
178: f.parseDateTime("0145:02:34");
179: fail();
180: } catch (IllegalArgumentException e) {
181: }
182: try {
183: f.parseDateTime("01:0:34");
184: fail();
185: } catch (IllegalArgumentException e) {
186: }
187: }
188:
189: //-----------------------------------------------------------------------
190: public void test_appendFixedSignedDecimal() {
191: DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
192: bld.appendFixedSignedDecimal(DateTimeFieldType.year(), 4);
193: DateTimeFormatter f = bld.toFormatter();
194:
195: assertEquals("2007", f.print(new DateTime("2007-01-01")));
196: assertEquals("0123", f.print(new DateTime("123-01-01")));
197: assertEquals("0001", f.print(new DateTime("1-2-3")));
198: assertEquals("99999", f.print(new DateTime("99999-2-3")));
199: assertEquals("-0099", f.print(new DateTime("-99-2-3")));
200: assertEquals("0000", f.print(new DateTime("0-2-3")));
201:
202: assertEquals(2001, f.parseDateTime("2001").getYear());
203: assertEquals(-2001, f.parseDateTime("-2001").getYear());
204: assertEquals(2001, f.parseDateTime("+2001").getYear());
205: try {
206: f.parseDateTime("20016");
207: fail();
208: } catch (IllegalArgumentException e) {
209: }
210: }
211: }
|