All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.Ostermiller.util.LabeledCSVParserTests Maven / Gradle / Ivy

Go to download

Open source (GPL) Java utilities maintained by Stephen Ostermiller with help from many contributors.

The newest version!
/*
 * Tests reading files in comma separated value format with a fist line of labels.
 *
 * Copyright (C) 2004 Campbell, Allen T. 
 *
 * Copyright (C) 2004 Stephen Ostermiller
 * http://ostermiller.org/contact.pl?regarding=Java+Utilities
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * See COPYING.TXT for details.
 */

package com.Ostermiller.util;

import java.io.*;

/**
 * Tests reading files in comma separated value format with a fist line of labels.
 *
 * @author Campbell, Allen T. 
 * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
 * @since ostermillerutils 1.03.00
 */
class LabeledCSVParserTests {

	/**
	 * Main method for running test
	 * @param args command line arguments (ignored)
	 */
	public static void main(String[] args){
		try {
			test01();
			test02();
			test03();
		} catch (Exception x){
			x.printStackTrace();
			System.exit(1);
		}
	}

	private static void test01() throws Exception {
		// test01: empty input file
		LabeledCSVParser parse = new LabeledCSVParser(
			new CSVParser(
				new StringReader(
					""
				)
			)
		);

		if (parse.getLine() != null){
			throw new Exception("Expected null from getLine()");
		}
		if (parse.getLabels() != null){
			throw new Exception("Expected null from getLabels()");
		}

		if (parse.getLastLineNumber() != -1){
			throw new Exception("Expected -1 from getLastLineNumber()");
		}
		parse.close();
	}

	private static void test02() throws Exception {
		// test02: input has only labels
		LabeledCSVParser parse = new LabeledCSVParser(
			new CSVParser(
				new StringReader(
					"FIELD01,FIELD02,FIELD03"
				)
			)
		);
		if (parse.getLine() != null){
			throw new Exception("Expected null from getLine()");
		}
		String[] labels = parse.getLabels();
		if (labels.length != 3 || !"FIELD01".equals(labels[0]) || !"FIELD02".equals(labels[1]) || !"FIELD03".equals(labels[2])){
			throw new Exception("Didn't get expected labels.");
		}
		if (parse.getLastLineNumber() != -1){
			throw new Exception("Expected -1 from getLastLineNumber()");
		}
		parse.close();
	}

	private static void test03() throws Exception {
		// test03: labels + data
		LabeledCSVParser parse = new LabeledCSVParser(
			new CSVParser(
				new StringReader(
					"FIELD01,FIELD02,FIELD03\n" +
					"9.23,\"FOO\",\"BAR\"\n" +
					"10.5,\"BAZ\",\"FOO2\"\n"
				)
			)
		);
		String[] labels = parse.getLabels();
		if (labels.length != 3 || !"FIELD01".equals(labels[0]) || !"FIELD02".equals(labels[1]) || !"FIELD03".equals(labels[2])){
			throw new Exception("Didn't get expected labels.");
		}
		if (parse.getLabelIdx("FIELD01") != 0){
			throw new Exception("FIELD01 expected index of zero");
		}
		if (parse.getLabelIdx("FIELD02") != 1){
			throw new Exception("FIELD02 expected index of one");
		}
		if (parse.getLabelIdx("FIELD03") != 2){
			throw new Exception("FIELD03 expected index of two");
		}
		if (parse.getLabelIdx("FIELD04") != -1){
			throw new Exception("FIELD04 expected not to be present");
		}
		if (parse.getValueByLabel("FIELD01") != null){
			throw new Exception("Line not yet read, expected value of FIELD01 to be null.");
		}

		String[] line = parse.getLine();
		if (line == null){
			throw new Exception("getLine() null on line 1");
		}
		if (parse.getLastLineNumber() != 1){
			throw new Exception("Expected 1 from getLastLineNumber()");
		}
		if (line.length != 3 || !"9.23".equals(line[0]) || !"FOO".equals(line[1]) || !"BAR".equals(line[2])){
			throw new Exception("Didn't get expected line.");
		}
		if (!"9.23".equals(parse.getValueByLabel("FIELD01"))){
			throw new Exception("FIELD01 expected to have value 9.23.");
		}

		String value = parse.nextValue();
		if (!"10.5".equals(value)){
			throw new Exception("Expected nextValue to return 10.5.");
		}
		try {
			parse.getValueByLabel("FIELD01");
			throw new Exception("IllegalStateException expected");
		} catch (IllegalStateException iex){
			// expected
		}
		if (parse.getLastLineNumber() != 2){
			throw new Exception("Expected 2 from getLastLineNumber()");
		}
		line = parse.getLine();
		if (line == null){
			throw new Exception("getLine() null on line 2");
		}
		if (parse.getLastLineNumber() != 2){
			throw new Exception("Expected 2 from getLastLineNumber()");
		}
		if (line.length != 2 || !"BAZ".equals(line[0]) || !"FOO2".equals(line[1])){
			throw new Exception("Didn't get expected line.");
		}
		try {
			parse.getValueByLabel("FIELD01");
			throw new Exception("IllegalStateException expected");
		} catch (IllegalStateException iex){
			// expected
		}

		if (parse.getLine() != null){
			throw new Exception("Expected null from getLine()");
		}
		parse.close();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy