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

Support ITestNGFactory customisation #3060

Conversation

krmahadevan
Copy link
Member

@krmahadevan krmahadevan commented Feb 8, 2024

Closes #3059

Fixes #3059

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
    • Added support for custom listener factories in TestNG, allowing for more flexibility in the creation of TestNG listeners.
  • Bug Fixes
    • Resolved issues with methods returning empty results and deprecated JUnit support.
  • Refactor
    • Deprecated a method in TestListenerHelper as part of enhancing listener management.
  • Tests
    • Introduced tests for the new listener factory configuration, ensuring its proper initialization and execution.

Copy link

coderabbitai bot commented Feb 8, 2024

Warning

Rate Limit Exceeded

@krmahadevan has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 9 minutes and 2 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 4af6e94 and 38b4fd4.

Walkthrough

The update introduces the ability to inject custom listener factories into TestNG, enhancing flexibility and customization for users. It addresses issues with empty results and deprecated JUnit support, while also adding new capabilities for listener management. This change allows for more dynamic and user-defined handling of TestNG listeners, improving the framework's adaptability to different testing scenarios.

Changes

File Pattern Change Summary
CommandLineArgs.java, TestNG.java, TestRunner.java, Configuration.java, IConfiguration.java Added support for custom listener factories, including new fields, methods, and imports related to listener factory handling.
TestListenerHelper.java Deprecated method related to listener factory creation.
CreationAttributes.java Simplified constructor by removing unnecessary checks.
test/listeners/factory/* Added test and example classes for the new listener factory feature, showcasing its implementation and usage.

Assessment against linked issues

Objective Addressed Explanation
Support the ability to inject custom listener factory (#3059)

Related issues

Poem

In the land of code where TestNG reigns,
A rabbit hopped, fixing chains.
Listener factories, now yours to mold,
Craft your tests, bold and bold. 🐰✨

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.
  • 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 from git 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: 7

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4af6e94 and 30a556e.
Files ignored due to path filters (1)
  • testng-core/src/test/resources/testng.xml is excluded by: !**/*.xml
Files selected for processing (12)
  • CHANGES.txt (1 hunks)
  • testng-core/src/main/java/org/testng/CommandLineArgs.java (1 hunks)
  • testng-core/src/main/java/org/testng/TestNG.java (4 hunks)
  • testng-core/src/main/java/org/testng/TestRunner.java (2 hunks)
  • testng-core/src/main/java/org/testng/internal/Configuration.java (3 hunks)
  • testng-core/src/main/java/org/testng/internal/IConfiguration.java (1 hunks)
  • testng-core/src/main/java/org/testng/internal/TestListenerHelper.java (1 hunks)
  • testng-core/src/main/java/org/testng/internal/objects/pojo/CreationAttributes.java (2 hunks)
  • testng-core/src/test/java/test/listeners/factory/ExampleListener.java (1 hunks)
  • testng-core/src/test/java/test/listeners/factory/SampleTestCase.java (1 hunks)
  • testng-core/src/test/java/test/listeners/factory/SampleTestFactory.java (1 hunks)
  • testng-core/src/test/java/test/listeners/factory/TestNGFactoryTest.java (1 hunks)
Files skipped from review due to trivial changes (1)
  • testng-core/src/test/java/test/listeners/factory/SampleTestCase.java
Additional comments: 7
testng-core/src/main/java/org/testng/internal/objects/pojo/CreationAttributes.java (1)
  • 12-17: > 📝 NOTE

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

Ensure that removing null checks on context parameters does not introduce potential null pointer exceptions. If contexts are expected to be non-null, consider reinstating null checks.

testng-core/src/main/java/org/testng/internal/IConfiguration.java (1)
  • 13-15: Changes to IConfiguration interface align with the PR's objective and adhere to best practices.
testng-core/src/main/java/org/testng/internal/Configuration.java (1)
  • 66-81: > 📝 NOTE

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

Changes to Configuration class correctly implement the new listener factory functionality and follow encapsulation principles.

