You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2012/01/24 09:12:44 UTC

svn commit: r1235160 - in /incubator/jena/Jena2/ARQ/trunk/src: main/java/com/hp/hpl/jena/sparql/resultset/ test/java/com/hp/hpl/jena/sparql/resultset/

Author: andy
Date: Tue Jan 24 08:12:44 2012
New Revision: 1235160

URL: http://svn.apache.org/viewvc?rev=1235160&view=rev
Log:
JENA-200 TSVInput header row processing.

Modified:
    incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInput.java
    incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java
    incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat1.java
    incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java

Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInput.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInput.java?rev=1235160&r1=1235159&r2=1235160&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInput.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInput.java Tue Jan 24 08:12:44 2012
@@ -54,13 +54,18 @@ public class TSVInput {
         {
         	//Here we try to parse only the Header Row
         	str = reader.readLine();
-        	String[] tokens = pattern.split(str,-1);
-        	for ( String token : tokens ) 
+        	if (str == null ) 
+        	    throw new ARQException("TSV Results malformed - input is empty (no header row)") ;
+        	if ( ! str.isEmpty() )
         	{
-        		if (token.startsWith("?")) token = token.substring(1);
-        		Var var = Var.alloc(token);
-        		vars.add(var);
-        		varNames.add(var.getName());
+            	String[] tokens = pattern.split(str,-1);
+            	for ( String token : tokens ) 
+            	{
+            		if (token.startsWith("?")) token = token.substring(1);
+            		Var var = Var.alloc(token);
+            		vars.add(var);
+            		varNames.add(var.getName());
+            	}
         	}
         } 
         catch ( IOException ex )

Modified: incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java?rev=1235160&r1=1235159&r2=1235160&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java Tue Jan 24 08:12:44 2012
@@ -82,22 +82,33 @@ public class TSVInputIterator extends Qu
 	
 	private boolean parseNextBinding()
 	{
-		String line;
-		try 
-		{
-			line = this.reader.readLine();
-			//Once EOF has been reached we'll see null for this call so we can return false because there are no further bindings
-			if (line == null) return false;
-		} 
-		catch (IOException e) 
-		{ throw new QueryException("Error parsing TSV results - " + e.getMessage()); 
-		}
-		String[] tokens = TSVInput.pattern.split(line, -1);
-		
+	    String line;
+	    try 
+	    {
+	        line = this.reader.readLine();
+	        //Once EOF has been reached we'll see null for this call so we can return false because there are no further bindings
+	        if (line == null) return false;
+	    } 
+	    catch (IOException e) 
+	    { throw new QueryException("Error parsing TSV results - " + e.getMessage()); }
+
+	    if ( line.isEmpty() )
+	    {
+	        // Empty input line - no bindings.
+	    	// Only valid when we expect zero/one values as otherwise we should get a sequence of tab characters
+	    	// which means a non-empty string which we handle normally
+	    	if (expectedItems > 1) throw new QueryException(String.format("Error Parsing TSV results - A result row had 0/1 values when %d were expected", expectedItems));
+	        this.binding = BindingFactory.create() ;
+	        return true ;
+	    }
+	    
+        String[] tokens = TSVInput.pattern.split(line, -1);
+	    
         if (tokens.length != expectedItems)
         	 throw new QueryException(String.format("Error Parsing TSV results - A result row had %d values instead of the expected %d.", tokens.length, expectedItems));
 
         this.binding = BindingFactory.create();
+        
         for ( int i = 0; i < tokens.length; i++ ) 
         {
         	String token = tokens[i];

Modified: incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat1.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat1.java?rev=1235160&r1=1235159&r2=1235160&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat1.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat1.java Tue Jan 24 08:12:44 2012
@@ -45,37 +45,52 @@ import com.hp.hpl.jena.vocabulary.XSD;
 @RunWith(Parameterized.class)
 public class TestResultSetFormat1
 {
-    static String[] $rs1 = {
+    // A result set of no variables and no rows.
+    static String[] $rs0 = { "(resultset ())" } ;
+    
+    // A result set of no variables and one row (e.g SELECT * {})
+    static String[] $rs1 = { "(resultset () (row))" } ;
+
+    static String[] $rs2 = {
         "(resultset (?a ?b ?c)",
         "  (row (?a 1) (?b 2)       )",
         "  (row (?a 1) (?b 4) (?c 3))",
         ")"} ;
 
-    static String[] $rs2 = {
+    static String[] $rs3 = {
         "(resultset (?a ?b ?c)",
         "  (row (?a 1) (?b 4) (?c 3))",
         "  (row (?a 1) (?b 2)       )",
         ")"} ;
 
-    static String[] $rs3 = {
+    static String[] $rs4 = {
         "(resultset (?a ?b ?c)", 
         "  (row (?a 1)        (?c 4))",
         "  (row (?a 1) (?b 2) (?c 3))",
         ")"} ;
     
-    static String[] $rs4 = {
+    static String[] $rs5 = {
+    	"(resultset (?a ?b)",
+    	" (row (?a 1)       )",
+    	" (row        (?b 2))",
+    	")" };
+    
+    static String[] $rs6 = {
     	"(resultset (?x)",
     	" (row (?x <" + RDF.type.toString() + ">))",
     	" (row (?x <" + RDFS.label.toString() + ">))",
     	" (row (?x <" + XSD.integer.toString() + ">))",
     	" (row (?x <" + OWL.sameAs.toString() + ">))",
+    	" (row )",
     	")" };
-
+    
+    static String[] $rs7 = {
+        "(resultset (?x) (row))" } ;
     
     @Parameters
     public static Collection<Object[]> data()
     {
-        return Arrays.asList(new Object[][] { {$rs1}, {$rs2}, {$rs3}, {$rs4} } ) ;
+        return Arrays.asList(new Object[][] { {$rs0}, {$rs1}, {$rs2}, {$rs3}, {$rs4}, {$rs5}, {$rs6} , {$rs7} } ) ;
     }
 
     private final String[] $rs ;

Modified: incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java?rev=1235160&r1=1235159&r2=1235160&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java Tue Jan 24 08:12:44 2012
@@ -26,33 +26,92 @@ import org.openjena.atlas.lib.StrUtils ;
 import com.hp.hpl.jena.query.QueryException ;
 import com.hp.hpl.jena.query.ResultSet ;
 import com.hp.hpl.jena.query.ResultSetFactory ;
+import com.hp.hpl.jena.sparql.ARQException;
 import com.hp.hpl.jena.sparql.engine.binding.Binding ;
 
 public class TestResultSetFormat2
 {
+    @Test
+    public void resultset_tsv_01()
+    {
+        // Empty Header Row (no variables), no rows.
+        parseTSV("\n");
+    }
+    
+    @Test 
+    public void resultset_tsv_02()
+    {
+        // No vars, one row.
+        String x = "\n\n" ;
+        parseTSV(x);
+    }
+    
+    @Test
+    public void resultset_tsv_03()
+    {
+        // One var, one row empty (unbound)
+        String x = "?x\n\n";
+        parseTSV(x);
+    }
+
+    @Test 
+    public void resultset_tsv_04()
+    {
+        // One var, no rows.
+        String x = "?x\n" ;
+        parseTSV(x);
+    }
+
+    @Test 
+    public void resultset_tsv_05()
+    {
+        // One var, one rows.
+        String x = "?x\n'a'\n" ;
+        parseTSV(x);
+    }
+    
+    @Test
+    public void resultset_tsv_06()
+    {
+    	// Two vars, one row empty other than the tab separator which is required
+    	// when two or more variables are present
+    	String x = "?x\t?y\n\t\n";
+    	parseTSV(x);
+    }
+    
     @Test (expected=QueryException.class) 
-    public void resultset_10()
+    public void resultset_bad_tsv_01()
     {
-        // This is illegal
         // Two vars, row of 3 values.
         String x = "?x\t?y\n'a'\t'b'\t'c'" ;
-        byte[] b = StrUtils.asUTF8bytes(x) ;
-        ByteArrayInputStream in = new ByteArrayInputStream(b) ;
-        ResultSet rs2 = ResultSetFactory.fromTSV(in) ;
-        
-        while (rs2.hasNext())
-        {
-        	Binding binding = rs2.nextBinding();
-        	System.out.println(binding);
-        }
+        parseTSV(x);
     }
 
     @Test (expected=QueryException.class) 
-    public void resultset_11()
+    public void resultset_bad_tsv_02()
     {
-        // This is illegal
         // Two vars, row of 1 value only.
         String x = "?x\t?y\n'a'" ;
+        parseTSV(x);
+    }
+
+    @Test (expected=ARQException.class)
+    public void resultset_bad_tsv_03()
+    {
+    	// No input
+    	parseTSV("");
+    }
+    
+    @Test (expected=QueryException.class)
+    public void resultset_bad_tsv_04()
+    {
+    	//Two vars but a completely empty row (should contain a tab)
+    	String x = "?x\t?y\n\n";
+    	parseTSV(x);
+    }
+    
+    public void parseTSV(String x)
+    {
         byte[] b = StrUtils.asUTF8bytes(x) ;
         ByteArrayInputStream in = new ByteArrayInputStream(b) ;
         ResultSet rs2 = ResultSetFactory.fromTSV(in) ;
@@ -60,8 +119,7 @@ public class TestResultSetFormat2
         while (rs2.hasNext())
         {
         	Binding binding = rs2.nextBinding();
-        	System.out.println(binding);
         }
-    }    
+    }
     
 }