~ubuntu-branches/ubuntu/precise/libjoda-time-java/precise

« back to all changes in this revision

Viewing changes to src/test/org/joda/time/format/TestDateTimeFormatterBuilder.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2011-02-09 23:05:14 UTC
  • mfrom: (5.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110209230514-22otglg8nm2y2y2n
Tags: 1.6.2-2
* Upload to unstable.
* Add (temporary) Build-Depends: ant.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright 2001-2005 Stephen Colebourne
3
 
 *
4
 
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 
 *  you may not use this file except in compliance with the License.
6
 
 *  You may obtain a copy of the License at
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
10
 
 *  Unless required by applicable law or agreed to in writing, software
11
 
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 *  See the License for the specific language governing permissions and
14
 
 *  limitations under the License.
15
 
 */
16
 
package org.joda.time.format;
17
 
 
18
 
import junit.framework.TestCase;
19
 
import junit.framework.TestSuite;
20
 
 
21
 
import org.joda.time.DateTime;
22
 
import org.joda.time.DateTimeFieldType;
23
 
 
24
 
/**
25
 
 * This class is a Junit unit test for DateTimeFormatterBuilder.
26
 
 *
27
 
 * @author Stephen Colebourne
28
 
 * @author Brian S O'Neill
29
 
 */
30
 
public class TestDateTimeFormatterBuilder extends TestCase {
31
 
 
32
 
    public static void main(String[] args) {
33
 
        junit.textui.TestRunner.run(suite());
34
 
    }
35
 
 
36
 
    public static TestSuite suite() {
37
 
        return new TestSuite(TestDateTimeFormatterBuilder.class);
38
 
    }
39
 
 
40
 
    public TestDateTimeFormatterBuilder(String name) {
41
 
        super(name);
42
 
    }
43
 
 
44
 
    protected void setUp() throws Exception {
45
 
    }
46
 
 
47
 
    protected void tearDown() throws Exception {
48
 
    }
49
 
 
50
 
    //-----------------------------------------------------------------------
51
 
    public void test_toFormatter() {
52
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
53
 
        try {
54
 
            bld.toFormatter();
55
 
            fail();
56
 
        } catch (UnsupportedOperationException ex) {}
57
 
        bld.appendLiteral('X');
58
 
        assertNotNull(bld.toFormatter());
59
 
    }
60
 
 
61
 
    public void test_toPrinter() {
62
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
63
 
        try {
64
 
            bld.toPrinter();
65
 
            fail();
66
 
        } catch (UnsupportedOperationException ex) {}
67
 
        bld.appendLiteral('X');
68
 
        assertNotNull(bld.toPrinter());
69
 
    }
70
 
 
71
 
    public void test_toParser() {
72
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
73
 
        try {
74
 
            bld.toParser();
75
 
            fail();
76
 
        } catch (UnsupportedOperationException ex) {}
77
 
        bld.appendLiteral('X');
78
 
        assertNotNull(bld.toParser());
79
 
    }
80
 
 
81
 
    //-----------------------------------------------------------------------
82
 
    public void test_canBuildFormatter() {
83
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
84
 
        assertEquals(false, bld.canBuildFormatter());
85
 
        bld.appendLiteral('X');
86
 
        assertEquals(true, bld.canBuildFormatter());
87
 
    }
88
 
 
89
 
    public void test_canBuildPrinter() {
90
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
91
 
        assertEquals(false, bld.canBuildPrinter());
92
 
        bld.appendLiteral('X');
93
 
        assertEquals(true, bld.canBuildPrinter());
94
 
    }
95
 
 
96
 
    public void test_canBuildParser() {
97
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
98
 
        assertEquals(false, bld.canBuildParser());
99
 
        bld.appendLiteral('X');
100
 
        assertEquals(true, bld.canBuildParser());
101
 
    }
102
 
 
103
 
    //-----------------------------------------------------------------------
104
 
    public void test_append_Formatter() {
105
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
106
 
        bld.appendLiteral('Y');
107
 
        DateTimeFormatter f = bld.toFormatter();
108
 
        
109
 
        DateTimeFormatterBuilder bld2 = new DateTimeFormatterBuilder();
110
 
        bld2.appendLiteral('X');
111
 
        bld2.append(f);
112
 
        bld2.appendLiteral('Z');
113
 
        assertEquals("XYZ", bld2.toFormatter().print(0L));
114
 
    }
115
 
 
116
 
    //-----------------------------------------------------------------------
117
 
    public void test_append_Printer() {
118
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
119
 
        bld.appendLiteral('Y');
120
 
        DateTimePrinter p = bld.toPrinter();
121
 
        
122
 
        DateTimeFormatterBuilder bld2 = new DateTimeFormatterBuilder();
123
 
        bld2.appendLiteral('X');
124
 
        bld2.append(p);
125
 
        bld2.appendLiteral('Z');
126
 
        assertEquals("XYZ", bld2.toFormatter().print(0L));
127
 
    }
128
 
 
129
 
    //-----------------------------------------------------------------------
130
 
    public void test_appendFixedDecimal() {
131
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
132
 
        bld.appendFixedDecimal(DateTimeFieldType.year(), 4);
133
 
        DateTimeFormatter f = bld.toFormatter();
134
 
 
135
 
        assertEquals("2007", f.print(new DateTime("2007-01-01")));
136
 
        assertEquals("0123", f.print(new DateTime("123-01-01")));
137
 
        assertEquals("0001", f.print(new DateTime("1-2-3")));
138
 
        assertEquals("99999", f.print(new DateTime("99999-2-3")));
139
 
        assertEquals("-0099", f.print(new DateTime("-99-2-3")));
140
 
        assertEquals("0000", f.print(new DateTime("0-2-3")));
141
 
 
142
 
        assertEquals(2001, f.parseDateTime("2001").getYear());
143
 
        try {
144
 
            f.parseDateTime("-2001");
145
 
            fail();
146
 
        } catch (IllegalArgumentException e) {
147
 
        }
148
 
        try {
149
 
            f.parseDateTime("200");
150
 
            fail();
151
 
        } catch (IllegalArgumentException e) {
152
 
        }
153
 
        try {
154
 
            f.parseDateTime("20016");
155
 
            fail();
156
 
        } catch (IllegalArgumentException e) {
157
 
        }
158
 
 
159
 
        bld = new DateTimeFormatterBuilder();
160
 
        bld.appendFixedDecimal(DateTimeFieldType.hourOfDay(), 2);
161
 
        bld.appendLiteral(':');
162
 
        bld.appendFixedDecimal(DateTimeFieldType.minuteOfHour(), 2);
163
 
        bld.appendLiteral(':');
164
 
        bld.appendFixedDecimal(DateTimeFieldType.secondOfMinute(), 2);
165
 
        f = bld.toFormatter();
166
 
 
167
 
        assertEquals("01:02:34", f.print(new DateTime("T1:2:34")));
168
 
 
169
 
        DateTime dt = f.parseDateTime("01:02:34");
170
 
        assertEquals(1, dt.getHourOfDay());
171
 
        assertEquals(2, dt.getMinuteOfHour());
172
 
        assertEquals(34, dt.getSecondOfMinute());
173
 
 
174
 
        try {
175
 
            f.parseDateTime("0145:02:34");
176
 
            fail();
177
 
        } catch (IllegalArgumentException e) {
178
 
        }
179
 
        try {
180
 
            f.parseDateTime("01:0:34");
181
 
            fail();
182
 
        } catch (IllegalArgumentException e) {
183
 
        }
184
 
    }
185
 
 
186
 
    //-----------------------------------------------------------------------
187
 
    public void test_appendFixedSignedDecimal() {
188
 
        DateTimeFormatterBuilder bld = new DateTimeFormatterBuilder();
189
 
        bld.appendFixedSignedDecimal(DateTimeFieldType.year(), 4);
190
 
        DateTimeFormatter f = bld.toFormatter();
191
 
 
192
 
        assertEquals("2007", f.print(new DateTime("2007-01-01")));
193
 
        assertEquals("0123", f.print(new DateTime("123-01-01")));
194
 
        assertEquals("0001", f.print(new DateTime("1-2-3")));
195
 
        assertEquals("99999", f.print(new DateTime("99999-2-3")));
196
 
        assertEquals("-0099", f.print(new DateTime("-99-2-3")));
197
 
        assertEquals("0000", f.print(new DateTime("0-2-3")));
198
 
 
199
 
        assertEquals(2001, f.parseDateTime("2001").getYear());
200
 
        assertEquals(-2001, f.parseDateTime("-2001").getYear());
201
 
        assertEquals(2001, f.parseDateTime("+2001").getYear());
202
 
        try {
203
 
            f.parseDateTime("20016");
204
 
            fail();
205
 
        } catch (IllegalArgumentException e) {
206
 
        }
207
 
    }
208
 
}