/**
 * LOGBack: the generic, reliable, fast and flexible logging framework.
 * 
 * Copyright (C) 1999-2006, QOS.ch
 * 
 * This library is free software, you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation.
 */
package ch.qos.logback.core.rolling.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.ContextBase;
import ch.qos.logback.core.util.Compare;
import ch.qos.logback.core.util.Constants;
import ch.qos.logback.core.util.StatusPrinter;


/**
 * @author Ceki Gulcu
 */
public class CompressTest extends TestCase {

  Compress compress;
  Context context = new ContextBase();
  
  public CompressTest(String arg0) {
    super(arg0);
    compress = new Compress();
    compress.setContext(context);
  }

  public void setUp() throws IOException {
	  //Copy source files
	  //Delete output files
	  {
		  File source = new File(Constants.TEST_DIR_PREFIX + "input/compress1.copy");
		  File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress1.txt");
		  copy(source, dest);
		  File target = new File(Constants.TEST_DIR_PREFIX + "output/compress1.txt.gz");
		  target.mkdirs();
		  target.delete();
	  }
	  {
		  File source = new File(Constants.TEST_DIR_PREFIX + "input/compress2.copy");
		  File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress2.txt");
		  copy(source, dest);
		  File target = new File(Constants.TEST_DIR_PREFIX + "output/compress2.txt.gz");
		  target.mkdirs();
		  target.delete();
	  }
	  {
		  File source = new File(Constants.TEST_DIR_PREFIX + "input/compress3.copy");
		  File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress3.txt");
		  copy(source, dest);
		  File target = new File(Constants.TEST_DIR_PREFIX + "output/compress3.txt.zip");
		  target.mkdirs();
		  target.delete();
	  }
  }

  public void tearDown() {
  }

  public void test1() throws Exception {
	compress.GZCompress(Constants.TEST_DIR_PREFIX + "input/compress1.txt", Constants.TEST_DIR_PREFIX + "output/compress1.txt.gz");
  
	StatusPrinter.print(context.getStatusManager());
	assertEquals(0, context.getStatusManager().getCount());
	assertTrue(Compare.gzCompare(Constants.TEST_DIR_PREFIX + "output/compress1.txt.gz",
			Constants.TEST_DIR_PREFIX + "witness/compress1.txt.gz"));
  }

  public void test2() throws Exception {
	compress.GZCompress(Constants.TEST_DIR_PREFIX + "input/compress2.txt", Constants.TEST_DIR_PREFIX + "output/compress2.txt");
    
	StatusPrinter.print(context.getStatusManager());
	assertEquals(0, context.getStatusManager().getCount());
    assertTrue(Compare.gzCompare(Constants.TEST_DIR_PREFIX + "output/compress2.txt.gz",
    		Constants.TEST_DIR_PREFIX + "witness/compress2.txt.gz"));
  }

  public void test3() throws Exception {
    compress.ZIPCompress(Constants.TEST_DIR_PREFIX + "input/compress3.txt", Constants.TEST_DIR_PREFIX + "output/compress3.txt");
	
	StatusPrinter.print(context.getStatusManager());
	assertEquals(0, context.getStatusManager().getCount());
    // assertTrue(Compare.compare("output/compress3.txt.zip",
    // "witness/compress3.txt.zip"));
  }

  public static Test suite() {
    TestSuite suite = new TestSuite();
    suite.addTest(new CompressTest("test1"));
    suite.addTest(new CompressTest("test2"));
    suite.addTest(new CompressTest("test3"));
    return suite;
  }
  
  private void copy(File src, File dst) throws IOException {
      InputStream in = new FileInputStream(src);
      OutputStream out = new FileOutputStream(dst);
      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
          out.write(buf, 0, len);
      }
      in.close();
      out.close();
  }
  
  
}
