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] Migrate the String.Format to String.Format(Locale.US) to handle different langauges for Edge, Chrome and Firefox #13934

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from

Conversation

MustafaAgamy
Copy link

@MustafaAgamy MustafaAgamy commented May 13, 2024

User description

Description

*Changes Done:

  • Migrate the String.Format to Formatter.Format in :
  • ChromeDriverSerivce.java
  • EdgeDriverService.java
  • RemoteWebDriver.java

Motivation and Context

*Issue explaination:

  • Having System Date & Language in Arabic caused with driver setup. Using String.Format for ports and some other lines caused them to be localized in the system language and in arabic case it caused the numbers and some other character to be localized in arabic, e.g. the port. And that caused an issues when appending the port to the URL in order to setup the browser

*Fix explaination:

  • Now it's utilizing the Formatter.Format and forcing the localization to be in (English.US) ny passing the corresponding Locale in the Formatter Constructor

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.

PR Type

Bug fix


Description

  • Updated ChromeDriverService, EdgeDriverService, and RemoteWebDriver to use Formatter.format with Locale.US to ensure consistent formatting across different system locales.
  • This change addresses issues where system locale settings (like Arabic) affected the formatting of numbers and text in logs and port settings, potentially causing errors in browser setup.
  • The use of Locale.US ensures that numbers and text are formatted in a standard way, avoiding issues related to localization.

Changes walkthrough 📝

Relevant files
Bug fix
ChromeDriverService.java
Use Formatter with Locale.US in ChromeDriverService           

