Merge pull request #497 from dicej/deflateoutputstream

update ZipOutputStreamTest to use write(byte[]) function as well as others
This commit is contained in:
Joshua Warner 2016-09-04 15:07:47 +00:00 committed by GitHub
commit cbcc15bf74

View File

@ -22,8 +22,9 @@ public class ZipOutputStreamTest
private static final String TEST3_CONTENTS = "74 68 69 73 20 69 73 20 61 20 74 65 73 74"; private static final String TEST3_CONTENTS = "74 68 69 73 20 69 73 20 61 20 74 65 73 74";
private static final String TEST4_CONTENTS = "01110100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01110100 01100101 01110011 01110100"; private static final String TEST4_CONTENTS = "01110100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01110100 01100101 01110011 01110100";
private static final String ONE_PARAM_ZIP_PREFIX = "zos1param"; private static final String BYTE_ZIP_PREFIX = "zosByte";
private static final String THREE_PARAM_ZIP_PREFIX = "zos3param"; private static final String ARRAY_ZIP_PREFIX = "zosArray";
private static final String ARRAY_OFFSET_LENGTH_ZIP_PREFIX = "zosArrayOffsetLength";
private static final String ZIP_SUFFIX = ".zip"; private static final String ZIP_SUFFIX = ".zip";
private static final Map<String, String> FILES_CONTENTS; private static final Map<String, String> FILES_CONTENTS;
@ -37,27 +38,49 @@ public class ZipOutputStreamTest
FILES_CONTENTS = Collections.unmodifiableMap(m); FILES_CONTENTS = Collections.unmodifiableMap(m);
} }
private static final boolean USE_ONE_PARAM_WRITE = true; private static enum WriteStyle {
private static final boolean USE_THREE_PARAM_WRITE = false; Byte(ARRAY_ZIP_PREFIX),
Array(ARRAY_ZIP_PREFIX),
ArrayOffsetLength(ARRAY_OFFSET_LENGTH_ZIP_PREFIX);
public final String prefix;
private WriteStyle(String prefix) {
this.prefix = prefix;
}
}
private static byte[] buffer = new byte[1024]; private static byte[] buffer = new byte[1024];
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
public static void main(String[] args) public static void main(String[] args)
throws Exception
{ {
List<File> zipFiles = new ArrayList<File>(2); List<File> zipFiles = new ArrayList<File>(2);
// Test 1-param write function try {
File f1 = createZip(USE_ONE_PARAM_WRITE); // Test byte-at-a-time write function
File f1 = createZip(WriteStyle.Byte);
zipFiles.add(f1); zipFiles.add(f1);
verifyContents(f1.getAbsolutePath()); verifyContents(f1.getAbsolutePath());
// Test 3-param write function // Test arraw write function
File f2 = createZip(USE_THREE_PARAM_WRITE); File f2 = createZip(WriteStyle.Array);
zipFiles.add(f2); zipFiles.add(f2);
verifyContents(f2.getAbsolutePath()); verifyContents(f2.getAbsolutePath());
// Test arraw write function
File f3 = createZip(WriteStyle.ArrayOffsetLength);
zipFiles.add(f3);
verifyContents(f3.getAbsolutePath());
} finally {
// Remove the created zip files // Remove the created zip files
cleanUp(zipFiles); cleanUp(zipFiles);
} }
}
private static File createZip(boolean useOneParam) private static File createZip(WriteStyle writeStyle)
throws Exception
{ {
FileOutputStream outputStream = null; FileOutputStream outputStream = null;
ZipOutputStream zipContents = null; ZipOutputStream zipContents = null;
@ -65,7 +88,7 @@ public class ZipOutputStreamTest
try try
{ {
// Create a temporary zip file for this test // Create a temporary zip file for this test
String prefix = useOneParam ? ONE_PARAM_ZIP_PREFIX : THREE_PARAM_ZIP_PREFIX; String prefix = writeStyle.prefix;
File outputZip = File.createTempFile(prefix, ZIP_SUFFIX); File outputZip = File.createTempFile(prefix, ZIP_SUFFIX);
System.out.println("Created " + outputZip.getAbsolutePath()); System.out.println("Created " + outputZip.getAbsolutePath());
@ -87,18 +110,26 @@ public class ZipOutputStreamTest
byte[] bytesToWrite = contents.getBytes(); byte[] bytesToWrite = contents.getBytes();
if (useOneParam) switch (writeStyle) {
{ case Byte: {
// Use the 1-parameter write method; takes a single byte // Use the 1-parameter write method; takes a single byte
for (int i = 0; i < bytesToWrite.length; i++) for (int i = 0; i < bytesToWrite.length; i++)
{ {
zipContents.write(bytesToWrite[i]); zipContents.write(bytesToWrite[i]);
} }
} } break;
else
{ case Array: {
// Use 3-parameter write method; takes a buffer, offset, and length
zipContents.write(bytesToWrite);
} break;
case ArrayOffsetLength: {
// Use 3-parameter write method; takes a buffer, offset, and length // Use 3-parameter write method; takes a buffer, offset, and length
zipContents.write(bytesToWrite, 0 , bytesToWrite.length); zipContents.write(bytesToWrite, 0 , bytesToWrite.length);
} break;
default: throw new RuntimeException("unexpected write style: " + writeStyle);
} }
// Done with this file // Done with this file
@ -111,27 +142,17 @@ public class ZipOutputStreamTest
System.out.println("Finished " + outputZip.getName() + " in " + ((endTime - startTime) / 1000.0) + " seconds"); System.out.println("Finished " + outputZip.getName() + " in " + ((endTime - startTime) / 1000.0) + " seconds");
return outputZip; return outputZip;
} }
catch (Exception e)
{
throw new RuntimeException(e);
}
finally finally
{
try
{ {
if (zipContents != null) if (zipContents != null)
zipContents.close(); zipContents.close();
if (outputStream != null) if (outputStream != null)
outputStream.close(); outputStream.close();
} }
catch (Exception e)
{
throw new RuntimeException(e);
}
}
} }
private static void verifyContents(String zipName) private static void verifyContents(String zipName)
throws Exception
{ {
System.out.println("Verify " + zipName); System.out.println("Verify " + zipName);
ZipFile zf = null; ZipFile zf = null;
@ -163,36 +184,24 @@ public class ZipOutputStreamTest
reader.close(); reader.close();
// Assert that this file's contents are correct // Assert that this file's contents are correct
assert(contents.equals(FILES_CONTENTS.get(entry.getName()))); expect(contents.equals(FILES_CONTENTS.get(entry.getName())));
} }
zf.close(); zf.close();
// Assert that the zip contained the correct number of files // Assert that the zip contained the correct number of files
assert(numFilesInZip == FILES_CONTENTS.size()); expect(numFilesInZip == FILES_CONTENTS.size());
}
catch (Exception e)
{
throw new RuntimeException(e);
} }
finally finally
{
try
{ {
if (zf != null) if (zf != null)
zf.close(); zf.close();
if (reader != null) if (reader != null)
reader.close(); reader.close();
} }
catch (Exception e)
{
throw new RuntimeException(e);
}
}
} }
private static void cleanUp(List<File> zipFiles) private static void cleanUp(List<File> zipFiles)
{ throws Exception
try
{ {
for (File f : zipFiles) for (File f : zipFiles)
{ {
@ -202,9 +211,4 @@ public class ZipOutputStreamTest
} }
} }
} }
catch (Exception e)
{
throw new RuntimeException(e);
}
}
} }