1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time;
17
18 import junit.framework.TestCase;
19 import junit.framework.TestSuite;
20
21 import org.joda.time.base.BasePartial;
22
23
24
25
26
27
28 public class TestBasePartial extends TestCase {
29
30 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
31
32 private long TEST_TIME_NOW =
33 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
34
35 private long TEST_TIME1 =
36 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
37 + 12L * DateTimeConstants.MILLIS_PER_HOUR
38 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
39
40 private long TEST_TIME2 =
41 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
42 + 14L * DateTimeConstants.MILLIS_PER_HOUR
43 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
44
45 private DateTimeZone zone = null;
46
47 public static void main(String[] args) {
48 junit.textui.TestRunner.run(suite());
49 }
50
51 public static TestSuite suite() {
52 return new TestSuite(TestBasePartial.class);
53 }
54
55 public TestBasePartial(String name) {
56 super(name);
57 }
58
59 protected void setUp() throws Exception {
60 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
61 zone = DateTimeZone.getDefault();
62 DateTimeZone.setDefault(DateTimeZone.UTC);
63 }
64
65 protected void tearDown() throws Exception {
66 DateTimeUtils.setCurrentMillisSystem();
67 DateTimeZone.setDefault(zone);
68 zone = null;
69 }
70
71
72 public void testSetMethods() throws Throwable {
73 MockPartial mock = new MockPartial();
74 assertEquals(1970, mock.getYear());
75 assertEquals(1, mock.getMonthOfYear());
76
77 mock.setYear(2004);
78 assertEquals(2004, mock.getYear());
79 assertEquals(1, mock.getMonthOfYear());
80
81 mock.setMonthOfYear(6);
82 assertEquals(2004, mock.getYear());
83 assertEquals(6, mock.getMonthOfYear());
84
85 mock.set(2005, 5);
86 assertEquals(2005, mock.getYear());
87 assertEquals(5, mock.getMonthOfYear());
88
89 try {
90 mock.setMonthOfYear(0);
91 fail();
92 } catch (IllegalArgumentException ex) {}
93 assertEquals(2005, mock.getYear());
94 assertEquals(5, mock.getMonthOfYear());
95
96 try {
97 mock.setMonthOfYear(13);
98 fail();
99 } catch (IllegalArgumentException ex) {}
100 assertEquals(2005, mock.getYear());
101 assertEquals(5, mock.getMonthOfYear());
102 }
103
104 static class MockPartial extends BasePartial {
105
106 MockPartial() {
107 super(new int[] {1970, 1}, null);
108 }
109
110 protected DateTimeField getField(int index, Chronology chrono) {
111 switch (index) {
112 case 0:
113 return chrono.year();
114 case 1:
115 return chrono.monthOfYear();
116 default:
117 throw new IndexOutOfBoundsException();
118 }
119 }
120
121 public int size() {
122 return 2;
123 }
124
125 public int getYear() {
126 return getValue(0);
127 }
128
129 public void setYear(int year) {
130 setValue(0, year);
131 }
132
133 public int getMonthOfYear() {
134 return getValue(1);
135 }
136
137 public void setMonthOfYear(int month) {
138 setValue(1, month);
139 }
140
141 public void set(int year, int month) {
142 setValues(new int[] {year, month});
143 }
144 }
145 }