java/src/org/openqa/selenium/chrome/ChromeDriverService.java

  • Replaced String.format with Formatter.format using Locale.US for
    consistent number formatting.
  • Ensured log file paths and log levels are formatted without
    localization issues.
  • +6/-4     
    EdgeDriverService.java
    Standardize EdgeDriverService Formatting with Locale.US   

    java/src/org/openqa/selenium/edge/EdgeDriverService.java

  • Switched from String.format to Formatter.format with Locale.US to
    avoid localization in log and port settings.
  • Adjusted logging and port settings to use non-localized number
    formats.
  • +6/-4     
    RemoteWebDriver.java
    Implement Formatter in RemoteWebDriver for Uniform Output

    java/src/org/openqa/selenium/remote/RemoteWebDriver.java

  • Updated string formatting to use Formatter with Locale.US for
    consistent command and log messages.
  • Modified session handling and logging to prevent localization issues.
  • +8/-6     

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

    @CLAassistant
    Copy link

    CLAassistant commented May 13, 2024

    CLA assistant check
    All committers have signed the CLA.

    Copy link

    PR Description updated to latest commit (1c526d0)

    Copy link

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are straightforward and localized to string formatting across a few files. The logic is simple, replacing String.format with Formatter to handle locale-specific formatting issues.

    🧪 Relevant tests

    No

    ⚡ Possible issues

    Resource Leak: Each new Formatter instance should be properly closed to avoid resource leaks. Consider using try-with-resources or ensuring each formatter is closed after use.

    Consistency Issue: In some places, Locale.US is used, and in one place, Locale.ENGLISH is used. It's important to ensure consistency across the application unless intentionally varied.

    🔒 Security concerns

    No

    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Resource management
    Ensure resource management by properly closing Formatter objects

    Ensure that the Formatter objects are properly closed to prevent resource leaks. Use
    try-with-resources to manage the Formatter lifecycle.

    java/src/org/openqa/selenium/chrome/ChromeDriverService.java [287]

    -args.add(String.valueOf(new Formatter(Locale.US).format("--port=%d", getPort())));
    +try (Formatter formatter = new Formatter(Locale.US)) {
    +    args.add(String.valueOf(formatter.format("--port=%d", getPort())));
    +}
     
    Suggestion importance[1-10]: 9

    Why: The suggestion correctly identifies a resource management issue with the Formatter class, which is crucial to prevent potential resource leaks in Java.

    9
    Performance
    Optimize resource usage by using a single Formatter instance

    Use a single Formatter instance within the method to optimize resource usage and improve
    performance.

    java/src/org/openqa/selenium/edge/EdgeDriverService.java [281]

    -args.add(String.valueOf(new Formatter(Locale.US).format("--port=%d", getPort())));
    +try (Formatter formatter = new Formatter(Locale.US)) {
    +    args.add(String.valueOf(formatter.format("--port=%d", getPort())));
    +    if (getLogFile() != null) {
    +        args.add(String.valueOf(formatter.format("--log-path=%s", getLogFile().getAbsolutePath())));
    +    }
    +    if (logLevel != null) {
    +        args.add(String.valueOf(formatter.format("--log-level=%s", logLevel.toString().toUpperCase())));
    +    }
    +    if (allowedListIps != null) {
    +        args.add(String.valueOf(formatter.format("--allowed-ips=%s", allowedListIps)));
    +    }
    +}
     
    Suggestion importance[1-10]: 8

    Why: This suggestion is valid and would improve performance by reducing the number of Formatter instances created, which is a good practice in resource management.

    8
    Code simplification
    Simplify code and reduce object creation by using direct String conversion

    Replace String.valueOf with direct String conversion for cleaner code and to avoid
    unnecessary object creation.

    java/src/org/openqa/selenium/remote/RemoteWebDriver.java [731]

    -copyToLog.setValue(String.valueOf(new Formatter(Locale.US).format("*%s response suppressed*", commandName)));
    +copyToLog.setValue(new Formatter(Locale.US).format("*%s response suppressed*", commandName).toString());
     
    Suggestion importance[1-10]: 7

    Why: The suggestion improves code readability and efficiency by eliminating an unnecessary method call, which is a good practice for cleaner and more efficient code.

    7
    Robustness
    Enhance robustness by handling exceptions in toUpperCase() method

    Consider handling potential exceptions that might be thrown by the toUpperCase() method in
    a locale-sensitive manner.

    java/src/org/openqa/selenium/remote/RemoteWebDriver.java [298]

    -args.add(String.valueOf(new Formatter(Locale.US).format("--log-level=%s", logLevel.toString().toUpperCase())));
    +try {
    +    args.add(String.valueOf(new Formatter(Locale.US).format("--log-level=%s", logLevel.toString().toUpperCase(Locale.ROOT))));
    +} catch (Exception e) {
    +    // Handle exception
    +}
     
    Suggestion importance[1-10]: 6

    Why: This suggestion adds robustness by considering locale sensitivity and exception handling, although toUpperCase() typically does not throw exceptions that need to be caught in this context, making the suggestion slightly less relevant.

    6

    @MustafaAgamy MustafaAgamy force-pushed the Chrome_Edge_Arabic_Date_Fix branch 2 times, most recently from c8523ff to f748920 Compare May 15, 2024 11:53
    @diemol diemol added this to the 4.22 milestone May 15, 2024
    @MustafaAgamy MustafaAgamy force-pushed the Chrome_Edge_Arabic_Date_Fix branch 2 times, most recently from 900e2be to db0efb4 Compare May 19, 2024 19:50
    @MustafaAgamy MustafaAgamy changed the title [Java] Migrate the String.Format to Formatter.Format to handle different langauges for Edge And Chrome [Java] Migrate the String.Format to String.Format(Locale.US) to handle different langauges for Edge, Chrome and Firefox May 21, 2024
    @pujagani
    Copy link
    Contributor

    pujagani commented May 29, 2024

    I think the changes in the PR are not necessary. I looked into this further to realize the findings below:

      public static String format(String format, Object... args) {
            return new Formatter().format(format, args).toString();
        } 
    

    This creates a Formatter instance:

      public Formatter() {
            this(Locale.getDefault(Locale.Category.FORMAT), new StringBuilder());
        }
    

    So setting the default using "setDefault(Locale)" should work out of the box without any changes.

    @pujagani
    Copy link
    Contributor

    Before we close this PR, can you try on your end to double-check if it works as expected using "setDefault"?

    @MustafaAgamy
    Copy link
    Author

    MustafaAgamy commented May 29, 2024

    @pujagani
    I just did this to minimize the scope of the affects

    But setting the setting the Default to Locale.US should work

    You can call it In the same create Args methods once

    I will change the fixes to that and give it a try?

    @MustafaAgamy
    Copy link
    Author

    @pujagani

    Done can you please check and let me know ? (it worked local)
    Also maybe another pipeline run to make sure everything is working as expected?

    - Migrate the String.Format to Formatter.Format in :
    - ChromeDriverSerivce.java
    - EdgeDriverService.java
    - RemoteWebDriver.java
    
    *Issue Explaination:
    - Having System Date & Language in Arabic caused with driver setup.
    Using String.Format for ports and some other lines caused them to be localized in the system language and in arabic case it caused the numbers and some other character to be localized in arabic,
    e.g. the port. And that caused an issues when appending the port to the URL in order to setup the browser
    
    *Fix explaination:
    - Now it's utilizaing the Formatter.Format and forcing the localization to be in English.US in the Formatter Constructor
    …he classes changed:
    
    - ChromeDriverSerivce.java
    - EdgeDriverService.java
    - RemoteWebDriver.java
    
    *Changes Done:
    - Add try with resoruces to handle memory leaks where it's applicables
    - Changing String.ValueOf to ToString()
    - ChromeDriverService.java
    - EdgeDriverService.java
    - RemoteWebDriver.java
    - DriverService.java
    * Add Tests for both Edge and Chrome:
    - ChromeArabicDateTest.java at test/chrome
    - EdgeArabicDateTest.java at test/edge
    …rService.java
    
    - Added Test Class for FirefoxArabicDateTest.java
    -Refactored the code of EdgeArabicDateTest.java And ChromeArabicDateTest.java to follow the same approach
    - ChromeDriverService.java
    - EdgeDriverService.java
    - GeckoDriverService.java
    - RemoteWebDriver.java
    - DriverService.java
    MustafaAgamy and others added 7 commits May 29, 2024 11:38
    - Test added to LARGE_TESTS in BUILD.bazel
    - Refacotor the Test Logic
    - EdgeArabicDateTest.java
    - ChomeArabicDateTest.java
    - FirefoxArabicDateTest.java
    - DriverService.java
    - RemoteWebDriver.java
    
    Change the logic to Locale.setDefault in :
    - ChomeDriverSerivce.java
    - EdgeDriverService.java
    - GeckDriverService.java
    @MustafaAgamy
    Copy link
    Author

    @pujagani I revisited the formatting issue

    Should be good now

    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.

    None yet

    4 participants