Skip to content

Commit

Permalink
Add zip file validation for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stepio committed Jan 16, 2016
1 parent 9e67f69 commit 8ba5cf6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (C) 2015 Patrice Brend'amour <[email protected]>
*
* 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 de.brendamour.jpasskit.signing;

import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;

/**
* @author stepio
*/
public class AssertZip {

private static final Logger LOGGER = LoggerFactory.getLogger(AssertZip.class);
private static final String ERROR_BAD_ZIP = "Failed to open zip file";

public static void assertValid(final File file) {
if (!isValid(file)) {
Assert.fail(ERROR_BAD_ZIP);
}
}

private static boolean isValid(final File file) {
try (ZipFile zip = new ZipFile(file)) {
return true;
} catch (IOException e) {
LOGGER.error(ERROR_BAD_ZIP, e);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void testPassZipGeneration() throws IOException, Exception {
IOUtils.copy(inputStream, new FileOutputStream(passZipFile));
Assert.assertTrue(passZipFile.exists());
Assert.assertTrue(passZipFile.length() > 0);
AssertZip.assertValid(passZipFile);
}

@Test
Expand All @@ -105,13 +106,14 @@ public void testJson() throws IOException, Exception {
new PKPassTemplateFolder(getPassFolderPath()), pkSigningInformation);
ByteArrayInputStream inputStream = new ByteArrayInputStream(signedAndZippedPkPassArchive);

File passJsonFile = new File("target/passJson.zip");
if (passJsonFile.exists()) {
passJsonFile.delete();
File passZipFile = new File("target/passJson.zip");
if (passZipFile.exists()) {
passZipFile.delete();
}
IOUtils.copy(inputStream, new FileOutputStream(passJsonFile));
Assert.assertTrue(passJsonFile.exists());
Assert.assertTrue(passJsonFile.length() > 0);
IOUtils.copy(inputStream, new FileOutputStream(passZipFile));
Assert.assertTrue(passZipFile.exists());
Assert.assertTrue(passZipFile.length() > 0);
AssertZip.assertValid(passZipFile);
}

private String getPassFolderPath() throws URISyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void testWithFolderBasedTemplate() throws JsonParseException, JsonMapping
IOUtils.copy(inputStream, new FileOutputStream(passZipFile));
Assert.assertTrue(passZipFile.exists());
Assert.assertTrue(passZipFile.length() > 0);
AssertZip.assertValid(passZipFile);
}

@Test
Expand Down Expand Up @@ -118,6 +119,7 @@ public void testWithInMemoryTemplate() throws JsonParseException, JsonMappingExc
IOUtils.copy(inputStream, new FileOutputStream(passZipFile));
Assert.assertTrue(passZipFile.exists());
Assert.assertTrue(passZipFile.length() > 0);
AssertZip.assertValid(passZipFile);
}

private String getPathFromClasspath(String path) throws URISyntaxException {
Expand Down

0 comments on commit 8ba5cf6

Please sign in to comment.