jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jpz6311...@apache.org
Subject svn commit: r1616480 - in /jena/Experimental/jena-csv/src: main/java/org/apache/jena/propertytable/impl/ test/java/org/apache/jena/propertytable/ test/java/org/apache/jena/propertytable/impl/
Date Thu, 07 Aug 2014 12:31:54 GMT
Author: jpz6311whu
Date: Thu Aug  7 12:31:53 2014
New Revision: 1616480

URL: http://svn.apache.org/r1616480
Log:
JENA 625 test improvements:
1. refactor PropertyTableBuilder for unit tests
2. tests and bug fixes for parsing irregular csv table
3. out of bound tests for PropertyTableArrayImpl

Added:
    jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java
  (with props)
    jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java
  (with props)
    jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java
  (with props)
Modified:
    jena/Experimental/jena-csv/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java
    jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java
    jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/BaseTest.java
    jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java
    jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java

Modified: jena/Experimental/jena-csv/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java?rev=1616480&r1=1616479&r2=1616480&view=diff
==============================================================================
--- jena/Experimental/jena-csv/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java
(original)
+++ jena/Experimental/jena-csv/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java
Thu Aug  7 12:31:53 2014
@@ -18,10 +18,13 @@
 
 package org.apache.jena.propertytable.impl;
 
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.jena.atlas.csv.CSVParser;
+import org.apache.jena.atlas.csv.CSVTokenIterator;
+import org.apache.jena.atlas.io.IO;
 import org.apache.jena.propertytable.PropertyTable;
 import org.apache.jena.propertytable.Row;
 import org.apache.jena.riot.lang.LangCSV;
@@ -65,12 +68,17 @@ public class PropertyTableBuilder {
 		}
 	}
 
+	protected static PropertyTable fillPropertyTable(PropertyTable table, String csvFilePath
){
+		InputStream input = IO.openFile(csvFilePath) ;
+		CSVTokenIterator iterator = new CSVTokenIterator(input) ;
+		return fillPropertyTable(table, iterator, csvFilePath);
+	}
 	
