Skip to content

Commit

Permalink
Merge pull request #1040 from tomasbjerre/master
Browse files Browse the repository at this point in the history
Informative error when ZipEntry not found #1039
  • Loading branch information
tomakehurst authored Nov 22, 2018
2 parents 75a053b + 744f381 commit 48fcb77
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
Expand All @@ -35,8 +34,6 @@
import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Iterators.find;
import static com.google.common.collect.Iterators.forEnumeration;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.Thread.currentThread;
import static java.util.Arrays.asList;
Expand Down Expand Up @@ -104,12 +101,17 @@ public TextFile getTextFileNamed(String name) {
}

private URI getZipEntryUri(final String name) {
ZipEntry zipEntry = find(forEnumeration(zipFile.entries()), new Predicate<ZipEntry>() {
public boolean apply(ZipEntry input) {
return input.getName().equals(path + "/" + name);
final String lookFor = path + "/" + name;
final Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
StringBuilder candidates = new StringBuilder();
while(enumeration.hasMoreElements()) {
final ZipEntry candidate = enumeration.nextElement();
if (candidate.getName().equals(lookFor)) {
return getUriFor(candidate);
}
});
return getUriFor(zipEntry);
candidates.append(candidate.getName() + "\n");
}
throw new RuntimeException("Was unable to find entry: \"" + lookFor + "\", found:\n" + candidates.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
import java.util.List;

import static com.github.tomakehurst.wiremock.testsupport.WireMatchers.fileNamed;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class ClasspathFileSourceTest {

Expand Down Expand Up @@ -75,6 +80,17 @@ public void readsBinaryFileFromZip() {
assertThat(contents, containsString("zip"));
}

@Test
public void readsBinaryFileFromZipWithoutMatch() {
classpathFileSource = new ClasspathFileSource("zippeddir");
try {
classpathFileSource.getBinaryFileNamed("thisWillNotBeFound.txt");
fail("Should have thrown exception.");
} catch (Exception e) {
assertThat("Informative error", e.getMessage(), startsWith("Was unable to find entry: \"zippeddir/thisWillNotBeFound.txt\", found:"));
}
}

@Test
public void readsBinaryFileFromFileSystem() {
initForFileSystem();
Expand Down

0 comments on commit 48fcb77

Please sign in to comment.