1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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 }