1

フォルダ構成はこちら

コンソール出力はこちら

以下の 2 つのメソッドのテスト クラスを書きたいと思います。

package jfe;

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

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;

public class JThreadFile {

    /**
     * uncompresses .tar file
     * @param in
     * @param out
     * @throws IOException
     */
    public static void decompressTar(String in, File out) throws IOException {
        try (TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(in))){
            TarArchiveEntry entry;
            while ((entry = tin.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }
                File curfile = new File(out, entry.getName());
                File parent = curfile.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }
                IOUtils.copy(tin, new FileOutputStream(curfile));
            }
        }
    }

    /**
     * uncompresses .7z file
     * @param in
     * @param destination
     * @throws IOException
     */
    public static void decompressSevenz(String in, File destination) throws IOException {
        //@SuppressWarnings("resource")
        SevenZFile sevenZFile = new SevenZFile(new File(in));
        SevenZArchiveEntry entry;
        while ((entry = sevenZFile.getNextEntry()) != null){
            if (entry.isDirectory()){
                continue;
            }
            File curfile = new File(destination, entry.getName());
            File parent = curfile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(curfile);
            byte[] content = new byte[(int) entry.getSize()];
            sevenZFile.read(content, 0, content.length);
            out.write(content);
            out.close();
        }
        sevenZFile.close();
    }

}

私は testNG を使用してフォルダーから読み取りを試み、特定の拡張子 (.tar および .7z) のフォルダーをフィルター処理して、それらのファイルを uncompress メソッドにフィードし、AssertEquals を使用して結果を actualOutput フォルダーと比較します。フォルダーからファイル名を読み取ることはできますが (コンソール出力を参照)、それらを decompressTar(String in, File out) にフィードすることはできません。これは、「結果」が文字列配列であり、文字列が必要だからですか? TestNG の DataProvider がデータをどのように処理するかはわかりません。どんな助けでもいただければ幸いです:)ありがとう:)

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class JThreadFileTest {

    protected static final File ACT_INPUT = new File("c:\\Test\\TestInput\\"); //input directory
    protected static final File EXP_OUTPUT = new File("c:\\Test\\ExpectedOutput\\"); //expected output directory
    protected static final File TEST_OUTPUT = new File("c:\\Test\\TestOutput\\");

    @DataProvider(name = "tarJobs")
    public Object[] getTarJobs() {

        //1
        String[] tarFiles = ACT_INPUT.list(new FilenameFilter()
        {
          public boolean accept(File dir, String name)
          {
            return name.endsWith(".tar");
          }
        });

        //2
        Object[] result = new Object[tarFiles.length];

        int i = 0;
        for (String filename : tarFiles) {
            result[i] = filename;
            i++;
        }
        return result;


    }

    @Test(dataProvider = "tarJobs")
    public void testTar(String result) throws IOException {
        System.out.println("Running test" + result);
        --> JThreadFile.decompressTar(result, TEST_OUTPUT);
        Assert.assertEquals(TEST_OUTPUT, EXP_OUTPUT);
    }

}
4

0 に答える 0