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).
  • Loading branch information
onobc committed Oct 17, 2024
1 parent 35c8b02 commit 8d16d8c
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -204,7 +205,7 @@ private void configureDependencyManagement(Project project) {
configuration.setCanBeResolved(false);
});
configurations
.matching((c) -> c.getName().endsWith("Classpath") || c.getName().toLowerCase().endsWith("annotationprocessor"))
.matching((c) -> c.getName().endsWith("Classpath") || c.getName().toLowerCase(Locale.ROOT).endsWith("annotationprocessor"))
.all((c) -> c.extendsFrom(dependencyManagement));
Dependency pulsarDependencies = project.getDependencies().enforcedPlatform(project.getDependencies()
.project(Collections.singletonMap("path", ":spring-pulsar-dependencies")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask;
Expand Down Expand Up @@ -121,7 +122,7 @@ private void configureAsciidoctorTask(Project project, AbstractAsciidoctorTask a
asciidoctorTask.baseDirFollowsSourceDir();
createSyncDocumentationSourceTask(project, asciidoctorTask);
if (asciidoctorTask instanceof AsciidoctorTask task) {
boolean pdf = task.getName().toLowerCase().contains("pdf");
boolean pdf = task.getName().toLowerCase(Locale.ROOT).contains("pdf");
String backend = (!pdf) ? "spring-html" : "spring-pdf";
task.outputOptions((outputOptions) -> outputOptions.backends(backend));
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/version-catalog-update.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def isNonStable = { String version ->
if (unstableKeyword) {
return true
}
def containsStableKeyword = ['RELEASE', 'FINAL', 'GA'].any { it -> version.toUpperCase().contains(it) }
def containsStableKeyword = ['RELEASE', 'FINAL', 'GA'].any { it -> version.toUpperCase(Locale.ROOT).contains(it) }
def containsStableSuffix = (version ==~ /^[0-9,.v-]+(-r)?$/)
return !containsStableKeyword && !containsStableSuffix
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -222,7 +223,7 @@ static PulsarSource rabbitPulsarSource(@Nullable FunctionStopPolicy stopPolicy)
// custom network and a network alias 'rabbitmq' and the exposed port '5672'.
// This differs from typical RabbitTemplate/RabbitProperties coordinates which
// require the mapped host and port (outside the container).
String suffix = stopPolicy != null ? ("-" + stopPolicy.name().toLowerCase()) : "";
String suffix = stopPolicy != null ? ("-" + stopPolicy.name().toLowerCase(Locale.ROOT)) : "";
Map<String, Object> configs = new HashMap<>();
configs.put("host", "rabbitmq");
configs.put("port", 5672);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ A common transactional pattern is where a consumer reads messages from a Pulsar
The framework supports this use case when transactions are enabled and your listener method uses a transactional `PulsarTemplate` to produce the transformed message.

Given the following listener method:
[source, java]

[source,java]
----
@PulsarListener(topics = "my-input-topic") // <1>
import java.util.Locale;@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 @@ -212,10 +213,10 @@ The DB transaction is committed first; if the Pulsar transaction fails to commit

[source,java]
----
@PulsarListener(topics = "my-input-topic")
import java.util.Locale;@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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.Locale;
import java.util.stream.Stream;

import org.apache.pulsar.common.schema.SchemaType;
Expand Down Expand Up @@ -204,7 +205,7 @@ void spelExpressionIsResolved(@Autowired DefaultTopicResolver topicResolver) {
@Test
void embeddedExpressionIsResolved(@Autowired DefaultTopicResolver topicResolver) {
assertThat(topicResolver.resolveTopic(null, MsgTypeWithTopicEmbeddedExpression.class, () -> defaultTopic)
.value().orElse(null)).isEqualTo("my-custom-property-topic".toUpperCase());
.value().orElse(null)).isEqualTo("my-custom-property-topic".toUpperCase(Locale.ROOT));
}
// @formatter:on

Expand All @@ -226,7 +227,8 @@ record MsgTypeWithTopicPropertyExpression(String value) {
record MsgTypeWithTopicSpELExpression(String value) {
}

@PulsarMessage(topic = "#{T(java.lang.String).valueOf('${app.customPropertyTopic}').toUpperCase()}")
@PulsarMessage(
topic = "#{T(java.lang.String).valueOf('${app.customPropertyTopic}').toUpperCase(java.util.Locale.ROOT)}")
record MsgTypeWithTopicEmbeddedExpression(String value) {
}

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 8d16d8c

Please sign in to comment.