Skip to content

Commit 66b7a46

Browse files
committed
test: add usecase test interceptor
Signed-off-by: ap891843 <[email protected]>
1 parent dc3bab5 commit 66b7a46

File tree

12 files changed

+328
-4
lines changed

12 files changed

+328
-4
lines changed

clients/cobol-lsp-vscode-extension/dist/note.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
-->
1515

1616
# Dialect support information
17-
Place bundled extension code into this folder
17+
Place bundled extension code into this folder

server/engine/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@
291291
<!--suppress UnresolvedMavenProperty -->
292292
<value>${listingSnap}</value>
293293
</property>
294+
<property>
295+
<name>usecase.test.repo.dir</name>
296+
<value>${usecase.test.repo.dir}</value>
297+
</property>
294298
</systemPropertyVariables>
295299
<excludes>
296300
<exclude>**/org/eclipse/lsp/cobol/dialects/**/*</exclude>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.experimental.UtilityClass;
18+
import org.junit.jupiter.api.extension.ExtensionContext;
19+
20+
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.nio.file.Paths;
24+
import java.util.ArrayList;
25+
import java.util.Arrays;
26+
import java.util.List;
27+
import java.util.Optional;
28+
import java.util.regex.Matcher;
29+
import java.util.regex.Pattern;
30+
31+
/**
32+
* Utility class for TestWatchers
33+
*/
34+
@UtilityClass
35+
public class TestWatcherHelper {
36+
private static final Pattern UNIT_TEST_ID_PATTERN =
37+
Pattern.compile("\\[.*?\\]/\\[.*?\\]/\\[.*?\\]/\\[.*?:(.*?)\\]");
38+
static final String ROOT_DIRECTORY_PROPERTY = "usecase.test.repo.dir";
39+
40+
Path fetchTargetPath(ExtensionContext context, String rootFolder)
41+
throws IOException {
42+
String className = context.getTestClass().get().getSimpleName();
43+
String methodName = context.getRequiredTestMethod().getName();
44+
Optional<String> parameterInvocation = getParameterInvocation(context);
45+
46+
List<String> subfolder = new ArrayList<>(Arrays.asList(className, methodName));
47+
parameterInvocation.ifPresent(subfolder::add);
48+
49+
Path mainFolderPath = Paths.get(rootFolder, subfolder.toArray(new String[0]));
50+
Files.createDirectories(mainFolderPath);
51+
return mainFolderPath;
52+
}
53+
54+
private static Optional<String> getParameterInvocation(ExtensionContext context) {
55+
Matcher matcher = UNIT_TEST_ID_PATTERN.matcher(context.getUniqueId());
56+
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
org.eclipse.lsp.cobol.usecases.suite.UniqueIdTestWatcher
16+
org.eclipse.lsp.cobol.test.engine.ExtensionContextProvider
17+
org.eclipse.lsp.cobol.test.engine.ExtensionContextCleaner
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
junit.jupiter.extensions.autodetection.enabled=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.test.engine;
16+
17+
import org.junit.jupiter.api.extension.AfterEachCallback;
18+
import org.junit.jupiter.api.extension.ExtensionContext;
19+
20+
/**
21+
* ExtensionContextCleaner
22+
*/
23+
public class ExtensionContextCleaner implements AfterEachCallback {
24+
25+
/**
26+
*
27+
* @param context
28+
*/
29+
@Override
30+
public void afterEach(ExtensionContext context) {
31+
ExtensionContextProvider.clearExtensionContext();
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
16+
package org.eclipse.lsp.cobol.test.engine;
17+
18+
import org.junit.jupiter.api.extension.BeforeEachCallback;
19+
import org.junit.jupiter.api.extension.ExtensionContext;
20+
import java.util.Optional;
21+
22+
/**
23+
* BeforeEachCallback for Use case test
24+
*/
25+
public class ExtensionContextProvider implements BeforeEachCallback {
26+
27+
private static final ThreadLocal<ExtensionContext> CONTEXT_HOLDER = new ThreadLocal<>();
28+
29+
@Override
30+
public void beforeEach(ExtensionContext context) {
31+
CONTEXT_HOLDER.set(context);
32+
}
33+
34+
/**
35+
* get junit extensionContext
36+
*
37+
* @return ExtensionContext
38+
*/
39+
public static Optional<ExtensionContext> getExtensionContext() {
40+
return Optional.ofNullable(CONTEXT_HOLDER.get());
41+
}
42+
43+
/**
44+
* clear context
45+
*/
46+
public static void clearExtensionContext() {
47+
CONTEXT_HOLDER.remove();
48+
}
49+
}

server/test/src/main/java/org/eclipse/lsp/cobol/test/engine/PreprocessedDocument.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* to the actual Language Engine, and the testDAta will be used to assert the result.
2626
*/
2727
@Value
28-
class PreprocessedDocument {
28+
public class PreprocessedDocument {
2929
String text;
3030
List<CobolText> copybooks;
3131
TestData testData;

server/test/src/main/java/org/eclipse/lsp/cobol/test/engine/TestData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/** This data class defines output of use-case text preprocessor */
2727
@Value
2828
@Builder(toBuilder = true)
29-
class TestData {
29+
public class TestData {
3030
String text;
3131
String copybookName;
3232
String dialectType;

server/test/src/main/java/org/eclipse/lsp/cobol/test/engine/UseCaseEngine.java

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.common.collect.ImmutableList;
2828
import com.google.common.collect.Multimap;
2929
import com.google.gson.JsonElement;
30+
3031
import java.util.*;
3132
import java.util.function.Function;
3233
import java.util.function.Predicate;
@@ -262,6 +263,7 @@ public AnalysisResult runTest(
262263
PreprocessedDocument document =
263264
AnnotatedDocumentCleaning.prepareDocument(
264265
text, copybooks, subroutineNames, expectedDiagnostics, sqlBackendSetting, analysisConfig.getCompilerOptions());
266+
265267
AnalysisResult actual =
266268
analyze(
267269
UseCase.builder()
@@ -278,6 +280,7 @@ public AnalysisResult runTest(
278280
.build(),
279281
languageId);
280282
assertResultEquals(actual, document.getTestData());
283+
UseCaseUtils.storeDocumentToUnitTextExtensionContext(document.getText(), document.getCopybooks(), document.getTestData());
281284
return actual;
282285
}
283286

0 commit comments

Comments
 (0)