From 6a68c8f7e06a455efd8e3dde19d47fb3f813e7b2 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 26 Oct 2016 11:10:38 +0100 Subject: [PATCH] Restore Handler logic that was changed during its merge This commit restores the logic in Handler that was changed when d20ac56a was merged, while leaving the structural improvements intact. In addition to a couple of changes where a typo meant the wrong variable was being referenced, some logic branches now return false rather than called super. This realigns our Handler's behaviour with that of the JDK's. Some more tests have also been added to try to catch the problems that were introduced during the merge. Closes gh-7021 --- .../boot/loader/jar/Handler.java | 22 +++++++------- .../boot/loader/jar/HandlerTests.java | 30 +++++++++++++++++++ 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java index d4c1841656..2b7a05670b 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java @@ -198,23 +198,23 @@ public class Handler extends URLStreamHandler { @Override protected boolean sameFile(URL u1, URL u2) { - if (!u1.getProtocol().equals("jar") || u2.getProtocol().equals("jar")) { - return super.sameFile(u1, u2); + if (!u1.getProtocol().equals("jar") || !u2.getProtocol().equals("jar")) { + return false; } int separator1 = u1.getFile().indexOf(SEPARATOR); - int separator2 = u1.getFile().indexOf(SEPARATOR); - if (separator1 < 0 || separator2 < 0) { + int separator2 = u2.getFile().indexOf(SEPARATOR); + if (separator1 == -1 || separator2 == -1) { return super.sameFile(u1, u2); } - String root1 = u1.getFile().substring(separator1 + SEPARATOR.length()); - String root2 = u2.getFile().substring(separator2 + SEPARATOR.length()); - if (!root1.equals(root2)) { - return super.sameFile(u1, u2); + String nested1 = u1.getFile().substring(separator1 + SEPARATOR.length()); + String nested2 = u2.getFile().substring(separator2 + SEPARATOR.length()); + if (!nested1.equals(nested2)) { + return false; } - String nested1 = u1.getFile().substring(0, separator1); - String nested2 = u1.getFile().substring(0, separator2); + String root1 = u1.getFile().substring(0, separator1); + String root2 = u2.getFile().substring(0, separator2); try { - return super.sameFile(new URL(nested1), new URL(nested2)); + return super.sameFile(new URL(root1), new URL(root2)); } catch (MalformedURLException ex) { // Continue diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java index 8b5dbe31b1..47862459a3 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java @@ -89,6 +89,36 @@ public class HandlerTests { .isEqualTo("jar:jar:file:/other.jar!/nested!/entry.txt"); } + @Test + public void sameFileReturnsFalseForUrlsWithDifferentProtocols() + throws MalformedURLException { + assertThat(this.handler.sameFile(new URL("jar:file:foo.jar!/content.txt"), + new URL("file:/foo.jar"))).isFalse(); + } + + @Test + public void sameFileReturnsFalseForDifferentFileInSameJar() + throws MalformedURLException { + assertThat(this.handler.sameFile( + new URL("jar:file:foo.jar!/the/path/to/the/first/content.txt"), + new URL("jar:file:/foo.jar!/content.txt"))).isFalse(); + } + + @Test + public void sameFileReturnsFalseForSameFileInDifferentJars() + throws MalformedURLException { + assertThat(this.handler.sameFile( + new URL("jar:file:/the/path/to/the/first.jar!/content.txt"), + new URL("jar:file:/second.jar!/content.txt"))).isFalse(); + } + + @Test + public void sameFileReturnsTrueForSameFileInSameJar() throws MalformedURLException { + assertThat(this.handler.sameFile( + new URL("jar:file:/the/path/to/the/first.jar!/content.txt"), + new URL("jar:file:/the/path/to/the/first.jar!/content.txt"))).isTrue(); + } + private URL createUrl(String file) throws MalformedURLException { return new URL("jar", null, -1, file, this.handler); }