Make ldclient freebase test more robust, ignore test-failures that are caused
by invalid turtle data served by freebase.
diff --git a/libraries/ldclient/ldclient-core/src/test/java/org/apache/marmotta/ldclient/test/provider/ProviderTestBase.java b/libraries/ldclient/ldclient-core/src/test/java/org/apache/marmotta/ldclient/test/provider/ProviderTestBase.java
index 1ff772f..d46184b 100644
--- a/libraries/ldclient/ldclient-core/src/test/java/org/apache/marmotta/ldclient/test/provider/ProviderTestBase.java
+++ b/libraries/ldclient/ldclient-core/src/test/java/org/apache/marmotta/ldclient/test/provider/ProviderTestBase.java
@@ -20,20 +20,24 @@
 import org.apache.commons.io.IOUtils;
 import org.apache.marmotta.commons.sesame.model.ModelCommons;
 import org.apache.marmotta.ldclient.api.ldclient.LDClientService;
+import org.apache.marmotta.ldclient.exception.DataRetrievalException;
 import org.apache.marmotta.ldclient.model.ClientResponse;
 import org.apache.marmotta.ldclient.services.ldclient.LDClient;
 import org.apache.marmotta.ldclient.test.helper.TestLDClient;
 import org.junit.*;
+import org.junit.internal.AssumptionViolatedException;
 import org.junit.rules.TestWatcher;
 import org.junit.runner.Description;
 import org.openrdf.query.BooleanQuery;
 import org.openrdf.query.QueryLanguage;
 import org.openrdf.repository.RepositoryConnection;
 import org.openrdf.rio.RDFFormat;
+import org.openrdf.rio.RDFParseException;
 import org.openrdf.rio.Rio;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
 
@@ -74,42 +78,46 @@
 
     protected void testResource(String uri) throws Exception {
 
-        Assume.assumeTrue(ldclient.ping(uri));
+        Assume.assumeTrue("LDClient endpoint for <" + uri + "> not available", ldclient.ping(uri));
 
         ClientResponse response = ldclient.retrieveResource(uri);
 
         RepositoryConnection connection = ModelCommons.asRepository(response.getData()).getConnection();
-        connection.begin();
-        Assert.assertTrue(connection.size() > 0);
-
-        connection.commit();
-        connection.close();
+        try {
+            connection.begin();
+            Assert.assertTrue(connection.size() > 0);
+        }finally {
+            connection.commit();
+            connection.close();
+        }
     }
 
     protected void testResource(String uri, String sparqlFile) throws Exception {
 
-        Assume.assumeTrue(ldclient.ping(uri));
+        Assume.assumeTrue("LDClient endpoint for <" + uri + "> not available", ldclient.ping(uri));
 
         ClientResponse response = ldclient.retrieveResource(uri);
 
         RepositoryConnection connection = ModelCommons.asRepository(response.getData()).getConnection();
-        connection.begin();
-        Assert.assertTrue(connection.size() > 0);
+        try {
+            connection.begin();
+            Assert.assertTrue(connection.size() > 0);
 
-        // run a SPARQL test to see if the returned data is correct
-        InputStream sparql = this.getClass().getResourceAsStream(sparqlFile);
-        BooleanQuery testLabel = connection.prepareBooleanQuery(QueryLanguage.SPARQL, IOUtils.toString(sparql, "UTF-8"));
-        Assert.assertTrue("SPARQL test query failed", testLabel.evaluate());
+            // run a SPARQL test to see if the returned data is correct
+            InputStream sparql = this.getClass().getResourceAsStream(sparqlFile);
+            BooleanQuery testLabel = connection.prepareBooleanQuery(QueryLanguage.SPARQL, IOUtils.toString(sparql, "UTF-8"));
+            Assert.assertTrue("SPARQL test query failed", testLabel.evaluate());
 
-        if(log.isDebugEnabled()) {
-            StringWriter out = new StringWriter();
-            connection.export(Rio.createWriter(RDFFormat.TURTLE, out));
-            log.debug("DATA:");
-            log.debug(out.toString());
+            if (log.isDebugEnabled()) {
+                StringWriter out = new StringWriter();
+                connection.export(Rio.createWriter(RDFFormat.TURTLE, out));
+                log.debug("DATA:");
+                log.debug(out.toString());
+            }
+        } finally {
+            connection.commit();
+            connection.close();
         }
-
-        connection.commit();
-        connection.close();
     }
 
 }
