Commit | Line | Data |
---|---|---|
a71d4075 NR |
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; | |
6e234797 NR |
16 | import be.nikiroo.utils.Cache; |
17 | import be.nikiroo.utils.Downloader; | |
a71d4075 NR |
18 | import be.nikiroo.utils.IOUtils; |
19 | import be.nikiroo.utils.test.TestCase; | |
a71d4075 NR |
20 | |
21 | /** | |
22 | * Base class for {@link BasicSupport}s testing. | |
1aaa6ba3 NR |
23 | * <p> |
24 | * It will use the paths: | |
25 | * <ul> | |
6e234797 NR |
26 | * <li><tt>test/???/source</tt>: the html source files</li> |
27 | * <li><tt>test/???/expected</tt>: the expected output</li> | |
28 | * <li><tt>test/???/actual</tt>: the actual output of the last test</li> | |
1aaa6ba3 | 29 | * </ul> |
a71d4075 NR |
30 | * |
31 | * @author niki | |
32 | */ | |
299a08f3 NR |
33 | abstract class TestBase extends TestCase { |
34 | private BasicSupport support; | |
6e234797 NR |
35 | private Cache cache; |
36 | private Downloader downloader; | |
299a08f3 | 37 | |
a71d4075 | 38 | public TestBase(BasicSupport support, String[] args) { |
299a08f3 NR |
39 | super(support.getType().toString()); |
40 | this.support = support; | |
6e234797 NR |
41 | try { |
42 | cache = new Cache(new File("test/source/" + support.getType()), -1, | |
43 | -1); | |
44 | downloader = new Downloader("gofetch", cache); | |
45 | } catch (IOException e) { | |
46 | throw new RuntimeException(e); | |
47 | } | |
48 | } | |
49 | ||
50 | protected InputStream download(URL url) throws IOException { | |
51 | return downloader.open(url); | |
a71d4075 NR |
52 | } |
53 | ||
1aaa6ba3 NR |
54 | static protected InputStream doOpen(BasicSupport support, |
55 | Map<URL, File> map, URL url) throws IOException { | |
a71d4075 NR |
56 | File file = map.get(url); |
57 | if (file == null) { | |
58 | throw new FileNotFoundException("Test file not found for URL: " | |
59 | + url); | |
60 | } | |
61 | ||
1aaa6ba3 NR |
62 | return new FileInputStream("test/source/" + support.getType() + "/" |
63 | + file); | |
a71d4075 NR |
64 | |
65 | } | |
66 | ||
299a08f3 NR |
67 | @Override |
68 | public void test() throws Exception { | |
69 | File expected = new File("test/expected/" + support.getType()); | |
70 | File actual = new File("test/result/" + support.getType()); | |
a71d4075 | 71 | |
299a08f3 NR |
72 | IOUtils.deltree(actual); |
73 | expected.mkdirs(); | |
74 | actual.mkdirs(); | |
1aaa6ba3 | 75 | |
299a08f3 NR |
76 | Output gopher = new Gopher(support.getType(), "", "", 70); |
77 | Output html = new Html(support.getType(), "", "", 80); | |
a71d4075 | 78 | |
299a08f3 NR |
79 | for (Story story : support.list()) { |
80 | support.fetch(story); | |
81 | IOUtils.writeSmallFile(new File(actual, story.getId() + ".header"), | |
82 | gopher.exportHeader(story)); | |
83 | IOUtils.writeSmallFile(new File(actual, story.getId() + ""), | |
84 | gopher.export(story)); | |
85 | IOUtils.writeSmallFile(new File(actual, story.getId() | |
86 | + ".header.html"), html.exportHeader(story)); | |
87 | IOUtils.writeSmallFile(new File(actual, story.getId() + ".html"), | |
88 | html.export(story)); | |
89 | } | |
a71d4075 | 90 | |
299a08f3 | 91 | assertEquals(expected, actual); |
a71d4075 NR |
92 | } |
93 | } |