Use toLowerCase() and toUpperCase() with Locale.ENGLISH

This commit updates all `toLowerCase()` and `toUpperCase` invocations to
use the variant that takes a `Locale` to avoid locale-specific side
effect.

Closes gh-12213
This commit is contained in:
Stephane Nicoll
2018-02-26 17:49:03 +01:00
parent 915eaf3447
commit b4a7e1d64b
38 changed files with 148 additions and 87 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.boot.ansi;
import java.util.Locale;
import org.springframework.util.Assert;
/**
@@ -35,7 +37,7 @@ public abstract class AnsiOutput {
private static Boolean ansiCapable;
private static final String OPERATING_SYSTEM_NAME = System.getProperty("os.name")
.toLowerCase();
.toLowerCase(Locale.ENGLISH);
private static final String ENCODE_START = "\033[";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.boot.bind;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
@@ -184,7 +185,7 @@ public class PropertySourcesPropertyValues implements PropertyValues {
// Probably could not convert to Object, weird, but ignorable
}
if (value == null) {
value = source.getProperty(propertyName.toUpperCase());
value = source.getProperty(propertyName.toUpperCase(Locale.ENGLISH));
}
putIfAbsent(propertyName, value, source);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.boot.bind;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Set;
import org.springframework.core.convert.ConversionFailedException;
@@ -127,7 +128,8 @@ class RelaxedConversionService implements ConversionService {
source = source.trim();
for (T candidate : (Set<T>) EnumSet.allOf(this.enumType)) {
RelaxedNames names = new RelaxedNames(
candidate.name().replace('_', '-').toLowerCase());
candidate.name().replace('_', '-')
.toLowerCase(Locale.ENGLISH));
for (String name : names) {
if (name.equals(source)) {
return candidate;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.boot.bind;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -91,7 +92,7 @@ public final class RelaxedNames implements Iterable<String> {
@Override
public String apply(String value) {
return value.isEmpty() ? value : value.toLowerCase();
return value.isEmpty() ? value : value.toLowerCase(Locale.ENGLISH);
}
},
@@ -100,7 +101,7 @@ public final class RelaxedNames implements Iterable<String> {
@Override
public String apply(String value) {
return value.isEmpty() ? value : value.toUpperCase();
return value.isEmpty() ? value : value.toUpperCase(Locale.ENGLISH);
}
};
@@ -225,7 +226,7 @@ public final class RelaxedNames implements Iterable<String> {
}
StringBuilder builder = new StringBuilder();
for (String field : SEPARATED_TO_CAMEL_CASE_PATTERN.split(value)) {
field = (caseInsensitive ? field.toLowerCase() : field);
field = (caseInsensitive ? field.toLowerCase(Locale.ENGLISH) : field);
builder.append(
builder.length() == 0 ? field : StringUtils.capitalize(field));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.boot.context.config;
import java.util.Locale;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiOutput.Enabled;
import org.springframework.boot.bind.RelaxedPropertyResolver;
@@ -40,7 +42,8 @@ public class AnsiOutputApplicationListener
event.getEnvironment(), "spring.output.ansi.");
if (resolver.containsProperty("enabled")) {
String enabled = resolver.getProperty("enabled");
AnsiOutput.setEnabled(Enum.valueOf(Enabled.class, enabled.toUpperCase()));
AnsiOutput.setEnabled(Enum.valueOf(Enabled.class,
enabled.toUpperCase(Locale.ENGLISH)));
}
if (resolver.containsProperty("console-available")) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.jar.JarFile;
import org.apache.commons.logging.Log;
@@ -177,7 +178,7 @@ public abstract class AbstractEmbeddedServletContainerFactory
this.logger.debug("Code archive: " + file);
}
if (file != null && file.exists() && !file.isDirectory()
&& file.getName().toLowerCase().endsWith(extension)) {
&& file.getName().toLowerCase(Locale.ENGLISH).endsWith(extension)) {
return file.getAbsoluteFile();
}
return null;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.apache.commons.logging.Log;
@@ -144,9 +145,9 @@ public class PropertySourcesLoader {
}
private boolean canLoadFileExtension(PropertySourceLoader loader, Resource resource) {
String filename = resource.getFilename().toLowerCase();
String filename = resource.getFilename().toLowerCase(Locale.ENGLISH);
for (String extension : loader.getFileExtensions()) {
if (filename.endsWith("." + extension.toLowerCase())) {
if (filename.endsWith("." + extension.toLowerCase(Locale.ENGLISH))) {
return true;
}
}

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.jdbc;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -133,7 +134,7 @@ public enum DatabaseDriver {
@Override
protected boolean matchProductName(String productName) {
return super.matchProductName(productName)
|| productName.toLowerCase().startsWith("firebird");
|| productName.toLowerCase(Locale.ENGLISH).startsWith("firebird");
}
},
@@ -146,7 +147,7 @@ public enum DatabaseDriver {
@Override
protected boolean matchProductName(String productName) {
return super.matchProductName(productName)
|| productName.toLowerCase().startsWith("db2/");
|| productName.toLowerCase(Locale.ENGLISH).startsWith("db2/");
}
},
@@ -170,7 +171,7 @@ public enum DatabaseDriver {
@Override
protected boolean matchProductName(String productName) {
return super.matchProductName(productName)
|| productName.toLowerCase().contains("as/400");
|| productName.toLowerCase(Locale.ENGLISH).contains("as/400");
}
},
@@ -222,7 +223,7 @@ public enum DatabaseDriver {
* @return the identifier
*/
public String getId() {
return name().toLowerCase();
return name().toLowerCase(Locale.ENGLISH);
}
protected boolean matchProductName(String productName) {
@@ -230,7 +231,7 @@ public enum DatabaseDriver {
}
protected Collection<String> getUrlPrefixes() {
return Collections.singleton(this.name().toLowerCase());
return Collections.singleton(this.name().toLowerCase(Locale.ENGLISH));
}
/**
@@ -265,7 +266,8 @@ public enum DatabaseDriver {
public static DatabaseDriver fromJdbcUrl(String url) {
if (StringUtils.hasLength(url)) {
Assert.isTrue(url.startsWith("jdbc"), "URL must start with 'jdbc'");
String urlWithoutPrefix = url.substring("jdbc".length()).toLowerCase();
String urlWithoutPrefix = url.substring("jdbc".length())
.toLowerCase(Locale.ENGLISH);
for (DatabaseDriver driver : values()) {
for (String urlPrefix : driver.getUrlPrefixes()) {
String prefix = ":" + urlPrefix + ":";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.boot.logging;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -370,7 +371,7 @@ public class LoggingApplicationListener implements GenericApplicationListener {
if ("false".equalsIgnoreCase(level)) {
return LogLevel.OFF;
}
return LogLevel.valueOf(level.toUpperCase());
return LogLevel.valueOf(level.toUpperCase(Locale.ENGLISH));
}
private void registerShutdownHookIfNecessary(Environment environment,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.logging.Log;
@@ -250,7 +251,8 @@ public class ApplicationPidFileWriter
private final String[] properties;
SystemProperty(String name) {
this.properties = new String[] { name.toUpperCase(), name.toLowerCase() };
this.properties = new String[] { name.toUpperCase(Locale.ENGLISH),
name.toLowerCase(Locale.ENGLISH) };
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.boot.system;
import java.io.File;
import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -113,10 +114,10 @@ public class EmbeddedServerPortFileWriter
String extension = StringUtils.getFilenameExtension(this.file.getName());
name = name.substring(0, name.length() - extension.length() - 1);
if (isUpperCase(name)) {
name = name + "-" + contextName.toUpperCase();
name = name + "-" + contextName.toUpperCase(Locale.ENGLISH);
}
else {
name = name + "-" + contextName.toLowerCase();
name = name + "-" + contextName.toLowerCase(Locale.ENGLISH);
}
if (StringUtils.hasLength(extension)) {
name = name + "." + extension;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.boot.web.servlet;
import java.util.Locale;
import javax.servlet.MultipartConfigElement;
import org.springframework.util.Assert;
@@ -110,7 +112,7 @@ public class MultipartConfigFactory {
private long parseSize(String size) {
Assert.hasLength(size, "Size must not be empty");
size = size.toUpperCase();
size = size.toUpperCase(Locale.ENGLISH);
if (size.endsWith("KB")) {
return Long.valueOf(size.substring(0, size.length() - 2)) * 1024;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.boot.system;
import java.io.File;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.junit.After;
@@ -103,7 +104,7 @@ public class EmbeddedServerPortFileWriterTests {
@Test
public void createUpperCaseManagementPortFile() throws Exception {
File file = this.temporaryFolder.newFile();
file = new File(file.getParentFile(), file.getName().toUpperCase());
file = new File(file.getParentFile(), file.getName().toUpperCase(Locale.ENGLISH));
EmbeddedServerPortFileWriter listener = new EmbeddedServerPortFileWriter(file);
listener.onApplicationEvent(mockEvent("management", 9090));
String managementFile = file.getName();