commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject [1/5] [text] Use Commons CSV to parse input data for ParserTest
Date Sun, 19 Apr 2015 09:48:37 GMT
Repository: commons-text
Updated Branches:
  refs/heads/master c4e8a3e0e -> 65852f808


Use Commons CSV to parse input data for ParserTest


Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/49ae4553
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/49ae4553
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/49ae4553

Branch: refs/heads/master
Commit: 49ae4553c1599ab82d77cf29684878e4b11b610f
Parents: c4e8a3e
Author: Benedikt Ritter <britter@apache.org>
Authored: Sun Apr 19 11:38:48 2015 +0200
Committer: Benedikt Ritter <britter@apache.org>
Committed: Sun Apr 19 11:38:48 2015 +0200

----------------------------------------------------------------------
 pom.xml                                         |   7 ++
 .../apache/commons/text/names/ParserTest.java   | 100 +++++++++----------
 .../org/apache/commons/text/names/testNames.txt |  63 ++++++------
 3 files changed, 87 insertions(+), 83 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/49ae4553/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index cdfa9c2..f26a8a5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -84,6 +84,13 @@
       <version>1.3</version>
       <scope>test</scope>
     </dependency>
+    <!-- Used to parse inputs for o.a.c.t.names.ParserTest-->
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-csv</artifactId>
+      <version>1.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <distributionManagement>

