001/* 002 * Copyright (C) 2016 Hobrasoft s.r.o. 003 * 004 * This program is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU Affero General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU Affero General Public License for more details. 013 * 014 * You should have received a copy of the GNU Affero General Public License 015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 016 */ 017package cz.hobrasoft.pdfmu; 018 019import cz.hobrasoft.pdfmu.jackson.Inspect; 020import cz.hobrasoft.pdfmu.jackson.SignatureDisplay; 021import java.io.File; 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.HashMap; 026import java.util.HashSet; 027import java.util.List; 028import java.util.Map; 029import java.util.Set; 030import org.junit.Assert; 031import org.junit.Rule; 032import org.junit.contrib.java.lang.system.ExpectedSystemExit; 033import org.junit.contrib.java.lang.system.SystemErrRule; 034import org.junit.rules.TemporaryFolder; 035 036/** 037 * @author Filip Bartek 038 */ 039abstract public class MainTest { 040 041 @Rule 042 public final ExpectedSystemExit exit = ExpectedSystemExit.none(); 043 044 @Rule 045 public final SystemErrRule systemErrRule = new SystemErrRule().mute().enableLog(); 046 047 /** 048 * Ensures that the directory is deleted after it has been used. 049 */ 050 private static class StrictTemporaryFolder extends TemporaryFolder { 051 052 @Override 053 protected void after() { 054 super.after(); 055 assert !getRoot().exists(); 056 } 057 } 058 059 @Rule 060 public final TemporaryFolder folder = new StrictTemporaryFolder(); 061 062 protected File newFile(String fileName, boolean exists) throws IOException { 063 return newFile(folder, fileName, exists); 064 } 065 066 public static File newFile(TemporaryFolder folder, String fileName, 067 boolean exists) throws IOException { 068 // TODO: Check whether this works if fileName is null 069 final File file = folder.newFile(fileName); 070 assert file != null; 071 assert file.exists(); 072 if (!exists) { 073 final boolean success = file.delete(); 074 assert success; 075 } 076 assert file.exists() == exists; 077 return file; 078 } 079 080 private static final List<String> IGNORED_PROPERTIES 081 = Arrays.asList(new String[]{"Producer", "ModDate", "CreationDate"}); 082 083 protected static Inspect newInspect(String version) { 084 Inspect inspect = new Inspect(); 085 inspect.version = version; 086 inspect.properties = new HashMap<>(); 087 for (String property : IGNORED_PROPERTIES) { 088 inspect.properties.put(property, null); 089 } 090 inspect.signatures = new SignatureDisplay(); 091 inspect.signatures.nRevisions = 0; 092 inspect.signatures.signatures = new ArrayList<>(); 093 return inspect; 094 } 095 096 private static void assertEqualsProperties( 097 final Map<String, String> expected, 098 final Map<String, String> actual) { 099 Assert.assertNotNull(expected); 100 Assert.assertNotNull(actual); 101 Assert.assertEquals(expected.keySet(), actual.keySet()); 102 Set<String> keySet = new HashSet<>(expected.keySet()); 103 keySet.removeAll(IGNORED_PROPERTIES); 104 for (String key : keySet) { 105 Assert.assertEquals(expected.get(key), actual.get(key)); 106 } 107 } 108 109 protected static void assertEquals(final Inspect expected, final Inspect actual) { 110 Assert.assertEquals(expected.version, actual.version); 111 assertEqualsProperties(expected.properties, actual.properties); 112 Assert.assertEquals(expected.signatures, actual.signatures); 113 } 114 115 protected static final PdfFileResource BLANK_12_PDF 116 = new PdfFileResource("blank-12.pdf", "1.2", "D:20160525204745+02'00'"); 117}