Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static String createNormalizedInternedPathname(String dir1, String dir2, String
Matcher m = MULTIPLE_SEPARATORS.matcher(pathname);
pathname = m.replaceAll(Matcher.quoteReplacement(File.separator));
// A single trailing slash needs to be taken care of separately
if (pathname.length() > 1 && pathname.endsWith(File.separator)) {
if (pathname.length() > 1 && pathname.charAt(pathname.length() - 1) == File.separatorChar) {
pathname = pathname.substring(0, pathname.length() - 1);
}
return pathname.intern();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,20 @@ public void jsonSerializationOfExecutorRegistration() throws IOException {

@Test
public void testNormalizeAndInternPathname() {
assertPathsMatch("/foo", "bar", "baz",
File.separator + "foo" + File.separator + "bar" + File.separator + "baz");
assertPathsMatch("//foo/", "bar/", "//baz",
File.separator + "foo" + File.separator + "bar" + File.separator + "baz");
assertPathsMatch("foo", "bar", "baz///",
"foo" + File.separator + "bar" + File.separator + "baz");
assertPathsMatch("/foo/", "/bar//", "/baz",
File.separator + "foo" + File.separator + "bar" + File.separator + "baz");
assertPathsMatch("/", "", "", File.separator);
assertPathsMatch("/", "/", "/", File.separator);
String sep = File.separator;
String expectedPathname1 = sep + "foo" + sep + "bar" + sep + "baz";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant this value only. The others are used only once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to write in single style if there is no strict rule that "value used only once must be inlined"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracting a variable which only used once just increases the indirection.

Especially here where the role of the value is trivial. If it would be something complex then I might understand to use a describing name but here not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, reverted.

String expectedPathname2 = "foo" + sep + "bar" + sep + "baz";
String expectedPathname3 = sep + "foo\\" + sep + "bar" + sep + "baz";
assertPathsMatch("/foo", "bar", "baz", expectedPathname1);
assertPathsMatch("//foo/", "bar/", "//baz", expectedPathname1);
assertPathsMatch("foo", "bar", "baz///", expectedPathname2);
assertPathsMatch("/foo/", "/bar//", "/baz", expectedPathname1);
assertPathsMatch("/", "", "", sep);
assertPathsMatch("/", "/", "/", sep);
if (SystemUtils.IS_OS_WINDOWS) {
assertPathsMatch("/foo\\/", "bar", "baz",
File.separator + "foo" + File.separator + "bar" + File.separator + "baz");
assertPathsMatch("/foo\\/", "bar", "baz", expectedPathname1);
} else {
assertPathsMatch("/foo\\/", "bar", "baz",
File.separator + "foo\\" + File.separator + "bar" + File.separator + "baz");
assertPathsMatch("/foo\\/", "bar", "baz", expectedPathname3);
}
}

Expand Down