Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map object to configurations #3089

Merged
merged 3 commits into from Mar 14, 2024
Merged

Conversation

krmahadevan
Copy link
Member

@krmahadevan krmahadevan commented Mar 14, 2024

Closes #3082

Fixes #3082 .

Did you remember to?

  • Add test case(s)
  • Update CHANGES.txt
  • Auto applied styling via ./gradlew autostyleApply

We encourage pull requests that:

  • Add new features to TestNG (or)
  • Fix bugs in TestNG

If your pull request involves fixing SonarQube issues then we would suggest that you please discuss this with the
TestNG-dev before you spend time working on it.

Note: For more information on contribution guidelines please make sure you refer our Contributing section for detailed set of steps.

Summary by CodeRabbit

  • New Features
    • Introduced user-defined ordering for TestNG listeners.
    • Added unique IDs to instantiated test class objects for better tracking.
    • Enhanced TestNG to dynamically adjust threads post-deprecation of specific interfaces.
    • Implemented new methods for better handling of class configuration methods in TestNG.
  • Bug Fixes
    • Updated method listener behavior to ensure correct functionality with test methods.
    • Addressed TestNG 7.x DataProvider behavior inconsistencies.
  • Refactor
    • Optimized method addition logic using computeIfAbsent.
    • Replaced synchronized blocks with ReentrantLock for improved concurrency handling.
    • Refactored method initialization and configuration invocation to enhance clarity and maintainability.
  • Documentation
    • Corrected documentation regarding the project's PGP artifact signing keys.
  • Tests
    • Added tests to verify listener functionality with correct test class instances and unique object tracking.

Copy link

coderabbitai bot commented Mar 14, 2024

Walkthrough

The TestNG updates encompass a range of improvements and fixes aimed at enhancing test method listener behaviors, documentation accuracy, unique ID assignments, synchronization enhancements, DataProvider behavior adjustments, and custom ordering of listeners. These changes focus on optimizing testing configurations, especially in parallel testing scenarios, to enhance reliability and flexibility.

Changes

Files Summary
CHANGES.txt Updated method listener behavior, corrected documentation, assigned unique IDs, synchronized changes, handled DataProvider behavior, managed thread adjustment, and introduced user-defined listener ordering.
.../ClassMethodMap.java, .../TestClass.java, .../internal/ConfigurationMethod.java, .../internal/ITestClassConfigInfo.java, .../internal/NoOpTestClass.java Refactored method handling, added new methods for after class configurations, replaced arrays with List for method containers, and improved method storage efficiency.
.../internal/invokers/TestNgMethodUtils.java, .../internal/invokers/ConfigInvoker.java, .../internal/invokers/TestInvoker.java, .../internal/invokers/TestMethodWorker.java Introduced Optional and Predicate, updated method signatures with an additional Object instance parameter, and enhanced method filtering based on instance equality.
.../test/listeners/..., .../test/listeners/issue3082/... Added tests and utilities for tracking method invocations and object instances, addressing issue #3082.

Assessment against linked issues

