Skip to content

Commit a48729d

Browse files
committed
add unit tests for template processing
1 parent 00142ff commit a48729d

File tree

1 file changed

+103
-2
lines changed

1 file changed

+103
-2
lines changed

src/test/java/sub/optimal/hacklace2/ConverterTest.java

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
*/
1818
package sub.optimal.hacklace2;
1919

20+
import java.io.BufferedReader;
2021
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;
2127
import org.junit.Assert;
2228
import junit.framework.TestSuite;
2329
import org.junit.Test;
@@ -119,9 +125,104 @@ public void testOutOfRangeWidth() {
119125
runConvertToHacklaceTest(fileName, expectedHl.toString());
120126
}
121127

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);
124213
String hlOutput = converter.convertToHacklace(image);
125214
Assert.assertEquals(expectedHl, hlOutput);
126215
}
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+
}
127228
}

0 commit comments

Comments
 (0)