- Revision
- 1056
- Author
- mauro
- Date
- 2009-02-07 19:13:03 -0600 (Sat, 07 Feb 2009)
Log Message
Increased test coverage.
Modified Paths
- sandbox/jbehave-web/web-io/src/main/java/org/jbehave/web/io/ArchivingFileManager.java
- sandbox/jbehave-web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java
- sandbox/jbehave-web/web-io/src/test/java/org/jbehave/web/io/ArchivingFileManagerTest.java
Diff
Modified: sandbox/jbehave-web/web-io/src/main/java/org/jbehave/web/io/ArchivingFileManager.java (1055 => 1056)
--- sandbox/jbehave-web/web-io/src/main/java/org/jbehave/web/io/ArchivingFileManager.java 2009-02-08 00:48:36 UTC (rev 1055) +++ sandbox/jbehave-web/web-io/src/main/java/org/jbehave/web/io/ArchivingFileManager.java 2009-02-08 01:13:03 UTC (rev 1056) @@ -9,7 +9,7 @@ import java.util.List; import org.apache.commons.fileupload.FileItem; -import org.jbehave.web.io.ZipFileArchiver.FileUnzipFailedException; +import org.jbehave.web.io.ZipFileArchiver.FileUnarchiveFailedException; public class ArchivingFileManager implements FileManager { @@ -40,20 +40,18 @@ } } - private boolean deleteFile(File file) { + private void deleteFile(File file) { if (file.isDirectory()) { // recursively delete children for (String child : file.list()) { - if (!deleteFile(new File(file, child))) { - return false; - } + deleteFile(new File(file, child)); } } if ( archiver.isArchive(file) ){ - // delete the unzipped directory too + // delete the unarchived directory too deleteFile(archiver.unarchivedDir(file)); } - return file.delete(); + file.delete(); } public List<File> write(List<FileItem> fileItems, List<String> errors) { @@ -66,7 +64,7 @@ if (archiver.isArchive(file)) { try { archiver.unarchive(file, directory); - } catch (FileUnzipFailedException e) { + } catch (FileUnarchiveFailedException e) { errors.add(e.getMessage()); } }
Modified: sandbox/jbehave-web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java (1055 => 1056)
--- sandbox/jbehave-web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java 2009-02-08 00:48:36 UTC (rev 1055) +++ sandbox/jbehave-web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java 2009-02-08 01:13:03 UTC (rev 1056) @@ -33,18 +33,14 @@ try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream( archive)); - // Compress the files for (File file : files) { FileInputStream in = new FileInputStream(file); // Add entry to output stream. out.putNextEntry(new ZipEntry(file.getAbsolutePath())); - // Transfer bytes from the file to the ZIP file - int len; - while ((len = in.read(BUFFER)) > 0) { - out.write(BUFFER, 0, len); - } + // copy from file to entry + copy(in, out); // close entry out.closeEntry(); @@ -53,10 +49,18 @@ // close stream out.close(); } catch (Exception e) { - throw new FileZipFailedException(archive, files); + throw new FileArchiveFailedException(archive, files); } } + private void copy(FileInputStream in, ZipOutputStream out) + throws IOException { + int len; + while ((len = in.read(BUFFER)) > 0) { + out.write(BUFFER, 0, len); + } + } + public void unarchive(File archive, File outputDir) { try { ZipFile zipfile = new ZipFile(archive); @@ -65,7 +69,7 @@ unzipEntry(zipfile, entry, outputDir); } } catch (Exception e) { - throw new FileUnzipFailedException(archive, outputDir); + throw new FileUnarchiveFailedException(archive, outputDir); } } @@ -102,18 +106,20 @@ } @SuppressWarnings("serial") - public static final class FileZipFailedException extends RuntimeException { + public static final class FileArchiveFailedException extends + RuntimeException { - public FileZipFailedException(File archive, List<File> files) { + public FileArchiveFailedException(File archive, List<File> files) { super(files.toString() + File.separator + files.toString()); } } @SuppressWarnings("serial") - public static final class FileUnzipFailedException extends RuntimeException { + public static final class FileUnarchiveFailedException extends + RuntimeException { - public FileUnzipFailedException(File archive, File outputDir) { + public FileUnarchiveFailedException(File archive, File outputDir) { super(outputDir.toString() + File.separator + archive.toString()); }
Modified: sandbox/jbehave-web/web-io/src/test/java/org/jbehave/web/io/ArchivingFileManagerTest.java (1055 => 1056)
--- sandbox/jbehave-web/web-io/src/test/java/org/jbehave/web/io/ArchivingFileManagerTest.java 2009-02-08 00:48:36 UTC (rev 1055) +++ sandbox/jbehave-web/web-io/src/test/java/org/jbehave/web/io/ArchivingFileManagerTest.java 2009-02-08 01:13:03 UTC (rev 1056) @@ -61,24 +61,93 @@ assertEquals(asList(zip, file1, file2), manager.list()); manager.delete(asList(file1.getAbsolutePath())); assertEquals(asList(zip, file2), manager.list()); + manager.delete(asList(zip.getAbsolutePath())); + assertEquals(asList(file2), manager.list()); } @Test - public void canWriteZippedFileItems() throws Exception { + public void canWriteFileItems() throws Exception { FileManager manager = new ArchivingFileManager(archiver, upload); List<String> errors = new ArrayList<String>(); - final FileItem zipFileItem = mockery.mock(FileItem.class); + final FileItem file2FileItem = mockery.mock(FileItem.class, "file2"); + final FileItem zipFileItem = mockery.mock(FileItem.class, "zip"); mockery.checking(new Expectations() { { allowing(zipFileItem).getName(); will(returnValue(zip.getName())); one(zipFileItem).write(zip); + allowing(file2FileItem).getName(); + will(returnValue(file2.getName())); + one(file2FileItem).write(file2); } }); - zip.delete(); // ensure it does not exist - manager.write(asList(zipFileItem), errors); + // ensure files do not exists + file2.delete(); + zip.delete(); + manager.write(asList(file2FileItem, zipFileItem), errors); } + + @Test + public void canIgnoreWritingFileItemsWithBlankNames() throws Exception { + FileManager manager = new ArchivingFileManager(archiver, upload); + List<String> errors = new ArrayList<String>(); + final FileItem file2FileItem = mockery.mock(FileItem.class, "file2"); + final FileItem zipFileItem = mockery.mock(FileItem.class, "zip"); + mockery.checking(new Expectations() { + { + allowing(zipFileItem).getName(); + will(returnValue("")); + allowing(file2FileItem).getName(); + will(returnValue("")); + } + }); + manager.write(asList(file2FileItem, zipFileItem), errors); + } + + @Test + public void cannotOverWriteExistingFileItems() throws Exception { + FileManager manager = new ArchivingFileManager(archiver, upload); + List<String> errors = new ArrayList<String>(); + final FileItem file2FileItem = mockery.mock(FileItem.class, "file2"); + final FileItem zipFileItem = mockery.mock(FileItem.class, "zip"); + mockery.checking(new Expectations() { + { + allowing(zipFileItem).getName(); + will(returnValue(zip.getName())); + allowing(file2FileItem).getName(); + will(returnValue(file2.getName())); + } + }); + manager.write(asList(file2FileItem, zipFileItem), errors); + assertEquals(2, errors.size()); + } + + @Test + public void cannotWriteFileItemsThatFail() throws Exception { + FileManager manager = new ArchivingFileManager(archiver, upload); + List<String> errors = new ArrayList<String>(); + final FileItem file2FileItem = mockery.mock(FileItem.class, "file2"); + final FileItem zipFileItem = mockery.mock(FileItem.class, "zip"); + mockery.checking(new Expectations() { + { + allowing(zipFileItem).getName(); + will(returnValue(zip.getName())); + one(zipFileItem).write(zip); + will(throwException(new IOException("zip write failed"))); + allowing(file2FileItem).getName(); + will(returnValue(file2.getName())); + one(file2FileItem).write(file2); + will(throwException(new IOException("file2 write failed"))); + } + }); + // ensure files do not exists + file2.delete(); + zip.delete(); + manager.write(asList(file2FileItem, zipFileItem), errors); + assertEquals(2, errors.size()); + } + private File create(String path) throws IOException { File file = new File(upload, path); file.createNewFile(); @@ -86,9 +155,11 @@ } private File createDir(String path) throws IOException { - File file = new File(upload, path); - file.mkdirs(); - return file; + File dir = new File(upload, path); + dir.mkdirs(); + File child = new File(dir, "child1"); + child.createNewFile(); + return dir; } } \ No newline at end of file
To unsubscribe from this list please visit: