Add tests (WIP)
authorNiki Roo <niki@nikiroo.be>
Sun, 23 Sep 2018 03:23:20 +0000 (05:23 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 23 Sep 2018 03:23:20 +0000 (05:23 +0200)
configure.sh
libs/nikiroo-utils-4.4.2-sources.jar [moved from libs/nikiroo-utils-4.4.0-sources.jar with 94% similarity]
src/be/nikiroo/gofetch/support/BasicSupport.java
src/be/nikiroo/gofetch/support/Phoronix.java
src/be/nikiroo/gofetch/support/TheRegister.java
src/be/nikiroo/gofetch/test/Test.java [new file with mode: 0644]
src/be/nikiroo/gofetch/test/TestBase.java [new file with mode: 0644]
src/be/nikiroo/gofetch/test/TestLWN.java [new file with mode: 0644]

index b2317b23b8daf86f8170d24048477922d9d7f953..ffb153cf505738b6b041a7176edd7fccf6fc3cca 100755 (executable)
@@ -57,7 +57,7 @@ fi;
 
 echo "MAIN = be/nikiroo/gofetch/Main" > Makefile
 echo "MORE = " >> Makefile
-echo "TEST = " >> Makefile
+echo "TEST = be/nikiroo/gofetch/test/Test" >> Makefile
 echo "TEST_PARAMS = $cols $ok $ko" >> Makefile
 echo "NAME = gofetch" >> Makefile
 echo "PREFIX = $PREFIX" >> Makefile
similarity index 94%
rename from libs/nikiroo-utils-4.4.0-sources.jar
rename to libs/nikiroo-utils-4.4.2-sources.jar
index 63f158ee48f9e6193e25703c056f6f3409d08f2b..a2869e605140a1bc1f7da7108456ecec03a024fc 100644 (file)
Binary files a/libs/nikiroo-utils-4.4.0-sources.jar and b/libs/nikiroo-utils-4.4.2-sources.jar differ
index 972852333fed4707ecd9ed2812e45c7f2ee00f8b..a748262857debc001dcb07cd557a7563a007e3d7 100644 (file)
@@ -31,8 +31,10 @@ import be.nikiroo.utils.StringUtils;
  * @author niki
  */
 public abstract class BasicSupport {
-       /** The downloader to use for all websites. */
-       static protected Downloader downloader = new Downloader("gofetcher");
+       /**
+        * The downloader to use for all websites via {@link BasicSupport#open(URL)}
+        */
+       static private Downloader downloader = new Downloader("gofetcher");
 
        static private String preselector;
 
@@ -55,7 +57,7 @@ public abstract class BasicSupport {
         * @return the selector
         */
        public String getSelector() {
-               return getSelector(type);
+               return getSelector(getType());
        }
 
        /**
@@ -87,7 +89,7 @@ public abstract class BasicSupport {
                                defaultCateg = "";
                        }
 
-                       InputStream in = downloader.open(url);
+                       InputStream in = open(url);
                        Document doc = DataUtil.load(in, "UTF-8", url.toString());
                        List<Element> articles = getArticles(doc);
                        for (Element article : articles) {
@@ -273,7 +275,7 @@ public abstract class BasicSupport {
                String fullContent = "";
 
                URL url = new URL(story.getUrlInternal());
-               InputStream in = downloader.open(url);
+               InputStream in = open(url);
                try {
                        Document doc = DataUtil.load(in, "UTF-8", url.toString());
                        Element article = getFullArticle(doc);
@@ -346,6 +348,23 @@ public abstract class BasicSupport {
         */
        abstract protected ElementProcessor getElementProcessorFullArticle();
 
+       /**
+        * Open a network resource.
+        * <p>
+        * You need to close the returned {@link InputStream} when done.
+        * 
+        * @param url
+        *            the source to open
+        * 
+        * @return the content
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       protected InputStream open(URL url) throws IOException {
+               return downloader.open(url);
+       }
+
        /**
         * Convert the comment elements into {@link Comment}s
         * 
index 8f257fbb0d7b3a36d17a8601a61fc3bfd3de6149..74096ad67afcd28fd478e62fe4a6e1edc132e596 100644 (file)
@@ -146,7 +146,7 @@ class Phoronix extends BasicSupport {
                                Element a = linkToComments.getElementsByTag("a").first();
                                if (a != null) {
                                        String url = a.absUrl("href");
-                                       InputStream in = downloader.open(new URL(url));
+                                       InputStream in = open(new URL(url));
                                        try {
                                                doc = DataUtil.load(in, "UTF-8", url.toString());
                                                return doc.getElementsByClass("b-post");
index 1195d3d5f9edf7200aef9318b2517af79b892b67..478381d43800895de5a157e7da9ceebe0b7d1f9a 100644 (file)
@@ -149,7 +149,7 @@ public class TheRegister extends BasicSupport {
                try {
                        URL url = new URL("https://forums.theregister.co.uk/forum/1"
                                        + intUrl.getPath());
-                       InputStream in = downloader.open(url);
+                       InputStream in = open(url);
                        try {
                                doc = DataUtil.load(in, "UTF-8", url.toString());
                                Element posts = doc.getElementById("forum_posts");
diff --git a/src/be/nikiroo/gofetch/test/Test.java b/src/be/nikiroo/gofetch/test/Test.java
new file mode 100644 (file)
index 0000000..7ece14a
--- /dev/null
@@ -0,0 +1,19 @@
+package be.nikiroo.gofetch.test;
+
+import be.nikiroo.utils.test.TestLauncher;
+
+/**
+ * Tests for GoFetch.
+ * 
+ * @author niki
+ */
+public class Test extends TestLauncher {
+       public Test(String[] args) {
+               super("GoFetch", args);
+               addSeries(new TestLWN(args));
+       }
+
+       public static void main(String[] args) {
+               System.exit(new Test(args).launch());
+       }
+}
diff --git a/src/be/nikiroo/gofetch/test/TestBase.java b/src/be/nikiroo/gofetch/test/TestBase.java
new file mode 100644 (file)
index 0000000..e8bfde0
--- /dev/null
@@ -0,0 +1,69 @@
+package be.nikiroo.gofetch.test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Map;
+
+import be.nikiroo.gofetch.data.Story;
+import be.nikiroo.gofetch.output.Gopher;
+import be.nikiroo.gofetch.output.Html;
+import be.nikiroo.gofetch.output.Output;
+import be.nikiroo.gofetch.support.BasicSupport;
+import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.test.TestCase;
+import be.nikiroo.utils.test.TestLauncher;
+
+/**
+ * Base class for {@link BasicSupport}s testing.
+ * 
+ * @author niki
+ */
+abstract class TestBase extends TestLauncher {
+       public TestBase(BasicSupport support, String[] args) {
+               super(support.getType().toString(), args);
+               addTest(support);
+       }
+
+       static protected InputStream doOpen(Map<URL, File> map, URL url)
+                       throws IOException {
+               File file = map.get(url);
+               if (file == null) {
+                       throw new FileNotFoundException("Test file not found for URL: "
+                                       + url);
+               }
+
+               return new FileInputStream(file);
+
+       }
+
+       private void addTest(final BasicSupport support) {
+               addTest(new TestCase("Processing example data") {
+                       @Override
+                       public void test() throws Exception {
+                               File expected = new File("test/expected/" + support.getType());
+                               File actual = new File("test/result/" + support.getType());
+
+                               Output gopher = new Gopher(support.getType(), "", "", 70);
+                               Output html = new Html(support.getType(), "", "", 80);
+
+                               for (Story story : support.list()) {
+                                       IOUtils.writeSmallFile(new File(actual, story.getId()
+                                                       + ".header"), gopher.exportHeader(story));
+                                       IOUtils.writeSmallFile(
+                                                       new File(actual, story.getId() + ""),
+                                                       gopher.export(story));
+                                       IOUtils.writeSmallFile(new File(actual, story.getId()
+                                                       + ".header.html"), html.exportHeader(story));
+                                       IOUtils.writeSmallFile(new File(actual, story.getId()
+                                                       + ".html"), html.export(story));
+                               }
+
+                               assertEquals(expected, actual);
+                       }
+               });
+       }
+}
diff --git a/src/be/nikiroo/gofetch/test/TestLWN.java b/src/be/nikiroo/gofetch/test/TestLWN.java
new file mode 100644 (file)
index 0000000..597e761
--- /dev/null
@@ -0,0 +1,35 @@
+package be.nikiroo.gofetch.test;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import be.nikiroo.gofetch.support.LWN;
+import be.nikiroo.gofetch.support.Type;
+
+public class TestLWN extends TestBase {
+
+       static private Map<URL, File> getMap() throws MalformedURLException {
+               Map<URL, File> map = new HashMap<URL, File>();
+               map.put(new URL("http://fanfan.be/"), new File("/tmp/none"));
+               return map;
+       }
+
+       public TestLWN(String[] args) {
+               super(new LWN() {
+                       @Override
+                       protected InputStream open(URL url) throws IOException {
+                               return doOpen(getMap(), url);
+                       }
+
+                       @Override
+                       public Type getType() {
+                               return Type.LWN;
+                       }
+               }, args);
+       }
+}