2017-12-05 3 views
0

파일 압축 방법을 구현했습니다. 이 방법은 하위 폴더를 압축하여 기존 zip 파일을 덮어 쓸 수 있습니다. 이제이 메서드에 대한 단위 테스트를 만들고 싶습니다. 그래서 내가 덮어 쓰기를 테스트 할 수있는 몇 가지 내용으로 기본 압축 파일로 시작 등폴더의 JUnit 테스트 압축

소스 파일 설정하는 나의 방법 :

@Before 
public void setUp() throws IOException, URISyntaxException { 

    util = new ZipFileUtil(); 

    parent = folder.newFolder("Parent"); 
    File subfolder = folder.newFolder("Parent", "Subfolder"); 
    File sub2 = folder.newFolder("Parent", "Subfolder", "Sub2"); 

    File f1 = File.createTempFile("file1", ".txt", parent); 
    File f2 = File.createTempFile("file2", ".txt", parent); 
    File f3 = File.createTempFile("file3", ".txt", parent); 

    File f4 = File.createTempFile("file4", ".txt", subfolder); 
    File f5 = File.createTempFile("file5", ".txt", subfolder); 

    File f6 = File.createTempFile("file6", ".txt", sub2); 
    File f7 = File.createTempFile("file7", ".txt", sub2); 

    File f8 = File.createTempFile("test", ".txt", sub2); 

    File f9 = File.createTempFile("test1", ".txt", parent); 

    files[0] = f1; 
    files[1] = f2; 
    files[2] = f3; 
    files[3] = f4; 
    files[4] = f5; 
    files[5] = f6; 
    files[6] = f7; 
    files[7] = f8; 
    files[8] = f9; 

    File file = new File(getClass().getClassLoader().getResource("test.zip").toURI()); 

    Files.copy(file.toPath(), Paths.get(folder.getRoot().toString(), "test.zip"), 
      StandardCopyOption.REPLACE_EXISTING); 
} 

그리고 내 시험 :

@Test 
public void testOverwriteFilesInExistingZip() throws IOException { 
    util.setZipNameGenerator(new ZipNamingStrategy() { 

     @Override 
     public String getName() { 
      // TODO Auto-generated method stub 
      return "test"; 
     } 

    }); 
    util.setOverwriteFilesInExistingZip(true); 
    util.setSourceFolder(parent); 
    util.setDestinationFolder(folder.getRoot()); 
    util.generateZip(new FilenameFilter() { 

     @Override 
     public boolean accept(File dir, String name) { 
      return true; 

     } 
    }); 
    FileSystem fs = FileSystems.newFileSystem(Paths.get(folder.getRoot().getAbsolutePath(), "test.zip"), null); 

    assertEquals("/" + files[0].getName(), 
      fs.getPath("/", parent.toPath().relativize(files[0].toPath()).toString()).toString()); 
    assertEquals("/" + files[1].getName(), 
      fs.getPath("/", parent.toPath().relativize(files[1].toPath()).toString()).toString()); 
    assertEquals("/" + files[2].getName(), 
      fs.getPath("/", parent.toPath().relativize(files[2].toPath()).toString()).toString()); 
    assertEquals(true, 
      new File(fs.getPath("/", parent.toPath().relativize(files[8].toPath()).toString()).toString()).exists()); 

    assertEquals("/file.txt", fs.getPath("/", "file.txt").toString()); 
    assertEquals("/New Text Document.txt", fs.getPath("/", "New Text Document.txt").toString()); 
    assertEquals("/test.txt", fs.getPath("/", "test.txt").toString()); 
    assertEquals("/test1.txt", fs.getPath("/", "test1.txt").toString()); 

    assertEquals("/Subfolder/file.txt", fs.getPath("/", "test1.txt").toString()); 
    assertEquals("/Subfolder/desktop.ini", fs.getPath("/", "test1.txt").toString()); 
    assertEquals("/Subfolder/test.txt", fs.getPath("/", "test1.txt").toString()); 
    assertEquals("/Subfolder/New Text Document.txt", fs.getPath("/", "test1.txt").toString()); 

    assertEquals(15, util.getProcessedFiles()); 

} 

을 zip 파일이 올바른 구조 (파일이 파일에 존재해야 함)가 있는지 제대로 확인하는 방법에 대해서는 다소 분실했습니다. 현재 나는 경로를 확인하고 있지만 이것은 너무 잘못되어서 알고 있습니다. 누구든지 이런 종류의 방법을 적절하게 테스트하는 방법에 대해 나에게 계몽 수 있습니까?

+0

이것은 약간 분명하지 않습니다. 압축을 풀고 내용을 확인할 수 있습니까? –

+0

@OliverCharlesworth 분명히 할 수 있겠지만, 아마도 경로가 엉망이 될 것이고 나는 ** ** ** zip 파일 안에 있는지 확인하고 싶을 것입니다. – XtremeBaumer

+0

'ZipFileUtil'이 당신이 작성한 클래스가 아니라는 것이 맞습니까? –

답변

1

이 zip 파일 내에 존재하는지 확인하려면 Files.exist(path)을 사용할 수 있습니다.

실제 내용을 확인하려면 다른 임시 디렉토리에 압축을 해제하거나 Java에 read directly from the zip file으로 문의하십시오.

+0

우편 번호 (파일도 실제로 존재해야 함) 내에있는 모든 파일 경로를 얻을 수있는 방법을 제공 할 수 있다면 답변을 수락합니다. – XtremeBaumer

+0

@ XtremeBaumer - [FileVisitor'] (https://docs.oracle.com/javase/tutorial/essential/io/walk.html) (이 답변을 추가 할 수있어서 행복합니다 :)를 사용할 수는 있지만, 그렇지 않습니다. 이것이 도움이 될만한 이유인지 - 파일이 있어야한다는 것을 이미 알고 있으므로 직접 존재 여부를 확인할 수 있습니다. –

+0

필자는 이미 filetree에 대해 생각해 봤지만 여러 번 호출되는 하나의 메서드 내에 모든 assert를 압축 할 수는 없습니다 (일부는 항상 실패합니다). 내 "최고의"촬영은 지금 어떻게 든 내 'FileSystem'의 경로를 얻는 것입니다 (단지 동일한 파일에 액세스해야합니다). 그러나 지금은 그렇게 할 방법을 찾을 수 없습니다 (매개 변수 i를 사용하는 것을 제외하고는 처음에는'FileSystem'을 만드는 데 사용됩니다) – XtremeBaumer