On 1 August 2013 22:11, <ggreg...@apache.org> wrote: > Author: ggregory > Date: Thu Aug 1 21:11:10 2013 > New Revision: 1509431 > > URL: http://svn.apache.org/r1509431 > Log: > CSVRecord.get(String) throws IAE if the column is not mapped (does not > exist). This is similar to what JDBC does in ResultSet.
OK >Add getBoolean(String) API and tests. -1 We don't have agreement to add these methods. Also please don't commit unrelated changes. > Added: > > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java > (with props) > Modified: > > commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java > > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java > > Modified: > commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java > URL: > http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java?rev=1509431&r1=1509430&r2=1509431&view=diff > ============================================================================== > --- > commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java > (original) > +++ > commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java > Thu Aug 1 21:11:10 2013 > @@ -80,12 +80,13 @@ public class CSVRecord implements Serial > * > * @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,17 +94,37 @@ public class CSVRecord implements Serial > "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. > * > * @return the comment for this record, or null if no comment for this > > Added: > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java > URL: > http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java?rev=1509431&view=auto > ============================================================================== > --- > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java > (added) > +++ > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java > Thu Aug 1 21:11:10 2013 > @@ -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; > + } > + > +} > > Propchange: > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java > ------------------------------------------------------------------------------ > svn:eol-style = native > > Propchange: > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordBooleanTest.java > ------------------------------------------------------------------------------ > svn:keywords = Id > > Modified: > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java > URL: > http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java?rev=1509431&r1=1509430&r2=1509431&view=diff > ============================================================================== > --- > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java > (original) > +++ > commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java > Thu Aug 1 21:11:10 2013 > @@ -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<String, Integer> 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()); > > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org