From 12ede8689a0e5a358ee7c9c6f91d657ca5797ab7 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 30 Apr 2014 17:33:23 +0100 Subject: [PATCH] Prevent duplicate files from getting onto sources --- .../boot/cli/util/ResourceUtils.java | 11 +++++++++-- .../boot/cli/util/ResourceUtilsTests.java | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/util/ResourceUtils.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/util/ResourceUtils.java index 7ef1c8ba76..256ad4826a 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/util/ResourceUtils.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/util/ResourceUtils.java @@ -115,7 +115,7 @@ public abstract class ResourceUtils { continue; } } - result.add(resource.getURL().toExternalForm()); + result.add(absolutePath(resource)); } } return result; @@ -127,12 +127,19 @@ public abstract class ResourceUtils { List childFiles = new ArrayList(); for (Resource child : children) { if (!child.getFile().isDirectory()) { - childFiles.add(child.getURL().toExternalForm()); + childFiles.add(absolutePath(child)); } } return childFiles; } + private static String absolutePath(Resource resource) throws IOException { + if (!resource.getURI().getScheme().equals("file")) { + return resource.getURL().toExternalForm(); + } + return resource.getFile().getAbsoluteFile().toURI().toString(); + } + private static String stripLeadingSlashes(String path) { while (path.startsWith("/")) { path = path.substring(1); diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/util/ResourceUtilsTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/util/ResourceUtilsTests.java index 7a338fb4b0..e440193e47 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/util/ResourceUtilsTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/util/ResourceUtilsTests.java @@ -16,6 +16,9 @@ package org.springframework.boot.cli.util; +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; import java.util.List; import org.junit.Test; @@ -39,6 +42,16 @@ public class ResourceUtilsTests { assertTrue(urls.get(0).startsWith("file:")); } + @Test + public void duplicateResource() throws Exception { + URLClassLoader loader = new URLClassLoader(new URL[] { + new URL("file:./src/test/resources/"), + new File("src/test/resources/").getAbsoluteFile().toURI().toURL() }); + List urls = ResourceUtils.getUrls("classpath:init.groovy", loader); + assertEquals(1, urls.size()); + assertTrue(urls.get(0).startsWith("file:")); + } + @Test public void explicitClasspathResourceWithSlash() { List urls = ResourceUtils.getUrls("classpath:/init.groovy",