From be05d7649c5e2d84cf40e61c04eafceaa9af2939 Mon Sep 17 00:00:00 2001 From: Julien Eluard Date: Thu, 16 May 2013 15:06:57 -0300 Subject: [PATCH] #getFullPath was not correctly identifying non-existing assets depending on the name. --- .../java/org/webjars/WebJarAssetLocator.java | 16 ++++++++++++---- .../java/org/webjars/WebJarAssetLocatorTest.java | 7 +++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/webjars/WebJarAssetLocator.java b/src/main/java/org/webjars/WebJarAssetLocator.java index f46b90b..a7fe518 100644 --- a/src/main/java/org/webjars/WebJarAssetLocator.java +++ b/src/main/java/org/webjars/WebJarAssetLocator.java @@ -183,6 +183,12 @@ public WebJarAssetLocator(final SortedMap fullPathIndex) { this.fullPathIndex = fullPathIndex; } + private String throwNotFoundException(final String partialPath) { + throw new IllegalArgumentException( + partialPath + + " could not be found. Make sure you've added the corresponding WebJar and please check for typos."); + } + /** * Given a distinct path within the WebJar index passed in return the full * path of the resource. @@ -200,14 +206,16 @@ public String getFullPath(final String partialPath) { .tailMap(reversePartialPath); if (fullPathTail.size() == 0) { - throw new IllegalArgumentException( - partialPath - + " could not be found. Make sure you've added the corresponding WebJar and please check for typos."); + throwNotFoundException(partialPath); } final Iterator> fullPathTailIter = fullPathTail .entrySet().iterator(); - final String fullPath = fullPathTailIter.next().getValue(); + final Entry fullPathEntry = fullPathTailIter.next(); + if (!fullPathEntry.getKey().startsWith(reversePartialPath)) { + throwNotFoundException(partialPath); + } + final String fullPath = fullPathEntry.getValue(); if (fullPathTailIter.hasNext() && fullPathTailIter.next().getKey() diff --git a/src/test/java/org/webjars/WebJarAssetLocatorTest.java b/src/test/java/org/webjars/WebJarAssetLocatorTest.java index 62beb42..312757a 100644 --- a/src/test/java/org/webjars/WebJarAssetLocatorTest.java +++ b/src/test/java/org/webjars/WebJarAssetLocatorTest.java @@ -45,6 +45,13 @@ public void get_full_path_from_partial_path_with_folders() { @Test public void should_throw_exception_when_asset_not_found() { + try { + new WebJarAssetLocator().getFullPath("asset-unknown.js"); + fail("Exception should have been thrown!"); + } catch (IllegalArgumentException e) { + assertEquals("asset-unknown.js could not be found. Make sure you've added the corresponding WebJar and please check for typos.", e.getMessage()); + } + try { new WebJarAssetLocator().getFullPath("unknown.js"); fail("Exception should have been thrown!");