1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT License.
3+
4+ package com .microsoft .applicationinsights .agent .internal .classicsdk ;
5+
6+ import com .azure .monitor .opentelemetry .autoconfigure .implementation .builders .ExceptionDetailBuilder ;
7+ import com .azure .monitor .opentelemetry .autoconfigure .implementation .utils .Strings ;
8+ import java .util .List ;
9+ import org .junit .jupiter .api .Test ;
10+ import static org .assertj .core .api .Assertions .assertThat ;
11+
12+ public class TelemetryUtilTest {
13+
14+ @ Test
15+ public void testStringsIsNullOrEmpty () {
16+ // Test the behavior of Strings.isNullOrEmpty with different inputs
17+
18+ // null should return true
19+ assertThat (Strings .isNullOrEmpty (null )).isTrue ();
20+
21+ // empty string should return true
22+ assertThat (Strings .isNullOrEmpty ("" )).isTrue ();
23+
24+ // string with only whitespace should return false (important!)
25+ assertThat (Strings .isNullOrEmpty (" " )).isFalse ();
26+
27+ // non-empty string should return false
28+ assertThat (Strings .isNullOrEmpty ("test" )).isFalse ();
29+ }
30+
31+ @ Test
32+ public void testExceptionMessageHandling () {
33+ // Create different types of exceptions and test the message logic
34+
35+ // Exception with null message
36+ NullPointerException nullMsgException = new NullPointerException ();
37+ assertThat (nullMsgException .getMessage ()).isNull ();
38+
39+ // Exception with empty message
40+ NullPointerException emptyMsgException = new NullPointerException ("" );
41+ assertThat (emptyMsgException .getMessage ()).isEqualTo ("" );
42+
43+ // Exception with whitespace message
44+ NullPointerException whitespaceMsgException = new NullPointerException (" " );
45+ assertThat (whitespaceMsgException .getMessage ()).isEqualTo (" " );
46+
47+ // Test our logic
48+ String [] testMessages = {null , "" , " " , "real message" };
49+ String [] expectedResults = {
50+ "java.lang.NullPointerException" ,
51+ "java.lang.NullPointerException" ,
52+ " " ,
53+ "real message"
54+ };
55+
56+ for (int i = 0 ; i < testMessages .length ; i ++) {
57+ String testMessage = testMessages [i ];
58+ String expectedResult = expectedResults [i ];
59+
60+ // Simulate TelemetryUtil logic
61+ String exceptionMessage = testMessage ;
62+ if (Strings .isNullOrEmpty (exceptionMessage )) {
63+ exceptionMessage = "java.lang.NullPointerException" ;
64+ }
65+
66+ assertThat (exceptionMessage ).isEqualTo (expectedResult );
67+ }
68+ }
69+
70+ @ Test
71+ public void testExceptionWithMessage () {
72+ // Create an exception with a message
73+ String testMessage = "Test exception message" ;
74+ NullPointerException exception = new NullPointerException (testMessage );
75+
76+ // Process the exception
77+ List <ExceptionDetailBuilder > exceptions = TelemetryUtil .getExceptions (exception );
78+
79+ // Should have one exception detail
80+ assertThat (exceptions ).hasSize (1 );
81+
82+ // Test the logic directly - the message should remain as original
83+ String exceptionMessage = exception .getMessage ();
84+ if (Strings .isNullOrEmpty (exceptionMessage )) {
85+ exceptionMessage = exception .getClass ().getName ();
86+ }
87+ assertThat (exceptionMessage ).isEqualTo (testMessage );
88+ }
89+
90+ @ Test
91+ public void testExceptionWithEmptyMessage () {
92+ // Create an exception with an empty message
93+ NullPointerException exception = new NullPointerException ("" );
94+
95+ // Process the exception
96+ List <ExceptionDetailBuilder > exceptions = TelemetryUtil .getExceptions (exception );
97+
98+ // Should have one exception detail
99+ assertThat (exceptions ).hasSize (1 );
100+
101+ // Test the logic directly - the message should be set to class name
102+ String exceptionMessage = exception .getMessage ();
103+ if (Strings .isNullOrEmpty (exceptionMessage )) {
104+ exceptionMessage = exception .getClass ().getName ();
105+ }
106+ assertThat (exceptionMessage ).isEqualTo ("java.lang.NullPointerException" );
107+ }
108+
109+ @ Test
110+ public void testExceptionWithNullMessage () {
111+ // Create an exception with null message (which is the default for most exceptions)
112+ RuntimeException exception = new RuntimeException ((String ) null );
113+
114+ // Process the exception
115+ List <ExceptionDetailBuilder > exceptions = TelemetryUtil .getExceptions (exception );
116+
117+ // Should have one exception detail
118+ assertThat (exceptions ).hasSize (1 );
119+
120+ // Test the logic directly - the message should be set to class name
121+ String exceptionMessage = exception .getMessage ();
122+ if (Strings .isNullOrEmpty (exceptionMessage )) {
123+ exceptionMessage = exception .getClass ().getName ();
124+ }
125+ assertThat (exceptionMessage ).isEqualTo ("java.lang.RuntimeException" );
126+ }
127+ }
0 commit comments