e8bfde089785b1ce73408430aa5c4a850320f169
[gofetch.git] / src / be / nikiroo / gofetch / test / TestBase.java
1 package be.nikiroo.gofetch.test;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.URL;
9 import java.util.Map;
10
11 import be.nikiroo.gofetch.data.Story;
12 import be.nikiroo.gofetch.output.Gopher;
13 import be.nikiroo.gofetch.output.Html;
14 import be.nikiroo.gofetch.output.Output;
15 import be.nikiroo.gofetch.support.BasicSupport;
16 import be.nikiroo.utils.IOUtils;
17 import be.nikiroo.utils.test.TestCase;
18 import be.nikiroo.utils.test.TestLauncher;
19
20 /**
21 * Base class for {@link BasicSupport}s testing.
22 *
23 * @author niki
24 */
25 abstract class TestBase extends TestLauncher {
26 public TestBase(BasicSupport support, String[] args) {
27 super(support.getType().toString(), args);
28 addTest(support);
29 }
30
31 static protected InputStream doOpen(Map<URL, File> map, URL url)
32 throws IOException {
33 File file = map.get(url);
34 if (file == null) {
35 throw new FileNotFoundException("Test file not found for URL: "
36 + url);
37 }
38
39 return new FileInputStream(file);
40
41 }
42
43 private void addTest(final BasicSupport support) {
44 addTest(new TestCase("Processing example data") {
45 @Override
46 public void test() throws Exception {
47 File expected = new File("test/expected/" + support.getType());
48 File actual = new File("test/result/" + support.getType());
49
50 Output gopher = new Gopher(support.getType(), "", "", 70);
51 Output html = new Html(support.getType(), "", "", 80);
52
53 for (Story story : support.list()) {
54 IOUtils.writeSmallFile(new File(actual, story.getId()
55 + ".header"), gopher.exportHeader(story));
56 IOUtils.writeSmallFile(
57 new File(actual, story.getId() + ""),
58 gopher.export(story));
59 IOUtils.writeSmallFile(new File(actual, story.getId()
60 + ".header.html"), html.exportHeader(story));
61 IOUtils.writeSmallFile(new File(actual, story.getId()
62 + ".html"), html.export(story));
63 }
64
65 assertEquals(expected, actual);
66 }
67 });
68 }
69 }