http://git-wip-us.apache.org/repos/asf/commons-text/blob/49ae4553/src/test/java/org/apache/commons/text/names/ParserTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/names/ParserTest.java b/src/test/java/org/apache/commons/text/names/ParserTest.java
index 3e4c9d8..6a3371f 100644
--- a/src/test/java/org/apache/commons/text/names/ParserTest.java
+++ b/src/test/java/org/apache/commons/text/names/ParserTest.java
@@ -22,10 +22,17 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.logging.Logger;
 
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVParser;
+import org.apache.commons.csv.CSVRecord;
 import org.apache.commons.lang3.StringUtils;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.Ignore;
 import org.junit.Test;
 
 /**
@@ -33,70 +40,59 @@ import org.junit.Test;
  */
 public class ParserTest {
 
-    private static final Logger LOGGER = Logger.getLogger(ParserTest.class.getName());
+    private CSVParser parser;
 
-    private static File testNames = null;
+    @Before
+    public void setUp() throws Exception {
+        parser = CSVParser.parse(
+                ParserTest.class.getResource("testNames.txt"), 
+                Charset.forName("UTF-8"), 
+                CSVFormat.DEFAULT.withDelimiter('|').withHeader());
+    }
 
-    @BeforeClass
-    public static void setUp() {
-        testNames = new File(ParserTest.class.getResource("/org/apache/commons/text/names/testNames.txt").getFile());
+    @After
+    public void tearDown() throws Exception {
+        if (parser != null) {
+            parser.close();
+        }
     }
 
     @Test
-    public void testAll() throws IOException {
-        BufferedReader buffer = null;
-        FileReader reader = null;
-
-        try {
-            reader = new FileReader(testNames);
-            buffer = new BufferedReader(reader);
-
-            String line = null;
-            while ((line = buffer.readLine()) != null) {
-                if (StringUtils.isBlank(line)) {
-                    LOGGER.warning("Empty line in testNames.txt");
-                    continue;
-                }
-
-                String[] tokens = line.split("\\|");
-                if (tokens.length != 7) {
-                    LOGGER.warning(String.format("Invalid line in testNames.txt: %s", line));
-                    continue;
-                }
-
-                validateLine(tokens);
-            }
-        } finally {
-            if (reader != null)
-                reader.close();
-            if (buffer != null)
-                buffer.close();
+    public void testInputs() {
+        for (CSVRecord record : parser) {
+            validateRecord(record);
         }
     }
 
+    
     /**
      * Validates a line in the testNames.txt file.
      *
-     * @param tokens the tokens with leading spaces
+     * @param record the tokens with leading spaces
      */
-    private void validateLine(String[] tokens) {
-        String name = tokens[0].trim();
-
-        String leadingInit = tokens[1].trim();
-        String first = tokens[2].trim();
-        String nickname = tokens[3].trim();
-        String middle = tokens[4].trim();
-        String last = tokens[5].trim();
-        String suffix = tokens[6].trim();
-
-        HumanNameParser parser = new HumanNameParser(name);
-
-        assertEquals(leadingInit, parser.getLeadingInit());
-        assertEquals(first, parser.getFirst());
-        assertEquals(nickname, parser.getNickname());
-        assertEquals(middle, parser.getMiddle());
-        assertEquals(last, parser.getLast());
-        assertEquals(suffix, parser.getSuffix());
+    private void validateRecord(CSVRecord record) {
+        HumanNameParser parser = new HumanNameParser(record.get(Colums.Name));
+
+        assertEquals("Wrong LeadingInit in record " + record.getRecordNumber(), 
+                record.get(Colums.LeadingInit), parser.getLeadingInit());
+        
+        assertEquals("Wrong FirstName in record " + record.getRecordNumber(), 
+                record.get(Colums.FirstName), parser.getFirst());
+        
+        assertEquals("Wrong NickName in record " + record.getRecordNumber(), 
+                record.get(Colums.NickName), parser.getNickname());
+        
+        assertEquals("Wrong MiddleName in record " + record.getRecordNumber(), 
+                record.get(Colums.MiddleName), parser.getMiddle());
+        
+        assertEquals("Wrong LastName in record " + record.getRecordNumber(), 
+                record.get(Colums.LastName), parser.getLast());
+        
+        assertEquals("Wrong Suffix in record " + record.getRecordNumber(), 
+                record.get(Colums.Suffix), parser.getSuffix());
     }
 
+    private enum Colums {
+        Name,LeadingInit,FirstName,NickName,MiddleName,LastName,Suffix
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-text/blob/49ae4553/src/test/resources/org/apache/commons/text/names/testNames.txt
----------------------------------------------------------------------
diff --git a/src/test/resources/org/apache/commons/text/names/testNames.txt b/src/test/resources/org/apache/commons/text/names/testNames.txt
index 83ddc31..8e32bf1 100644
--- a/src/test/resources/org/apache/commons/text/names/testNames.txt
+++ b/src/test/resources/org/apache/commons/text/names/testNames.txt
@@ -1,31 +1,32 @@
-Björn O'Malley| | Björn| | | O'Malley| 
-Bin Lin| | Bin| | | Lin| 
-Linda Jones| | Linda| | | Jones| 
-Jason H. Priem| | Jason| | H.| Priem| 
-Björn O'Malley-Muñoz| | Björn| | | O'Malley-Muñoz| 
-Björn C. O'Malley| | Björn| | C.| O'Malley| 
-Björn "Bill" O'Malley| | Björn| Bill| | O'Malley| 
-Björn ("Bill") O'Malley| | Björn| Bill| | O'Malley| 
-Björn ("Wild Bill") O'Malley| | Björn| Wild Bill| | O'Malley| 
-Björn (Bill) O'Malley| | Björn| Bill| | O'Malley| 
-Björn 'Bill' O'Malley| | Björn| Bill| | O'Malley| 
-Björn C O'Malley| | Björn| | C| O'Malley| 
-Björn C. R. O'Malley| | Björn| | C. R.| O'Malley| 
-Björn Charles O'Malley| | Björn| | Charles| O'Malley| 
-Björn Charles R. O'Malley| | Björn| | Charles R.| O'Malley| 
-Björn van O'Malley| | Björn| | | van O'Malley| 
-Björn Charles van der O'Malley| | Björn| | Charles| van der O'Malley| 
-Björn Charles O'Malley y Muñoz| | Björn| | Charles| O'Malley y Muñoz| 
-Björn O'Malley, Jr.| | Björn| | | O'Malley| Jr.
-Björn O'Malley Jr| | Björn| | | O'Malley| Jr
-B O'Malley| | B| | | O'Malley| 
-William Carlos Williams| | William| | Carlos| Williams| 
-C. Björn Roger O'Malley| C.| Björn| | Roger| O'Malley| 
-B. C. O'Malley| | B.| | C.| O'Malley| 
-B C O'Malley| | B| | C| O'Malley| 
-B.J. Thomas| | B.J.| | | Thomas| 
-O'Malley, Björn| | Björn| | | O'Malley| 
-O'Malley, Björn Jr| | Björn| | | O'Malley| Jr
-O'Malley, C. Björn| C.| Björn| | | O'Malley| 
-O'Malley, C. Björn III| C.| Björn| | | O'Malley| III
-O'Malley y Muñoz, C. Björn Roger III| C.| Björn| | Roger| O'Malley y Muñoz| III
\ No newline at end of file
+Name|LeadingInit|FirstName|NickName|MiddleName|LastName|Suffix
+Björn O'Malley||Björn|||O'Malley|
+Bin Lin||Bin|||Lin|
+Linda Jones||Linda|||Jones|
+Jason H. Priem||Jason||H.|Priem|
+Björn O'Malley-Muñoz||Björn|||O'Malley-Muñoz|
+Björn C. O'Malley||Björn||C.|O'Malley|
+Björn "Bill" O'Malley||Björn|Bill||O'Malley|
+Björn ("Bill") O'Malley||Björn|Bill||O'Malley|
+Björn ("Wild Bill") O'Malley||Björn|Wild Bill||O'Malley|
+Björn (Bill) O'Malley||Björn|Bill||O'Malley|
+Björn 'Bill' O'Malley||Björn|Bill||O'Malley|
+Björn C O'Malley||Björn||C|O'Malley|
+Björn C. R. O'Malley||Björn||C. R.|O'Malley|
+Björn Charles O'Malley||Björn||Charles|O'Malley|
+Björn Charles R. O'Malley||Björn||Charles R.|O'Malley|
+Björn van O'Malley||Björn|||van O'Malley|
+Björn Charles van der O'Malley||Björn||Charles|van der O'Malley|
+Björn Charles O'Malley y Muñoz||Björn||Charles|O'Malley y Muñoz|
+Björn O'Malley, Jr.||Björn|||O'Malley|Jr.
+Björn O'Malley Jr||Björn|||O'Malley|Jr
+B O'Malley||B|||O'Malley|
+William Carlos Williams||William||Carlos|Williams|
+C. Björn Roger O'Malley|C.|Björn||Roger|O'Malley|
+B. C. O'Malley||B.||C.|O'Malley|
+B C O'Malley||B||C|O'Malley|
+B.J. Thomas||B.J.|||Thomas|
+O'Malley, Björn||Björn|||O'Malley|
+O'Malley, Björn Jr||Björn|||O'Malley|Jr
+O'Malley, C. Björn|C.|Björn|||O'Malley|
+O'Malley, C. Björn III|C.|Björn|||O'Malley|III
+O'Malley y Muñoz, C. Björn Roger III|C.|Björn||Roger|O'Malley y Muñoz|III
\ No newline at end of file


Mime
View raw message