1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.propagation.events;
18
19 import java.util.Collections;
20 import java.util.NoSuchElementException;
21
22 import org.junit.jupiter.api.Assertions;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.mockito.Mockito;
26 import org.orekit.propagation.SpacecraftState;
27 import org.orekit.propagation.events.handlers.EventHandler;
28 import org.orekit.propagation.events.intervals.AdaptableInterval;
29 import org.orekit.time.AbsoluteDate;
30
31
32
33
34
35
36 public class OrDetectorTest {
37
38
39 private MockDetector a;
40
41 private MockDetector b;
42
43 private SpacecraftState s;
44
45 private BooleanDetector or;
46
47
48 @BeforeEach
49 public void setUp() {
50 a = new MockDetector();
51 b = new MockDetector();
52 s = null;
53 or = BooleanDetector.orCombine(a, b);
54 }
55
56
57
58
59 @Test
60 public void testG() {
61
62 a.g = b.g = 0.0;
63 Assertions.assertEquals(0.0, or.g(s), 0);
64 a.g = -1;
65 b.g = 0;
66 Assertions.assertEquals(0.0, or.g(s), 0);
67 a.g = 0;
68 b.g = -1;
69 Assertions.assertEquals(0.0, or.g(s), 0);
70
71
72 a.g = -1;
73 b.g = -1;
74 Assertions.assertTrue(or.g(s) < 0, "negative");
75
76
77 a.g = 0;
78 b.g = 1;
79 Assertions.assertTrue(or.g(s) > 0, "positive");
80 a.g = 1;
81 b.g = -1;
82 Assertions.assertTrue(or.g(s) > 0, "positive");
83 a.g = 1;
84 b.g = 0;
85 Assertions.assertTrue(or.g(s) > 0, "positive");
86 a.g = -1;
87 b.g = 1;
88 Assertions.assertTrue(or.g(s) > 0, "positive");
89 a.g = 1;
90 b.g = 1;
91 Assertions.assertTrue(or.g(s) > 0, "positive");
92
93 }
94
95
96
97
98 @Test
99 public void testCancellation() {
100 a.g = -1e-10;
101 b.g = -1e10;
102 Assertions.assertTrue(or.g(s) < 0, "negative");
103 a.g = -1e10;
104 b.g = -1e-10;
105 Assertions.assertTrue(or.g(s) < 0, "negative");
106 a.g = -1e10;
107 b.g = 1e-10;
108 Assertions.assertTrue(or.g(s) > 0, "positive");
109 a.g = 1e-10;
110 b.g = -1e10;
111 Assertions.assertTrue(or.g(s) > 0, "positive");
112 a.g = 1e10;
113 b.g = -1e-10;
114 Assertions.assertTrue(or.g(s) > 0, "positive");
115 a.g = -1e-10;
116 b.g = 1e10;
117 Assertions.assertTrue(or.g(s) > 0, "positive");
118 }
119
120
121
122
123 @Test
124 public void testInit() {
125
126 EventDetector a = Mockito.mock(EventDetector.class);
127 Mockito.when(a.getMaxCheckInterval()).thenReturn(AdaptableInterval.of(AbstractDetector.DEFAULT_MAX_CHECK));
128 Mockito.when(a.getThreshold()).thenReturn(AbstractDetector.DEFAULT_THRESHOLD);
129 EventDetector b = Mockito.mock(EventDetector.class);
130 Mockito.when(b.getMaxCheckInterval()).thenReturn(AdaptableInterval.of(AbstractDetector.DEFAULT_MAX_CHECK));
131 Mockito.when(b.getThreshold()).thenReturn(AbstractDetector.DEFAULT_THRESHOLD);
132 EventHandler c = Mockito.mock(EventHandler.class);
133 BooleanDetector or = BooleanDetector.orCombine(a, b).withHandler(c);
134 AbsoluteDate t = AbsoluteDate.CCSDS_EPOCH;
135 s = Mockito.mock(SpacecraftState.class);
136 Mockito.when(s.getDate()).thenReturn(t.shiftedBy(60.0));
137
138
139 or.init(s, t);
140
141
142 Assertions.assertEquals(2, or.getDetectors().size());
143 Mockito.verify(a).init(s, t);
144 Mockito.verify(b).init(s, t);
145 Mockito.verify(c).init(s, t, or);
146 }
147
148
149 @Test
150 public void testZeroDetectors() {
151
152 try {
153 BooleanDetector.orCombine(Collections.emptyList());
154 Assertions.fail("Expected Exception");
155 } catch (NoSuchElementException e) {
156
157 }
158 }
159
160
161 private static class MockDetector implements EventDetector {
162
163
164 public double g = 0;
165
166 @Override
167 public void init(SpacecraftState s0, AbsoluteDate t) {
168
169 }
170
171 @Override
172 public double g(SpacecraftState s) {
173 return this.g;
174 }
175
176 @Override
177 public EventDetectionSettings getDetectionSettings() {
178 return EventDetectionSettings.getDefaultEventDetectionSettings();
179 }
180
181 @Override
182 public EventHandler getHandler() {
183 return (state, detector, increasing) -> null;
184 }
185 }
186 }