|
15 | 15 | package org.apache.geode.cache.query.internal; |
16 | 16 |
|
17 | 17 | import static org.apache.geode.cache.Region.SEPARATOR; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
18 | 19 | import static org.junit.Assert.assertEquals; |
19 | 20 | import static org.junit.Assert.assertFalse; |
20 | 21 | import static org.junit.Assert.assertTrue; |
@@ -60,10 +61,12 @@ public class QueryTraceJUnitTest { |
60 | 61 | @Before |
61 | 62 | public void setUp() throws Exception { |
62 | 63 | CacheUtils.startCache(); |
| 64 | + DefaultQuery.testHook = new BeforeQueryExecutionHook(); |
63 | 65 | } |
64 | 66 |
|
65 | 67 | @After |
66 | 68 | public void tearDown() throws Exception { |
| 69 | + DefaultQuery.testHook = null; |
67 | 70 | CacheUtils.closeCache(); |
68 | 71 | } |
69 | 72 |
|
@@ -104,7 +107,11 @@ public void testTraceOnPartitionedRegionWithTracePrefix() throws Exception { |
104 | 107 | assertTrue(((DefaultQuery) query).isTraced()); |
105 | 108 |
|
106 | 109 | SelectResults results = (SelectResults) query.execute(); |
107 | | - assertTrue(QueryObserverHolder.getInstance() instanceof IndexTrackingQueryObserver); |
| 110 | + |
| 111 | + // The IndexTrackingObserver should have been set |
| 112 | + BeforeQueryExecutionHook hook = (BeforeQueryExecutionHook) DefaultQuery.testHook; |
| 113 | + assertThat(hook.getObserver()).isInstanceOf(IndexTrackingQueryObserver.class); |
| 114 | + |
108 | 115 | // The query should return all elements in region. |
109 | 116 | assertEquals(region.size(), results.size()); |
110 | 117 | QueryObserverHolder.reset(); |
@@ -141,7 +148,11 @@ public void testTraceOnLocalRegionWithTracePrefix() throws Exception { |
141 | 148 | assertTrue(((DefaultQuery) query).isTraced()); |
142 | 149 |
|
143 | 150 | SelectResults results = (SelectResults) query.execute(); |
144 | | - assertTrue(QueryObserverHolder.getInstance() instanceof IndexTrackingQueryObserver); |
| 151 | + |
| 152 | + // The IndexTrackingObserver should have been set |
| 153 | + BeforeQueryExecutionHook hook = (BeforeQueryExecutionHook) DefaultQuery.testHook; |
| 154 | + assertThat(hook.getObserver()).isInstanceOf(IndexTrackingQueryObserver.class); |
| 155 | + |
145 | 156 | // The query should return all elements in region. |
146 | 157 | assertEquals(region.size(), results.size()); |
147 | 158 | QueryObserverHolder.reset(); |
@@ -183,7 +194,11 @@ public void testNegTraceOnPartitionedRegionWithTracePrefix() throws Exception { |
183 | 194 | assertFalse(((DefaultQuery) query).isTraced()); |
184 | 195 |
|
185 | 196 | SelectResults results = (SelectResults) query.execute(); |
186 | | - assertFalse(QueryObserverHolder.getInstance() instanceof IndexTrackingQueryObserver); |
| 197 | + |
| 198 | + // The IndexTrackingObserver should not have been set |
| 199 | + BeforeQueryExecutionHook hook = (BeforeQueryExecutionHook) DefaultQuery.testHook; |
| 200 | + assertThat(hook.getObserver()).isNotInstanceOf(IndexTrackingQueryObserver.class); |
| 201 | + |
187 | 202 | // The query should return all elements in region. |
188 | 203 | assertEquals(region.size(), results.size()); |
189 | 204 | QueryObserverHolder.reset(); |
@@ -223,7 +238,11 @@ public void testNegTraceOnLocalRegionWithTracePrefix() throws Exception { |
223 | 238 | assertFalse(((DefaultQuery) query).isTraced()); |
224 | 239 |
|
225 | 240 | SelectResults results = (SelectResults) query.execute(); |
226 | | - assertFalse(QueryObserverHolder.getInstance() instanceof IndexTrackingQueryObserver); |
| 241 | + |
| 242 | + // The IndexTrackingObserver should not have been set |
| 243 | + BeforeQueryExecutionHook hook = (BeforeQueryExecutionHook) DefaultQuery.testHook; |
| 244 | + assertThat(hook.getObserver()).isNotInstanceOf(IndexTrackingQueryObserver.class); |
| 245 | + |
227 | 246 | // The query should return all elements in region. |
228 | 247 | assertEquals(region.size(), results.size()); |
229 | 248 | QueryObserverHolder.reset(); |
@@ -262,7 +281,11 @@ public void testTraceOnPartitionedRegionWithTracePrefixNoComments() throws Excep |
262 | 281 | assertTrue(((DefaultQuery) query).isTraced()); |
263 | 282 |
|
264 | 283 | SelectResults results = (SelectResults) query.execute(); |
265 | | - assertTrue(QueryObserverHolder.getInstance() instanceof IndexTrackingQueryObserver); |
| 284 | + |
| 285 | + // The IndexTrackingObserver should have been set |
| 286 | + BeforeQueryExecutionHook hook = (BeforeQueryExecutionHook) DefaultQuery.testHook; |
| 287 | + assertThat(hook.getObserver()).isInstanceOf(IndexTrackingQueryObserver.class); |
| 288 | + |
266 | 289 | // The query should return all elements in region. |
267 | 290 | assertEquals(region.size(), results.size()); |
268 | 291 | QueryObserverHolder.reset(); |
@@ -296,8 +319,11 @@ public void testTraceOnLocalRegionWithTracePrefixNoComments() throws Exception { |
296 | 319 | assertTrue(((DefaultQuery) query).isTraced()); |
297 | 320 |
|
298 | 321 | SelectResults results = (SelectResults) query.execute(); |
299 | | - assertTrue(QueryObserverHolder.getInstance() instanceof IndexTrackingQueryObserver); |
300 | | - // The query should return all elements in region. |
| 322 | + |
| 323 | + // The IndexTrackingObserver should have been set |
| 324 | + BeforeQueryExecutionHook hook = (BeforeQueryExecutionHook) DefaultQuery.testHook; |
| 325 | + assertThat(hook.getObserver()).isInstanceOf(IndexTrackingQueryObserver.class); |
| 326 | + |
301 | 327 | assertEquals(region.size(), results.size()); |
302 | 328 | QueryObserverHolder.reset(); |
303 | 329 | } |
@@ -331,7 +357,11 @@ public void testTraceOnPartitionedRegionWithSmallTracePrefixNoComments() throws |
331 | 357 | assertTrue(((DefaultQuery) query).isTraced()); |
332 | 358 |
|
333 | 359 | SelectResults results = (SelectResults) query.execute(); |
334 | | - assertTrue(QueryObserverHolder.getInstance() instanceof IndexTrackingQueryObserver); |
| 360 | + |
| 361 | + // The IndexTrackingObserver should have been set |
| 362 | + BeforeQueryExecutionHook hook = (BeforeQueryExecutionHook) DefaultQuery.testHook; |
| 363 | + assertThat(hook.getObserver()).isInstanceOf(IndexTrackingQueryObserver.class); |
| 364 | + |
335 | 365 | // The query should return all elements in region. |
336 | 366 | assertEquals(region.size(), results.size()); |
337 | 367 | QueryObserverHolder.reset(); |
@@ -366,7 +396,11 @@ public void testTraceOnLocalRegionWithSmallTracePrefixNoComments() throws Except |
366 | 396 | assertTrue(((DefaultQuery) query).isTraced()); |
367 | 397 |
|
368 | 398 | SelectResults results = (SelectResults) query.execute(); |
369 | | - assertTrue(QueryObserverHolder.getInstance() instanceof IndexTrackingQueryObserver); |
| 399 | + |
| 400 | + // The IndexTrackingObserver should have been set |
| 401 | + BeforeQueryExecutionHook hook = (BeforeQueryExecutionHook) DefaultQuery.testHook; |
| 402 | + assertThat(hook.getObserver()).isInstanceOf(IndexTrackingQueryObserver.class); |
| 403 | + |
370 | 404 | // The query should return all elements in region. |
371 | 405 | assertEquals(region.size(), results.size()); |
372 | 406 | QueryObserverHolder.reset(); |
@@ -438,4 +472,21 @@ public void testQueryFailLocalRegionWithSmallTracePrefixNoSpace() throws Excepti |
438 | 472 | } |
439 | 473 | } |
440 | 474 |
|
| 475 | + private class BeforeQueryExecutionHook implements DefaultQuery.TestHook { |
| 476 | + private QueryObserver observer = null; |
| 477 | + |
| 478 | + @Override |
| 479 | + public void doTestHook(final SPOTS spot, final DefaultQuery _ignored, |
| 480 | + final ExecutionContext executionContext) { |
| 481 | + switch (spot) { |
| 482 | + case BEFORE_QUERY_EXECUTION: |
| 483 | + observer = QueryObserverHolder.getInstance(); |
| 484 | + break; |
| 485 | + } |
| 486 | + } |
| 487 | + |
| 488 | + public QueryObserver getObserver() { |
| 489 | + return observer; |
| 490 | + } |
| 491 | + } |
441 | 492 | } |
0 commit comments