Skip to content

Commit e41946a

Browse files
Merge pull request #66 from SAP/test-custom-stacktrace
Add Test-Case for Custom Stacktraces
2 parents d92e8f8 + 10d5546 commit e41946a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

cf-java-logging-support-core/src/test/java/com/sap/hcp/cf/logging/common/converter/DefaultStacktraceConverterTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55
import static org.hamcrest.core.IsNot.not;
66
import static org.junit.Assert.assertThat;
77

8+
import java.io.BufferedReader;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.io.PrintWriter;
12+
import java.io.Reader;
13+
814
import org.junit.Assert;
15+
import org.junit.Ignore;
916
import org.junit.Test;
17+
import org.mockito.Matchers;
18+
import org.mockito.Mockito;
19+
import org.mockito.invocation.InvocationOnMock;
20+
import org.mockito.stubbing.Answer;
1021

1122
import com.sap.hcp.cf.logging.common.helper.StacktraceGenerator;
1223

@@ -128,4 +139,35 @@ private String getThreadClassName() {
128139
return stackTraceElement.subSequence(0, i) + Thread.class.getSimpleName();
129140
}
130141

142+
/**
143+
* This test case can be used for failure analysis. Paste a custom stacktrace
144+
* into src/test/resources/com/sap/hcp/cf/logging/converter/stacktrace.txt and
145+
* unignore this test. It will print the formatted stacktrace to the console.
146+
*
147+
* @throws Exception
148+
*/
149+
@Test
150+
@Ignore("Use this test for failure analysis of custom stacktraces.")
151+
public void customStacktrace() throws Exception {
152+
try (InputStream input = getClass().getResourceAsStream("stacktrace.txt");
153+
Reader reader = new InputStreamReader(input);
154+
BufferedReader stacktrace = new BufferedReader(reader)) {
155+
Throwable exception = Mockito.mock(Throwable.class);
156+
Mockito.when(exception.getMessage()).thenReturn("Test-Message");
157+
Mockito.doAnswer(new Answer<Void>() {
158+
159+
@Override
160+
public Void answer(InvocationOnMock invocation) throws Throwable {
161+
PrintWriter writer = (PrintWriter) invocation.getArguments()[0];
162+
stacktrace.lines().forEach(l -> writer.println(l));
163+
return null;
164+
}
165+
}).when(exception).printStackTrace(Matchers.any(PrintWriter.class));
166+
DefaultStacktraceConverter converter = new DefaultStacktraceConverter();
167+
StringBuilder sb = new StringBuilder();
168+
converter.convert(exception, sb);
169+
System.out.print(sb.toString());
170+
}
171+
}
172+
131173
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Insert your custom stacktrace here:
2+
3+
Lorem ipsum dolor sit amet,
4+
consectetur adipiscing elit,
5+
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
6+
7+
Ut enim ad minim veniam,
8+
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
9+
10+
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
11+
12+
Excepteur sint occaecat cupidatat non proident,
13+
sunt in culpa qui officia deserunt mollit anim id est laborum

0 commit comments

Comments
 (0)