Objective Addressed Explanation
Utilize IInvokedMethodListener for segmented reporting in multithreaded testing (#3082)
Ensure correct instance returns in listener methods (#3082)
Verify issue reproducibility on IntelliJ (#3082) This objective involves manual testing and is not directly related to code changes.

Poem

"In the realm of tests, where changes bloom bright,
A rabbit weaves updates, in the soft glow of light.
With IDs unique and threads aligned just right,
TestNG sings harmony, in the day and the night.
So here's to the code, where improvements take flight,
Testing stronger together, in the glow of the byte." 🐇🌟

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3308b3c and 92d1313.
Files selected for processing (14)
  • CHANGES.txt (1 hunks)
  • testng-core/src/main/java/org/testng/ClassMethodMap.java (1 hunks)
  • testng-core/src/main/java/org/testng/TestClass.java (5 hunks)
  • testng-core/src/main/java/org/testng/TestRunner.java (2 hunks)
  • testng-core/src/main/java/org/testng/internal/ITestClassConfigInfo.java (3 hunks)
  • testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java (1 hunks)
  • testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java (1 hunks)
  • testng-core/src/main/java/org/testng/internal/invokers/TestMethodWorker.java (2 hunks)
  • testng-core/src/main/java/org/testng/internal/invokers/TestNgMethodUtils.java (2 hunks)
  • testng-core/src/test/java/test/listeners/ListenersTest.java (3 hunks)
  • testng-core/src/test/java/test/listeners/issue3082/IUniqueObject.java (1 hunks)
  • testng-core/src/test/java/test/listeners/issue3082/ObjectRepository.java (1 hunks)
  • testng-core/src/test/java/test/listeners/issue3082/ObjectTrackingMethodListener.java (1 hunks)
  • testng-core/src/test/java/test/listeners/issue3082/TestClassSample.java (1 hunks)
Files skipped from review due to trivial changes (1)
  • testng-core/src/test/java/test/listeners/issue3082/IUniqueObject.java
Additional comments: 20
testng-core/src/test/java/test/listeners/issue3082/ObjectTrackingMethodListener.java (1)
  • 1-16: The implementation of ObjectTrackingMethodListener correctly tracks method invocations for objects implementing IUniqueObject. Good use of type checking and repository usage.
testng-core/src/test/java/test/listeners/issue3082/ObjectRepository.java (1)
  • 1-39: The implementation of ObjectRepository uses concurrent data structures effectively, ensuring thread safety. Methods for managing invocations and errors are correctly implemented.
testng-core/src/main/java/org/testng/internal/ITestClassConfigInfo.java (1)
  • 29-50: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-49]

The enhancements to ITestClassConfigInfo with methods for after class configurations and static utility methods for fetching these configurations from ITestClass instances are well-designed, improving the interface's functionality while maintaining backward compatibility.

testng-core/src/test/java/test/listeners/issue3082/TestClassSample.java (1)
  • 1-68: The TestClassSample is well-implemented, correctly using TestNG features like factory methods, data providers, and before/after annotations. The custom tracking of method invocations using ObjectRepository is logically sound and supports the test's objectives.
testng-core/src/main/java/org/testng/ClassMethodMap.java (1)
  • 35-35: Refactoring to use computeIfAbsent for adding methods to the map in ClassMethodMap is an excellent improvement, enhancing efficiency, readability, and thread safety.
testng-core/src/main/java/org/testng/internal/invokers/TestNgMethodUtils.java (1)
  • 71-101: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [5-260]

The enhancements in TestNgMethodUtils with the introduction of Optional, Predicate, and the sameInstance predicate, along with the modifications to method signatures, significantly improve the handling of instance-specific configurations. These changes enhance the code's functionality, readability, and correctness.

testng-core/src/main/java/org/testng/TestClass.java (1)
  • 235-263: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [7-260]

The updates to TestClass, including the addition of afterClassConfig and related methods, as well as the use of Stream for method processing, significantly improve the handling of after class configurations. These changes follow modern Java practices and enhance the class's functionality, efficiency, and readability.

testng-core/src/main/java/org/testng/internal/invokers/TestMethodWorker.java (1)
  • 134-134: The modifications in TestMethodWorker to ensure correct instance management during method execution and configuration method handling are well-implemented. These changes address the issue of incorrect instance management and improve the code's correctness and functionality.

Also applies to: 235-236

testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java (1)
  • 239-239: Adding a null argument before arguments.getTestClass() in the filterMethods call could potentially introduce nullability issues or affect the method's behavior in unexpected ways. Please verify the necessity of this change and consider documenting its purpose or refactoring to avoid passing null directly if possible.
testng-core/src/test/java/test/listeners/ListenersTest.java (3)
  • 14-14: The addition of SoftAssertions from AssertJ is a good choice for making multiple assertions in a single test without stopping at the first failure. This approach enhances the test's ability to report multiple issues at once, which is particularly useful in complex testing scenarios like the one being addressed.
  • 52-53: The imports for ObjectRepository and ObjectTrackingMethodListener are crucial for the new test method to function. These classes seem to be part of the solution to track object instances correctly and validate the fix. Ensuring these are correctly imported and utilized is key to the test's success.
  • 527-543: The new test method ensureListenerWorksWithCorrectTestClassInstance is well-structured and follows best practices for testing. It sets up a TestNG instance with specific configurations, adds the ObjectTrackingMethodListener, and asserts the state of ObjectRepository using SoftAssertions. This method effectively validates the changes made to address the issue of incorrect instance returns in IInvokedMethodListener.

However, it's important to ensure that the ObjectRepository and ObjectTrackingMethodListener are implemented in a way that accurately tracks and reports instances. The test assumes that the ObjectRepository correctly tracks instances and errors, and that the ObjectTrackingMethodListener accurately captures method invocations.

Given the context of the PR, this test method is a critical piece of the validation process. It's designed to ensure that the changes made to TestNG correctly handle instances in parallel testing scenarios, which is the core issue the PR aims to resolve.

testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java (2)
  • 186-186: The addition of the instance parameter to filterBeforeTestMethods is aligned with the PR's objective to ensure correct instance handling during method filtering. This change is crucial for achieving accurate instance management in parallel testing scenarios.
  • 188-188: Similarly, the addition of the instance parameter to filterAfterTestMethods supports the goal of accurate instance management. This modification is essential for maintaining consistency in instance handling across different phases of testing.
testng-core/src/main/java/org/testng/TestRunner.java (2)
  • 489-495: The modifications to the fixMethodsWithClass calls to use the new classLevelConfigs method for retrieving before and after class methods are a significant change. This approach centralizes the logic for obtaining class-level configuration methods, which can improve maintainability and readability. However, it's crucial to ensure that this change does not inadvertently alter the behavior of test initialization, especially in complex scenarios involving inheritance and overrides.

Ensure comprehensive testing around these changes, particularly in scenarios involving inheritance and overrides of configuration methods, to verify that the behavior remains consistent with previous versions.

  • 559-565: The introduction of the classLevelConfigs method is a good practice as it encapsulates the logic for retrieving before and after class methods into a single, reusable method. This change enhances the code's readability and maintainability by reducing duplication and centralizing the logic.

Consider adding detailed Javadoc comments to the classLevelConfigs method, explaining its purpose, parameters, and the significance of the beforeConfig flag. This documentation will help future maintainers and contributors understand the method's role in the test initialization process.

CHANGES.txt (4)
  • 2-2: The entry for the current version (7.10.0) is present, ensuring the document is up-to-date with the latest changes.
  • 2-5: The summary of changes for version 7.10.0 is well-detailed, covering various fixes and new features, which helps in understanding the scope of the update.
  • 2-5: Ensure consistency in the formatting of the entries across different versions. Each entry should follow a similar structure to maintain readability and ease of understanding for the users.
  • 2-5: Check for any typographical or grammatical issues in the descriptions of the changes to ensure clarity and professionalism in the documentation.

@krmahadevan krmahadevan requested a review from juherr March 14, 2024 06:59
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 92d1313 and 73308ee.
Files selected for processing (5)
  • testng-core/src/main/java/org/testng/TestClass.java (5 hunks)
  • testng-core/src/main/java/org/testng/internal/ConfigurationMethod.java (7 hunks)
  • testng-core/src/main/java/org/testng/internal/ITestClassConfigInfo.java (2 hunks)
  • testng-core/src/main/java/org/testng/internal/NoOpTestClass.java (5 hunks)
  • testng-core/src/main/java/org/testng/internal/invokers/TestNgMethodUtils.java (3 hunks)
Files skipped from review as they are similar to previous changes (3)
  • testng-core/src/main/java/org/testng/TestClass.java
  • testng-core/src/main/java/org/testng/internal/ITestClassConfigInfo.java
  • testng-core/src/main/java/org/testng/internal/invokers/TestNgMethodUtils.java
Additional comments: 8
testng-core/src/main/java/org/testng/internal/NoOpTestClass.java (5)
  • 15-25: The replacement of arrays with List for method containers (m_beforeClassMethods, m_beforeTestMethods, etc.) is a positive change that enhances flexibility and potentially improves performance. However, ensure that all usages of these fields throughout the codebase have been updated to work with List instead of arrays to avoid runtime errors.
  • 42-51: When initializing List fields in the constructor with List.of(), ensure that the methods being passed (testClass.getBeforeSuiteMethods(), testClass.getBeforeTestConfigurationMethods(), etc.) return non-null values. List.of() does not accept null elements. If there's a possibility of null values, consider using a null-safe approach or filtering nulls before creating the list.
  • 59-63: The methods setBeforeTestMethods and setAfterTestMethod now accept arrays and convert them to List using List.of(). This is a good use of List.of() for immutable list creation. However, ensure that callers of these methods are aware of the change and that the passed arrays are not expected to be modified after being passed to these methods, as the resulting List will be immutable.
  • 39-90: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [69-113]

The conversion of method containers to List and then back to arrays using toArray(ITestNGMethod[]::new) in getter methods is correctly implemented. This ensures compatibility with existing interfaces while leveraging the benefits of List. Just ensure that the performance impact of these conversions is minimal, especially in contexts where these methods are called frequently.

  • 125-125: The conversion of m_afterGroupsMethods to an array in getAfterGroupsMethods is consistent with the changes made to other method containers. This maintains the consistency of the API while internally using List for better flexibility.
testng-core/src/main/java/org/testng/internal/ConfigurationMethod.java (3)
  • 144-150: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [147-192]

The method createMethods correctly replaces arrays with List<ITestNGMethod> for creating configuration methods. This change enhances flexibility and code readability. Ensure that all callers of this method have been updated to handle List instead of arrays.

  • 189-198: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [195-218]

The methods createSuiteConfigurationMethods, createTestConfigurationMethods, and others correctly utilize the createMethods utility to generate lists of configuration methods. This maintains consistency in how configuration methods are created and stored, leveraging the benefits of using List.

  • 292-298: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [295-322]

The use of Arrays.stream() followed by .parallel() in createAfterConfigurationMethods is a good practice for parallel processing of arrays. However, ensure that the operations performed within the stream are thread-safe and that parallel processing does not introduce any unintended side effects, especially since ConfigurationMethod instances are being created and modified.

@krmahadevan krmahadevan requested a review from juherr March 14, 2024 07:36
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 73308ee and 8c9f8ac.
Files selected for processing (2)
  • testng-core/src/main/java/org/testng/TestRunner.java (2 hunks)
  • testng-core/src/main/java/org/testng/internal/ITestClassConfigInfo.java (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • testng-core/src/main/java/org/testng/TestRunner.java
  • testng-core/src/main/java/org/testng/internal/ITestClassConfigInfo.java

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 73308ee and f5123a3.
Files selected for processing (3)
  • testng-core/src/main/java/org/testng/TestRunner.java (2 hunks)
  • testng-core/src/main/java/org/testng/internal/ITestClassConfigInfo.java (2 hunks)
  • testng-core/src/main/java/org/testng/internal/NoOpTestClass.java (5 hunks)
Files skipped from review as they are similar to previous changes (3)
  • testng-core/src/main/java/org/testng/TestRunner.java
  • testng-core/src/main/java/org/testng/internal/ITestClassConfigInfo.java
  • testng-core/src/main/java/org/testng/internal/NoOpTestClass.java

fixMethodsWithClass(tc.getBeforeTestMethods(), tc, null);
fixMethodsWithClass(tc.getAfterTestMethods(), tc, null);
fixMethodsWithClass(tc.getAfterClassMethods(), tc, afterClassMethods);
fixMethodsWithClass(afterClassConfigMethods(tc), tc, afterClassMethods);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't catch it before but why not have a fixMethodsWithClass(Array) that will call fixMethodsWithClass(List)? That will be a good start for cleaning without the need to change everything.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to list has been made and pushed already. Lets go ahead with that.

I will raise a separate PR, that will deprecate all the user facing methods on ITestNGMethod that return an array and have it replaced with a List.

@krmahadevan krmahadevan requested a review from juherr March 14, 2024 08:15
@krmahadevan krmahadevan merged commit 23bb95f into testng-team:master Mar 14, 2024
4 of 9 checks passed
@krmahadevan krmahadevan deleted the fix_3082 branch March 14, 2024 10:55
krmahadevan added a commit to krmahadevan/testng that referenced this pull request Mar 21, 2024
This test has recently started failing after the PR
testng-team#3089
got merged. 

With the below exception

org.ops4j.pax.exam.TestContainerException: java.lang.IllegalArgumentException: Can not set java.util.List field org.testng.internal.NoOpTestClass.m_beforeTestMethods to [Lorg.testng.ITestNGMethod;

This PR changes the type of a field
Which was previously an array to a list.

The PaxExam listener is making 
Use of reflection to access the data member 
“m_beforeTestMethods” instead of it just using
The “setter” that is part of NoOpTestClass.

Since this library is being used ONLY for test and 
Since the library in question seems to be something
That is not updated for quite sometime, resorting 
to class path overriding wherein we duplicate the 
Same class into TestNG codebase and then fix the 
Problem locally so that the tests will pass.
@krmahadevan krmahadevan mentioned this pull request Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants