View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.hobrasoft.pdfmu;
18  
19  import cz.hobrasoft.pdfmu.jackson.Inspect;
20  import cz.hobrasoft.pdfmu.jackson.SignatureDisplay;
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  import org.junit.Assert;
31  import org.junit.Rule;
32  import org.junit.contrib.java.lang.system.ExpectedSystemExit;
33  import org.junit.contrib.java.lang.system.SystemErrRule;
34  import org.junit.rules.TemporaryFolder;
35  
36  /**
37   * @author Filip Bartek
38   */
39  abstract public class MainTest {
40  
41      @Rule
42      public final ExpectedSystemExit exit = ExpectedSystemExit.none();
43  
44      @Rule
45      public final SystemErrRule systemErrRule = new SystemErrRule().mute().enableLog();
46  
47      /**
48       * Ensures that the directory is deleted after it has been used.
49       */
50      private static class StrictTemporaryFolder extends TemporaryFolder {
51  
52          @Override
53          protected void after() {
54              super.after();
55              assert !getRoot().exists();
56          }
57      }
58  
59      @Rule
60      public final TemporaryFolder folder = new StrictTemporaryFolder();
61  
62      protected File newFile(String fileName, boolean exists) throws IOException {
63          return newFile(folder, fileName, exists);
64      }
65  
66      public static File newFile(TemporaryFolder folder, String fileName,
67              boolean exists) throws IOException {
68          // TODO: Check whether this works if fileName is null
69          final File file = folder.newFile(fileName);
70          assert file != null;
71          assert file.exists();
72          if (!exists) {
73              final boolean success = file.delete();
74              assert success;
75          }
76          assert file.exists() == exists;
77          return file;
78      }
79  
80      private static final List<String> IGNORED_PROPERTIES
81              = Arrays.asList(new String[]{"Producer", "ModDate", "CreationDate"});
82  
83      protected static Inspect newInspect(String version) {
84          Inspect inspect = new Inspect();
85          inspect.version = version;
86          inspect.properties = new HashMap<>();
87          for (String property : IGNORED_PROPERTIES) {
88              inspect.properties.put(property, null);
89          }
90          inspect.signatures = new SignatureDisplay();
91          inspect.signatures.nRevisions = 0;
92          inspect.signatures.signatures = new ArrayList<>();
93          return inspect;
94      }
95  
96      private static void assertEqualsProperties(
97              final Map<String, String> expected,
98              final Map<String, String> actual) {
99          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 }