1 16 package org.joda.time; 17 18 import java.util.Date ; 19 import java.util.Locale ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.joda.time.chrono.ISOChronology; 25 import org.joda.time.convert.ConverterManager; 26 import org.joda.time.convert.MockZeroNullIntegerConverter; 27 28 33 public class TestInstant_Constructors extends TestCase { 34 35 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 36 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 37 38 private long TEST_TIME_NOW = 40 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 41 42 private long TEST_TIME1 = 44 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 45 + 12L * DateTimeConstants.MILLIS_PER_HOUR 46 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 47 48 private long TEST_TIME2 = 50 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 51 + 14L * DateTimeConstants.MILLIS_PER_HOUR 52 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 53 54 private DateTimeZone zone = null; 55 private Locale locale = null; 56 57 public static void main(String [] args) { 58 junit.textui.TestRunner.run(suite()); 59 } 60 61 public static TestSuite suite() { 62 return new TestSuite(TestInstant_Constructors.class); 63 } 64 65 public TestInstant_Constructors(String name) { 66 super(name); 67 } 68 69 protected void setUp() throws Exception { 70 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 71 zone = DateTimeZone.getDefault(); 72 locale = Locale.getDefault(); 73 DateTimeZone.setDefault(LONDON); 74 java.util.TimeZone.setDefault(LONDON.toTimeZone()); 75 Locale.setDefault(Locale.UK); 76 } 77 78 protected void tearDown() throws Exception { 79 DateTimeUtils.setCurrentMillisSystem(); 80 DateTimeZone.setDefault(zone); 81 java.util.TimeZone.setDefault(zone.toTimeZone()); 82 Locale.setDefault(locale); 83 zone = null; 84 } 85 86 90 public void testConstructor() throws Throwable { 91 Instant test = new Instant(); 92 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 93 assertEquals(TEST_TIME_NOW, test.getMillis()); 94 } 95 96 100 public void testConstructor_long1() throws Throwable { 101 Instant test = new Instant(TEST_TIME1); 102 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 103 assertEquals(TEST_TIME1, test.getMillis()); 104 } 105 106 109 public void testConstructor_long2() throws Throwable { 110 Instant test = new Instant(TEST_TIME2); 111 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 112 assertEquals(TEST_TIME2, test.getMillis()); 113 } 114 115 119 public void testConstructor_Object() throws Throwable { 120 Date date = new Date (TEST_TIME1); 121 Instant test = new Instant(date); 122 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 123 assertEquals(TEST_TIME1, test.getMillis()); 124 } 125 126 129 public void testConstructor_invalidObject() throws Throwable { 130 try { 131 new Instant(new Object ()); 132 fail(); 133 } catch (IllegalArgumentException ex) {} 134 } 135 136 139 public void testConstructor_nullObject() throws Throwable { 140 Instant test = new Instant((Object ) null); 141 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 142 assertEquals(TEST_TIME_NOW, test.getMillis()); 143 } 144 145 148 public void testConstructor_badconverterObject() throws Throwable { 149 try { 150 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 151 Instant test = new Instant(new Integer (0)); 152 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 153 assertEquals(0L, test.getMillis()); 154 } finally { 155 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 156 } 157 } 158 159 } 160 | Popular Tags |