Back to Repositories

Testing ZIP File Operations Integration in AndroidUtilCode

This test suite validates the ZipUtils functionality in the AndroidUtilCode library, covering essential ZIP file operations like compression, decompression, and metadata handling. The tests ensure reliable file compression and extraction capabilities while handling different character encodings and file structures.

Test Coverage Overview

The test suite provides comprehensive coverage of ZIP file operations including:

  • Single file compression with custom comments
  • Multiple files/directories compression
  • File extraction with optional keyword filtering
  • ZIP metadata retrieval including file paths and comments
  • Character encoding handling (UTF-8 support for Chinese characters)

Implementation Analysis

The testing approach utilizes JUnit 4 framework with a systematic setup-execute-verify pattern. Each test method focuses on a specific ZIP operation, with proper test isolation through @Before and @After annotations. The implementation leverages TestConfig paths and FileUtils for consistent test environment management.

Technical Details

Testing infrastructure includes:

  • JUnit 4 test framework
  • Custom BaseTest class extension
  • TestConfig for path configurations
  • FileUtils for directory management
  • Temporary directory handling for test isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test setup and cleanup using @Before and @After
  • Isolated test cases for each functionality
  • Clear test method naming reflecting purpose
  • Verification of operation success using assertions
  • Comprehensive error handling and resource cleanup

blankj/androidutilcode

lib/utilcode/src/test/java/com/blankj/utilcode/util/ZipUtilsTest.java

            
package com.blankj.utilcode.util;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static com.blankj.utilcode.util.TestConfig.PATH_TEMP;
import static com.blankj.utilcode.util.TestConfig.PATH_ZIP;
import static junit.framework.TestCase.assertTrue;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/09/10
 *     desc  : test ZipUtils
 * </pre>
 */
public class ZipUtilsTest extends BaseTest {

    private String zipFile  = PATH_TEMP + "zipFile.zip";
    private String zipFiles = PATH_TEMP + "zipFiles.zip";

    @Before
    public void setUp() throws Exception {
        FileUtils.createOrExistsDir(PATH_TEMP);
        assertTrue(ZipUtils.zipFile(PATH_ZIP, zipFile, "测试zip"));
    }

    @Test
    public void zipFiles() throws Exception {
        List<String> files = new ArrayList<>();
        files.add(PATH_ZIP + "test.txt");
        files.add(PATH_ZIP);
        files.add(PATH_ZIP + "testDir");
        assertTrue(ZipUtils.zipFiles(files, zipFiles));
    }

    @Test
    public void unzipFile() throws Exception {
        System.out.println(ZipUtils.unzipFile(zipFile, PATH_TEMP));
    }

    @Test
    public void unzipFileByKeyword() throws Exception {
        System.out.println((ZipUtils.unzipFileByKeyword(zipFile, PATH_TEMP, null)).toString());
    }

    @Test
    public void getFilesPath() throws Exception {
        System.out.println(ZipUtils.getFilesPath(zipFile));
    }

    @Test
    public void getComments() throws Exception {
        System.out.println(ZipUtils.getComments(zipFile));
    }

    @After
    public void tearDown() {
        FileUtils.deleteAllInDir(PATH_TEMP);
    }
}