diff --git a/libraries/ldclient/ldclient-provider-freebase/src/test/java/org/apache/marmotta/ldclient/test/freebase/TestFreebaseProvider.java b/libraries/ldclient/ldclient-provider-freebase/src/test/java/org/apache/marmotta/ldclient/test/freebase/TestFreebaseProvider.java
index 34de383..6cc2cb1 100644
--- a/libraries/ldclient/ldclient-provider-freebase/src/test/java/org/apache/marmotta/ldclient/test/freebase/TestFreebaseProvider.java
+++ b/libraries/ldclient/ldclient-provider-freebase/src/test/java/org/apache/marmotta/ldclient/test/freebase/TestFreebaseProvider.java
@@ -17,9 +17,14 @@
  */
 package org.apache.marmotta.ldclient.test.freebase;
 
+import org.apache.marmotta.ldclient.exception.DataRetrievalException;
 import org.apache.marmotta.ldclient.test.provider.ProviderTestBase;
+import org.junit.Assume;
 import org.junit.Ignore;
 import org.junit.Test;
+import org.openrdf.rio.RDFParseException;
+
+import java.io.IOException;
 
 /**
  * Some tests over random data to Freebase to warranty that the provider
@@ -34,6 +39,28 @@
     private static final String SERGIO = "http://rdf.freebase.com/ns/m.07zqbwz";
     private static final String WAS = "http://rdf.freebase.com/ns/m.0h21k1c";
 
+    @Override
+    protected void testResource(String uri) throws Exception {
+        try {
+            super.testResource(uri);
+        } catch (final Exception e) {
+            // Unfortunately, freebase often serves corrupt/invalid/unparsable data, e.g. non-escaped quotes in literals
+            Assume.assumeFalse("Freebase provided invalid RDF data for <" + uri + ">", checkCauseStack(e, DataRetrievalException.class, IOException.class, DataRetrievalException.class, RDFParseException.class));
+            throw e;
+        }
+    }
+
+    @Override
+    protected void testResource(String uri, String sparqlFile) throws Exception {
+        try {
+            super.testResource(uri, sparqlFile);
+        } catch (final Exception e) {
+            // Unfortunately, freebase often serves corrupt/invalid/unparsable data, e.g. non-escaped quotes in literals
+            Assume.assumeFalse("Freebase provided invalid RDF data for <" + uri + ">", checkCauseStack(e, DataRetrievalException.class, IOException.class, DataRetrievalException.class, RDFParseException.class));
+            throw e;
+        }
+    }
+
     /**
      * Tests accessing ASF's page from Freebase.
      *
@@ -85,7 +112,6 @@
     }
 
     @Test
-    @Ignore("Error in the Response-Encoding: newline")
     public void test_m_04jpl() throws Exception {
         testResource("http://rdf.freebase.com/ns/m.04jpl");
     }
@@ -100,4 +126,17 @@
         testResource("http://rdf.freebase.com/ns/m.01d0fp");
     }
 
+
+    @SafeVarargs
+    protected static boolean checkCauseStack(Throwable t, Class<? extends Throwable>... stack) {
+        return checkCauseStack(t, 0, stack);
+    }
+
+    @SafeVarargs
+    private static boolean checkCauseStack(Throwable t, int i, Class<? extends Throwable>... stack) {
+        return i >= stack.length || stack[i].isInstance(t) && checkCauseStack(t.getCause(), i + 1, stack);
+    }
+
+
+
 }