| 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.Cache; |
| 17 | import be.nikiroo.utils.Downloader; |
| 18 | import be.nikiroo.utils.IOUtils; |
| 19 | import be.nikiroo.utils.test.TestCase; |
| 20 | |
| 21 | /** |
| 22 | * Base class for {@link BasicSupport}s testing. |
| 23 | * <p> |
| 24 | * It will use the paths: |
| 25 | * <ul> |
| 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> |
| 29 | * </ul> |
| 30 | * |
| 31 | * @author niki |
| 32 | */ |
| 33 | abstract class TestBase extends TestCase { |
| 34 | private BasicSupport support; |
| 35 | private Cache cache; |
| 36 | private Downloader downloader; |
| 37 | |
| 38 | public TestBase(BasicSupport support, String[] args) { |
| 39 | super(support.getType().toString()); |
| 40 | this.support = support; |
| 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); |
| 52 | } |
| 53 | |
| 54 | static protected InputStream doOpen(BasicSupport support, |
| 55 | Map<URL, File> map, URL url) throws IOException { |
| 56 | File file = map.get(url); |
| 57 | if (file == null) { |
| 58 | throw new FileNotFoundException("Test file not found for URL: " |
| 59 | + url); |
| 60 | } |
| 61 | |
| 62 | return new FileInputStream("test/source/" + support.getType() + "/" |
| 63 | + file); |
| 64 | |
| 65 | } |
| 66 | |
| 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()); |
| 71 | |
| 72 | IOUtils.deltree(actual); |
| 73 | expected.mkdirs(); |
| 74 | actual.mkdirs(); |
| 75 | |
| 76 | Output gopher = new Gopher(support.getType(), "", "", 70); |
| 77 | Output html = new Html(support.getType(), "", "", 80); |
| 78 | |
| 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 | } |
| 90 | |
| 91 | assertEquals(expected, actual); |
| 92 | } |
| 93 | } |