1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.utils;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.stream.Collectors;
24
25 import org.hamcrest.CoreMatchers;
26 import org.hamcrest.MatcherAssert;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.orekit.Utils;
32 import org.orekit.errors.TimeStampedCacheException;
33 import org.orekit.time.AbsoluteDate;
34
35
36
37
38
39
40 public class ImmutableTimeStampedCacheTest {
41
42
43
44
45 private static final AbsoluteDate date = AbsoluteDate.CCSDS_EPOCH;
46
47
48
49
50 @BeforeClass
51 public static void setUpBefore() {
52 Utils.setDataRoot("regular-data");
53 }
54
55
56
57
58 private List<AbsoluteDate> data;
59
60
61
62
63 private ImmutableTimeStampedCache<AbsoluteDate> cache;
64
65
66
67
68 @Before
69 public void setUp() {
70 data = Arrays.asList(date, date.shiftedBy(1), date.shiftedBy(2),
71 date.shiftedBy(3), date.shiftedBy(4),
72 date.shiftedBy(5));
73 cache = new ImmutableTimeStampedCache<AbsoluteDate>(3, data);
74 }
75
76
77
78
79
80 @Test
81 public void testImmutableTimeStampedCache() {
82
83 try {
84 new ImmutableTimeStampedCache<AbsoluteDate>(data.size() + 1, data);
85 Assert.fail("Expected Exception");
86 } catch (IllegalArgumentException e) {
87
88 }
89
90
91 try {
92 new ImmutableTimeStampedCache<AbsoluteDate>(0, data);
93 Assert.fail("Expected Exception");
94 } catch (IllegalArgumentException e) {
95
96 }
97
98
99 try {
100 new ImmutableTimeStampedCache<AbsoluteDate>(1, null);
101 Assert.fail("Expected Exception");
102 } catch (NullPointerException e) {
103
104 }
105
106
107 try {
108 new ImmutableTimeStampedCache<AbsoluteDate>(
109 1,
110 Collections
111 .<AbsoluteDate> emptyList());
112 Assert.fail("Expected Exception");
113 } catch (IllegalArgumentException e) {
114
115 }
116 }
117
118
119
120
121
122
123
124 @Test
125 public void testGetNeighbors()
126 throws TimeStampedCacheException {
127
128 int size = data.size();
129
130
131
132
133 try {
134 cache.getNeighbors(data.get(0).shiftedBy(-1));
135 Assert.fail("Expected Exception");
136 } catch (TimeStampedCacheException e) {
137
138 }
139
140
141 Assert.assertArrayEquals(cache.getNeighbors(data.get(0)).toArray(), data
142 .subList(0, 3).toArray());
143
144 Assert.assertArrayEquals(cache.getNeighbors(data.get(0).shiftedBy(0.5))
145 .toArray(), data.subList(0, 3).toArray());
146
147 Assert.assertArrayEquals(cache.getNeighbors(data.get(2)).toArray(), data
148 .subList(1, 4).toArray());
149
150 Assert.assertArrayEquals(cache.getNeighbors(data.get(2).shiftedBy(0.5))
151 .toArray(), data.subList(1, 4).toArray());
152
153 Assert.assertArrayEquals(cache
154 .getNeighbors(data.get(size - 1).shiftedBy(-0.5)).toArray(), data
155 .subList(size - 3, size).toArray());
156
157 Assert.assertArrayEquals(cache.getNeighbors(data.get(size - 1)).toArray(),
158 data.subList(size - 3, size).toArray());
159
160
161 AbsoluteDate central = data.get(size - 1).shiftedBy(1);
162 try {
163 cache.getNeighbors(central);
164 Assert.fail("Expected Exception");
165 } catch (TimeStampedCacheException e) {
166
167 MatcherAssert.assertThat(e.getMessage(),
168 CoreMatchers.containsString(central.toString()));
169 }
170 }
171
172
173
174
175 @Test
176 public void testGetNeighborsSize() {
177 Assert.assertEquals(cache.getNeighborsSize(), 3);
178 }
179
180
181
182
183 @Test
184 public void testGetEarliest() {
185 Assert.assertEquals(cache.getEarliest(), data.get(0));
186 }
187
188
189
190
191 @Test
192 public void testGetLatest() {
193 Assert.assertEquals(cache.getLatest(), data.get(data.size() - 1));
194 }
195
196
197
198
199 @Test
200 public void testGetAll() {
201 Assert.assertArrayEquals(cache.getAll().toArray(), data.toArray());
202 }
203
204
205
206
207
208
209
210 @Test
211 public void testImmutable()
212 throws TimeStampedCacheException {
213
214 List<AbsoluteDate> actuals;
215 List<AbsoluteDate> expecteds = new ArrayList<AbsoluteDate>(data);
216 AbsoluteDate different = date.shiftedBy(-50);
217
218
219
220
221 data.set(0, different);
222 Assert.assertArrayEquals(cache.getAll().toArray(), expecteds.toArray());
223
224
225 actuals = cache.getAll();
226 try {
227 actuals.set(0, different);
228 } catch (UnsupportedOperationException e) {
229
230 }
231 Assert.assertArrayEquals(cache.getAll().toArray(), expecteds.toArray());
232
233
234 actuals = cache.getNeighbors(date).collect(Collectors.toList());
235 Assert.assertArrayEquals(cache.getAll().toArray(), expecteds.toArray());
236 }
237
238
239
240
241 @Test
242 public void testEmptyCache() {
243
244 cache = ImmutableTimeStampedCache.emptyCache();
245
246
247 try {
248 cache.getNeighbors(date);
249 Assert.fail("Expected Exception");
250 } catch (TimeStampedCacheException e) {
251
252 }
253 try {
254 cache.getEarliest();
255 Assert.fail("Expected Exception");
256 } catch (IllegalStateException e) {
257
258 }
259 try {
260 cache.getLatest();
261 Assert.fail("Expected Exception");
262 } catch (IllegalStateException e) {
263
264 }
265 Assert.assertEquals(cache.getAll().size(), 0);
266 Assert.assertEquals(cache.getNeighborsSize(), 0);
267 }
268 }