jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rve...@apache.org
Subject [14/50] [abbrv] git commit: 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
Date Thu, 02 Oct 2014 09:48:23 GMT
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

git-svn-id: http://svn.apache.org/repos/asf/jena/Experimental/jena-csv@1616480 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/973eab60
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/973eab60
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/973eab60

Branch: refs/heads/jena-csv
Commit: 973eab60997f8deb01c997025191cca183ed9883
Parents: 4522c99
Author: Ying Jiang <jpz6311whu@apache.org>
Authored: Thu Aug 7 12:31:53 2014 +0000
Committer: Ying Jiang <jpz6311whu@apache.org>
Committed: Thu Aug 7 12:31:53 2014 +0000

----------------------------------------------------------------------
 .../impl/PropertyTableBuilder.java              |  25 +++-
 .../jena/propertytable/AbstractColumnTest.java  |  10 --
 .../org/apache/jena/propertytable/BaseTest.java |  12 ++
 .../jena/propertytable/TS_PropertyTable.java    |   6 +-
 .../impl/AbstractPropertyTableBuilderTest.java  | 142 +++++++++++++++++++
 .../impl/PropertyTableArrayImplTest.java        |  36 +++++
 .../PropertyTableBuilderForArrayImplTest.java   |  21 +++
 .../PropertyTableBuilderForHashMapImplTest.java |  16 +++
 8 files changed, 252 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/973eab60/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java b/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java
index ed64018..7a2a74f 100644
--- a/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java
+++ b/src/main/java/org/apache/jena/propertytable/impl/PropertyTableBuilder.java
@@ -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;
+	}
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/973eab60/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java b/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java
index 26f6a85..0682618 100644
--- a/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java
+++ b/src/test/java/org/apache/jena/propertytable/AbstractColumnTest.java
@@ -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 extends BaseTest{
 		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;
-	}
 
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/973eab60/src/test/java/org/apache/jena/propertytable/BaseTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/jena/propertytable/BaseTest.java b/src/test/java/org/apache/jena/propertytable/BaseTest.java
index 3402704..282f649 100644
--- a/src/test/java/org/apache/jena/propertytable/BaseTest.java
+++ b/src/test/java/org/apache/jena/propertytable/BaseTest.java
@@ -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;
+	}
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/973eab60/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java b/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java
index 5c3c124..9c916e9 100644
--- a/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java
+++ b/src/test/java/org/apache/jena/propertytable/TS_PropertyTable.java
@@ -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 {
 

http://git-wip-us.apache.org/repos/asf/jena/blob/973eab60/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java
b/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java
new file mode 100644
index 0000000..ae7f565
--- /dev/null
+++ b/src/test/java/org/apache/jena/propertytable/impl/AbstractPropertyTableBuilderTest.java
@@ -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);
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/973eab60/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java
b/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java
index 6669a9d..99e98ad 100644
--- a/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java
+++ b/src/test/java/org/apache/jena/propertytable/impl/PropertyTableArrayImplTest.java
@@ -20,7 +20,9 @@ package org.apache.jena.propertytable.impl;
 
 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 extends AbstractPropertyTableTest{
 		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));
+		}
+	}
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/973eab60/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java
b/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java
new file mode 100644
index 0000000..ce83f98
--- /dev/null
+++ b/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForArrayImplTest.java
@@ -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;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/973eab60/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java
b/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java
new file mode 100644
index 0000000..6585327
--- /dev/null
+++ b/src/test/java/org/apache/jena/propertytable/impl/PropertyTableBuilderForHashMapImplTest.java
@@ -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;
+	}
+}


Mime
View raw message