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 java.util.Date; 019 import java.util.Locale; 020 021 import junit.framework.TestCase; 022 import junit.framework.TestSuite; 023 024 import org.joda.time.chrono.ISOChronology; 025 import org.joda.time.convert.ConverterManager; 026 import org.joda.time.convert.MockZeroNullIntegerConverter; 027 import org.joda.time.format.DateTimeFormat; 028 import org.joda.time.format.DateTimeFormatter; 029 030 /** 031 * This class is a Junit unit test for Instant. 032 * 033 * @author Stephen Colebourne 034 */ 035 public class TestInstant_Constructors extends TestCase { 036 037 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 038 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 039 040 // 1970-06-09 041 private long TEST_TIME_NOW = 042 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 043 044 // 1970-04-05 045 private long TEST_TIME1 = 046 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 047 + 12L * DateTimeConstants.MILLIS_PER_HOUR 048 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 049 050 // 1971-05-06 051 private long TEST_TIME2 = 052 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 053 + 14L * DateTimeConstants.MILLIS_PER_HOUR 054 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 055 056 private DateTimeZone zone = null; 057 private Locale locale = null; 058 059 public static void main(String[] args) { 060 junit.textui.TestRunner.run(suite()); 061 } 062 063 public static TestSuite suite() { 064 return new TestSuite(TestInstant_Constructors.class); 065 } 066 067 public TestInstant_Constructors(String name) { 068 super(name); 069 } 070 071 protected void setUp() throws Exception { 072 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 073 zone = DateTimeZone.getDefault(); 074 locale = Locale.getDefault(); 075 DateTimeZone.setDefault(LONDON); 076 java.util.TimeZone.setDefault(LONDON.toTimeZone()); 077 Locale.setDefault(Locale.UK); 078 } 079 080 protected void tearDown() throws Exception { 081 DateTimeUtils.setCurrentMillisSystem(); 082 DateTimeZone.setDefault(zone); 083 java.util.TimeZone.setDefault(zone.toTimeZone()); 084 Locale.setDefault(locale); 085 zone = null; 086 } 087 088 //----------------------------------------------------------------------- 089 /** 090 * Test now () 091 */ 092 public void test_now() throws Throwable { 093 Instant test = Instant.now(); 094 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 095 assertEquals(TEST_TIME_NOW, test.getMillis()); 096 } 097 098 //----------------------------------------------------------------------- 099 public void testParse_noFormatter() throws Throwable { 100 assertEquals(new DateTime(2010, 6, 30, 0, 20, ISOChronology.getInstance(LONDON)).toInstant(), Instant.parse("2010-06-30T01:20+02:00")); 101 assertEquals(new DateTime(2010, 1, 2, 14, 50, ISOChronology.getInstance(LONDON)).toInstant(), Instant.parse("2010-002T14:50")); 102 } 103 104 public void testParse_formatter() throws Throwable { 105 DateTimeFormatter f = DateTimeFormat.forPattern("yyyy--dd MM HH").withChronology(ISOChronology.getInstance(PARIS)); 106 assertEquals(new DateTime(2010, 6, 30, 13, 0, ISOChronology.getInstance(PARIS)).toInstant(), Instant.parse("2010--30 06 13", f)); 107 } 108 109 //----------------------------------------------------------------------- 110 /** 111 * Test constructor () 112 */ 113 public void testConstructor() throws Throwable { 114 Instant test = new Instant(); 115 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 116 assertEquals(TEST_TIME_NOW, test.getMillis()); 117 } 118 119 //----------------------------------------------------------------------- 120 /** 121 * Test constructor (long) 122 */ 123 public void testConstructor_long1() throws Throwable { 124 Instant test = new Instant(TEST_TIME1); 125 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 126 assertEquals(TEST_TIME1, test.getMillis()); 127 } 128 129 /** 130 * Test constructor (long) 131 */ 132 public void testConstructor_long2() throws Throwable { 133 Instant test = new Instant(TEST_TIME2); 134 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 135 assertEquals(TEST_TIME2, test.getMillis()); 136 } 137 138 //----------------------------------------------------------------------- 139 /** 140 * Test constructor (Object) 141 */ 142 public void testConstructor_Object() throws Throwable { 143 Date date = new Date(TEST_TIME1); 144 Instant test = new Instant(date); 145 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 146 assertEquals(TEST_TIME1, test.getMillis()); 147 } 148 149 /** 150 * Test constructor (Object) 151 */ 152 public void testConstructor_invalidObject() throws Throwable { 153 try { 154 new Instant(new Object()); 155 fail(); 156 } catch (IllegalArgumentException ex) {} 157 } 158 159 /** 160 * Test constructor (Object=null) 161 */ 162 public void testConstructor_nullObject() throws Throwable { 163 Instant test = new Instant((Object) null); 164 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 165 assertEquals(TEST_TIME_NOW, test.getMillis()); 166 } 167 168 /** 169 * Test constructor (Object=null) 170 */ 171 public void testConstructor_badconverterObject() throws Throwable { 172 try { 173 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 174 Instant test = new Instant(new Integer(0)); 175 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 176 assertEquals(0L, test.getMillis()); 177 } finally { 178 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 179 } 180 } 181 182 }