Skip to content

Commit 5a0cd74

Browse files
committed
Test null messages
1 parent df2497c commit 5a0cd74

File tree

8 files changed

+74
-8
lines changed

8 files changed

+74
-8
lines changed

smoke-tests/apps/JavaUtilLogging/src/main/java/com/microsoft/applicationinsights/smoketestapp/JavaUtilLoggingWithExceptionServlet.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public class JavaUtilLoggingWithExceptionServlet extends HttpServlet {
1616
private static final Logger logger = Logger.getLogger("smoketestapp");
1717

1818
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
19-
logger.log(Level.SEVERE, "This is an exception!", new Exception("Fake Exception"));
19+
Exception e = testNullMessage(request) ? new Exception() : new Exception("Fake Exception");
20+
logger.log(Level.SEVERE, "This is an exception!", e);
21+
}
22+
23+
private static boolean testNullMessage(HttpServletRequest request) {
24+
String testNullMessage = request.getParameter("test-null-message");
25+
return "true".equalsIgnoreCase(testNullMessage);
2026
}
2127
}

smoke-tests/apps/JavaUtilLogging/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/JavaUtilLoggingTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ void test() throws Exception {
7878
@Test
7979
@TargetUri("/testWithException")
8080
void testWithException() throws Exception {
81+
testWithException(false);
82+
}
83+
84+
@Test
85+
@TargetUri("/testWithException?test-null-message=true")
86+
void testWithExceptionWithNullMessage() throws Exception {
87+
testWithException(true);
88+
}
89+
90+
private void testWithException(boolean testNullMessage) throws Exception {
8191
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
8292

8393
Envelope rdEnvelope = rdList.get(0);
@@ -97,7 +107,11 @@ void testWithException() throws Exception {
97107
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData();
98108

99109
assertThat(ed.getExceptions().get(0).getTypeName()).isEqualTo("java.lang.Exception");
100-
assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("Fake Exception");
110+
if (testNullMessage) {
111+
assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("java.lang.Exception");
112+
} else {
113+
assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("Fake Exception");
114+
}
101115
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
102116
assertThat(ed.getProperties()).containsEntry("Logger Message", "This is an exception!");
103117
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");

smoke-tests/apps/Log4j1/src/main/java/com/microsoft/applicationinsights/smoketestapp/Log4j1WithExceptionServlet.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ public class Log4j1WithExceptionServlet extends HttpServlet {
3535
@Override
3636
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
3737
MDC.put("MDC key", "MDC value");
38-
logger.error("This is an exception!", new Exception("Fake Exception"));
38+
Exception e = testNullMessage(request) ? new Exception() : new Exception("Fake Exception");
39+
logger.error("This is an exception!", e);
3940
MDC.remove("MDC key");
4041
}
42+
43+
private static boolean testNullMessage(HttpServletRequest request) {
44+
String testNullMessage = request.getParameter("test-null-message");
45+
return "true".equalsIgnoreCase(testNullMessage);
46+
}
4147
}

smoke-tests/apps/Log4j1/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/Log4j1Test.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ private void logDisableMessage() {
9999
@Test
100100
@TargetUri("/testWithException")
101101
void testWithException() throws Exception {
102+
testWithException(false);
103+
}
104+
105+
@Test
106+
@TargetUri("/testWithException?test-null-message=true")
107+
void testWithExceptionWithNullMessage() throws Exception {
108+
testWithException(true);
109+
}
110+
111+
private void testWithException(boolean testNullMessage) throws Exception {
102112
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
103113

104114
Envelope rdEnvelope = rdList.get(0);
@@ -119,7 +129,11 @@ void testWithException() throws Exception {
119129
ExceptionDetails ex = details.get(0);
120130

121131
assertThat(ex.getTypeName()).isEqualTo("java.lang.Exception");
122-
assertThat(ex.getMessage()).isEqualTo("Fake Exception");
132+
if (testNullMessage) {
133+
assertThat(ex.getMessage()).isEqualTo("java.lang.Exception");
134+
} else {
135+
assertThat(ex.getMessage()).isEqualTo("Fake Exception");
136+
}
123137
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
124138
assertThat(ed.getProperties()).containsEntry("Logger Message", "This is an exception!");
125139
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");

smoke-tests/apps/Log4j2/src/main/java/com/microsoft/applicationinsights/smoketestapp/Log4j2WithExceptionServlet.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ public class Log4j2WithExceptionServlet extends HttpServlet {
1818

1919
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
2020
ThreadContext.put("MDC key", "MDC value");
21-
logger.error("This is an exception!", new Exception("Fake Exception"));
21+
Exception e = testNullMessage(request) ? new Exception() : new Exception("Fake Exception");
22+
logger.error("This is an exception!", e);
2223
ThreadContext.remove("MDC key");
2324
}
25+
26+
private static boolean testNullMessage(HttpServletRequest request) {
27+
String testNullMessage = request.getParameter("test-null-message");
28+
return "true".equalsIgnoreCase(testNullMessage);
29+
}
2430
}

smoke-tests/apps/Log4j2/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/Log4j2Test.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ void test() throws Exception {
110110
@Test
111111
@TargetUri("/testWithException")
112112
void testWithException() throws Exception {
113+
testWithException(false);
114+
}
115+
116+
@Test
117+
@TargetUri("/testWithException?test-null-message=true")
118+
void testWithExceptionWithNullMessage() throws Exception {
119+
testWithException(true);
120+
}
121+
122+
private void testWithException(boolean testNullMessage) throws Exception {
113123
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
114124

115125
Envelope rdEnvelope = rdList.get(0);
@@ -130,7 +140,11 @@ void testWithException() throws Exception {
130140
ExceptionDetails ex = details.get(0);
131141

132142
assertThat(ex.getTypeName()).isEqualTo("java.lang.Exception");
133-
assertThat(ex.getMessage()).isEqualTo("Fake Exception");
143+
if (testNullMessage) {
144+
assertThat(ex.getMessage()).isEqualTo("java.lang.Exception");
145+
} else {
146+
assertThat(ex.getMessage()).isEqualTo("Fake Exception");
147+
}
134148
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
135149
assertThat(ed.getProperties()).containsEntry("Logger Message", "This is an exception!");
136150
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");

smoke-tests/apps/Logback/src/main/java/com/microsoft/applicationinsights/smoketestapp/LogbackWithExceptionServlet.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ public class LogbackWithExceptionServlet extends HttpServlet {
1818

1919
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
2020
MDC.put("MDC key", "MDC value");
21-
logger.error("This is an exception!", new Exception("Fake Exception"));
21+
Exception e = testNullMessage(request) ? new Exception() : new Exception("Fake Exception");
22+
logger.error("This is an exception!", e);
2223
MDC.remove("MDC key");
2324
}
25+
26+
private static boolean testNullMessage(HttpServletRequest request) {
27+
String testNullMessage = request.getParameter("test-null-message");
28+
return "true".equalsIgnoreCase(testNullMessage);
29+
}
2430
}

smoke-tests/apps/Logback/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LogbackTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void testWithException() {
138138
"ClassName",
139139
"com.microsoft.applicationinsights.smoketestapp.LogbackWithExceptionServlet")
140140
.hasProperty("MethodName", "doGet")
141-
.hasProperty("LineNumber", "21")
141+
.hasProperty("LineNumber", "22")
142142
.hasPropertiesSize(9)));
143143
} else {
144144
testing.waitAndAssertTrace(

0 commit comments

Comments
 (0)