/*
 * 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.text.names;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.nio.charset.Charset;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests the {@code HumanNameParser} class.
 */
public class HumanNameParserTest {

    private CSVParser parser;

    @Before
    public void setUp() throws Exception {
        parser = CSVParser.parse(
                HumanNameParserTest.class.getResource("testNames.txt"), 
                Charset.forName("UTF-8"), 
                CSVFormat.DEFAULT.withDelimiter('|').withHeader());
    }

    @After
    public void tearDown() throws Exception {
        if (parser != null) {
            parser.close();
        }
    }

    @Test(expected = NullPointerException.class)
    public void shouldThrowNullPointerException_WhenNullIsParsed() throws Exception {
        HumanNameParser parser = new HumanNameParser();
        parser.parse(null);
    }

    @Test
    public void testInputs() {
        for (CSVRecord record : parser) {
            validateRecord(record);
        }
    }

    
    /**
     * Validates a line in the testNames.txt file.
     *
     * @param record a CSVRecord representing one record in the input file.
     */
    private void validateRecord(CSVRecord record) {
        HumanNameParser parser = new HumanNameParser();
        Name result = parser.parse(record.get(Colums.Name));

        long recordNum = record.getRecordNumber();
        assertThat("Wrong LeadingInit in record " + recordNum,
                result.getLeadingInitial(), equalTo(record.get(Colums.LeadingInit)));

        assertThat("Wrong FirstName in record " + recordNum,
                result.getFirstName(), equalTo(record.get(Colums.FirstName)));

        assertThat("Wrong NickName in record " + recordNum,
                result.getNickName(), equalTo(record.get(Colums.NickName)));

        assertThat("Wrong MiddleName in record " + recordNum,
                result.getMiddleName(), equalTo(record.get(Colums.MiddleName)));

        assertThat("Wrong LastName in record " + recordNum,
                result.getLastName(), equalTo(record.get(Colums.LastName)));

        assertThat("Wrong Suffix in record " + recordNum,
                result.getSuffix(), equalTo(record.get(Colums.Suffix)));
    }

    private enum Colums {
        Name,LeadingInit,FirstName,NickName,MiddleName,LastName,Suffix
    }
}

@@ -1261,25 +1261,70 @@
 e CSVParser 
-p
+inputParser;%0A    private HumanNameParser nameP
 arser;%0A%0A    
@@ -1378,25 +1378,30 @@
 n %7B%0A        
-p
+inputP
 arser = CSVP
@@ -1593,24 +1593,68 @@
 hHeader());%0A
+        nameParser = new HumanNameParser();%0A
     %7D%0A%0A    @
@@ -1717,17 +1717,22 @@
     if (
-p
+inputP
 arser !=
@@ -1748,25 +1748,30 @@
             
-p
+inputP
 arser.close(
@@ -1937,65 +1937,13 @@
     
-HumanNameParser parser = new HumanNameParser();%0A        p
+nameP
 arse
@@ -2037,17 +2037,22 @@
 ecord : 
-p
+inputP
 arser) %7B
@@ -2324,79 +2324,27 @@
     
-HumanNameParser parser = new HumanNameParser();%0A        Name result = p
+Name result = nameP
 arse

Use a shared parser instance for tests