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; 017 018 import junit.framework.TestCase; 019 import junit.framework.TestSuite; 020 021 import org.joda.time.base.AbstractPartial; 022 import org.joda.time.chrono.BuddhistChronology; 023 import org.joda.time.chrono.ISOChronology; 024 import org.joda.time.field.AbstractPartialFieldProperty; 025 026 /** 027 * This class is a Junit unit test for YearMonthDay. 028 * 029 * @author Stephen Colebourne 030 */ 031 public class TestAbstractPartial extends TestCase { 032 033 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 034 035 private long TEST_TIME_NOW = 036 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 037 038 private long TEST_TIME1 = 039 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 040 + 12L * DateTimeConstants.MILLIS_PER_HOUR 041 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 042 043 private long TEST_TIME2 = 044 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 045 + 14L * DateTimeConstants.MILLIS_PER_HOUR 046 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 047 048 private DateTimeZone zone = null; 049 050 public static void main(String[] args) { 051 junit.textui.TestRunner.run(suite()); 052 } 053 054 public static TestSuite suite() { 055 return new TestSuite(TestAbstractPartial.class); 056 } 057 058 public TestAbstractPartial(String name) { 059 super(name); 060 } 061 062 protected void setUp() throws Exception { 063 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 064 zone = DateTimeZone.getDefault(); 065 DateTimeZone.setDefault(DateTimeZone.UTC); 066 } 067 068 protected void tearDown() throws Exception { 069 DateTimeUtils.setCurrentMillisSystem(); 070 DateTimeZone.setDefault(zone); 071 zone = null; 072 } 073 074 //----------------------------------------------------------------------- 075 public void testGetValue() throws Throwable { 076 MockPartial mock = new MockPartial(); 077 assertEquals(1970, mock.getValue(0)); 078 assertEquals(1, mock.getValue(1)); 079 080 try { 081 mock.getValue(-1); 082 fail(); 083 } catch (IndexOutOfBoundsException ex) {} 084 try { 085 mock.getValue(2); 086 fail(); 087 } catch (IndexOutOfBoundsException ex) {} 088 } 089 090 public void testGetValues() throws Throwable { 091 MockPartial mock = new MockPartial(); 092 int[] vals = mock.getValues(); 093 assertEquals(2, vals.length); 094 assertEquals(1970, vals[0]); 095 assertEquals(1, vals[1]); 096 } 097 098 public void testGetField() throws Throwable { 099 MockPartial mock = new MockPartial(); 100 assertEquals(BuddhistChronology.getInstanceUTC().year(), mock.getField(0)); 101 assertEquals(BuddhistChronology.getInstanceUTC().monthOfYear(), mock.getField(1)); 102 103 try { 104 mock.getField(-1); 105 fail(); 106 } catch (IndexOutOfBoundsException ex) {} 107 try { 108 mock.getField(2); 109 fail(); 110 } catch (IndexOutOfBoundsException ex) {} 111 } 112 113 public void testGetFieldType() throws Throwable { 114 MockPartial mock = new MockPartial(); 115 assertEquals(DateTimeFieldType.year(), mock.getFieldType(0)); 116 assertEquals(DateTimeFieldType.monthOfYear(), mock.getFieldType(1)); 117 118 try { 119 mock.getFieldType(-1); 120 fail(); 121 } catch (IndexOutOfBoundsException ex) {} 122 try { 123 mock.getFieldType(2); 124 fail(); 125 } catch (IndexOutOfBoundsException ex) {} 126 } 127 128 public void testGetFieldTypes() throws Throwable { 129 MockPartial mock = new MockPartial(); 130 DateTimeFieldType[] vals = mock.getFieldTypes(); 131 assertEquals(2, vals.length); 132 assertEquals(DateTimeFieldType.year(), vals[0]); 133 assertEquals(DateTimeFieldType.monthOfYear(), vals[1]); 134 } 135 136 public void testGetPropertyEquals() throws Throwable { 137 MockProperty0 prop0 = new MockProperty0(); 138 assertEquals(true, prop0.equals(prop0)); 139 assertEquals(true, prop0.equals(new MockProperty0())); 140 assertEquals(false, prop0.equals(new MockProperty1())); 141 assertEquals(false, prop0.equals(new MockProperty0Val())); 142 assertEquals(false, prop0.equals(new MockProperty0Field())); 143 assertEquals(false, prop0.equals(new MockProperty0Chrono())); 144 assertEquals(false, prop0.equals("")); 145 assertEquals(false, prop0.equals(null)); 146 } 147 148 //----------------------------------------------------------------------- 149 static class MockPartial extends AbstractPartial { 150 151 int[] val = new int[] {1970, 1}; 152 153 MockPartial() { 154 super(); 155 } 156 157 protected DateTimeField getField(int index, Chronology chrono) { 158 switch (index) { 159 case 0: 160 return chrono.year(); 161 case 1: 162 return chrono.monthOfYear(); 163 default: 164 throw new IndexOutOfBoundsException(); 165 } 166 } 167 168 public int size() { 169 return 2; 170 } 171 172 public int getValue(int index) { 173 return val[index]; 174 } 175 176 public void setValue(int index, int value) { 177 val[index] = value; 178 } 179 180 public Chronology getChronology() { 181 return BuddhistChronology.getInstanceUTC(); 182 } 183 } 184 185 static class MockProperty0 extends AbstractPartialFieldProperty { 186 MockPartial partial = new MockPartial(); 187 public DateTimeField getField() { 188 return partial.getField(0); 189 } 190 public ReadablePartial getReadablePartial() { 191 return partial; 192 } 193 public int get() { 194 return partial.getValue(0); 195 } 196 } 197 static class MockProperty1 extends AbstractPartialFieldProperty { 198 MockPartial partial = new MockPartial(); 199 public DateTimeField getField() { 200 return partial.getField(1); 201 } 202 public ReadablePartial getReadablePartial() { 203 return partial; 204 } 205 public int get() { 206 return partial.getValue(1); 207 } 208 } 209 static class MockProperty0Field extends MockProperty0 { 210 public DateTimeField getField() { 211 return BuddhistChronology.getInstanceUTC().hourOfDay(); 212 } 213 } 214 static class MockProperty0Val extends MockProperty0 { 215 public int get() { 216 return 99; 217 } 218 } 219 static class MockProperty0Chrono extends MockProperty0 { 220 public ReadablePartial getReadablePartial() { 221 return new MockPartial() { 222 public Chronology getChronology() { 223 return ISOChronology.getInstanceUTC(); 224 } 225 }; 226 } 227 } 228 }