Add test for Slashdot + fix style
[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
19 /**
20 * Base class for {@link BasicSupport}s testing.
21 * <p>
22 * It will use the paths:
23 * <ul>
24 * <li><tt>test/XXX/source</tt>: the html source files</li>
25 * <li><tt>test/XXX/expected</tt>: the expected output</li>
26 * <li><tt>test/XXX/actual</tt>: the actual output of the last test</li>
27 * </ul>
28 *
29 * @author niki
30 */
31 abstract class TestBase extends TestCase {
32 private BasicSupport support;
33
34 public TestBase(BasicSupport support, String[] args) {
35 super(support.getType().toString());
36 this.support = support;
37 }
38
39 static protected InputStream doOpen(BasicSupport support,
40 Map<URL, File> map, URL url) throws IOException {
41 File file = map.get(url);
42 if (file == null) {
43 throw new FileNotFoundException("Test file not found for URL: "
44 + url);
45 }
46
47 return new FileInputStream("test/source/" + support.getType() + "/"
48 + file);
49
50 }
51
52 @Override
53 public void test() throws Exception {
54 File expected = new File("test/expected/" + support.getType());
55 File actual = new File("test/result/" + support.getType());
56
57 IOUtils.deltree(actual);
58 expected.mkdirs();
59 actual.mkdirs();
60
61 Output gopher = new Gopher(support.getType(), "", "", 70);
62 Output html = new Html(support.getType(), "", "", 80);
63
64 for (Story story : support.list()) {
65 support.fetch(story);
66 IOUtils.writeSmallFile(new File(actual, story.getId() + ".header"),
67 gopher.exportHeader(story));
68 IOUtils.writeSmallFile(new File(actual, story.getId() + ""),
69 gopher.export(story));
70 IOUtils.writeSmallFile(new File(actual, story.getId()
71 + ".header.html"), html.exportHeader(story));
72 IOUtils.writeSmallFile(new File(actual, story.getId() + ".html"),
73 html.export(story));
74 }
75
76 assertEquals(expected, actual);
77 }
78 }