Updated Branches: refs/heads/master ec7a022bd -> df71f0007 refs/heads/sandbox/resourcefinder af8492c47 -> a10b71187
WICKET-4617 fix test for jenkins Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/df71f000 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/df71f000 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/df71f000 Branch: refs/heads/master Commit: df71f0007882f1a5d77135492a03f30cf30c5d62 Parents: ec7a022 Author: Carl-Eric Menzel <cmen...@wicketbuch.de> Authored: Thu Jun 28 10:13:03 2012 +0200 Committer: Carl-Eric Menzel <cmen...@wicketbuch.de> Committed: Thu Jun 28 10:19:51 2012 +0200 ---------------------------------------------------------------------- .../org/apache/wicket/util/resource/PathTest.java | 89 ++++++++++++--- 1 files changed, 72 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/df71f000/wicket-core/src/test/java/org/apache/wicket/util/resource/PathTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/util/resource/PathTest.java b/wicket-core/src/test/java/org/apache/wicket/util/resource/PathTest.java index e3ba41e..9025853 100644 --- a/wicket-core/src/test/java/org/apache/wicket/util/resource/PathTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/util/resource/PathTest.java @@ -16,40 +16,95 @@ */ package org.apache.wicket.util.resource; -import static org.apache.wicket.util.resource.ResourceStreamLocatorTest.*; - import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.Arrays; import org.apache.wicket.WicketTestCase; +import org.apache.wicket.util.file.Folder; import org.apache.wicket.util.file.Path; import org.junit.Test; public class PathTest extends WicketTestCase { - private static final String PACKAGE_PATH = PathTest.class.getPackage() - .getName() - .replace('.', File.separatorChar); - private static final String CLASSPATH_ROOT = PathTest.class.getResource("/").getFile(); - - private static final String FILENAME = ResourceStreamLocatorTest.class.getSimpleName() + ".txt"; - @Test public void loadFromRootUsingSubpathInFilename() throws Exception { - Path path = new Path(); - path.add(CLASSPATH_ROOT); - IResourceStream rs = path.find(PathTest.class, PACKAGE_PATH + File.separatorChar + FILENAME); + final String contents = PathTest.class.getName() + ": loaded from root"; + final File file = createTempFile(contents); + final File root = findRoot(file); + final Path path = new Path(new Folder(root.getCanonicalPath())); + IResourceStream rs = path.find(PathTest.class, file.getCanonicalPath()); assertNotNull(rs); - assertEquals(FILENAME, getFilename(rs)); + assertContents(contents, rs); + } + + public static void assertContents(String expectedContents, IResourceStream rs) + throws ResourceStreamNotFoundException, IOException + { + InputStream in = rs.getInputStream(); + try + { + final byte[] expectedBytes = expectedContents.getBytes(Charset.defaultCharset()); + final int expectedLength = expectedBytes.length; + byte[] buf = new byte[expectedLength * 2]; + int read = in.read(buf, 0, buf.length); + assertEquals("contents do not match", expectedLength, read); + byte[] buf2 = new byte[expectedLength]; + System.arraycopy(buf, 0, buf2, 0, expectedLength); + assertTrue("contents do not match", Arrays.equals(expectedBytes, buf2)); + } + finally + { + in.close(); + } } @Test public void loadFilenameFromPath() throws Exception { - Path path = new Path(); - path.add(CLASSPATH_ROOT + File.separatorChar + PACKAGE_PATH); - IResourceStream rs = path.find(PathTest.class, FILENAME); + final String contents = PathTest.class.getName() + ": loaded from prefix"; + final File file = createTempFile(contents); + final File parent = file.getParentFile(); + final Path path = new Path(new Folder(parent.getCanonicalPath())); + IResourceStream rs = path.find(PathTest.class, file.getName()); assertNotNull(rs); - assertEquals(FILENAME, getFilename(rs)); + assertContents(contents, rs); + } + + public static File createTempFile(String contents) throws IOException + { + FileOutputStream out = null; + try + { + File tmp = File.createTempFile("temp", "temp"); + tmp.deleteOnExit(); + out = new FileOutputStream(tmp); + out.write(contents.getBytes(Charset.defaultCharset())); + return tmp; + } + finally + { + if (out != null) + { + out.close(); + } + } + } + + public static File findRoot(File file) + { + final File parent = file.getParentFile(); + if (parent == null) + { + return file; + } + else + { + return findRoot(parent); + } } }