Package org.apache.jena.propertytable.impl

Source Code of org.apache.jena.propertytable.impl.AbstractPropertyTableBuilderTest

/*
* 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;


/**
* Tests related to PropertyTableBuilder, or more explicitly for the CSV parser in the current release.
*
*/
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);
    }
  }
}
TOP

Related Classes of org.apache.jena.propertytable.impl.AbstractPropertyTableBuilderTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.