blob: 8694c4a28b5d0c1b425cf0b1bb5b23e9994cdbc0 [file] [log] [blame]
/*
* Copyright 2013-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.facebook.buck.zip;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.facebook.buck.testutil.Zip;
import com.google.common.collect.ImmutableSet;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class UnzipStepTest {
private final byte[] DUMMY_FILE_CONTENTS = "BUCK Unzip Test String!\nNihao\n".getBytes();
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();
private File zipFile;
@Before
public void setUp() throws Exception {
zipFile = new File(tmpFolder.getRoot(), "tmp.zip");
try (Zip zip = new Zip(zipFile, true)) {
zip.add("1.bin", DUMMY_FILE_CONTENTS);
zip.add("subdir/2.bin", DUMMY_FILE_CONTENTS);
zip.addDir("emptydir");
}
}
@Test
public void testExtractZipFile() throws IOException {
ImmutableSet<String> filesToExtract = ImmutableSet.of();
File extractFolder = tmpFolder.newFolder();
UnzipStep.extractZipFile(zipFile.getAbsolutePath(),
extractFolder.getAbsolutePath(),
filesToExtract,
false);
assertTrue(new File(extractFolder.getAbsolutePath() + "/1.bin").exists());
File bin2 = new File(extractFolder.getAbsolutePath() + "/subdir/2.bin");
assertTrue(bin2.exists());
assertTrue(new File(extractFolder.getAbsolutePath() + "/emptydir").isDirectory());
try (FileInputStream input = new FileInputStream(bin2)) {
byte[] buffer = new byte[DUMMY_FILE_CONTENTS.length];
int bytesRead = input.read(buffer, 0, DUMMY_FILE_CONTENTS.length);
assertEquals(DUMMY_FILE_CONTENTS.length, bytesRead);
for (int i = 0; i < DUMMY_FILE_CONTENTS.length; i++) {
assertEquals(DUMMY_FILE_CONTENTS[i], buffer[i]);
}
}
}
}