1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.utils;
import org.assertj.core.api.Assertions;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
public class DateUtilsTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void parseDate_valid_format() {
Date date = DateUtils.parseDate("2010-05-18");
assertThat(date.getDate(), is(18));
}
@Test
public void parseDate_not_valid_format() {
thrown.expect(SonarException.class);
DateUtils.parseDate("2010/05/18");
}
@Test
public void parseDateQuietly() {
assertNull(DateUtils.parseDateQuietly("2010/05/18"));
Date date = DateUtils.parseDateQuietly("2010-05-18");
assertThat(date.getDate(), is(18));
}
@Test
public void parseDate_fail_if_additional_characters() {
thrown.expect(SonarException.class);
DateUtils.parseDate("1986-12-04foo");
}
@Test
public void parseDateTime_valid_format() {
Date date = DateUtils.parseDateTime("2010-05-18T15:50:45+0100");
assertThat(date.getMinutes(), is(50));
}
@Test
public void parseDateTime_not_valid_format() {
thrown.expect(SonarException.class);
DateUtils.parseDate("2010/05/18 10:55");
}
@Test
public void parseDateTime_fail_if_additional_characters() {
thrown.expect(SonarException.class);
DateUtils.parseDateTime("1986-12-04T01:02:03+0300foo");
}
@Test
public void parseDateTimeQuietly() {
assertNull(DateUtils.parseDateTimeQuietly("2010/05/18 10:55"));
Date date = DateUtils.parseDateTimeQuietly("2010-05-18T15:50:45+0100");
assertThat(date.getMinutes(), is(50));
}
@Test
public void shouldFormatDate() {
assertThat(DateUtils.formatDate(new Date()), startsWith("20"));
assertThat(DateUtils.formatDate(new Date()).length(), is(10));
}
@Test
public void shouldFormatDateTime() {
assertThat(DateUtils.formatDateTime(new Date()), startsWith("20"));
assertThat(DateUtils.formatDateTime(new Date()).length(), greaterThan(20));
}
@Test
public void format_date_time_null_safe() {
Assertions.assertThat(DateUtils.formatDateTimeNullSafe(new Date())).startsWith("20");
Assertions.assertThat(DateUtils.formatDateTimeNullSafe(new Date()).length()).isGreaterThan(20);
Assertions.assertThat(DateUtils.formatDateTimeNullSafe(null)).isEmpty();
}
@Test
public void long_to_date() {
Date date = new Date();
Assertions.assertThat(DateUtils.longToDate(date.getTime())).isEqualTo(date);
Assertions.assertThat(DateUtils.longToDate(null)).isNull();
}
@Test
public void date_to_long() {
Date date = new Date();
Assertions.assertThat(DateUtils.dateToLong(date)).isEqualTo(date.getTime());
Assertions.assertThat(DateUtils.dateToLong(null)).isEqualTo(null);
}
/**
* Cordially copied from XStream unit test
* See http://koders.com/java/fid8A231D75F2C6E6909FB26BCA11C12D08AD05FB50.aspx?s=ThreadSafeDateFormatTest
*/
@Test
public void shouldBeThreadSafe() throws Exception {
final DateUtils.ThreadSafeDateFormat format = new DateUtils.ThreadSafeDateFormat("yyyy-MM-dd'T'HH:mm:ss,S z");
final Date now = new Date();
final List<Throwable> throwables = new ArrayList<>();
final ThreadGroup tg = new ThreadGroup("shouldBeThreadSafe") {
@Override
public void uncaughtException(Thread t, Throwable e) {
throwables.add(e);
super.uncaughtException(t, e);
}
};
final int[] counter = new int[1];
counter[0] = 0;
final Thread[] threads = new Thread[10];
for (int i = 0; i < threads.length; ++i) {
threads[i] = new Thread(tg, "JUnit Thread " + i) {
@Override
public void run() {
int i = 0;
try {
synchronized (this) {
notifyAll();
wait();
}
while (i < 1000 && !interrupted()) {
String formatted = format.format(now);
Thread.yield();
assertEquals(now, format.parse(formatted));
++i;
}
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
synchronized (counter) {
counter[0] += i;
}
}
};
}
for (int i = 0; i < threads.length; ++i) {
synchronized (threads[i]) {
threads[i].start();
threads[i].wait();
}
}
for (int i = 0; i < threads.length; ++i) {
synchronized (threads[i]) {
threads[i].notifyAll();
}
}
Thread.sleep(1000);
for (int i = 0; i < threads.length; ++i) {
threads[i].interrupt();
}
for (int i = 0; i < threads.length; ++i) {
synchronized (threads[i]) {
threads[i].join();
}
}
assertThat(throwables.size(), is(0));
assertThat(counter[0], greaterThanOrEqualTo(threads.length));
}
}
|