From 50b7f793381ca6d8fd74a01d55d214c1c3e5762d Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Thu, 1 Aug 2013 21:11:10 +0000 Subject: [PATCH] CSVRecord.get(String) throws IAE if the column is not mapped (does not exist). This is similar to what JDBC does in ResultSet. Add getBoolean(String) API and tests. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1509431 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/csv/CSVRecord.java | 35 ++++++++--- .../commons/csv/CSVRecordBooleanTest.java | 63 +++++++++++++++++++ .../org/apache/commons/csv/CSVRecordTest.java | 21 ++++++- 3 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java index 455dac21..24bf4b85 100644 --- a/src/main/java/org/apache/commons/csv/CSVRecord.java +++ b/src/main/java/org/apache/commons/csv/CSVRecord.java @@ -80,12 +80,13 @@ public class CSVRecord implements Serializable, Iterable { * * @param name * the name of the column to be retrieved. - * @return the column value, or {@code null} if the column name is not found + * @return the column value, maybe null depending on {@link CSVFormat#getNullString()}. * @throws IllegalStateException * if no header mapping was provided * @throws IllegalArgumentException - * if the record is inconsistent + * if {@code name} is not mapped or if the record is inconsistent * @see #isConsistent() + * @see CSVFormat#withNullString(String) */ public String get(final String name) { if (mapping == null) { @@ -93,16 +94,36 @@ public class CSVRecord implements Serializable, Iterable { "No header mapping was specified, the record values can't be accessed by name"); } final Integer index = mapping.get(name); + if (index == null) { + throw new IllegalArgumentException(String.format("Mapping for %s not found, expected one of %s", name, + mapping.keySet())); + } try { - return index != null ? values[index.intValue()] : null; + return values[index.intValue()]; } catch (final ArrayIndexOutOfBoundsException e) { - throw new IllegalArgumentException( - String.format( - "Index for header '%s' is %d but CSVRecord only has %d values!", - name, index, Integer.valueOf(values.length))); + throw new IllegalArgumentException(String.format( + "Index for header '%s' is %d but CSVRecord only has %d values!", name, index, + Integer.valueOf(values.length))); } } + /** + * Returns a value by name. + * + * @param name + * the name of the column to be retrieved. + * @return the column value + * @throws IllegalStateException + * if no header mapping was provided + * @throws IllegalArgumentException + * if the record is inconsistent + * @see #isConsistent() + */ + public boolean getBoolean(String name) { + String s = this.get(name); + return s != null ? Boolean.parseBoolean(s) : false; + } + /** * Returns the comment for this record, if any. * diff --git a/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java b/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java new file mode 100644 index 00000000..ea618858 --- /dev/null +++ b/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java @@ -0,0 +1,63 @@ +/* + * 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.commons.csv; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class CSVRecordBooleanTest { + + private CSVRecord record; + + @Before + public void setUp() throws IOException { + this.record = createTestRecord(); + } + + @Test + public void testGetBooleanByString() { + Assert.assertEquals(Boolean.TRUE, Boolean.valueOf(record.getBoolean("A"))); + Assert.assertEquals(Boolean.TRUE, Boolean.valueOf(record.getBoolean("B"))); + Assert.assertEquals(Boolean.FALSE, Boolean.valueOf(record.getBoolean("C"))); + Assert.assertEquals(Boolean.FALSE, Boolean.valueOf(record.getBoolean("D"))); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetBooleanByMissingString() { + Assert.assertEquals(null, Boolean.valueOf(record.getBoolean("ABSENT"))); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetBooleanByNullString() { + Assert.assertEquals(null, Boolean.valueOf(record.getBoolean(null))); + } + + /** + * @return + * @throws IOException + */ + private CSVRecord createTestRecord() throws IOException { + String csv = "A,B,C,D\ntrue, TRUE, false, foo"; + CSVRecord record = CSVParser.parseString(csv, CSVFormat.DEFAULT.withHeader().withIgnoreSurroundingSpaces(true)) + .iterator().next(); + return record; + } + +} diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java b/src/test/java/org/apache/commons/csv/CSVRecordTest.java index 0bf7dddd..46fbdef9 100644 --- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java +++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java @@ -29,6 +29,8 @@ import org.junit.Test; public class CSVRecordTest { + private enum EnumFixture { UNKNOWN_COLUMN }; + private String[] values; private CSVRecord record, recordWithHeader; private Map header; @@ -69,11 +71,26 @@ public class CSVRecordTest { recordWithHeader.get("fourth"); } - @Test - public void testGetUnmapped() { + @Test(expected = IllegalArgumentException.class) + public void testGetUnmappedName() { assertNull(recordWithHeader.get("fourth")); } + @Test(expected = IllegalArgumentException.class) + public void testGetUnmappedEnum() { + assertNull(recordWithHeader.get(EnumFixture.UNKNOWN_COLUMN)); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testGetUnmappedNegativeInt() { + assertNull(recordWithHeader.get(Integer.MIN_VALUE)); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testGetUnmappedPositiveInt() { + assertNull(recordWithHeader.get(Integer.MAX_VALUE)); + } + @Test public void testIsConsistent() { assertTrue(record.isConsistent());