Specify locale in toLowerCase|toUpperCase (#893)

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.
This commit is contained in:
Chris Bono
2024-10-17 16:26:26 -05:00
committed by GitHub
parent 35c8b02604
commit 123b17df0d
5 changed files with 25 additions and 5 deletions

View File

@@ -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>
----
@@ -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));
}

View File

@@ -20,6 +20,7 @@ import java.util.ArrayList;
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;
@@ -314,7 +315,7 @@ public class PulsarFunctionAdministration implements SmartLifecycle {
}
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));
}
/**

View File

@@ -16,6 +16,7 @@
package org.springframework.pulsar.support.header;
import java.util.Locale;
import java.util.Set;
import org.springframework.core.log.LogAccessor;
@@ -92,13 +93,13 @@ public interface PulsarHeaderMatcher {
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,

View File

@@ -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" />

View File

@@ -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"/>