Skip to content

Commit

Permalink
Specify locale in toLowerCase|toUpperCase
Browse files Browse the repository at this point in the history
This commit makes sure that all usages of String toLowerCase and
toUpperCase specify a Locale (default of Locale.ROOT).

Also, a checkstyle rule is added to prevent usage of the no-arg variant
of String toLowerCase and toUpperCase.
  • Loading branch information
onobc committed Oct 17, 2024
1 parent 35c8b02 commit 871ca72
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Given the following listener method:
----
@PulsarListener(topics = "my-input-topic") // <1>
void listen(String msg) { // <2>
var transformedMsg = msg.toUpperCase(); // <3>
var transformedMsg = msg.toUpperCase(Locale.ROOT); // <3>
this.transactionalTemplate.send("my-output-topic", transformedMsg); // <4>
} // <5> <6>
----
Expand Down Expand Up @@ -215,7 +215,7 @@ The DB transaction is committed first; if the Pulsar transaction fails to commit
@PulsarListener(topics = "my-input-topic")
@Transactional("dataSourceTransactionManager")
void listen(String msg) {
var transformedMsg = msg.toUpperCase();
var transformedMsg = msg.toUpperCase(Locale.ROOT);
this.pulsarTemplate.send("my-output-topic", transformedMsg);
this.jdbcTemplate.execute("insert into my_table (data) values ('%s')".formatted(transformedMsg));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
Expand Down Expand Up @@ -314,7 +315,7 @@ private Optional<Exception> safeInvoke(Runnable invocation) {
}

private String functionDesc(PulsarFunctionOperations<?> function) {
return "'%s' %s".formatted(function.name(), function.type().toString().toLowerCase());
return "'%s' %s".formatted(function.name(), function.type().toString().toLowerCase(Locale.ROOT));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.pulsar.support.header;

import java.util.Locale;
import java.util.Set;

import org.springframework.core.log.LogAccessor;
Expand Down Expand Up @@ -92,13 +93,13 @@ public static PatternMatch fromPatternString(String pattern) {

public PatternMatch(String pattern, boolean negate) {
Assert.notNull(pattern, "Pattern must not be null");
this.pattern = pattern.toLowerCase();
this.pattern = pattern.toLowerCase(Locale.ROOT);
this.negate = negate;
}

@Override
public boolean matchHeader(String headerName) {
if (!PatternMatchUtils.simpleMatch(this.pattern, headerName.toLowerCase())) {
if (!PatternMatchUtils.simpleMatch(this.pattern, headerName.toLowerCase(Locale.ROOT))) {
return false;
}
LOGGER.debug(() -> "headerName=[%s] WILL %s be mapped, matched pattern=%s".formatted(headerName,
Expand Down
2 changes: 2 additions & 0 deletions src/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<suppress files="PulsarFunctionAdministrationIntegrationTests" checks="Regexp" />
<suppress files="Proto" checks=".*"/>
<suppress files=".*Tests" checks="HideUtilityClassConstructor" />
<suppress files=".*Tests" checks="RegexpSinglelineJava" id="toLowerCaseWithoutLocale"/>
<suppress files=".*Tests" checks="RegexpSinglelineJava" id="toUpperCaseWithoutLocale"/>
<suppress files="[\\/]spring-pulsar-docs[\\/]" checks="JavadocPackage|JavadocType|JavadocVariable|SpringDeprecatedCheck" />
<suppress files="[\\/]spring-pulsar-docs[\\/]" checks="SpringJavadoc" message="\@since" />
<suppress files="[\\/]spring-pulsar-docs[\\/].*jooq" checks="AvoidStaticImport" />
Expand Down
16 changes: 16 additions & 0 deletions src/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@
value="Please use AssertJ imports."/>
<property name="ignoreComments" value="true"/>
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="id" value="toLowerCaseWithoutLocale"/>
<property name="format" value="\.toLowerCase\(\)"/>
<property name="maximum" value="0"/>
<property name="message"
value="String.toLowerCase() should be String.toLowerCase(Locale.ROOT)"/>
<property name="ignoreComments" value="true"/>
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="id" value="toUpperCaseWithoutLocale"/>
<property name="format" value="\.toUpperCase\(\)"/>
<property name="maximum" value="0"/>
<property name="message"
value="String.toUpperCase() should be String.toUpperCase(Locale.ROOT)"/>
<property name="ignoreComments" value="true"/>
</module>
<module name="Regexp">
<property name="format" value="[ \t]+$"/>
<property name="illegalPattern" value="true"/>
Expand Down

0 comments on commit 871ca72

Please sign in to comment.