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

[Java] Java language level aids #13834

Merged
merged 12 commits into from
May 3, 2024

Conversation

iampopovich
Copy link
Contributor

@iampopovich iampopovich commented Apr 17, 2024

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

Introduced changes to reduce boilerplate code in control structures and removed redundant method calls for boxing and unboxing data types.

Motivation and Context

These are minor changes aimed at making the code cleaner and reducing the overall number of warnings from IDE code inspections.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
    I've ran CI-Java in forked repo
    I've also run gitpod project build with bazel before opening the PR

Type

enhancement


Description

  • Simplified file reading operations using Files.readString for better readability and efficiency.
  • Removed unnecessary boxing and unboxing in test assertions to improve code clarity and performance.
  • Utilized the String.repeat method to simplify string repetition logic.
  • Enhanced JSON parsing and error handling tests by simplifying assertions and using primitive types.

Changes walkthrough

Relevant files
Enhancement
ExecutingJavascriptTest.java
Simplify File Reading with Files.readString                           

java/test/org/openqa/selenium/ExecutingJavascriptTest.java

  • Replaced new String(Files.readAllBytes(jqueryFile), US_ASCII) with
    Files.readString(jqueryFile, US_ASCII) for reading files into a
    string.
  • +1/-1     
    ProxyTest.java
    Remove Unnecessary Boxing in Proxy Tests                                 

    java/test/org/openqa/selenium/ProxyTest.java

    • Removed unnecessary boxing of integer values in assertions.
    +3/-3     
    Utf8Handler.java
    Update File Reading to Use Files.readString                           

    java/test/org/openqa/selenium/environment/webserver/Utf8Handler.java

  • Updated file reading to use Files.readString for efficiency and
    simplicity.
  • +1/-1     
    AddingNodesTest.java
    Optimize Assertions by Removing Unnecessary Boxing             

    java/test/org/openqa/selenium/grid/distributor/AddingNodesTest.java

  • Removed unnecessary boxing in assertions across several test methods.
  • +4/-4     
    JsonOutputTest.java
    Simplify Assertions in JsonOutputTest                                       

    java/test/org/openqa/selenium/json/JsonOutputTest.java

    • Simplified assertions by removing unnecessary boxing.
    +1/-1     
    JsonTest.java
    Enhance JSON Parsing Tests with Simplified Assertions       

    java/test/org/openqa/selenium/json/JsonTest.java

  • Replaced boxed types with primitives in assertions for clarity and
    performance.
  • Simplified numeric comparisons in JSON parsing tests.
  • +8/-8     
    DesiredCapabilitiesTest.java
    Simplify String Repetition in DesiredCapabilitiesTest       

    java/test/org/openqa/selenium/remote/DesiredCapabilitiesTest.java

    • Simplified string repetition using the repeat method.
    +1/-5     
    W3CHttpResponseCodecTest.java
    Update Error Handling Tests with Primitive Assertions       

    java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java

    • Updated error handling tests to use primitive types in assertions.
    +4/-4     

    PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link
    Contributor

    PR Description updated to latest commit (0f122f8)

    Copy link
    Contributor

    PR Review

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are straightforward, focusing on code simplification and modernization without altering the logic. The modifications are consistent across multiple files, involving updates to method calls and data type handling, which are easy to verify for correctness.

    🧪 Relevant tests

    No

    🔍 Possible issues

    Possible Regression: While the changes aim to simplify code, there's a risk of subtle behavioral differences, especially with the conversion from manual string building and number boxing to more concise alternatives. It's important to ensure that these changes do not inadvertently alter the expected outcomes, particularly in edge cases not covered by existing tests.

    🔒 Security concerns

    No


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link
    Contributor

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Use explicit character encoding when reading files.

    Consider using a character encoding explicitly when reading files to ensure consistent
    behavior across different platforms and environments.

    java/test/org/openqa/selenium/ExecutingJavascriptTest.java [410]

    -String jquery = Files.readString(jqueryFile, US_ASCII);
    +String jquery = Files.readString(jqueryFile, StandardCharsets.US_ASCII);
     
    Use enums or constants for status code comparisons.

    Ensure that the status code comparison uses the appropriate enum or constant for clarity
    and maintainability.

    java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java [81]

    -assertThat(decoded.getStatus()).isEqualTo(ErrorCodes.UNHANDLED_ERROR);
    +assertThat(decoded.getStatus()).isEqualTo(ErrorCodes.UNHANDLED_ERROR.getCode());
     
    Enhancement
    Use consistent types for similar assertions.

    For consistency and clarity, consider using the same type for similar assertions within
    the same test method.

    java/test/org/openqa/selenium/json/JsonTest.java [64-66]

    -assertThat((Number) new Json().toType("42", Number.class)).isEqualTo(42L);
    +assertThat((Long) new Json().toType("42", Long.class)).isEqualTo(42L);
     assertThat((Integer) new Json().toType("42", Integer.class)).isEqualTo(42);
     assertThat((Double) new Json().toType("42", Double.class)).isEqualTo(42.0);
     
    Simplify string repetition.

    Utilize Java's built-in string repeat method for better readability and efficiency.

    java/test/org/openqa/selenium/remote/DesiredCapabilitiesTest.java [136]

    -return "x".repeat(Math.max(0, length));
    +return "x".repeat(length);
     
    Possible issue
    Handle potential IOExceptions when reading files.

    When reading files and setting content, ensure to handle potential IOExceptions that might
    be thrown if the file doesn't exist or can't be read.

    java/test/org/openqa/selenium/environment/webserver/Utf8Handler.java [54]

    -.setContent(Contents.utf8String(Files.readString(target)));
    +try {
    +  .setContent(Contents.utf8String(Files.readString(target)));
    +} catch (IOException e) {
    +  // Handle exception
    +}
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    Copy link
    Member

    @diemol diemol left a comment

    Choose a reason for hiding this comment

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

    Can you please run the format script?

    ./scripts/format.sh

    @iampopovich iampopovich changed the title Java language level aids [Java] Java language level aids Apr 18, 2024
    @iampopovich iampopovich requested a review from diemol April 29, 2024 13:26
    @diemol diemol added this to the 4.21 milestone Apr 29, 2024
    Copy link
    Member

    @diemol diemol left a comment

    Choose a reason for hiding this comment

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

    Thank you, @iampopovich!

    sandeepsuryaprasad pushed a commit to sandeepsuryaprasad/selenium that referenced this pull request Oct 29, 2024
    * Improvements from Java 11 API have been incorporated into the code.
    
    * removed unnecessary values boxing
    
    * remove unnecessary values unboxing
    
    * fix for file java/test/org/openqa/selenium/environment/webserver/Utf8Handler.java format
    
    * Format script
    
    ---------
    
    Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
    Co-authored-by: Diego Molina <diemol@gmail.com>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants