ZipTransformTest.java

package org.zeroturnaround.zip;
/**
 *    Copyright (C) 2012 ZeroTurnaround LLC <support@zeroturnaround.com>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import junit.framework.TestCase;

import org.zeroturnaround.zip.commons.FileUtils;
import org.zeroturnaround.zip.commons.IOUtils;
import org.zeroturnaround.zip.transform.ByteArrayZipEntryTransformer;
import org.zeroturnaround.zip.transform.FileZipEntryTransformer;
import org.zeroturnaround.zip.transform.StreamZipEntryTransformer;
import org.zeroturnaround.zip.transform.StringZipEntryTransformer;

public class ZipTransformTest extends TestCase {

  public void testZipTransformNotInPlaceButSameLocation() throws IOException {
    //Create dummy file and transformer
    File file = File.createTempFile("temp", null);
    StreamZipEntryTransformer arrayZipEntryTransformer = new StreamZipEntryTransformer() {
      protected void transform(ZipEntry zipEntry, InputStream in, OutputStream out) throws IOException { }
    };
    try {
      ZipUtil.transformEntry(file, "", arrayZipEntryTransformer, file);
      //This line should not be reached.
      assertTrue(false);
    } catch(IllegalArgumentException e){
      //Do nothing, test passed.
    }
  }

  public void testByteArrayTransformer() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {
      // Create the ZIP file
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
      try {
        zos.putNextEntry(new ZipEntry(name));
        zos.write(contents);
        zos.closeEntry();
      }
      finally {
        IOUtils.closeQuietly(zos);
      }

      // Transform the ZIP file
      ZipUtil.transformEntry(file1, name, new ByteArrayZipEntryTransformer() {
        protected byte[] transform(ZipEntry zipEntry, byte[] input) throws IOException {
          String s = new String(input);
          assertEquals(new String(contents), s);
          return s.toUpperCase().getBytes();
        }
      }, file2);

      // Test the ZipUtil
      byte[] actual = ZipUtil.unpackEntry(file2, name);
      assertNotNull(actual);
      assertEquals(new String(contents).toUpperCase(), new String(actual));
    }
    finally {
      FileUtils.deleteQuietly(file1);
      FileUtils.deleteQuietly(file2);
    }
  }

  public void testStreamTransformerIdentity() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {
      // Create the ZIP file
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
      try {
        zos.putNextEntry(new ZipEntry(name));
        zos.write(contents);
        zos.closeEntry();
      }
      finally {
        IOUtils.closeQuietly(zos);
      }

      // Transform the ZIP file
      ZipUtil.transformEntry(file1, name, new StreamZipEntryTransformer() {
        protected void transform(ZipEntry zipEntry, InputStream in, OutputStream out) throws IOException {
          IOUtils.copy(in, out);
        }
      }, file2);

      // Test the ZipUtil
      byte[] actual = ZipUtil.unpackEntry(file2, name);
      assertNotNull(actual);
      assertEquals(new String(contents), new String(actual));
    }
    finally {
      FileUtils.deleteQuietly(file1);
      FileUtils.deleteQuietly(file2);
    }
  }

  public void testStreamTransformer() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();
    final byte[] transformed = "cbs".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {
      // Create the ZIP file
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
      try {
        zos.putNextEntry(new ZipEntry(name));
        zos.write(contents);
        zos.closeEntry();
      }
      finally {
        IOUtils.closeQuietly(zos);
      }

      // Transform the ZIP file
      ZipUtil.transformEntry(file1, name, new StreamZipEntryTransformer() {
        protected void transform(ZipEntry zipEntry, InputStream in, OutputStream out) throws IOException {
          int b;
          while ((b = in.read()) != -1)
            out.write(b + 1);
        }
      }, file2);

      // Test the ZipUtil
      byte[] actual = ZipUtil.unpackEntry(file2, name);
      assertNotNull(actual);
      assertEquals(new String(transformed), new String(actual));
    }
    finally {
      FileUtils.deleteQuietly(file1);
      FileUtils.deleteQuietly(file2);
    }
  }

  public void testByteArrayTransformerInStream() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {
      // Create the ZIP file
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
      try {
        zos.putNextEntry(new ZipEntry(name));
        zos.write(contents);
        zos.closeEntry();
      }
      finally {
        IOUtils.closeQuietly(zos);
      }

      // Transform the ZIP file
      FileInputStream in = null;
      FileOutputStream out = null;
      try {
        in = new FileInputStream(file1);
        out = new FileOutputStream(file2);
        
        ZipUtil.transformEntry(in, name, new ByteArrayZipEntryTransformer() {
          protected byte[] transform(ZipEntry zipEntry, byte[] input) throws IOException {
            String s = new String(input);
            assertEquals(new String(contents), s);
            return s.toUpperCase().getBytes();
          }
        }, out);
      }
      finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
      }

      // Test the ZipUtil
      byte[] actual = ZipUtil.unpackEntry(file2, name);
      assertNotNull(actual);
      assertEquals(new String(contents).toUpperCase(), new String(actual));
    }
    finally {
      FileUtils.deleteQuietly(file1);
      FileUtils.deleteQuietly(file2);
    }
  }
  
  public void testFileZipEntryTransformerInStream() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {
      // Create the ZIP file
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
      try {
        zos.putNextEntry(new ZipEntry(name));
        zos.write(contents);
        zos.closeEntry();
      }
      finally {
        IOUtils.closeQuietly(zos);
      }

      // Transform the ZIP file
      FileInputStream in = null;
      FileOutputStream out = null;
      try {
        in = new FileInputStream(file1);
        out = new FileOutputStream(file2);
        
        ZipUtil.transformEntry(in, name, new FileZipEntryTransformer() {
          protected void transform(ZipEntry zipEntry, File in, File out) throws IOException {
            FileWriter fw = new FileWriter(out);
            fw.write("CAFEBABE");
            fw.close();
          }
        }, out);
      }
      finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
      }

      // Test the ZipUtil
      byte[] actual = ZipUtil.unpackEntry(file2, name);
      assertNotNull(actual);
      assertEquals("CAFEBABE", new String(actual));
    }
    finally {
      FileUtils.deleteQuietly(file1);
      FileUtils.deleteQuietly(file2);
    }
  }
  
  public void testStringZipEntryTransformerInStream() throws IOException {
    final String name = "foo";
    String FILE_CONTENTS = "bar";
    final byte[] contents = FILE_CONTENTS.getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {
      // Create the ZIP file
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
      try {
        zos.putNextEntry(new ZipEntry(name));
        zos.write(contents);
        zos.closeEntry();
      }
      finally {
        IOUtils.closeQuietly(zos);
      }

      // Transform the ZIP file
      FileInputStream in = null;
      FileOutputStream out = null;
      try {
        in = new FileInputStream(file1);
        out = new FileOutputStream(file2);
        
        ZipUtil.transformEntry(in, name, new StringZipEntryTransformer("UTF-8") {
          protected String transform(ZipEntry zipEntry, String input) throws IOException {
            return input.toUpperCase();
          }
        } , out);
      }
      finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
      }

      // Test the ZipUtil
      byte[] actual = ZipUtil.unpackEntry(file2, name);
      assertEquals(FILE_CONTENTS.toUpperCase(), new String(actual));
    }
    finally {
      FileUtils.deleteQuietly(file1);
      FileUtils.deleteQuietly(file2);
    }
  }
}