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