diff --git a/TestResultSummaryService/parsers/Parser.js b/TestResultSummaryService/parsers/Parser.js index ca8d6e56..180b85aa 100644 --- a/TestResultSummaryService/parsers/Parser.js +++ b/TestResultSummaryService/parsers/Parser.js @@ -10,24 +10,43 @@ class Parser { } exactJavaVersion(output) { - const javaVersionRegex = - /=JAVA VERSION OUTPUT BEGIN=[\r\n]+([\s\S]*?)[\r\n]+.*=JAVA VERSION OUTPUT END=/; - const javaBuildDateRegex = - /\s([0-9]{4})-?(0[1-9]|1[012])-?(0[1-9]|[12][0-9]|3[01])/; + const javaVersionRegex = /=JAVA VERSION OUTPUT BEGIN=[\r\n]+([\s\S]*?)[\r\n]+.*=JAVA VERSION OUTPUT END=/; const sdkResourceRegex = /.*?SDK_RESOURCE\=(.*)[\r\n]+/; let curRegexResult = null; let javaVersion, jdkDate, sdkResource; + if ((curRegexResult = javaVersionRegex.exec(output)) !== null) { javaVersion = removeTimestamp(curRegexResult[1]); + } else { + return null; // Return null if markers are missing } + curRegexResult = null; + // parse jdk date from javaVersion if ((curRegexResult = sdkResourceRegex.exec(output)) != null) { sdkResource = curRegexResult[1]; } curRegexResult = null; - // parse jdk date from javaVersion - if ((curRegexResult = javaBuildDateRegex.exec(javaVersion)) !== null) { - jdkDate = curRegexResult[0]; + + // Refine jdkDate extraction to match specific lines for + const openj9BuildDateRegex = + /Eclipse OpenJ9 VM \([^)]+ (\d{4})(\d{2})(\d{2})/; // e.g., 20240627 + const hotspotBuildDateRegex = + /OpenJDK Runtime Environment [^\r\n]*-([0-9]{8})/; // e.g., 20240626 + const java8BuildDateRegex = + /OpenJDK Runtime Environment.*\(build [^\d]*(\d{4})(\d{2})(\d{2})/; // e.g., 1.8.0_412-b08 + const javaBuildDateRegex = + /\s([0-9]{4})-?(0[1-9]|1[012])-?(0[1-9]|[12][0-9]|3[01])/; + + // Attempt to extract jdkDate using specific regexes first + if ((curRegexResult = hotspotBuildDateRegex.exec(output)) !== null) { + jdkDate = `${curRegexResult[1].slice(0, 4)}-${curRegexResult[1].slice(4, 6)}-${curRegexResult[1].slice(6, 8)}`; + } else if ((curRegexResult = openj9BuildDateRegex.exec(output)) !== null) { + jdkDate = `${curRegexResult[1]}-${curRegexResult[2]}-${curRegexResult[3]}`; + } else if ((curRegexResult = java8BuildDateRegex.exec(output)) !== null) { + jdkDate = `${curRegexResult[1]}-${curRegexResult[2]}-${curRegexResult[3]}`; + } else { + return null; // Return null if no jdkDate is found } return { javaVersion, jdkDate, sdkResource }; } diff --git a/TestResultSummaryService/parsers/Parser.test.js b/TestResultSummaryService/parsers/Parser.test.js new file mode 100644 index 00000000..65439b67 --- /dev/null +++ b/TestResultSummaryService/parsers/Parser.test.js @@ -0,0 +1,125 @@ +const Parser = require('./Parser'); // Adjust the path if necessary + +describe('Parser', () => { + let parser; + + beforeEach(() => { + parser = new Parser('TestBuild'); + }); + + test('should extract Java version and build date for HotSpot implementation', () => { + const hotspotOutput = ` +16:38:54 =JAVA VERSION OUTPUT BEGIN= +16:38:54 openjdk version "21.0.4-beta" 2024-07-16 +16:38:54 OpenJDK Runtime Environment Temurin-21.0.4+6-202406261902 (build 21.0.4-beta+6-ea) +16:38:54 OpenJDK 64-Bit Server VM Temurin-21.0.4+6-202406261902 (build 21.0.4-beta+6-ea, mixed mode, sharing) +16:38:54 =JAVA VERSION OUTPUT END= + `; + + const result = parser.exactJavaVersion(hotspotOutput); + expect(result.jdkDate).toBe('2024-06-26'); + }); + + test('should extract Java version and build date for OpenJ9 implementation', () => { + const openj9Output = ` +11:53:15 =JAVA VERSION OUTPUT BEGIN= +11:53:19 openjdk version "11.0.24-internal" 2024-07-16 +11:53:19 OpenJDK Runtime Environment (build 11.0.24-internal+0-adhoc.jenkins.BuildJDK11aarch64macPersonal) +11:53:19 Eclipse OpenJ9 VM (build master-2a2df9f1117, JRE 11 Mac OS X aarch64-64-Bit 20240627_514 (JIT enabled, AOT enabled) +11:53:19 OpenJ9 - 2a2df9f1117 +11:53:19 OMR - 47a9d248db0 +11:53:19 JCL - c535515f053 based on jdk-11.0.24+6) +11:53:19 =JAVA VERSION OUTPUT END= + `; + + const result = parser.exactJavaVersion(openj9Output); + expect(result.jdkDate).toBe('2024-06-27'); + }); + + test('should extract Java version and build date for Java 8 implementation', () => { + const java8Output = ` +16:38:54 =JAVA VERSION OUTPUT BEGIN= +16:38:54 openjdk version "1.8.0_412" +16:38:54 OpenJDK Runtime Environment (Temurin)(build 1.8.0_412-b08) +16:38:54 OpenJDK 64-Bit Server VM (Temurin)(build 25.412-b08, mixed mode) +16:38:54 =JAVA VERSION OUTPUT END= + `; + + const result = parser.exactJavaVersion(java8Output); + expect(result).toBeNull(); + }); + + test('should extract Java version and build date for Java 8u152-b01 implementation', () => { + const java8u152Output = ` +16:38:54 =JAVA VERSION OUTPUT BEGIN= +16:38:54 openjdk version "1.8.0_152" +16:38:54 OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_152-b01) +16:38:54 OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.152-b01, mixed mode) +16:38:54 =JAVA VERSION OUTPUT END= + `; + + const result = parser.exactJavaVersion(java8u152Output); + expect(result).toBeNull(); + }); + + test('should extract Java version and build date for Java 9.0.4+11 implementation', () => { + const java904Output = ` +16:38:54 =JAVA VERSION OUTPUT BEGIN= +16:38:54 openjdk version "9.0.4" +16:38:54 OpenJDK Runtime Environment (build 9.0.4+11) +16:38:54 OpenJDK 64-Bit Server VM (build 9.0.4+11, mixed mode) +16:38:54 =JAVA VERSION OUTPUT END= + `; + + const result = parser.exactJavaVersion(java904Output); + expect(result).toBeNull(); + }); + + test('should extract Java version and build date for Java 10.0.2+13.1 implementation', () => { + const java1002Output = ` +16:38:54 =JAVA VERSION OUTPUT BEGIN= +16:38:54 openjdk version "10.0.2" +16:38:54 OpenJDK Runtime Environment (build 10.0.2+13.1) +16:38:54 OpenJDK 64-Bit Server VM (build 10.0.2+13.1, mixed mode) +16:38:54 =JAVA VERSION OUTPUT END= + `; + + const result = parser.exactJavaVersion(java1002Output); + expect(result).toBeNull(); + }); + + test('should extract Java version and build date for Java 11.0.4+11.4 implementation', () => { + const java1104Output = ` +16:38:54 =JAVA VERSION OUTPUT BEGIN= +16:38:54 openjdk version "11.0.4" +16:38:54 OpenJDK Runtime Environment (build 11.0.4+11.4) +16:38:54 OpenJDK 64-Bit Server VM (build 11.0.4+11.4, mixed mode) +16:38:54 =JAVA VERSION OUTPUT END= + `; + + const result = parser.exactJavaVersion(java1104Output); + expect(result).toBeNull(); + }); + + test('should extract Java version and build date for Java 13+33_openj9-0.16.0 implementation', () => { + const java1333Output = ` +16:38:54 =JAVA VERSION OUTPUT BEGIN= +16:38:54 openjdk version "13" +16:38:54 OpenJDK Runtime Environment (build 13+33) +16:38:54 Eclipse OpenJ9 VM (build 13+33_openj9-0.16.0, JRE 13 Mac OS X aarch64-64-Bit) +16:38:54 =JAVA VERSION OUTPUT END= + `; + + const result = parser.exactJavaVersion(java1333Output); + expect(result).toBeNull(); + }); + + test('should return null if no Java version regex match', () => { + const invalidOutput = ` +16:38:54 Some invalid output that does not contain Java version information + `; + + const result = parser.exactJavaVersion(invalidOutput); + expect(result).toBeNull(); + }); +});