|
27 | 27 | import java.io.ByteArrayInputStream; |
28 | 28 | import java.io.FileInputStream; |
29 | 29 | import java.io.IOException; |
| 30 | +import java.io.InputStream; |
30 | 31 | import java.nio.charset.StandardCharsets; |
31 | 32 | import java.util.List; |
32 | 33 | import java.util.Optional; |
@@ -59,129 +60,147 @@ void testStartFontMetrics() throws IOException |
59 | 60 | @Test |
60 | 61 | void testEndFontMetrics() throws IOException |
61 | 62 | { |
62 | | - AFMParser parser = new AFMParser( |
63 | | - new FileInputStream("src/test/resources/afm/NoEndFontMetrics.afm")); |
64 | | - try |
| 63 | + try (InputStream is = new FileInputStream("src/test/resources/afm/NoEndFontMetrics.afm")) |
65 | 64 | { |
66 | | - parser.parse(); |
67 | | - fail("The AFMParser should have thrown an IOException because of a missing " |
68 | | - + AFMParser.END_FONT_METRICS); |
69 | | - } |
70 | | - catch (IOException e) |
71 | | - { |
72 | | - assertTrue(e.getMessage().contains("Unknown AFM key")); |
| 65 | + AFMParser parser = new AFMParser(is); |
| 66 | + try |
| 67 | + { |
| 68 | + parser.parse(); |
| 69 | + fail("The AFMParser should have thrown an IOException because of a missing " |
| 70 | + + AFMParser.END_FONT_METRICS); |
| 71 | + } |
| 72 | + catch (IOException e) |
| 73 | + { |
| 74 | + assertTrue(e.getMessage().contains("Unknown AFM key")); |
| 75 | + } |
73 | 76 | } |
74 | 77 | } |
75 | 78 |
|
76 | 79 | @Test |
77 | 80 | void testMalformedFloat() throws IOException |
78 | 81 | { |
79 | | - AFMParser parser = new AFMParser( |
80 | | - new FileInputStream("src/test/resources/afm/MalformedFloat.afm")); |
81 | | - try |
| 82 | + try (InputStream is = new FileInputStream("src/test/resources/afm/MalformedFloat.afm")) |
82 | 83 | { |
83 | | - parser.parse(); |
84 | | - fail("The AFMParser should have thrown an IOException because of a malformed float value"); |
85 | | - } |
86 | | - catch (IOException e) |
87 | | - { |
88 | | - assertTrue(e.getCause() instanceof NumberFormatException); |
89 | | - assertTrue(e.getMessage().contains("4,1ab")); |
| 84 | + AFMParser parser = new AFMParser(is); |
| 85 | + try |
| 86 | + { |
| 87 | + parser.parse(); |
| 88 | + fail("The AFMParser should have thrown an IOException because of a malformed float value"); |
| 89 | + } |
| 90 | + catch (IOException e) |
| 91 | + { |
| 92 | + assertTrue(e.getCause() instanceof NumberFormatException); |
| 93 | + assertTrue(e.getMessage().contains("4,1ab")); |
| 94 | + } |
90 | 95 | } |
91 | 96 | } |
92 | 97 |
|
93 | 98 | @Test |
94 | 99 | void testMalformedInteger() throws IOException |
95 | 100 | { |
96 | | - AFMParser parser = new AFMParser( |
97 | | - new FileInputStream("src/test/resources/afm/MalformedInteger.afm")); |
98 | | - try |
99 | | - { |
100 | | - parser.parse(); |
101 | | - fail("The AFMParser should have thrown an IOException because of a malformed int value"); |
102 | | - } |
103 | | - catch (IOException e) |
| 101 | + try (InputStream is = new FileInputStream("src/test/resources/afm/MalformedInteger.afm")) |
104 | 102 | { |
105 | | - assertTrue(e.getCause() instanceof NumberFormatException); |
106 | | - assertTrue(e.getMessage().contains("3.4")); |
| 103 | + try |
| 104 | + { |
| 105 | + AFMParser parser = new AFMParser(is); |
| 106 | + parser.parse(); |
| 107 | + fail("The AFMParser should have thrown an IOException because of a malformed int value"); |
| 108 | + } |
| 109 | + catch (IOException e) |
| 110 | + { |
| 111 | + assertTrue(e.getCause() instanceof NumberFormatException); |
| 112 | + assertTrue(e.getMessage().contains("3.4")); |
| 113 | + } |
107 | 114 | } |
108 | 115 | } |
109 | 116 |
|
110 | 117 | @Test |
111 | 118 | void testHelveticaFontMetrics() throws IOException |
112 | 119 | { |
113 | | - AFMParser parser = new AFMParser( |
114 | | - new FileInputStream("src/test/resources/afm/Helvetica.afm")); |
115 | | - checkHelveticaFontMetrics(parser.parse()); |
| 120 | + try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm")) |
| 121 | + { |
| 122 | + AFMParser parser = new AFMParser(is); |
| 123 | + checkHelveticaFontMetrics(parser.parse()); |
| 124 | + } |
116 | 125 | } |
117 | 126 |
|
118 | 127 | @Test |
119 | 128 | void testHelveticaCharMetrics() throws IOException |
120 | 129 | { |
121 | | - AFMParser parser = new AFMParser( |
122 | | - new FileInputStream("src/test/resources/afm/Helvetica.afm")); |
123 | | - FontMetrics fontMetrics = parser.parse(); |
| 130 | + try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm")) |
| 131 | + { |
| 132 | + AFMParser parser = new AFMParser(is); |
| 133 | + FontMetrics fontMetrics = parser.parse(); |
124 | 134 |
|
125 | | - // char metrics |
126 | | - checkHelveticaCharMetrics(fontMetrics.getCharMetrics()); |
| 135 | + // char metrics |
| 136 | + checkHelveticaCharMetrics(fontMetrics.getCharMetrics()); |
| 137 | + } |
127 | 138 | } |
128 | 139 |
|
129 | 140 | @Test |
130 | 141 | void testHelveticaKernPairs() throws IOException |
131 | 142 | { |
132 | | - AFMParser parser = new AFMParser( |
133 | | - new FileInputStream("src/test/resources/afm/Helvetica.afm")); |
134 | | - FontMetrics fontMetrics = parser.parse(); |
| 143 | + try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm")) |
| 144 | + { |
| 145 | + AFMParser parser = new AFMParser(is); |
| 146 | + FontMetrics fontMetrics = parser.parse(); |
135 | 147 |
|
136 | | - // KernPairs |
137 | | - List<KernPair> kernPairs = fontMetrics.getKernPairs(); |
138 | | - assertEquals(2705, kernPairs.size()); |
139 | | - // check "KPX A Ucircumflex -50" |
140 | | - checkKernPair(kernPairs, "A", "Ucircumflex", -50, 0); |
141 | | - // check "KPX W agrave -40" |
142 | | - checkKernPair(kernPairs, "W", "agrave", -40, 0); |
143 | | - // KernPairs0 |
144 | | - assertTrue(fontMetrics.getKernPairs0().isEmpty()); |
145 | | - // KernPairs1 |
146 | | - assertTrue(fontMetrics.getKernPairs1().isEmpty()); |
147 | | - // composite data |
148 | | - assertTrue(fontMetrics.getComposites().isEmpty()); |
| 148 | + // KernPairs |
| 149 | + List<KernPair> kernPairs = fontMetrics.getKernPairs(); |
| 150 | + assertEquals(2705, kernPairs.size()); |
| 151 | + // check "KPX A Ucircumflex -50" |
| 152 | + checkKernPair(kernPairs, "A", "Ucircumflex", -50, 0); |
| 153 | + // check "KPX W agrave -40" |
| 154 | + checkKernPair(kernPairs, "W", "agrave", -40, 0); |
| 155 | + // KernPairs0 |
| 156 | + assertTrue(fontMetrics.getKernPairs0().isEmpty()); |
| 157 | + // KernPairs1 |
| 158 | + assertTrue(fontMetrics.getKernPairs1().isEmpty()); |
| 159 | + // composite data |
| 160 | + assertTrue(fontMetrics.getComposites().isEmpty()); |
| 161 | + } |
149 | 162 | } |
150 | 163 |
|
151 | 164 | @Test |
152 | 165 | void testHelveticaFontMetricsReducedDataset() throws IOException |
153 | 166 | { |
154 | | - AFMParser parser = new AFMParser( |
155 | | - new FileInputStream("src/test/resources/afm/Helvetica.afm")); |
156 | | - checkHelveticaFontMetrics(parser.parse(true)); |
| 167 | + try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm")) |
| 168 | + { |
| 169 | + AFMParser parser = new AFMParser(is); |
| 170 | + checkHelveticaFontMetrics(parser.parse(true)); |
| 171 | + } |
157 | 172 | } |
158 | 173 |
|
159 | 174 | @Test |
160 | 175 | void testHelveticaCharMetricsReducedDataset() throws IOException |
161 | 176 | { |
162 | | - AFMParser parser = new AFMParser( |
163 | | - new FileInputStream("src/test/resources/afm/Helvetica.afm")); |
164 | | - FontMetrics fontMetrics = parser.parse(true); |
| 177 | + try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm")) |
| 178 | + { |
| 179 | + AFMParser parser = new AFMParser(is); |
| 180 | + FontMetrics fontMetrics = parser.parse(true); |
165 | 181 |
|
166 | | - // char metrics |
167 | | - checkHelveticaCharMetrics(fontMetrics.getCharMetrics()); |
| 182 | + // char metrics |
| 183 | + checkHelveticaCharMetrics(fontMetrics.getCharMetrics()); |
| 184 | + } |
168 | 185 | } |
169 | 186 |
|
170 | 187 | @Test |
171 | 188 | void testHelveticaKernPairsReducedDataset() throws IOException |
172 | 189 | { |
173 | | - AFMParser parser = new AFMParser( |
174 | | - new FileInputStream("src/test/resources/afm/Helvetica.afm")); |
175 | | - FontMetrics fontMetrics = parser.parse(true); |
| 190 | + try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm")) |
| 191 | + { |
| 192 | + AFMParser parser = new AFMParser(is);// |
| 193 | + FontMetrics fontMetrics = parser.parse(true); |
176 | 194 |
|
177 | | - // KernPairs, empty due to reducedDataset == true |
178 | | - assertTrue(fontMetrics.getKernPairs().isEmpty()); |
179 | | - // KernPairs0 |
180 | | - assertTrue(fontMetrics.getKernPairs0().isEmpty()); |
181 | | - // KernPairs1 |
182 | | - assertTrue(fontMetrics.getKernPairs1().isEmpty()); |
183 | | - // composite data |
184 | | - assertTrue(fontMetrics.getComposites().isEmpty()); |
| 195 | + // KernPairs, empty due to reducedDataset == true |
| 196 | + assertTrue(fontMetrics.getKernPairs().isEmpty()); |
| 197 | + // KernPairs0 |
| 198 | + assertTrue(fontMetrics.getKernPairs0().isEmpty()); |
| 199 | + // KernPairs1 |
| 200 | + assertTrue(fontMetrics.getKernPairs1().isEmpty()); |
| 201 | + // composite data |
| 202 | + assertTrue(fontMetrics.getComposites().isEmpty()); |
| 203 | + } |
185 | 204 | } |
186 | 205 |
|
187 | 206 | private void checkHelveticaCharMetrics(List<CharMetric> charMetrics) |
|
0 commit comments