testng-core/src/main/java/org/testng/internal/TestListenerHelper.java (1)
  • 164-165: Deprecation of createListenerFactory method aligns with the introduction of new listener factory functionality.
testng-core/src/main/java/org/testng/CommandLineArgs.java (1)
  • 169-172: Addition of listenerFactory field and LISTENER_FACTORY constant correctly implements the new functionality for specifying custom listener factories through command line arguments.
testng-core/src/main/java/org/testng/TestRunner.java (1)
  • 381-382: The use of Optional.ofNullable followed by .orElse for initializing ITestNGListenerFactory is correctly implemented. This approach cleanly handles potential null values and provides a clear fallback mechanism.
CHANGES.txt (1)
  • 3-3: The added line correctly summarizes a new feature introduced in TestNG, aligning with the expected format and content for this document.

testng-core/src/main/java/org/testng/TestNG.java Outdated Show resolved Hide resolved
Comment on lines 7 to 17
public static ExampleListener instance;

public ExampleListener() {
setInstance(this);
}

private static void setInstance(ExampleListener instance) {
ExampleListener.instance = instance;
}
Copy link

Choose a reason for hiding this comment

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

The use of a public static instance variable for singleton pattern is not thread-safe. Consider using a thread-safe singleton pattern implementation.

Comment on lines 18 to 21
public void onExecutionStart() {}

@Override
public void onExecutionFinish() {}
Copy link

Choose a reason for hiding this comment

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

Empty implementation of onExecutionStart and onExecutionFinish. If these are placeholders, consider adding comments to clarify their intended use.

Comment on lines 9 to 23
public static ITestNGListenerFactory instance;

public boolean wasInvoked = false;

public SampleTestFactory() {
setInstance(this);
}

private static void setInstance(ITestNGListenerFactory instance) {
SampleTestFactory.instance = instance;
}
Copy link

Choose a reason for hiding this comment

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

The use of a public static instance variable for singleton pattern is not thread-safe. Consider using a thread-safe singleton pattern implementation.


public static ITestNGListenerFactory instance;

public boolean wasInvoked = false;
Copy link

Choose a reason for hiding this comment

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

The wasInvoked flag is publicly accessible, which could lead to unintended modifications. Consider making it private and providing a public method to access its value.

Comment on lines 11 to 40
public class TestNGFactoryTest extends SimpleBaseTest {

@Test(description = "GITHUB-3059")
public void testListenerFactoryViaConfigurationArg() {
String[] args =
new String[] {
CommandLineArgs.LISTENER_FACTORY,
SampleTestFactory.class.getName(),
CommandLineArgs.TEST_CLASS,
SampleTestCase.class.getName(),
CommandLineArgs.LISTENER,
ExampleListener.class.getName()
};
TestNG testng = TestNG.privateMain(args, null);
assertThat(SampleTestFactory.instance).isNotNull();
assertThat(ExampleListener.instance).isNotNull();
assertThat(testng.getStatus()).isZero();
}

@Test(description = "GITHUB-3059")
public void testListenerFactoryViaTestNGApi() {
TestNG testng = new TestNG();
SampleTestFactory factory = new SampleTestFactory();
testng.setListenerFactory(factory);
testng.setListenerClasses(List.of(ExampleListener.class));
testng.setTestClasses(new Class[] {SampleTestCase.class});
testng.run();
assertThat(testng.getStatus()).isZero();
assertThat(factory.wasInvoked).isTrue();
}
Copy link

Choose a reason for hiding this comment

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

Tests cover basic functionality of the listener factory feature. Consider adding more comprehensive tests to cover edge cases or potential failure scenarios.

@krmahadevan krmahadevan force-pushed the allow_testlistener_factory_to_be_configurable branch from 30a556e to 38b4fd4 Compare February 8, 2024 08:53
@krmahadevan krmahadevan merged commit 4c7653d into testng-team:master Feb 8, 2024
7 of 9 checks passed
@krmahadevan krmahadevan deleted the allow_testlistener_factory_to_be_configurable branch February 8, 2024 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support the ability to inject custom listener factory
2 participants