1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time;
17
18 import java.util.Arrays;
19
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22
23 import org.joda.time.chrono.GregorianChronology;
24 import org.joda.time.chrono.ISOChronology;
25
26
27
28
29
30
31 public class TestPartial_Constructors extends TestCase {
32
33 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
34 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
35 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
36 private static final Chronology GREGORIAN_PARIS = GregorianChronology.getInstance(PARIS);
37 private static final Chronology GREGORIAN_UTC = GregorianChronology.getInstanceUTC();
38 private static final int OFFSET = 1;
39
40 private long TEST_TIME_NOW =
41 10L * DateTimeConstants.MILLIS_PER_HOUR
42 + 20L * DateTimeConstants.MILLIS_PER_MINUTE
43 + 30L * DateTimeConstants.MILLIS_PER_SECOND
44 + 40L;
45
46 private long TEST_TIME1 =
47 1L * DateTimeConstants.MILLIS_PER_HOUR
48 + 2L * DateTimeConstants.MILLIS_PER_MINUTE
49 + 3L * DateTimeConstants.MILLIS_PER_SECOND
50 + 4L;
51
52 private long TEST_TIME2 =
53 1L * DateTimeConstants.MILLIS_PER_DAY
54 + 5L * DateTimeConstants.MILLIS_PER_HOUR
55 + 6L * DateTimeConstants.MILLIS_PER_MINUTE
56 + 7L * DateTimeConstants.MILLIS_PER_SECOND
57 + 8L;
58
59 private DateTimeZone zone = null;
60
61 public static void main(String[] args) {
62 junit.textui.TestRunner.run(suite());
63 }
64
65 public static TestSuite suite() {
66 return new TestSuite(TestPartial_Constructors.class);
67 }
68
69 public TestPartial_Constructors(String name) {
70 super(name);
71 }
72
73 protected void setUp() throws Exception {
74 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
75 zone = DateTimeZone.getDefault();
76 DateTimeZone.setDefault(LONDON);
77 }
78
79 protected void tearDown() throws Exception {
80 DateTimeUtils.setCurrentMillisSystem();
81 DateTimeZone.setDefault(zone);
82 zone = null;
83 }
84
85
86
87
88
89 public void testConstructor() throws Throwable {
90 Partial test = new Partial();
91 assertEquals(ISO_UTC, test.getChronology());
92 assertEquals(0, test.size());
93 }
94
95
96
97
98
99 public void testConstructor_Chrono() throws Throwable {
100 Partial test = new Partial((Chronology) null);
101 assertEquals(ISO_UTC, test.getChronology());
102 assertEquals(0, test.size());
103
104 test = new Partial(GREGORIAN_PARIS);
105 assertEquals(GREGORIAN_UTC, test.getChronology());
106 assertEquals(0, test.size());
107 }
108
109
110
111
112
113 public void testConstructor_Type_int() throws Throwable {
114 Partial test = new Partial(DateTimeFieldType.dayOfYear(), 4);
115 assertEquals(ISO_UTC, test.getChronology());
116 assertEquals(1, test.size());
117 assertEquals(4, test.getValue(0));
118 assertEquals(4, test.get(DateTimeFieldType.dayOfYear()));
119 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear()));
120 }
121
122
123
124
125 public void testConstructorEx1_Type_int() throws Throwable {
126 try {
127 new Partial(null, 4);
128 fail();
129 } catch (IllegalArgumentException ex) {
130 assertMessageContains(ex, "must not be null");
131 }
132 }
133
134
135
136
137 public void testConstructorEx2_Type_int() throws Throwable {
138 try {
139 new Partial(DateTimeFieldType.dayOfYear(), 0);
140 fail();
141 } catch (IllegalArgumentException ex) {
142
143 }
144 }
145
146
147
148
149
150 public void testConstructor_Type_int_Chrono() throws Throwable {
151 Partial test = new Partial(DateTimeFieldType.dayOfYear(), 4, GREGORIAN_PARIS);
152 assertEquals(GREGORIAN_UTC, test.getChronology());
153 assertEquals(1, test.size());
154 assertEquals(4, test.getValue(0));
155 assertEquals(4, test.get(DateTimeFieldType.dayOfYear()));
156 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear()));
157 }
158
159
160
161
162 public void testConstructorEx_Type_int_Chrono() throws Throwable {
163 try {
164 new Partial(null, 4, ISO_UTC);
165 fail();
166 } catch (IllegalArgumentException ex) {
167 assertMessageContains(ex, "must not be null");
168 }
169 }
170
171
172
173
174 public void testConstructorEx2_Type_int_Chrono() throws Throwable {
175 try {
176 new Partial(DateTimeFieldType.dayOfYear(), 0, ISO_UTC);
177 fail();
178 } catch (IllegalArgumentException ex) {
179
180 }
181 }
182
183
184
185
186
187 public void testConstructor_TypeArray_intArray() throws Throwable {
188 DateTimeFieldType[] types = new DateTimeFieldType[] {
189 DateTimeFieldType.year(),
190 DateTimeFieldType.dayOfYear()
191 };
192 int[] values = new int[] {2005, 33};
193 Partial test = new Partial(types, values);
194 assertEquals(ISO_UTC, test.getChronology());
195 assertEquals(2, test.size());
196 assertEquals(2005, test.getValue(0));
197 assertEquals(2005, test.get(DateTimeFieldType.year()));
198 assertEquals(true, test.isSupported(DateTimeFieldType.year()));
199 assertEquals(33, test.getValue(1));
200 assertEquals(33, test.get(DateTimeFieldType.dayOfYear()));
201 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear()));
202 assertEquals(true, Arrays.equals(test.getFieldTypes(), types));
203 assertEquals(true, Arrays.equals(test.getValues(), values));
204 }
205
206
207
208
209 public void testConstructor2_TypeArray_intArray() throws Throwable {
210 DateTimeFieldType[] types = new DateTimeFieldType[0];
211 int[] values = new int[0];
212 Partial test = new Partial(types, values);
213 assertEquals(ISO_UTC, test.getChronology());
214 assertEquals(0, test.size());
215 }
216
217
218
219
220 public void testConstructorEx1_TypeArray_intArray() throws Throwable {
221 try {
222 new Partial((DateTimeFieldType[]) null, new int[] {1});
223 fail();
224 } catch (IllegalArgumentException ex) {
225 assertMessageContains(ex, "must not be null");
226 }
227 }
228
229
230
231
232 public void testConstructorEx3_TypeArray_intArray() throws Throwable {
233 try {
234 new Partial(new DateTimeFieldType[] {DateTimeFieldType.dayOfYear()}, null);
235 fail();
236 } catch (IllegalArgumentException ex) {
237 assertMessageContains(ex, "must not be null");
238 }
239 }
240
241
242
243
244 public void testConstructorEx5_TypeArray_intArray() throws Throwable {
245 try {
246 new Partial(new DateTimeFieldType[] {DateTimeFieldType.dayOfYear()}, new int[2]);
247 fail();
248 } catch (IllegalArgumentException ex) {
249 assertMessageContains(ex, "same length");
250 }
251 }
252
253
254
255
256 public void testConstructorEx6_TypeArray_intArray() throws Throwable {
257 try {
258 new Partial(new DateTimeFieldType[] {null, DateTimeFieldType.dayOfYear()}, new int[2]);
259 fail();
260 } catch (IllegalArgumentException ex) {
261 assertMessageContains(ex, "contain null");
262 }
263 try {
264 new Partial(new DateTimeFieldType[] {DateTimeFieldType.dayOfYear(), null}, new int[2]);
265 fail();
266 } catch (IllegalArgumentException ex) {
267 assertMessageContains(ex, "contain null");
268 }
269 }
270
271
272
273
274 public void testConstructorEx7_TypeArray_intArray() throws Throwable {
275 int[] values = new int[] {1, 1, 1};
276 DateTimeFieldType[] types = new DateTimeFieldType[] {
277 DateTimeFieldType.dayOfMonth(), DateTimeFieldType.year(), DateTimeFieldType.monthOfYear() };
278 try {
279 new Partial(types, values);
280 fail();
281 } catch (IllegalArgumentException ex) {
282 assertMessageContains(ex, "must be in order", "largest-smallest");
283 }
284
285 types = new DateTimeFieldType[] {
286 DateTimeFieldType.year(), DateTimeFieldType.dayOfMonth(), DateTimeFieldType.monthOfYear() };
287 try {
288 new Partial(types, values);
289 fail();
290 } catch (IllegalArgumentException ex) {
291 assertMessageContains(ex, "must be in order", "largest-smallest");
292 }
293
294 types = new DateTimeFieldType[] {
295 DateTimeFieldType.year(), DateTimeFieldType.era(), DateTimeFieldType.monthOfYear() };
296 try {
297 new Partial(types, values);
298 fail();
299 } catch (IllegalArgumentException ex) {
300 assertMessageContains(ex, "must be in order", "largest-smallest");
301 }
302
303 types = new DateTimeFieldType[] {
304 DateTimeFieldType.year(), DateTimeFieldType.dayOfMonth(), DateTimeFieldType.era() };
305 try {
306 new Partial(types, values);
307 fail();
308 } catch (IllegalArgumentException ex) {
309 assertMessageContains(ex, "must be in order", "largest-smallest");
310 }
311
312 types = new DateTimeFieldType[] {
313 DateTimeFieldType.year(), DateTimeFieldType.dayOfMonth(), DateTimeFieldType.dayOfYear() };
314 try {
315 new Partial(types, values);
316 fail();
317 } catch (IllegalArgumentException ex) {
318 assertMessageContains(ex, "must be in order", "largest-smallest");
319 }
320
321 types = new DateTimeFieldType[] {
322 DateTimeFieldType.yearOfEra(), DateTimeFieldType.year(), DateTimeFieldType.dayOfYear() };
323 try {
324 new Partial(types, values);
325 fail();
326 } catch (IllegalArgumentException ex) {
327 assertMessageContains(ex, "must be in order", "largest-smallest");
328 }
329 }
330
331
332
333
334 public void testConstructorEx8_TypeArray_intArray() throws Throwable {
335 int[] values = new int[] {1, 1, 1};
336 DateTimeFieldType[] types = new DateTimeFieldType[] {
337 DateTimeFieldType.era(), DateTimeFieldType.year(), DateTimeFieldType.year() };
338 try {
339 new Partial(types, values);
340 fail();
341 } catch (IllegalArgumentException ex) {
342 assertMessageContains(ex, "must not", "duplicate");
343 }
344
345 types = new DateTimeFieldType[] {
346 DateTimeFieldType.era(), DateTimeFieldType.era(), DateTimeFieldType.monthOfYear() };
347 try {
348 new Partial(types, values);
349 fail();
350 } catch (IllegalArgumentException ex) {
351 assertMessageContains(ex, "must not", "duplicate");
352 }
353
354 types = new DateTimeFieldType[] {
355 DateTimeFieldType.dayOfYear(), DateTimeFieldType.dayOfMonth(), DateTimeFieldType.dayOfMonth() };
356 try {
357 new Partial(types, values);
358 fail();
359 } catch (IllegalArgumentException ex) {
360 assertMessageContains(ex, "must not", "duplicate");
361 }
362 }
363
364
365
366
367 public void testConstructorEx9_TypeArray_intArray() throws Throwable {
368 int[] values = new int[] {3, 0};
369 DateTimeFieldType[] types = new DateTimeFieldType[] {
370 DateTimeFieldType.dayOfMonth(), DateTimeFieldType.dayOfWeek()};
371 try {
372 new Partial(types, values);
373 fail();
374 } catch (IllegalArgumentException ex) {
375
376 }
377 }
378
379
380
381
382
383 public void testConstructor_TypeArray_intArray_Chrono() throws Throwable {
384 DateTimeFieldType[] types = new DateTimeFieldType[] {
385 DateTimeFieldType.year(),
386 DateTimeFieldType.dayOfYear()
387 };
388 int[] values = new int[] {2005, 33};
389 Partial test = new Partial(types, values, GREGORIAN_PARIS);
390 assertEquals(GREGORIAN_UTC, test.getChronology());
391 assertEquals(2, test.size());
392 assertEquals(2005, test.getValue(0));
393 assertEquals(2005, test.get(DateTimeFieldType.year()));
394 assertEquals(true, test.isSupported(DateTimeFieldType.year()));
395 assertEquals(33, test.getValue(1));
396 assertEquals(33, test.get(DateTimeFieldType.dayOfYear()));
397 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear()));
398 assertEquals(true, Arrays.equals(test.getFieldTypes(), types));
399 assertEquals(true, Arrays.equals(test.getValues(), values));
400 }
401
402
403
404
405
406 public void testConstructor_Partial() throws Throwable {
407 YearMonthDay ymd = new YearMonthDay(2005, 6, 25, GREGORIAN_PARIS);
408 Partial test = new Partial(ymd);
409 assertEquals(GREGORIAN_UTC, test.getChronology());
410 assertEquals(3, test.size());
411 assertEquals(2005, test.getValue(0));
412 assertEquals(2005, test.get(DateTimeFieldType.year()));
413 assertEquals(true, test.isSupported(DateTimeFieldType.year()));
414 assertEquals(6, test.getValue(1));
415 assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
416 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear()));
417 assertEquals(25, test.getValue(2));
418 assertEquals(25, test.get(DateTimeFieldType.dayOfMonth()));
419 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth()));
420 }
421
422
423
424
425 public void testConstructorEx_Partial() throws Throwable {
426 try {
427 new Partial((ReadablePartial) null);
428 fail();
429 } catch (IllegalArgumentException ex) {
430 assertMessageContains(ex, "must not be null");
431 }
432 }
433
434
435
436
437
438
439
440
441 private void assertMessageContains(Exception ex, String str) {
442 assertEquals(ex.getMessage() + ": " + str, true, ex.getMessage().indexOf(str) >= 0);
443 }
444
445
446
447
448
449
450
451
452 private void assertMessageContains(Exception ex, String str1, String str2) {
453 assertEquals(ex.getMessage() + ": " + str1 + "/" + str2, true,
454 ex.getMessage().indexOf(str1) >= 0 &&
455 ex.getMessage().indexOf(str2) >= 0 &&
456 ex.getMessage().indexOf(str1) < ex.getMessage().indexOf(str2));
457 }
458
459 }