fix(java-junit): parse StackTraceElement with custom classloader

Fixes #208
This commit is contained in:
Ats Uiboupin 2022-11-19 21:00:04 +02:00
parent 3a48f6e045
commit aebbb4d7c4
No known key found for this signature in database
GPG key ID: E191BE89059ADCF0
2 changed files with 56 additions and 8 deletions

View file

@ -27,11 +27,37 @@ describe('parseStackTraceLine tests', () => {
}) })
it('starts with whitespaces', async () => { it('starts with whitespaces', async () => {
const line = " \tat org.apache.pulsar.AddMissingPatchVersionTest.testVersionStrings(AddMissingPatchVersionTest.java:29)" const line =
' \tat org.apache.pulsar.AddMissingPatchVersionTest.testVersionStrings(AddMissingPatchVersionTest.java:29)'
expect(parseStackTraceElement(line)).toEqual({ expect(parseStackTraceElement(line)).toEqual({
tracePath: "org.apache.pulsar.AddMissingPatchVersionTest.testVersionStrings", tracePath: 'org.apache.pulsar.AddMissingPatchVersionTest.testVersionStrings',
fileName: "AddMissingPatchVersionTest.java", fileName: 'AddMissingPatchVersionTest.java',
lineStr: "29" lineStr: '29'
})
})
describe('since Java 9', () => {
it('with classloader and module', async () => {
// Based on Java 9 StackTraceElement.toString() Doc: https://docs.oracle.com/javase/9/docs/api/java/lang/StackTraceElement.html#toString--
const line = 'at com.foo.loader/foo@9.0/com.foo.Main.run(Main.java:101)'
expect(parseStackTraceElement(line)).toEqual({
classLoader: 'com.foo.loader',
moduleNameAndVersion: 'foo@9.0',
tracePath: 'com.foo.Main.run',
fileName: 'Main.java',
lineStr: '101'
})
})
it('with classloader', async () => {
const line = 'at com.foo.loader//com.foo.Main.run(Main.java:101)'
expect(parseStackTraceElement(line)).toEqual({
classLoader: 'com.foo.loader',
moduleNameAndVersion: undefined,
tracePath: 'com.foo.Main.run',
fileName: 'Main.java',
lineStr: '101'
})
}) })
}) })
}) })

View file

@ -1,18 +1,24 @@
export interface StackTraceElement { export interface StackTraceElement {
classLoader: string | undefined
moduleNameAndVersion: string | undefined
tracePath: string tracePath: string
fileName: string fileName: string
lineStr: string lineStr: string
} }
// simple format: // classloader and module name are optional:
// at <FULLY_QUALIFIED_METHOD_NAME>(<FILE_NAME>:<LINE_NUMBER>) // at <CLASSLOADER>/<MODULE_NAME_AND_VERSION>/<FULLY_QUALIFIED_METHOD_NAME>(<FILE_NAME>:<LINE_NUMBER>)
const re = /^\s*at (.*)\((.*):(\d+)\)$/ // https://github.com/eclipse-openj9/openj9/issues/11452#issuecomment-754946992
const re = /^\s*at (\S+\/\S*\/)?(.*)\((.*):(\d+)\)$/
export function parseStackTraceElement(stackTraceLine: string): StackTraceElement | undefined { export function parseStackTraceElement(stackTraceLine: string): StackTraceElement | undefined {
const match = stackTraceLine.match(re) const match = stackTraceLine.match(re)
if (match !== null) { if (match !== null) {
const [_, tracePath, fileName, lineStr] = match const [_, maybeClassLoaderAndModuleNameAndVersion, tracePath, fileName, lineStr] = match
const {classLoader, moduleNameAndVersion} = parseClassLoaderAndModule(maybeClassLoaderAndModuleNameAndVersion)
return { return {
classLoader,
moduleNameAndVersion,
tracePath, tracePath,
fileName, fileName,
lineStr lineStr
@ -20,3 +26,19 @@ export function parseStackTraceElement(stackTraceLine: string): StackTraceElemen
} }
return undefined return undefined
} }
function parseClassLoaderAndModule(maybeClassLoaderAndModuleNameAndVersion?: string): {
classLoader?: string
moduleNameAndVersion?: string
} {
if (maybeClassLoaderAndModuleNameAndVersion) {
const res = maybeClassLoaderAndModuleNameAndVersion.split('/')
const classLoader = res[0]
let moduleNameAndVersion: string | undefined = res[1]
if (moduleNameAndVersion === '') {
moduleNameAndVersion = undefined
}
return {classLoader, moduleNameAndVersion}
}
return {classLoader: undefined, moduleNameAndVersion: undefined}
}