Only consider letters when checking if a name is upper-case

Previously, for a string to be considered upper-case,
EmbeddedServerPortFileWriter required every character in the
string to be upper-case. This meant that strings containing numbers were
considered lower-case even if every letter in the string was upper-case.
OS X’s case-preserving, case-insensitive file system masked this problem
as the tests were still able to find the created file, even though the
case of its name was not as expected.

This commit updates EmbeddedServerPortFileWriter to only require
characters that are letters (as defined by Character.isLetter()) to be
upper-case. It also updates the tests to verify that the case of the
created file’s name is correct in such a way that it will fail, even
on OS X, when it is not.

Fixes gh-1676
This commit is contained in:
Andy Wilkinson 2014-10-09 14:17:18 +01:00
parent 8ffe7ec4b8
commit 6ec0b4ca81
2 changed files with 20 additions and 2 deletions

View File

@ -35,6 +35,8 @@ import org.springframework.util.StringUtils;
*
* @author David Liu
* @author Phillip Webb
* @author Andy Wilkinson
*
* @since 1.2.0
*/
public class EmbeddedServerPortFileWriter implements
@ -122,7 +124,8 @@ public class EmbeddedServerPortFileWriter implements
private boolean isUpperCase(String name) {
for (int i = 0; i < name.length(); i++) {
if (!Character.isUpperCase(name.charAt(i))) {
if (Character.isLetter(name.charAt(i))
&& !Character.isUpperCase(name.charAt(i))) {
return false;
}
}

View File

@ -18,6 +18,8 @@ package org.springframework.boot.actuate.system;
import java.io.File;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
@ -31,6 +33,7 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@ -40,6 +43,7 @@ import static org.mockito.Mockito.mock;
*
* @author David Liu
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class EmbeddedServerPortFileWriterTests {
@ -84,6 +88,7 @@ public class EmbeddedServerPortFileWriterTests {
+ StringUtils.getFilenameExtension(file.getName());
assertThat(FileCopyUtils.copyToString(new FileReader(new File(file
.getParentFile(), managementFile))), equalTo("9090"));
assertThat(collectFileNames(file.getParentFile()), hasItem(managementFile));
}
@Test
@ -99,7 +104,7 @@ public class EmbeddedServerPortFileWriterTests {
+ StringUtils.getFilenameExtension(file.getName());
assertThat(FileCopyUtils.copyToString(new FileReader(new File(file
.getParentFile(), managementFile))), equalTo("9090"));
assertThat(collectFileNames(file.getParentFile()), hasItem(managementFile));
}
private EmbeddedServletContainerInitializedEvent mockEvent(String name, int port) {
@ -112,4 +117,14 @@ public class EmbeddedServerPortFileWriterTests {
return event;
}
private Set<String> collectFileNames(File directory) {
Set<String> names = new HashSet<String>();
if (directory.isDirectory()) {
for (File file : directory.listFiles()) {
names.add(file.getName());
}
}
return names;
}
}