1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 import org.joda.time.format.DateTimeFormat;
28 import org.joda.time.format.DateTimeFormatter;
29
30
31
32
33
34
35 public class TestInstant_Constructors extends TestCase {
36
37 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
38 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
39
40
41 private long TEST_TIME_NOW =
42 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
43
44
45 private long TEST_TIME1 =
46 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
47 + 12L * DateTimeConstants.MILLIS_PER_HOUR
48 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
49
50
51 private long TEST_TIME2 =
52 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
53 + 14L * DateTimeConstants.MILLIS_PER_HOUR
54 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
55
56 private DateTimeZone zone = null;
57 private Locale locale = null;
58
59 public static void main(String[] args) {
60 junit.textui.TestRunner.run(suite());
61 }
62
63 public static TestSuite suite() {
64 return new TestSuite(TestInstant_Constructors.class);
65 }
66
67 public TestInstant_Constructors(String name) {
68 super(name);
69 }
70
71 protected void setUp() throws Exception {
72 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
73 zone = DateTimeZone.getDefault();
74 locale = Locale.getDefault();
75 DateTimeZone.setDefault(LONDON);
76 java.util.TimeZone.setDefault(LONDON.toTimeZone());
77 Locale.setDefault(Locale.UK);
78 }
79
80 protected void tearDown() throws Exception {
81 DateTimeUtils.setCurrentMillisSystem();
82 DateTimeZone.setDefault(zone);
83 java.util.TimeZone.setDefault(zone.toTimeZone());
84 Locale.setDefault(locale);
85 zone = null;
86 }
87
88
89
90
91
92 public void test_now() throws Throwable {
93 Instant test = Instant.now();
94 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology());
95 assertEquals(TEST_TIME_NOW, test.getMillis());
96 }
97
98
99 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
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
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
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
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
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
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
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 }