|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Broadcom. |
| 3 | + * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 4 | + * |
| 5 | + * This program and the accompanying materials are made |
| 6 | + * available under the terms of the Eclipse Public License 2.0 |
| 7 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Broadcom, Inc. - initial API and implementation |
| 13 | + * |
| 14 | + */ |
| 15 | +package org.eclipse.lsp.cobol.usecases.suite; |
| 16 | + |
| 17 | +import lombok.SneakyThrows; |
| 18 | +import lombok.extern.slf4j.Slf4j; |
| 19 | +import org.eclipse.lsp.cobol.test.CobolText; |
| 20 | +import org.eclipse.lsp.cobol.test.engine.PreprocessedDocument; |
| 21 | +import org.eclipse.lsp.cobol.test.engine.TestData; |
| 22 | +import org.eclipse.lsp4j.DiagnosticSeverity; |
| 23 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 24 | +import org.junit.jupiter.api.extension.TestWatcher; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.util.*; |
| 30 | + |
| 31 | +import static org.eclipse.lsp.cobol.usecases.suite.TestWatcherHelper.ROOT_DIRECTORY_PROPERTY; |
| 32 | + |
| 33 | +/** |
| 34 | + * This is unit test watcher class and is responsible to write test data if "usecase.test.repo.dir" |
| 35 | + * system property is configured |
| 36 | + */ |
| 37 | +@Slf4j |
| 38 | +public class UniqueIdTestWatcher implements TestWatcher { |
| 39 | + public static final String SOURCE_CBL = "source.cbl"; |
| 40 | + public static final String COPYBOOK_EXT = ".cpy"; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void testSuccessful(ExtensionContext context) { |
| 44 | + interceptor(context); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void testFailed(ExtensionContext context, Throwable cause) { |
| 49 | + LOG.info("{} test failed", context.getRequiredTestMethod().toString()); |
| 50 | + interceptor(context); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void testAborted(ExtensionContext context, Throwable cause) { |
| 55 | + LOG.info("{} test is aborted", context.getRequiredTestMethod().toString()); |
| 56 | + interceptor(context); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void testDisabled(ExtensionContext context, Optional<String> reason) { |
| 61 | + LOG.info( |
| 62 | + "{} test is disabled due to {}", |
| 63 | + context.getRequiredTestMethod().toString(), |
| 64 | + reason.orElse("unknown reason")); |
| 65 | + } |
| 66 | + |
| 67 | + @SneakyThrows |
| 68 | + private static void interceptor(ExtensionContext context) { |
| 69 | + Optional<String> rootDirectory = |
| 70 | + Optional.ofNullable(System.getProperty(ROOT_DIRECTORY_PROPERTY)); |
| 71 | + if (rootDirectory.isPresent()) { |
| 72 | + String rootFolder = rootDirectory.get(); |
| 73 | + PreprocessedDocument document = getDocumentFromContext(context); |
| 74 | + |
| 75 | + Path mainFolderPath = TestWatcherHelper.fetchTargetPath(context, rootFolder); |
| 76 | + writeCobolDocument(document, mainFolderPath); |
| 77 | + writeCopybooks(document.getCopybooks(), mainFolderPath); |
| 78 | + if (Objects.nonNull(document.getTestData())) { |
| 79 | + writeTestData(document.getTestData(), mainFolderPath); |
| 80 | + createZeroDiagnosticIndicatorFile(document.getTestData(), mainFolderPath); |
| 81 | + } else { |
| 82 | + LOG.info("no test data for {}", context.getRequiredTestMethod().toString()); |
| 83 | + Files.createFile(mainFolderPath.resolve("NoTestData")); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static PreprocessedDocument getDocumentFromContext(ExtensionContext context) { |
| 89 | + return (PreprocessedDocument) |
| 90 | + context |
| 91 | + .getStore(ExtensionContext.Namespace.create(context.getRequiredTestMethod())) |
| 92 | + .get("document"); |
| 93 | + } |
| 94 | + |
| 95 | + private static void writeCobolDocument(PreprocessedDocument document, Path mainFolderPath) |
| 96 | + throws IOException { |
| 97 | + Path cobolDocPath = mainFolderPath.resolve(SOURCE_CBL); |
| 98 | + writeToFile(cobolDocPath, document.getText()); |
| 99 | + } |
| 100 | + |
| 101 | + private static void writeCopybooks(List<CobolText> copybooks, Path mainFolderPath) { |
| 102 | + copybooks.forEach( |
| 103 | + cobolText -> { |
| 104 | + try { |
| 105 | + Path filePath = mainFolderPath.resolve(cobolText.getFileName() + COPYBOOK_EXT); |
| 106 | + writeToFile(filePath, cobolText.getFullText()); |
| 107 | + System.out.println("Created file: " + filePath); |
| 108 | + } catch (IOException e) { |
| 109 | + System.err.println("Error writing copybook: " + e.getMessage()); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + private static void writeTestData(TestData testData, Path mainFolderPath) throws IOException { |
| 115 | + Path testDataPath = mainFolderPath.resolve("testData.txt"); |
| 116 | + Files.deleteIfExists(testDataPath); |
| 117 | + writeToFile(testDataPath, testData.toString()); |
| 118 | + } |
| 119 | + |
| 120 | + private static void writeToFile(Path filePath, String content) throws IOException { |
| 121 | + Files.write(filePath, content.getBytes()); |
| 122 | + } |
| 123 | + |
| 124 | + private static void createZeroDiagnosticIndicatorFile(TestData testData, Path mainFolderPath) |
| 125 | + throws IOException { |
| 126 | + boolean hasErrorDiagnostics = |
| 127 | + testData.getDiagnostics().values().stream() |
| 128 | + .flatMap(List::stream) |
| 129 | + .anyMatch(d -> d.getSeverity() == DiagnosticSeverity.Error); |
| 130 | + |
| 131 | + Path diaFlag = mainFolderPath.resolve("ZeroDiagnosticsFlag"); |
| 132 | + if (!hasErrorDiagnostics) { |
| 133 | + if (!Files.exists(diaFlag)) Files.createFile(diaFlag); |
| 134 | + } else { |
| 135 | + Files.deleteIfExists(diaFlag); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments