Title: [1348] trunk/web/web-io/src/test/java/org/jbehave/web/io: Refactored relativeTo() impl to use commons-io FilenameUtils.
Revision
1348
Author
mauro
Date
2009-10-17 06:10:20 -0500 (Sat, 17 Oct 2009)

Log Message

Refactored relativeTo() impl to use commons-io FilenameUtils.

Modified Paths


Diff

Modified: trunk/web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java (1347 => 1348)

--- trunk/web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java	2009-10-15 21:18:43 UTC (rev 1347)
+++ trunk/web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java	2009-10-17 11:10:20 UTC (rev 1348)
@@ -1,7 +1,8 @@
 package org.jbehave.web.io;
 
+import static org.apache.commons.io.FilenameUtils.separatorsToUnix;
+import static org.apache.commons.lang.StringUtils.remove;
 import static org.apache.commons.lang.StringUtils.removeEnd;
-import static org.apache.commons.lang.StringUtils.removeStart;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -24,6 +25,7 @@
  */
 public class ZipFileArchiver implements FileArchiver {
 
+	private static final String UNIX_SEPARATOR = "/";
 	private static final String ZIP = "zip";
 	private static final String ZIP_EXT = ".zip";
 	private ArchiveStreamFactory factory = new ArchiveStreamFactory();
@@ -51,12 +53,15 @@
 	}
 
 	public File relativeTo(File file, File directory) {
-		return new File(removeStart(normalisePath(file.getPath()), normalisePath(directory.getPath()) + "/"));
+		String filePath = separatorsToUnix(file.getPath());
+		String directoryPath = separatorsToUnix(directory.getPath());
+		if ( !directoryPath.endsWith(UNIX_SEPARATOR) ){ 
+			// ensure directory has a trailing separator 
+			// that will be removed from the full file path
+			directoryPath = directoryPath + UNIX_SEPARATOR;
+		}
+		return new File(remove(filePath, directoryPath));
 	}
-
-	private String normalisePath(String path) {
-		return path.replace('\\', '/');
-	}
 	
 	private void zipEntry(ZipArchiveEntry entry, ArchiveOutputStream out,
 			File file) throws IOException, FileNotFoundException {

Modified: trunk/web/web-io/src/test/java/org/jbehave/web/io/ZipFileArchiverTest.java (1347 => 1348)

--- trunk/web/web-io/src/test/java/org/jbehave/web/io/ZipFileArchiverTest.java	2009-10-15 21:18:43 UTC (rev 1347)
+++ trunk/web/web-io/src/test/java/org/jbehave/web/io/ZipFileArchiverTest.java	2009-10-17 11:10:20 UTC (rev 1348)
@@ -1,6 +1,7 @@
 package org.jbehave.web.io;
 
 import static java.util.Arrays.asList;
+import static org.apache.commons.io.FilenameUtils.separatorsToUnix;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
@@ -46,11 +47,23 @@
 				"archive/subdir1/subfile1.txt"));
 	}
 
-	private void clearDir(File dir) {
-		dir.delete();
-		dir.mkdir();
+	@Test
+	public void canResolveFileRelativeToDirectoryUsingUnixSeparators(){
+		assertRelativeFileUsesUnixSeparators("target/dir", "archive/file.txt");
+		assertRelativeFileUsesUnixSeparators("/tmp/dir", "archive/file.txt");
+		assertRelativeFileUsesUnixSeparators("\\\\UNC\\share\\dir", "archive\\file.txt");
+		assertRelativeFileUsesUnixSeparators("C:\\Documents and Settings\\user\\dir", "archive\\file.txt");
+		assertRelativeFileUsesUnixSeparators(System.getProperty("java.io.tmpdir"), "archive\\file.txt");
+		assertRelativeFileUsesUnixSeparators(System.getProperty("java.io.tmpdir"), "archive/file.txt");
 	}
 
+	private void assertRelativeFileUsesUnixSeparators(String directoryPath, String relativePath) {
+		String unixPath = separatorsToUnix(relativePath);
+		File directory = new File(directoryPath);
+		File file = new File(directoryPath +"/"+ relativePath);		
+		assertEquals(new File(unixPath), archiver.relativeTo(file, directory));
+	}
+	
 	@Test
 	public void canListFileContentOfUnarchiveZip() throws IOException {
 		File zip = resourceFile("archive.zip");
@@ -80,8 +93,13 @@
 		}
 	}
 
+	private void clearDir(File dir) {
+		dir.delete();
+		dir.mkdir();
+	}
+
 	private File resourceFile(String path) {
 		return new File(getClass().getClassLoader().getResource(path).getFile());
 	}
-
+	
 }
\ No newline at end of file


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to