From 1debd4074c61767759bc84c506f9b8ec867e0957 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Thu, 17 Dec 2009 09:28:56 +0000
Subject: [PATCH] SE-178 improve error messages in column header validation

SVN: 13981
---
 .../validation/DataSetValidatorForTSV.java    | 22 ++++++++--
 .../DataSetValidatorForTSVTest.java           | 43 ++++++++++++++++++-
 2 files changed, 60 insertions(+), 5 deletions(-)

diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/validation/DataSetValidatorForTSV.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/validation/DataSetValidatorForTSV.java
index 0778dbfe558..3b2e2dd9638 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/validation/DataSetValidatorForTSV.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/validation/DataSetValidatorForTSV.java
@@ -224,7 +224,8 @@ class DataSetValidatorForTSV implements IDataSetValidator
         {
             if (definitions[i] == null)
             {
-                definitions[i] = getDefinition(remainingDefinitions, columnHeaders, i);
+                ColumnDefinition orderedColumDefinitionOrNull = orderedDefinitions.get(i + 1);
+                definitions[i] = getDefinition(remainingDefinitions, orderedColumDefinitionOrNull, columnHeaders, i);
             }
         }
         String list = createListOfMissingColumns(remainingDefinitions);
@@ -290,7 +291,7 @@ class DataSetValidatorForTSV implements IDataSetValidator
     }
 
     private ColumnDefinition getDefinition(List<ColumnDefinition> definitions,
-            List<String> columnHeaders, int i)
+            ColumnDefinition orderedColumDefinitionOrNull, List<String> columnHeaders, int i)
     {
         String columnHeader = columnHeaders.get(i);
         StringBuilder builder = new StringBuilder();
@@ -307,12 +308,25 @@ class DataSetValidatorForTSV implements IDataSetValidator
                 return columnDefinition;
             } else
             {
-                builder.append("\nColumn Definition '").append(columnDefinition.getName());
-                builder.append("' does not match: Reason: ").append(result);
+                addMessage(builder, columnDefinition, result);
+            }
+        }
+        if (orderedColumDefinitionOrNull != null)
+        {
+            Result result = orderedColumDefinitionOrNull.validateHeader(columnHeader);
+            if (result.isValid() == false)
+            {
+                addMessage(builder, orderedColumDefinitionOrNull, result);
             }
         }
         throw new UserFailureException("No column definition matches the header of the " + (i + 1)
                 + ". column: " + columnHeader + builder);
     }
 
+    private void addMessage(StringBuilder builder, ColumnDefinition columnDefinition, Result result)
+    {
+        builder.append("\nColumn Definition '").append(columnDefinition.getName());
+        builder.append("' does not match: Reason: ").append(result);
+    }
+
 }
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/validation/DataSetValidatorForTSVTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/validation/DataSetValidatorForTSVTest.java
index cce7002e50c..2484792d8c3 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/validation/DataSetValidatorForTSVTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/validation/DataSetValidatorForTSVTest.java
@@ -203,6 +203,45 @@ public class DataSetValidatorForTSVTest extends AbstractFileSystemTestCase
         MockValidatorFactory.assertSatisfied();
     }
     
+    @Test
+    public void testNoColumnDefinitionFound2()
+    {
+        Properties properties = new Properties();
+        properties.setProperty(DataSetValidatorForTSV.PATH_PATTERNS_KEY, "*");
+        properties.setProperty(DataSetValidatorForTSV.COLUMNS_KEY, "id, a, b, c");
+        properties.setProperty("id." + ColumnDefinition.HEADER_PATTERN_KEY, "ID");
+        properties.setProperty("id." + ColumnDefinition.MANDATORY_KEY, "true");
+        properties.setProperty("id." + ColumnDefinition.ORDER_KEY, "1");
+        properties.setProperty("id." + ColumnDefinition.VALUE_VALIDATOR_KEY, MOCK_FACTORY);
+        properties.setProperty("a." + ColumnDefinition.HEADER_PATTERN_KEY, "A");
+        properties.setProperty("a." + ColumnDefinition.ORDER_KEY, "2");
+        properties.setProperty("a." + ColumnDefinition.VALUE_VALIDATOR_KEY, MOCK_FACTORY);
+        properties.setProperty("b." + ColumnDefinition.HEADER_PATTERN_KEY, "B");
+        properties.setProperty("b." + ColumnDefinition.ORDER_KEY, "3");
+        properties.setProperty("b." + ColumnDefinition.VALUE_VALIDATOR_KEY, MOCK_FACTORY);
+        properties.setProperty("c." + ColumnDefinition.HEADER_PATTERN_KEY, "C[0-9]*");
+        properties.setProperty("c." + ColumnDefinition.CAN_DEFINE_MULTIPLE_COLUMNS_KEY, "true");
+        properties.setProperty("c." + ColumnDefinition.VALUE_VALIDATOR_KEY, MOCK_FACTORY);
+        DataSetValidatorForTSV validator = new DataSetValidatorForTSV(properties);
+        
+        FileUtilities.writeToFile(new File(workingDirectory, "a.txt"), "ID\ta\tC1\n");
+        
+        try
+        {
+            validator.assertValidDataSet(null, workingDirectory);
+            fail("UserFailureException expected");
+        } catch (UserFailureException ex)
+        {
+            assertEquals("No column definition matches the header of the 2. column: a\n"
+                    + "Column Definition 'c' does not match: Reason: "
+                    + "Does not match the following regular expression: C[0-9]*\n"
+                    + "Column Definition 'a' does not match: Reason: "
+                    + "Does not match the following regular expression: A", ex.getMessage());
+        }
+        
+        MockValidatorFactory.assertSatisfied();
+    }
+    
     @Test
     public void testMissingMandatoryColumn()
     {
@@ -443,7 +482,9 @@ public class DataSetValidatorForTSVTest extends AbstractFileSystemTestCase
             fail("UserFailureException expected");
         } catch (UserFailureException ex)
         {
-            assertEquals("No column definition matches the header of the 1. column: ID", ex.getMessage());
+            assertEquals("No column definition matches the header of the 1. column: ID\n"
+                    + "Column Definition 'c1' does not match: Reason: "
+                    + "Does not match the following regular expression: Name", ex.getMessage());
         }
         
         MockValidatorFactory.assertSatisfied();
-- 
GitLab