-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix NPE in Spring when handling exceptions during LOG_ERROR strategy (#…
…93) Adds a null check prior to adding the exception to the segment.
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...k-spring/src/test/java/com/amazonaws/xray/spring/aop/BaseAbstractXRayInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.amazonaws.xray.spring.aop; | ||
|
||
import com.amazonaws.xray.AWSXRay; | ||
import com.amazonaws.xray.AWSXRayRecorder; | ||
import com.amazonaws.xray.entities.Segment; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.Signature; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class BaseAbstractXRayInterceptorTest { | ||
|
||
class ImplementedXRayInterceptor extends BaseAbstractXRayInterceptor { | ||
@Override | ||
protected void xrayEnabledClasses() {} | ||
} | ||
|
||
private BaseAbstractXRayInterceptor xRayInterceptor = new ImplementedXRayInterceptor(); | ||
|
||
@Mock | ||
private AWSXRayRecorder mockRecorder; | ||
|
||
@Mock | ||
private ProceedingJoinPoint mockPjp; | ||
|
||
@Mock | ||
private Signature mockSignature; | ||
|
||
@Before | ||
public void setup() { | ||
AWSXRay.setGlobalRecorder(mockRecorder); | ||
|
||
when(mockPjp.getArgs()).thenReturn(new Object[]{}); | ||
when(mockPjp.getSignature()).thenReturn(mockSignature); | ||
when(mockSignature.getName()).thenReturn("testSpringName"); | ||
} | ||
|
||
@Test | ||
public void testNullSegmentOnProcessFailure() throws Throwable { | ||
// Test to ensure that getCurrentSegment()/getCurrentSegmentOptional() don't throw NPEs when customers are using | ||
// the Log context missing strategy during the processXRayTrace call. | ||
Exception expectedException = new RuntimeException("An intended exception"); | ||
// Cause an intended exception to be thrown so that getCurrentSegmentOptional() is called. | ||
when(mockRecorder.beginSubsegment(any())).thenThrow(expectedException); | ||
|
||
when(mockRecorder.getCurrentSegmentOptional()).thenReturn(Optional.empty()); | ||
when(mockRecorder.getCurrentSegment()).thenReturn(null); | ||
|
||
try { | ||
xRayInterceptor.processXRayTrace(mockPjp); | ||
} catch (Exception e){ | ||
// A null pointer exception here could potentially indicate that a call to getCurrentSegment() is returning null. | ||
// i.e. there is another exception other than our intended exception that is thrown that's not supposed to be thrown. | ||
assertNotEquals(NullPointerException.class, e.getClass()); | ||
assertEquals(expectedException.getMessage(), e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSegmentOnProcessFailure() throws Throwable { | ||
// Test to ensure that the exception is populated to the segment when an error occurs during the | ||
// processXRayTrace call. | ||
Exception expectedException = new RuntimeException("An intended exception"); | ||
// Cause an intended exception to be thrown so that getCurrentSegmentOptional() is called. | ||
when(mockRecorder.beginSubsegment(any())).thenThrow(expectedException); | ||
|
||
Segment mockSegment = mock(Segment.class); | ||
when(mockRecorder.getCurrentSegmentOptional()).thenReturn(Optional.of(mockSegment)); | ||
when(mockRecorder.getCurrentSegment()).thenReturn(mockSegment); | ||
|
||
try { | ||
xRayInterceptor.processXRayTrace(mockPjp); | ||
} catch (Exception e){ | ||
verify(mockSegment).addException(expectedException); | ||
} | ||
} | ||
} |