RFR: 7903965: Fix Launching Tests from Context Menu

Oleksii Sylichenko duke at openjdk.org
Mon Mar 10 12:59:48 UTC 2025


Currently, there is an issue with running TestNG/JUnit tests when using the context menu on the editor tab. TestNG/JUnit intercepts the test execution from JTReg.

After fixing this issue, I decided to extend the fix to cover all potential use cases of the context menu.

As part of this pull request, I propose enhancing the process of running tests via the context menu. The context menu can be used in the following cases:

- Editor tab
- File in the project tree
- Directory in the project tree
- Any location within a file

The current changes automatically determine what needs to be executed:

- Directory
- File
- Method

Additionally, the issue with intercepting test execution through Application, TestNG, and JUnit configurations when using the context menu on the editor tab has been fixed.

# Summary

- Ensure that run configuration priority is no longer lost to Application, TestNG, or JUnit setups when launching via the context menu.  
- Fix run configuration created via the context menu: find and launch the proper test element, depending on the current PSI element.  
- Improve the logic for comparing existing run configurations with newly generated ones.  
- Add JUnit plugin in the dependencies.

# Detailed changes

## Add JUnit plugin into dependencies
- build.gradle
- plugin.xml

## JTRegClassConfigurationProducer.java
- Refactor `setupConfigurationFromContext`:
  - Move the check for whether a file can be run to `JTRegUtils#isRunnableByJTReg`.  
  - Allow this method to create configurations for directories as well.  
  - Implement `preventRunPriorityLoss` to prevent priority takeover by other run configuration types (Application, TestNG, JUnit) under certain conditions.  
  - Replace the use of the current PSI element for generating names and querying run configurations with the specific element intended for execution.  
- Implement `preventRunPriorityLoss`.
- Add Javadoc for `nameForElement`.

## JTRegConfigurationProducer.java
- Fix logic in `isConfigurationFromContext` for checking if two configurations are identical:
  - Replace the use of the current PSI element for queries with the exact element intended for execution.  
- Compare string-typed configuration parameters using a "not-nullify" approach (`null == ""`).
- Implement `findExactRunElement` to locate the specific PSI element for execution among the current element’s parents.
- Add a JUnit check to the `shouldReplace` method.

## JTRegUtils.java
- Add a predicate to compare two strings using a "not-nullify" approach (`null == ""`).
- Add predicates to determine whether a PSI method or class is a TestNG or JUnit element.
- Add predicates to check if a PSI element is a file or a directory.
- Extract and refactor code from `JTRegClassConfigurationProducer.java` into a method `isRunnableByJTReg`.
- Simplified method "isInJTRegRoot" in order to remove code check warning.

# Testing the Current Changes

Here are the sample test files: [atest.zip](https://github.com/user-attachments/files/19093354/atest.zip)

I performed testing on the following cases.

### Type of test
- TestNG
- JUnit
- JTReg

### Position of right-mouse click
- Before class definition -> run class or file
- Class name -> run class or file
- Body of the class -> run class or file
- Not test method -> run class of file
- Test method (Regardless of how deeply the click was made within the internal structures of the method) -> run test method
- Nected class (JUnit) -> run nested class
- Test method in the nested class (JUnit) -> run test method of the nested class
- Static class (JUnit) -> run main class or file
- Common JUnit test: `org.junit.Test`
- Jupiter test: `org.junit.jupiter.api.Test`
- Data provider method (TestNG) -> run class or file
- Annotated method with ParameterizedTest (JUnit) -> run method
- All possible green arrows on the side field of the editor -> run configuration should correspond
- More than one class is present in the file -> plain extra classes should be ignored
- JTReg test contains "main" method -> always run file
- TestNG and JUnit test file without directive "@test" in the comments should not be run by JTReg

## Some examples from JDK

### TestNG
- test/langtools/jdk/jshell/ToolSimpleTest.java
- test/jdk/jdk/internal/util/ArraysSupport/NewLength.java
- test/langtools/jdk/jshell/AnalysisTest.java

### JUnit
- test/jdk/jdk/internal/util/VersionTest.java
- test/jdk/jdk/internal/util/OSTest.java
- test/jdk/java/util/Currency/CurrencyTest.java

### JTReg
- test/langtools/tools/javac/cast/BoxedArray.java
- test/langtools/tools/javac/4241573/T4241573.java

-------------

Commit messages:
 - Fix typo: "left" -> "rest of the"
 - Remove `final` from local variables
 - JTRegConfigurationProducer.java:
 - JTRegConfigurationProducer.java:
 - JTRegUtils.java:
 - JTRegUtils.java:
 - Add JUnit plugin into dependencies:

Changes: https://git.openjdk.org/jtreg/pull/252/files
  Webrev: https://webrevs.openjdk.org/?repo=jtreg&pr=252&range=00
  Issue: https://bugs.openjdk.org/browse/CODETOOLS-7903965
  Stats: 239 lines in 5 files changed: 209 ins; 4 del; 26 mod
  Patch: https://git.openjdk.org/jtreg/pull/252.diff
  Fetch: git fetch https://git.openjdk.org/jtreg.git pull/252/head:pull/252

PR: https://git.openjdk.org/jtreg/pull/252


More information about the jtreg-dev mailing list