-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
TemporalAmountAdapterTest.groovy
187 lines (147 loc) · 6.67 KB
/
TemporalAmountAdapterTest.groovy
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
package net.fortuna.ical4j.model
import net.fortuna.ical4j.util.CompatibilityHints
import spock.lang.Specification
import spock.lang.Unroll
import java.time.Duration
class TemporalAmountAdapterTest extends Specification {
def "verify string representation"() {
expect:
new TemporalAmountAdapter(duration) as String == expectedValue
where:
duration | expectedValue
Duration.ofHours(4) | "PT4H"
Duration.ofHours(-4) | "-PT4H"
Duration.ofDays(12) | "P12D"
java.time.Period.ofDays(12) | "P12D"
java.time.Period.ofWeeks(7) | "P7W"
java.time.Period.ofDays(365) | "P365D"
java.time.Period.ofDays(364) | "P52W"
java.time.Period.ofYears(1) | "P52W"
java.time.Period.ofMonths(6) | "P24W"
java.time.Period.ofMonths(-6) | "-P24W"
Duration.ofDays(15).plusHours(5).plusSeconds(20) | 'P15DT5H0M20S'
}
def "verify relaxed string parsing"() {
setup: 'enable relaxed parsing'
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, true)
expect:
TemporalAmountAdapter.parse(stringValue).duration == expectedDuration
where:
stringValue | expectedDuration
"P" | java.time.Period.ZERO
}
def 'verify temporalamount creation'() {
expect:
TemporalAmountAdapter.from(duration).duration == expectedTemporalAmount
where:
duration | expectedTemporalAmount
new Dur(1, 2, 3, 4) | Duration.ofDays(1).plusHours(2).plusMinutes(3).plusSeconds(4)
new Dur('PT-8H') | Duration.ofHours(-8)
new Dur(5) | java.time.Period.ofWeeks(5)
new Dur(-9) | java.time.Period.ofWeeks(-9)
}
def 'test creation from date range'() {
expect:
TemporalAmountAdapter.fromDateRange(new DateTime(start), new DateTime(end)).duration == expectedTemporalAmount
where:
start | end | expectedTemporalAmount
'20200107T000000' | '20200331T000000' | java.time.Period.ofWeeks(12)
}
@Unroll
def 'validate string representation: #dur'() {
expect: 'derived string representation equals expected'
TemporalAmountAdapter.from(dur).toString() == expectedString
where:
dur | expectedString
new Dur(33) | 'P33W'
new Dur('-P2D') | '-P2D'
new Dur(-2, 0, 0, 0) | '-P2D'
}
@Unroll
def 'verify duration plus time operations: #duration'() {
expect: 'derived end time value equals expected'
TemporalAmountAdapter.from(new Dur(duration)).getTime(new DateTime(start)) == new DateTime(expectedEnd)
where:
duration | start | expectedEnd
'P1D' | '20110326T110000' | '20110327T110000'
}
@Unroll
def 'verify duration plus time operations in different timezones: #duration'() {
setup: 'initialise timezone registry'
def tzRegistry = TimeZoneRegistryFactory.instance.createRegistry()
expect: 'derived end time value equals expected'
def tz = tzRegistry.getTimeZone(timezone)
TemporalAmountAdapter.from(new Dur(duration)).getTime(new DateTime(start, tz)) == new DateTime(expectedEnd, tz)
where:
duration | timezone | start | expectedEnd
'P1D' | 'America/Los_Angeles' | '20110326T110000' | '20110327T110000'
}
@Unroll
def 'verify duration plus time operations in different timezones with overridden platform default: #duration'() {
setup: 'override platform default timezone'
def originalPlatformTz = TimeZone.default
TimeZone.default = TimeZone.getTimeZone('Europe/Paris')
and: 'initialise timezone registry'
def tzRegistry = TimeZoneRegistryFactory.instance.createRegistry()
expect: 'derived end time value equals expected'
def tz = tzRegistry.getTimeZone(timezone)
TemporalAmountAdapter.from(new Dur(duration)).getTime(new DateTime(start, tz)) == new DateTime(expectedEnd, tz)
cleanup: 'restore platform default timezone'
TimeZone.default = originalPlatformTz
where:
duration | timezone | start | expectedEnd
'P1D' | 'America/Los_Angeles' | '20110326T110000' | '20110327T110000'
}
@Unroll
def 'verify duration plus date operations: #duration'() {
expect: 'derived end date value equals expected'
TemporalAmountAdapter.from(new Dur(duration)).getTime(new Date(start)) == new Date(expectedEnd)
where:
duration | start | expectedEnd
'P1D' | '20110312' | '20110313'
'P1D' | '20110313' | '20110314'
}
@Unroll
def 'verify duration plus date operations with overriden platform default timezone: #duration'() {
setup: 'override platform default timezone'
def originalPlatformTz = TimeZone.default
TimeZone.default = TimeZone.getTimeZone('America/New_York')
expect: 'derived end date value equals expected'
TemporalAmountAdapter.from(new Dur(duration)).getTime(new Date(start)) == new Date(expectedEnd)
cleanup: 'restore platform default timezone'
TimeZone.default = originalPlatformTz
where:
duration | start | expectedEnd
'P1D' | '20110312' | '20110313'
'P1D' | '20110313' | '20110314'
}
def 'extension module test: plus'() {
expect:
TemporalAmountAdapter.from(new Dur('P1D')).duration + TemporalAmountAdapter.from(new Dur('P1D')).duration == TemporalAmountAdapter.from(new Dur('P2D')).duration
}
def 'extension module test: negative'() {
expect:
-TemporalAmountAdapter.from(new Dur('P1D')).duration == TemporalAmountAdapter.from(new Dur('-P1D')).duration
}
def 'test hashcode equality'() {
given: 'a temporal amount adapter'
TemporalAmountAdapter adapter1 = TemporalAmountAdapter.parse('P1D')
and: 'a second identical period'
TemporalAmountAdapter adapter2 = TemporalAmountAdapter.parse('P1D')
expect: 'object equality'
adapter1 == adapter2
and: 'hashcode equality'
adapter1.hashCode() == adapter2.hashCode()
}
def 'week period parsing and values'() {
given: 'a one week amount adapter'
TemporalAmountAdapter adapter1 = TemporalAmountAdapter.parse('P1W')
and: 'a negative one week identical period'
TemporalAmountAdapter adapter2 = TemporalAmountAdapter.parse('-P1W')
expect: 'same duration, except for the sign'
adapter1.duration == -adapter2.duration
}
def 'testTemporalAmountAdapter_durationToString_DropsMinutes'() {
expect: "P1DT1H4M" == TemporalAmountAdapter.parse("P1DT1H4M") as String
}
}