-	private static PropertyTable fillPropertyTable(PropertyTable table, String csvFilePath ){
+	protected static PropertyTable fillPropertyTable(PropertyTable table, CSVTokenIterator iterator,
String csvFilePath){
 		if (table == null){
 			return null;
 		}
-		CSVParser parser = CSVParser.create(csvFilePath);
+		CSVParser parser = new CSVParser (iterator);
 		List<String> rowLine = null;
 		ArrayList<Node> predicates = new ArrayList<Node>();
 		int rowNum = 0;
@@ -79,8 +87,7 @@ public class PropertyTableBuilder {
 			if (rowNum == 0) {
 				table.createColumn(CSV_ROW_NODE);
 				for (String column : rowLine) {
-					String uri = IRIResolver.resolveString(csvFilePath) + "#"
-							+ LangCSV.toSafeLocalname(column);
+					String uri = createColumnKeyURI(csvFilePath, column);
 					Node p = NodeFactory.createURI(uri);
 					predicates.add(p);
 					table.createColumn(p);
@@ -92,9 +99,12 @@ public class PropertyTableBuilder {
 				row.setValue(table.getColumn(CSV_ROW_NODE), NodeFactory.createLiteral(
 						(rowNum + ""), XSDDatatype.XSDinteger));
 
-				for (int col = 0; col < rowLine.size(); col++) {
+				for (int col = 0; col < rowLine.size() && col<predicates.size(); col++)
{
 
 					String columnValue = rowLine.get(col).trim();
+					if("".equals(columnValue)){
+						continue;
+					}
 					Node o;
 					try {
 						// Try for a double.
@@ -111,4 +121,9 @@ public class PropertyTableBuilder {
 		}
 		return table;
 	}
+	
+	protected static String createColumnKeyURI(String csvFilePath, String column){
+		String uri = IRIResolver.resolveString(csvFilePath) + "#" + LangCSV.toSafeLocalname(column);
+		return uri;
+	}
 }

Modified: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java?rev=1616480&r1=1616479&r2=1616480&view=diff
==============================================================================
--- jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java
(original)
+++ jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java
Thu Aug  7 12:31:53 2014
@@ -18,7 +18,6 @@
 
 package org.apache.jena.propertytable;
 
-import java.util.Collection;
 import java.util.List;
 
 import org.junit.Assert;
@@ -68,19 +67,10 @@ public abstract class AbstractColumnTest
 		Assert.assertNull(table.getColumn( URI("nonExistentColumnName")));
 	}
 
-	
 	@Test
 	public void testGetTable() {
 		Column something = table.createColumn(URI("something"));
 		Assert.assertEquals(table, something.getTable());
 	}
-	protected static boolean collectionContains(
-			final Collection<Column> columns, final Node columnkey) {
-		for (final Column column : columns) {
-			if (column.getColumnKey().equals(columnkey))
-				return true;
-		}
-		return false;
-	}
 
 }

Modified: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/BaseTest.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/BaseTest.java?rev=1616480&r1=1616479&r2=1616480&view=diff
==============================================================================
--- jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/BaseTest.java (original)
+++ jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/BaseTest.java Thu
Aug  7 12:31:53 2014
@@ -18,6 +18,8 @@
 
 package org.apache.jena.propertytable;
 
+import java.util.Collection;
+
 import com.hp.hpl.jena.graph.Node;
 import com.hp.hpl.jena.graph.NodeFactory;
 
@@ -27,9 +29,19 @@ public abstract class BaseTest {
 	protected Row row;
 	private static final String ns = "eh:foo/bar#";
 	protected static final Node rowSubject = URI("rowSubject");
+	protected static final String csvFilePath = "src/test/resources/test.csv";
 	
 	
 	protected static Node URI(String localName) {
 		return NodeFactory.createURI(ns + localName);
 	}
+	
+	protected static boolean collectionContains(
+			final Collection<Column> columns, final Node columnkey) {
+		for (final Column column : columns) {
+			if (column.getColumnKey().equals(columnkey))
+				return true;
+		}
+		return false;
+	}
 }

Modified: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java?rev=1616480&r1=1616479&r2=1616480&view=diff
==============================================================================
--- jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java
(original)
+++ jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java
Thu Aug  7 12:31:53 2014
@@ -20,6 +20,8 @@ package org.apache.jena.propertytable;
 
 import org.apache.jena.propertytable.impl.GraphCSVTest;
 import org.apache.jena.propertytable.impl.PropertyTableArrayImplTest;
+import org.apache.jena.propertytable.impl.PropertyTableBuilderForArrayImplTest;
+import org.apache.jena.propertytable.impl.PropertyTableBuilderForHashMapImplTest;
 import org.apache.jena.propertytable.impl.PropertyTableHashMapImplTest;
 import org.junit.runner.RunWith;
 import org.junit.runners.Suite;
@@ -29,7 +31,9 @@ import org.junit.runners.Suite;
 @Suite.SuiteClasses( {
 	PropertyTableArrayImplTest.class,
 	PropertyTableHashMapImplTest.class,
-	GraphCSVTest.class
+	GraphCSVTest.class,
+	PropertyTableBuilderForArrayImplTest.class,
+	PropertyTableBuilderForHashMapImplTest.class,
 })
 public class TS_PropertyTable {
 

Added: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java?rev=1616480&view=auto
==============================================================================
--- jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java
(added)
+++ jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java
Thu Aug  7 12:31:53 2014
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.propertytable.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.jena.atlas.csv.CSVTokenIterator;
+import org.apache.jena.propertytable.BaseTest;
+import org.apache.jena.propertytable.Row;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.graph.NodeFactory;
+
+public abstract class AbstractPropertyTableBuilderTest extends BaseTest {
+
+	@Test
+	public void testFillPropertyTable() {
+		CSVTokenIterator iterator = csv("a,b\nc,d\ne,f");
+		PropertyTableBuilder.fillPropertyTable(table, iterator, csvFilePath);
+
+		Assert.assertEquals(3, table.getColumns().size());
+		containsColumn(PropertyTableBuilder.CSV_ROW_NODE);
+		containsColumn("a");
+		containsColumn("b");
+
+		Assert.assertEquals(2, table.getAllRows().size());
+		containsValue(0, "a", "c");
+		containsValue(0, "b", "d");
+
+		containsValue(1, "a", "e");
+		containsValue(1, "b", "f");
+
+	}
+
+	@Test
+	public void testIrregularTable1() {
+		CSVTokenIterator iterator = csv("a,b\nc\ne,f");
+		PropertyTableBuilder.fillPropertyTable(table, iterator, csvFilePath);
+
+		Assert.assertEquals(3, table.getColumns().size());
+		containsColumn(PropertyTableBuilder.CSV_ROW_NODE);
+		containsColumn("a");
+		containsColumn("b");
+
+		Assert.assertEquals(2, table.getAllRows().size());
+		containsValue(0, "a", "c");
+		nullValue(0, "b");
+
+		containsValue(1, "a", "e");
+		containsValue(1, "b", "f");
+	}
+
+	@Test
+	public void testIrregularTable2() {
+		CSVTokenIterator iterator = csv("a,b\nc,d1,d2\ne,f");
+		PropertyTableBuilder.fillPropertyTable(table, iterator, csvFilePath);
+
+		Assert.assertEquals(3, table.getColumns().size());
+		containsColumn(PropertyTableBuilder.CSV_ROW_NODE);
+		containsColumn("a");
+		containsColumn("b");
+
+		Assert.assertEquals(2, table.getAllRows().size());
+		containsValue(0, "a", "c");
+		containsValue(0, "b", "d1");
+
+		containsValue(1, "a", "e");
+		containsValue(1, "b", "f");
+	}
+
+	@Test
+	public void testIrregularTable3() {
+		CSVTokenIterator iterator = csv("a,b\n,d\ne,f");
+		PropertyTableBuilder.fillPropertyTable(table, iterator, csvFilePath);
+
+		Assert.assertEquals(3, table.getColumns().size());
+		containsColumn(PropertyTableBuilder.CSV_ROW_NODE);
+		containsColumn("a");
+		containsColumn("b");
+
+		Assert.assertEquals(2, table.getAllRows().size());
+		nullValue(0, "a");
+		containsValue(0, "b", "d");
+
+		containsValue(1, "a", "e");
+		containsValue(1, "b", "f");
+	}
+
+	private void nullValue(int rowIndex, String column) {
+		Row row = table.getAllRows().get(rowIndex);
+		Node v = row.getValue(NodeFactory.createURI(getColumnKey(column)));
+		Assert.assertEquals(null, v);
+	}
+
+	private void containsValue(int rowIndex, String column, String value) {
+		Row row = table.getAllRows().get(rowIndex);
+		Node v = row.getValue(NodeFactory.createURI(getColumnKey(column)));
+		Assert.assertEquals(value, v.getLiteralValue());
+	}
+
+	private String getColumnKey(String column) {
+		return PropertyTableBuilder.createColumnKeyURI(csvFilePath, column);
+	}
+
+	private void containsColumn(String column) {
+		containsColumn(NodeFactory.createURI(getColumnKey(column)));
+	}
+
+	private void containsColumn(Node columnKey) {
+		Assert.assertTrue(collectionContains(table.getColumns(), columnKey));
+	}
+
+	private CSVTokenIterator csv(String input) {
+		try {
+			InputStream in = new ByteArrayInputStream(input.getBytes("UTF-8"));
+			CSVTokenIterator iterator = new CSVTokenIterator(in);
+			return iterator;
+		} catch (UnsupportedEncodingException e) {
+			throw new RuntimeException(e);
+		}
+	}
+}

Propchange: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java?rev=1616480&r1=1616479&r2=1616480&view=diff
==============================================================================
--- jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java
(original)
+++ jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java
Thu Aug  7 12:31:53 2014
@@ -20,7 +20,9 @@ package org.apache.jena.propertytable.im
 
 import org.apache.jena.propertytable.AbstractPropertyTableTest;
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
+import org.junit.Test;
 
 public class PropertyTableArrayImplTest extends AbstractPropertyTableTest{
 	
@@ -41,4 +43,38 @@ public class PropertyTableArrayImplTest 
 		table2 = null;
 		row = null;
 	}
+	
+	@Test
+	public void testColumnOutofBounds1() {
+		for (int i=0;i<columnNum;i++){
+			table.createColumn(URI("something_"+i));
+		}
+		Assert.assertEquals(columnNum, table.getColumns().size());
+	}
+	
+	@Test(expected = IllegalArgumentException.class)
+	public void testColumnOutofBounds2() {
+		for (int i=0;i<columnNum+1;i++){
+			table.createColumn(URI("something_"+i));
+		}
+	}
+	
+	@Test
+	public void testRowOutofBounds1() {
+		
+		// we've already created a new Row in @Before
+		for (int i=0;i<rowNum-1;i++){
+			table.createRow(URI("something_"+i));
+		}
+		Assert.assertEquals(rowNum, table.getAllRows().size());
+	}
+	
+	@Test(expected = IllegalArgumentException.class)
+	public void testRowOutofBounds2() {
+		
+		// we've already created a new Row in @Before
+		for (int i=0;i<rowNum;i++){
+			table.createRow(URI("something_"+i));
+		}
+	}
 }

Added: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java?rev=1616480&view=auto
==============================================================================
--- jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java
(added)
+++ jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java
Thu Aug  7 12:31:53 2014
@@ -0,0 +1,21 @@
+package org.apache.jena.propertytable.impl;
+
+import org.junit.After;
+import org.junit.Before;
+
+public class PropertyTableBuilderForArrayImplTest extends AbstractPropertyTableBuilderTest{
+	
+	private static int rowNum = 10;
+	private static int columnNum = 10 ;
+	
+	@Before
+	public void setUp() {
+		table = new PropertyTableArrayImpl(rowNum, columnNum);
+	}
+
+	@After
+	public void tearDown() {
+		table = null;
+	}
+
+}

Propchange: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java?rev=1616480&view=auto
==============================================================================
--- jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java
(added)
+++ jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java
Thu Aug  7 12:31:53 2014
@@ -0,0 +1,16 @@
+package org.apache.jena.propertytable.impl;
+
+import org.junit.After;
+import org.junit.Before;
+
+public class PropertyTableBuilderForHashMapImplTest extends AbstractPropertyTableBuilderTest{
+	@Before
+	public void setUp() {
+		table = new PropertyTableHashMapImpl();
+	}
+
+	@After
+	public void tearDown() {
+		table = null;
+	}
+}

Propchange: jena/Experimental/jena-csv/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message