Question

I have the following code for adding to/extracting from Zip. I'm trying to refactor this to make it test-ready. Can someone provide pointers on how I can accomplish this? Aside: I'm using Moq as my mock framework and MSTest as my Unit Testing tool

private const long BufferSize = 4096;

public static void ExtractZip(string zipFilename, string folder) {
  using (var zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) {
    foreach (var part in zip.GetParts()) {
      using (var reader = new StreamReader(part.GetStream(FileMode.Open, FileAccess.Read))) {
        using (var writer = new FileStream(folder + "\\" + Path.GetFileName(part.Uri.OriginalString),
                                           FileMode.Create, FileAccess.Write)) {
          var buffer = System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd());
          writer.Write(buffer, 0, buffer.Length);
        }
      }
    }
  }
}

public static void AddFileToZip(string zipFilename, string fileToAdd) {
  using (var zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) {
    var destFilename = ".\\" + Path.GetFileName(fileToAdd);
    var uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
    if (zip.PartExists(uri)) {
      zip.DeletePart(uri);
    }
    var part = zip.CreatePart(uri, "", CompressionOption.Normal);
    using (var fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read)) {
      using (var dest = part.GetStream()) {
        CopyStream(fileStream, dest);
      }
    }
  }
}

Thanks in advance.

Was it helpful?

Solution

I would make these two static methods (ExtractZip and AddFileToZip) instance methods, and put it into an interface:

public interface IZipper
{
  void ExtractZip(string zipFilename, string folder);
  void AddFileToZip(string zipFilename, string fileToAdd);
}

public class Zipper : IZipper
{
  public void ExtractZip(string zipFilename, string folder)
  { 
    //...
  }

  void AddFileToZip(string zipFilename, string fileToAdd)
  {
    //...
  }
}


// client code
class Foo
{
  private IZipper myZipper;
  // gets an instance of the zipper (injection), but implements only 
  // against the interface. Allows mocks on the IZipper interface.
  public Foo(IZipper zipper)
  {
    myZipper = zipper;
  }

}

Client code is now easy to test.

What about the Zipper class?

  • Consider if it is worth to test anyway.
  • In our project, we distinguish between unit tests (isolated) and integration tests, where it is possible to use the database or the file system. You could declare it as an "file system integration test". Of course, only the test target folder is allowed to be used. This shouldn't make any problems.
  • Consider to move the file operations out, and only work with streams. Then you can easily test the zipping on memory streams. The file operations still need to be somewhere and aren't tested.

OTHER TIPS

Abstract away the creation of FileStreams behind IStreamProvider and pass it to AddFileToZip and ExtractZip. You'll have to abstract filesystem operations unless you're willing to do disk IO while doing unit tests.

I'm pasting the final code here as it might help someone and also allow me to get feedback. Thanks to Stefan for pointing me in the right direction.

/// <summary>
/// The commented methods are marked for future
/// </summary>
public interface IZipper
{
    //void Create();
    void ExtractAll();
    void ExtractAll(string folder);
    //void Extract(string fileName);
    void AddFile(string fileName);
    //void DeleteFile(string fileName);
}
public interface IZipStreamProvider
{
    Stream GetStream(string fileName);
}

public class ZipStreamProvider : IZipStreamProvider
{
    public Stream GetStream(string fileName)
    {
        //Create a read/writable file
        return new FileStream(fileName, FileMode.Create);
    }
}

public class Zipper : IZipper
{
    private const long BufferSize = 4096;
    public string ZipFileName { get; set;}

    //seam.. to use property injection
    private IZipStreamProvider ZipStreamProvider { get; set;}

    public Zipper(string zipFilename)
    {
        ZipFileName = zipFilename;
        //By default, write to file
        ZipStreamProvider = new ZipStreamProvider();
    }

    public void ExtractAll()
    {
        ExtractAll(Environment.CurrentDirectory);
    }

    public void ExtractAll(string folder)
    {
        using (var zip = System.IO.Packaging.Package.Open(ZipStreamProvider.GetStream(ZipFileName)))
        {
            foreach (var part in zip.GetParts())
            {
                using (var reader = new StreamReader(part.GetStream(FileMode.Open, FileAccess.Read)))
                {
                    using (var writer = ZipStreamProvider.GetStream(folder + "\\" + Path.GetFileName(part.Uri.OriginalString)))
                    {
                        var buffer = System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd());
                        writer.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }
    }

    public void AddFile(string fileToAdd)
    {
        using (var zip = System.IO.Packaging.Package.Open(ZipFileName, FileMode.OpenOrCreate))
        {
            var destFilename = ".\\" + Path.GetFileName(fileToAdd);
            var uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
            if (zip.PartExists(uri))
            {
                zip.DeletePart(uri);
            }
            var part = zip.CreatePart(uri, "", CompressionOption.Normal);
            using (var fileStream = ZipStreamProvider.GetStream(fileToAdd))
            {
                using (var dest = part.GetStream())
                {
                    CopyStream(fileStream, dest);
                }
            }
        }
    }

    private long CopyStream(Stream inputStream, Stream outputStream)
    {
        var bufferSize = inputStream.Length < BufferSize ? inputStream.Length : BufferSize;
        var buffer = new byte[bufferSize];
        int bytesRead;
        var bytesWritten = 0L;
        while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            outputStream.Write(buffer, 0, bytesRead);
            bytesWritten += bufferSize;
        }

        return bytesWritten;
    }
}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top