KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TestInstant_Constructors


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time;
17
18 import java.util.Date JavaDoc;
19 import java.util.Locale JavaDoc;
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 /**
29  * This class is a Junit unit test for Instant.
30  *
31  * @author Stephen Colebourne
32  */

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     // 1970-06-09
39
private long TEST_TIME_NOW =
40             (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
41             
42     // 1970-04-05
43
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     // 1971-05-06
49
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 JavaDoc locale = null;
56
57     public static void main(String JavaDoc[] 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 JavaDoc name) {
66         super(name);
67     }
68
69     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
79         DateTimeUtils.setCurrentMillisSystem();
80         DateTimeZone.setDefault(zone);
81         java.util.TimeZone.setDefault(zone.toTimeZone());
82         Locale.setDefault(locale);
83         zone = null;
84     }
85
86     //-----------------------------------------------------------------------
87
/**
88      * Test constructor ()
89      */

90     public void testConstructor() throws Throwable JavaDoc {
91         Instant test = new Instant();
92         assertEquals(ISOChronology.getInstanceUTC(), test.getChronology());
93         assertEquals(TEST_TIME_NOW, test.getMillis());
94     }
95
96     //-----------------------------------------------------------------------
97
/**
98      * Test constructor (long)
99      */

100     public void testConstructor_long1() throws Throwable JavaDoc {
101         Instant test = new Instant(TEST_TIME1);
102         assertEquals(ISOChronology.getInstanceUTC(), test.getChronology());
103         assertEquals(TEST_TIME1, test.getMillis());
104     }
105
106     /**
107      * Test constructor (long)
108      */

109     public void testConstructor_long2() throws Throwable JavaDoc {
110         Instant test = new Instant(TEST_TIME2);
111         assertEquals(ISOChronology.getInstanceUTC(), test.getChronology());
112         assertEquals(TEST_TIME2, test.getMillis());
113     }
114
115     //-----------------------------------------------------------------------
116
/**
117      * Test constructor (Object)
118      */

119     public void testConstructor_Object() throws Throwable JavaDoc {
120         Date JavaDoc date = new Date JavaDoc(TEST_TIME1);
121         Instant test = new Instant(date);
122         assertEquals(ISOChronology.getInstanceUTC(), test.getChronology());
123         assertEquals(TEST_TIME1, test.getMillis());
124     }
125
126     /**
127      * Test constructor (Object)
128      */

129     public void testConstructor_invalidObject() throws Throwable JavaDoc {
130         try {
131             new Instant(new Object JavaDoc());
132             fail();
133         } catch (IllegalArgumentException JavaDoc ex) {}
134     }
135
136     /**
137      * Test constructor (Object=null)
138      */

139     public void testConstructor_nullObject() throws Throwable JavaDoc {
140         Instant test = new Instant((Object JavaDoc) null);
141         assertEquals(ISOChronology.getInstanceUTC(), test.getChronology());
142         assertEquals(TEST_TIME_NOW, test.getMillis());
143     }
144
145     /**
146      * Test constructor (Object=null)
147      */

148     public void testConstructor_badconverterObject() throws Throwable JavaDoc {
149         try {
150             ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
151             Instant test = new Instant(new Integer JavaDoc(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