Add test for Slashdot + fix style
[gofetch.git] / src / be / nikiroo / gofetch / test / TestBase.java
CommitLineData
a71d4075
NR
1package be.nikiroo.gofetch.test;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStream;
8import java.net.URL;
9import java.util.Map;
10
11import be.nikiroo.gofetch.data.Story;
12import be.nikiroo.gofetch.output.Gopher;
13import be.nikiroo.gofetch.output.Html;
14import be.nikiroo.gofetch.output.Output;
15import be.nikiroo.gofetch.support.BasicSupport;
16import be.nikiroo.utils.IOUtils;
17import be.nikiroo.utils.test.TestCase;
a71d4075
NR
18
19/**
20 * Base class for {@link BasicSupport}s testing.
1aaa6ba3
NR
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>
a71d4075
NR
28 *
29 * @author niki
30 */
299a08f3
NR
31abstract class TestBase extends TestCase {
32 private BasicSupport support;
33
a71d4075 34 public TestBase(BasicSupport support, String[] args) {
299a08f3
NR
35 super(support.getType().toString());
36 this.support = support;
a71d4075
NR
37 }
38
1aaa6ba3
NR
39 static protected InputStream doOpen(BasicSupport support,
40 Map<URL, File> map, URL url) throws IOException {
a71d4075
NR
41 File file = map.get(url);
42 if (file == null) {
43 throw new FileNotFoundException("Test file not found for URL: "
44 + url);
45 }
46
1aaa6ba3
NR
47 return new FileInputStream("test/source/" + support.getType() + "/"
48 + file);
a71d4075
NR
49
50 }
51
299a08f3
NR
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());
a71d4075 56
299a08f3
NR
57 IOUtils.deltree(actual);
58 expected.mkdirs();
59 actual.mkdirs();
1aaa6ba3 60
299a08f3
NR
61 Output gopher = new Gopher(support.getType(), "", "", 70);
62 Output html = new Html(support.getType(), "", "", 80);
a71d4075 63
299a08f3
NR
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 }
a71d4075 75
299a08f3 76 assertEquals(expected, actual);
a71d4075
NR
77 }
78}