|
17 | 17 | import static org.apache.geode.cache.Region.SEPARATOR; |
18 | 18 | import static org.junit.Assert.assertEquals; |
19 | 19 | import static org.junit.Assert.assertTrue; |
| 20 | +import static org.junit.Assert.fail; |
20 | 21 |
|
| 22 | +import java.util.ArrayList; |
21 | 23 | import java.util.Collection; |
22 | 24 |
|
23 | 25 | import org.junit.After; |
@@ -163,4 +165,58 @@ public void testIndexInfoOnLocalRegion() throws Exception { |
163 | 165 | assertEquals(results.size(), ((Integer) rslts).intValue()); |
164 | 166 | } |
165 | 167 |
|
| 168 | + /** |
| 169 | + * Test for GEODE-10526: afterIndexLookup should handle null indexMap gracefully |
| 170 | + * |
| 171 | + * This test verifies that afterIndexLookup does not throw NullPointerException |
| 172 | + * when the ThreadLocal indexMap has not been initialized. This can occur in |
| 173 | + * partitioned region queries when afterIndexLookup is called without a |
| 174 | + * corresponding beforeIndexLookup call, or when beforeIndexLookup fails |
| 175 | + * before initializing the ThreadLocal. |
| 176 | + */ |
| 177 | + @Test |
| 178 | + public void testAfterIndexLookupWithUninitializedThreadLocal() { |
| 179 | + // Create a new IndexTrackingQueryObserver without initializing its ThreadLocal |
| 180 | + IndexTrackingQueryObserver observer = new IndexTrackingQueryObserver(); |
| 181 | + |
| 182 | + // Create a mock result collection |
| 183 | + Collection<Object> results = new ArrayList<>(); |
| 184 | + results.add(new Object()); |
| 185 | + |
| 186 | + try { |
| 187 | + // Call afterIndexLookup without calling beforeIndexLookup first |
| 188 | + // This simulates the scenario where the ThreadLocal is not initialized |
| 189 | + // Before the fix, this would throw NullPointerException at line 110 |
| 190 | + observer.afterIndexLookup(results); |
| 191 | + |
| 192 | + // If we reach here, the fix is working correctly |
| 193 | + // The method should return gracefully when indexMap is null |
| 194 | + } catch (NullPointerException e) { |
| 195 | + fail("GEODE-10526: afterIndexLookup should not throw NullPointerException when " |
| 196 | + + "ThreadLocal is uninitialized. This indicates the null check is missing. " |
| 197 | + + "Exception: " + e.getMessage()); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Test for GEODE-10526: afterIndexLookup should handle null results parameter |
| 203 | + * |
| 204 | + * Verify that the existing null check for results parameter still works. |
| 205 | + */ |
| 206 | + @Test |
| 207 | + public void testAfterIndexLookupWithNullResults() { |
| 208 | + IndexTrackingQueryObserver observer = new IndexTrackingQueryObserver(); |
| 209 | + |
| 210 | + try { |
| 211 | + // Call afterIndexLookup with null results |
| 212 | + // This should return early without any exceptions |
| 213 | + observer.afterIndexLookup(null); |
| 214 | + |
| 215 | + // Success - method handled null results correctly |
| 216 | + } catch (Exception e) { |
| 217 | + fail("afterIndexLookup should handle null results parameter gracefully. " |
| 218 | + + "Exception: " + e.getMessage()); |
| 219 | + } |
| 220 | + } |
| 221 | + |
166 | 222 | } |
0 commit comments