001 /* 002 * Copyright 2001-2009 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.field; 017 018 import java.io.ByteArrayInputStream; 019 import java.io.ByteArrayOutputStream; 020 import java.io.ObjectInputStream; 021 import java.io.ObjectOutputStream; 022 023 import junit.framework.TestCase; 024 import junit.framework.TestSuite; 025 026 import org.joda.time.DurationField; 027 import org.joda.time.DurationFieldType; 028 import org.joda.time.chrono.ISOChronology; 029 030 /** 031 * This class is a Junit unit test for PreciseDurationField. 032 * 033 * @author Stephen Colebourne 034 */ 035 public class TestPreciseDurationField extends TestCase { 036 037 private static final long LONG_INTEGER_MAX = Integer.MAX_VALUE; 038 private static final int INTEGER_MAX = Integer.MAX_VALUE; 039 private static final long LONG_MAX = Long.MAX_VALUE; 040 041 private PreciseDurationField iField; 042 043 public static void main(String[] args) { 044 junit.textui.TestRunner.run(suite()); 045 } 046 047 public static TestSuite suite() { 048 return new TestSuite(TestPreciseDurationField.class); 049 } 050 051 public TestPreciseDurationField(String name) { 052 super(name); 053 } 054 055 protected void setUp() throws Exception { 056 iField = new PreciseDurationField(DurationFieldType.seconds(), 1000); 057 } 058 059 protected void tearDown() throws Exception { 060 iField = null; 061 } 062 063 //----------------------------------------------------------------------- 064 public void test_constructor() { 065 try { 066 new PreciseDurationField(null, 10); 067 fail(); 068 } catch (IllegalArgumentException ex) {} 069 } 070 071 //----------------------------------------------------------------------- 072 public void test_getType() { 073 assertEquals(DurationFieldType.seconds(), iField.getType()); 074 } 075 076 public void test_getName() { 077 assertEquals("seconds", iField.getName()); 078 } 079 080 public void test_isSupported() { 081 assertEquals(true, iField.isSupported()); 082 } 083 084 public void test_isPrecise() { 085 assertEquals(true, iField.isPrecise()); 086 } 087 088 public void test_getUnitMillis() { 089 assertEquals(1000, iField.getUnitMillis()); 090 } 091 092 public void test_toString() { 093 assertEquals("DurationField[seconds]", iField.toString()); 094 } 095 096 //----------------------------------------------------------------------- 097 public void test_getValue_long() { 098 assertEquals(0, iField.getValue(0L)); 099 assertEquals(12345, iField.getValue(12345678L)); 100 assertEquals(-1, iField.getValue(-1234L)); 101 assertEquals(INTEGER_MAX, iField.getValue(LONG_INTEGER_MAX * 1000L + 999L)); 102 try { 103 iField.getValue(LONG_INTEGER_MAX * 1000L + 1000L); 104 fail(); 105 } catch (ArithmeticException ex) {} 106 } 107 108 public void test_getValueAsLong_long() { 109 assertEquals(0L, iField.getValueAsLong(0L)); 110 assertEquals(12345L, iField.getValueAsLong(12345678L)); 111 assertEquals(-1L, iField.getValueAsLong(-1234L)); 112 assertEquals(LONG_INTEGER_MAX + 1L, iField.getValueAsLong(LONG_INTEGER_MAX * 1000L + 1000L)); 113 } 114 115 public void test_getValue_long_long() { 116 assertEquals(0, iField.getValue(0L, 567L)); 117 assertEquals(12345, iField.getValue(12345678L, 567L)); 118 assertEquals(-1, iField.getValue(-1234L, 567L)); 119 assertEquals(INTEGER_MAX, iField.getValue(LONG_INTEGER_MAX * 1000L + 999L, 567L)); 120 try { 121 iField.getValue(LONG_INTEGER_MAX * 1000L + 1000L, 567L); 122 fail(); 123 } catch (ArithmeticException ex) {} 124 } 125 126 public void test_getValueAsLong_long_long() { 127 assertEquals(0L, iField.getValueAsLong(0L, 567L)); 128 assertEquals(12345L, iField.getValueAsLong(12345678L, 567L)); 129 assertEquals(-1L, iField.getValueAsLong(-1234L, 567L)); 130 assertEquals(LONG_INTEGER_MAX + 1L, iField.getValueAsLong(LONG_INTEGER_MAX * 1000L + 1000L, 567L)); 131 } 132 133 //----------------------------------------------------------------------- 134 public void test_getMillis_int() { 135 assertEquals(0, iField.getMillis(0)); 136 assertEquals(1234000L, iField.getMillis(1234)); 137 assertEquals(-1234000L, iField.getMillis(-1234)); 138 assertEquals(LONG_INTEGER_MAX * 1000L, iField.getMillis(INTEGER_MAX)); 139 } 140 141 public void test_getMillis_long() { 142 assertEquals(0L, iField.getMillis(0L)); 143 assertEquals(1234000L, iField.getMillis(1234L)); 144 assertEquals(-1234000L, iField.getMillis(-1234L)); 145 try { 146 iField.getMillis(LONG_MAX); 147 fail(); 148 } catch (ArithmeticException ex) {} 149 } 150 151 public void test_getMillis_int_long() { 152 assertEquals(0L, iField.getMillis(0, 567L)); 153 assertEquals(1234000L, iField.getMillis(1234, 567L)); 154 assertEquals(-1234000L, iField.getMillis(-1234, 567L)); 155 assertEquals(LONG_INTEGER_MAX * 1000L, iField.getMillis(INTEGER_MAX, 567L)); 156 } 157 158 public void test_getMillis_long_long() { 159 assertEquals(0L, iField.getMillis(0L, 567L)); 160 assertEquals(1234000L, iField.getMillis(1234L, 567L)); 161 assertEquals(-1234000L, iField.getMillis(-1234L, 567L)); 162 try { 163 iField.getMillis(LONG_MAX, 567L); 164 fail(); 165 } catch (ArithmeticException ex) {} 166 } 167 168 //----------------------------------------------------------------------- 169 public void test_add_long_int() { 170 assertEquals(567L, iField.add(567L, 0)); 171 assertEquals(567L + 1234000L, iField.add(567L, 1234)); 172 assertEquals(567L - 1234000L, iField.add(567L, -1234)); 173 try { 174 iField.add(LONG_MAX, 1); 175 fail(); 176 } catch (ArithmeticException ex) {} 177 } 178 179 public void test_add_long_long() { 180 assertEquals(567L, iField.add(567L, 0L)); 181 assertEquals(567L + 1234000L, iField.add(567L, 1234L)); 182 assertEquals(567L - 1234000L, iField.add(567L, -1234L)); 183 try { 184 iField.add(LONG_MAX, 1L); 185 fail(); 186 } catch (ArithmeticException ex) {} 187 try { 188 iField.add(1L, LONG_MAX); 189 fail(); 190 } catch (ArithmeticException ex) {} 191 } 192 193 //----------------------------------------------------------------------- 194 public void test_getDifference_long_int() { 195 assertEquals(0, iField.getDifference(1L, 0L)); 196 assertEquals(567, iField.getDifference(567000L, 0L)); 197 assertEquals(567 - 1234, iField.getDifference(567000L, 1234000L)); 198 assertEquals(567 + 1234, iField.getDifference(567000L, -1234000L)); 199 try { 200 iField.getDifference(LONG_MAX, -1L); 201 fail(); 202 } catch (ArithmeticException ex) {} 203 } 204 205 public void test_getDifferenceAsLong_long_long() { 206 assertEquals(0L, iField.getDifferenceAsLong(1L, 0L)); 207 assertEquals(567L, iField.getDifferenceAsLong(567000L, 0L)); 208 assertEquals(567L - 1234L, iField.getDifferenceAsLong(567000L, 1234000L)); 209 assertEquals(567L + 1234L, iField.getDifferenceAsLong(567000L, -1234000L)); 210 try { 211 iField.getDifferenceAsLong(LONG_MAX, -1L); 212 fail(); 213 } catch (ArithmeticException ex) {} 214 } 215 216 //----------------------------------------------------------------------- 217 public void test_equals() { 218 assertEquals(true, iField.equals(iField)); 219 assertEquals(false, iField.equals(ISOChronology.getInstance().minutes())); 220 DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0); 221 assertEquals(false, iField.equals(dummy)); 222 dummy = new PreciseDurationField(DurationFieldType.seconds(), 1000); 223 assertEquals(true, iField.equals(dummy)); 224 dummy = new PreciseDurationField(DurationFieldType.millis(), 1000); 225 assertEquals(false, iField.equals(dummy)); 226 assertEquals(false, iField.equals("")); 227 assertEquals(false, iField.equals(null)); 228 } 229 230 public void test_hashCode() { 231 assertEquals(true, iField.hashCode() == iField.hashCode()); 232 assertEquals(false, iField.hashCode() == ISOChronology.getInstance().minutes().hashCode()); 233 DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0); 234 assertEquals(false, iField.hashCode() == dummy.hashCode()); 235 dummy = new PreciseDurationField(DurationFieldType.seconds(), 1000); 236 assertEquals(true, iField.hashCode() == dummy.hashCode()); 237 dummy = new PreciseDurationField(DurationFieldType.millis(), 1000); 238 assertEquals(false, iField.hashCode() == dummy.hashCode()); 239 } 240 241 //----------------------------------------------------------------------- 242 public void test_compareTo() { 243 assertEquals(0, iField.compareTo(iField)); 244 assertEquals(-1, iField.compareTo(ISOChronology.getInstance().minutes())); 245 DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0); 246 assertEquals(1, iField.compareTo(dummy)); 247 // try { 248 // iField.compareTo(""); 249 // fail(); 250 // } catch (ClassCastException ex) {} 251 try { 252 iField.compareTo(null); 253 fail(); 254 } catch (NullPointerException ex) {} 255 } 256 257 //----------------------------------------------------------------------- 258 public void testSerialization() throws Exception { 259 DurationField test = iField; 260 261 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 262 ObjectOutputStream oos = new ObjectOutputStream(baos); 263 oos.writeObject(test); 264 byte[] bytes = baos.toByteArray(); 265 oos.close(); 266 267 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 268 ObjectInputStream ois = new ObjectInputStream(bais); 269 DurationField result = (DurationField) ois.readObject(); 270 ois.close(); 271 272 assertEquals(test, result); 273 } 274 275 }