|
17 | 17 | */ |
18 | 18 | package sub.optimal.hacklace2; |
19 | 19 |
|
| 20 | +import java.io.BufferedReader; |
20 | 21 | import java.io.File; |
| 22 | +import java.io.FileNotFoundException; |
| 23 | +import java.io.FileOutputStream; |
| 24 | +import java.io.FileReader; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.OutputStreamWriter; |
21 | 27 | import org.junit.Assert; |
22 | 28 | import junit.framework.TestSuite; |
23 | 29 | import org.junit.Test; |
@@ -119,9 +125,104 @@ public void testOutOfRangeWidth() { |
119 | 125 | runConvertToHacklaceTest(fileName, expectedHl.toString()); |
120 | 126 | } |
121 | 127 |
|
122 | | - private void runConvertToHacklaceTest(String fileName, String expectedHl) { |
123 | | - File image = new File(fileName); |
| 128 | + @Test |
| 129 | + public void testTemplateProcessorExistingImageFile() { |
| 130 | + try { |
| 131 | + String templateBasename = "template_existing_image_file"; |
| 132 | + File hacklaceFile = runTemplateProcessorTest(templateBasename); |
| 133 | + assertTemplateProcessortTest(templateBasename, hacklaceFile); |
| 134 | + } catch (IOException ex) { |
| 135 | + System.err.println("no exception expected: " + ex.getMessage()); |
| 136 | + Assert.fail(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testTemplateProcessorNotAnImageFile() { |
| 142 | + try { |
| 143 | + String templateBasename = "template_not_an_image_file"; |
| 144 | + File hacklaceFile = runTemplateProcessorTest(templateBasename); |
| 145 | + assertTemplateProcessortTest(templateBasename, hacklaceFile); |
| 146 | + } catch (IOException ex) { |
| 147 | + System.err.println("no exception expected: " + ex.getMessage()); |
| 148 | + Assert.fail(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testTemplateProcessorNotExistingImageFile() { |
| 154 | + try { |
| 155 | + String templateBasename = "template_not_existing_image_file"; |
| 156 | + File hacklaceFile = runTemplateProcessorTest(templateBasename); |
| 157 | + assertTemplateProcessortTest(templateBasename, hacklaceFile); |
| 158 | + } catch (IOException ex) { |
| 159 | + System.err.println("no exception expected: " + ex.getMessage()); |
| 160 | + Assert.fail(); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Compares the expected output file wth the actual created output file. |
| 166 | + * |
| 167 | + * @param templateBasename basename of the template file |
| 168 | + * @param hacklaceFile {@link File} object representing the Hacklace file which is created |
| 169 | + * during the conversion of the template |
| 170 | + * @throws IOException in case reading from one file is failed |
| 171 | + */ |
| 172 | + private void assertTemplateProcessortTest(String templateBasename, File hacklaceFile) throws IOException { |
| 173 | + File expectedOutput = new File(getExpectedResultFilename(templateBasename)); |
| 174 | + BufferedReader expectedHl = new BufferedReader(new FileReader(expectedOutput)); |
| 175 | + BufferedReader actualHl = new BufferedReader(new FileReader(hacklaceFile)); |
| 176 | + String inLine; |
| 177 | + String outLine; |
| 178 | + do { |
| 179 | + inLine = expectedHl.readLine(); |
| 180 | + outLine = actualHl.readLine(); |
| 181 | + Assert.assertEquals(inLine, outLine); |
| 182 | + |
| 183 | + } while (inLine != null && outLine != null); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Executes the template processor test. |
| 188 | + * |
| 189 | + * @param templateBasename basename of the template file |
| 190 | + * @return a {@link File} object representing the Hacklace file which is created during the |
| 191 | + * conversion of the template |
| 192 | + * @throws IOException |
| 193 | + */ |
| 194 | + private File runTemplateProcessorTest(String templateBasename) throws IOException { |
| 195 | + File testInput = new File(getTemplateName(templateBasename)); |
| 196 | + File hacklaceFile = new File(getHacklaceName(templateBasename)); |
| 197 | + if (hacklaceFile.exists()) { |
| 198 | + hacklaceFile.delete(); |
| 199 | + } |
| 200 | + TemplateProcessor templateProcessor = TemplateProcessor.getInstance(); |
| 201 | + templateProcessor.processTemplateFile(testInput); |
| 202 | + hacklaceFile.deleteOnExit(); |
| 203 | + return hacklaceFile; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Executes the convert to hacklace test. |
| 208 | + * @param imageFileName name of the image file to convert |
| 209 | + * @param expectedHl the expected Hacklace hexadecimal bytes string |
| 210 | + */ |
| 211 | + private void runConvertToHacklaceTest(String imageFileName, String expectedHl) { |
| 212 | + File image = new File(imageFileName); |
124 | 213 | String hlOutput = converter.convertToHacklace(image); |
125 | 214 | Assert.assertEquals(expectedHl, hlOutput); |
126 | 215 | } |
| 216 | + |
| 217 | + private String getTemplateName(String templateBasename) { |
| 218 | + return templateBasename + ".txt"; |
| 219 | + } |
| 220 | + |
| 221 | + private String getHacklaceName(String templateBasename) { |
| 222 | + return templateBasename + ".hl"; |
| 223 | + } |
| 224 | + |
| 225 | + private String getExpectedResultFilename(String templateBasename) { |
| 226 | + return templateBasename + ".expected"; |
| 227 | + } |
127 | 228 | } |
0 commit comments