sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject [04/50] [abbrv] git commit: SQOOP-1465: Sqoop2: Validations: Add support for multiple validation messages to Model classes
Date Fri, 10 Oct 2014 03:06:56 GMT
SQOOP-1465: Sqoop2: Validations: Add support for multiple validation messages to Model classes


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/f89368e2
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/f89368e2
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/f89368e2

Branch: refs/heads/sqoop2
Commit: f89368e2a0cc6236b2c50dfe544134f9d55208b9
Parents: e71dd75
Author: Jarek Jarcec Cecho <jarcec@apache.org>
Authored: Mon Sep 8 23:16:19 2014 -0700
Committer: Abraham Elmahrek <abraham@elmahrek.com>
Committed: Thu Oct 9 17:58:17 2014 -0700

----------------------------------------------------------------------
 .../java/org/apache/sqoop/model/FormUtils.java  |  10 +-
 .../apache/sqoop/model/MValidatedElement.java   | 107 +++++++------------
 .../org/apache/sqoop/validation/Status.java     |  16 ++-
 .../org/apache/sqoop/model/TestFormUtils.java   |  31 ------
 .../sqoop/model/TestMValidatedElement.java      |  36 +++----
 .../apache/sqoop/shell/utils/FormDisplayer.java |   5 +-
 .../apache/sqoop/shell/utils/FormFiller.java    |  17 ++-
 7 files changed, 88 insertions(+), 134 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/f89368e2/common/src/main/java/org/apache/sqoop/model/FormUtils.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/model/FormUtils.java b/common/src/main/java/org/apache/sqoop/model/FormUtils.java
index bfd97d5..586e0fe 100644
--- a/common/src/main/java/org/apache/sqoop/model/FormUtils.java
+++ b/common/src/main/java/org/apache/sqoop/model/FormUtils.java
@@ -314,9 +314,9 @@ public class FormUtils {
 
     if (messages.containsKey(name)) {
       Validation.Message message = messages.get(name);
-      element.setValidationMessage(message.getStatus(), message.getMessage());
+      element.addValidationMessage(new Message(message.getStatus(), message.getMessage()));
     } else {
-      element.setValidationMessage(Status.getDefault(), null);
+      element.addValidationMessage(new Message(Status.getDefault(), null));
     }
   }
 
@@ -349,11 +349,9 @@ public class FormUtils {
     List<Message> messages = result.getMessages().get(element.getName());
 
     if(messages != null) {
-      // TODO(SQOOP-1465) Add support for multiple messages (showing only the first one for
now)
-      Message message = messages.get(0);
-      element.setValidationMessage(message.getStatus(), message.getMessage());
+      element.setValidationMessages(messages);
     } else {
-      element.setValidationMessage(Status.getDefault(), null);
+      element.resetValidationMessages();
     }
   }
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/f89368e2/common/src/main/java/org/apache/sqoop/model/MValidatedElement.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/model/MValidatedElement.java b/common/src/main/java/org/apache/sqoop/model/MValidatedElement.java
index ab5473c..a50c815 100644
--- a/common/src/main/java/org/apache/sqoop/model/MValidatedElement.java
+++ b/common/src/main/java/org/apache/sqoop/model/MValidatedElement.java
@@ -17,122 +17,87 @@
  */
 package org.apache.sqoop.model;
 
+import org.apache.sqoop.validation.Message;
 import org.apache.sqoop.validation.Status;
 
+import java.util.LinkedList;
+import java.util.List;
+
 /**
- * Element that can be validated for correctness.
- *
- * Two severity levels are supported at the moment - warning and error.
- *
- * Warning:
- * Warning is something suspicious, potentially wrong but something that
- * can be ignored. For example in case of JDBC URL element, warning would
- * be if specified host is not responding - it's warning because specified
- * URL might be wrong. However at the same time URL might be right as only
- * target host might be down.
- *
- * Error:
- * Error represents unacceptable element content. For example in case of JDBC
- * URL path, error would be empty element or element containing invalid URL.
+ * Element that can have associated validation messages (0..N).
  */
 public abstract class MValidatedElement extends MNamedElement {
 
   /**
-   * Validation message.
-   *
-   * One element can have only one message regardless of the type.
+   * Validation messages.
    */
-  private String validationMessage;
+  private List<Message> validationMessages;
 
   /**
-   * Severity of the message.
+   * The worst status of all validation messages.
    */
   private Status validationStatus;
 
   public MValidatedElement(String name) {
     super(name);
-    // Everything is fine by default
-    this.validationStatus = Status.getDefault();
+    resetValidationMessages();
   }
 
   public MValidatedElement(MValidatedElement other) {
     super(other);
-    this.validationMessage = other.validationMessage;
     this.validationStatus = other.validationStatus;
+    this.validationMessages.addAll(other.validationMessages);
   }
 
   /**
-   * Set validation message and given severity.
-   *
-   * @param status Message validation status
-   * @param msg Message itself
-   */
-  public void setValidationMessage(Status status, String msg) {
-    this.validationMessage = msg;
-    this.validationStatus = status;
-  }
-
-  /**
-   * Return validation message for given severity.
+   * Reset this validated element back to default state.
    *
-   * Return either associated message for given severity or null in case
-   * that there is no message with given severity.
-   *
-   * @param status Message validation status
+   * Will remove all associated messages and validation status.
    */
-  public String getValidationMessage(Status status) {
-    return (validationStatus.equals(status)) ? validationMessage : null;
+  public void resetValidationMessages() {
+    this.validationStatus = Status.getDefault();
+    this.validationMessages = new LinkedList<Message>();
   }
 
   /**
-   * Return validation message.
+   * Set validation messages (override anything that has been set before).
    *
-   * Return current validation message.
-   */
-  public String getValidationMessage() {
-    return validationMessage;
-  }
-
-  /**
-   * Return message validation status.
+   * @param msg Message itself
    */
-  public Status getValidationStatus() {
-    return validationStatus;
+  public void addValidationMessage(Message msg) {
+    this.validationMessages.add(msg);
+    this.validationStatus = Status.getWorstStatus(this.validationStatus, msg.getStatus());
   }
 
   /**
-   * Set error message for this element.
+   * Override all previously existing validation messages.
    *
-   * @param errMsg Error message
+   * @param messages
    */
-  public void setErrorMessage(String errMsg) {
-    setValidationMessage(Status.UNACCEPTABLE, errMsg);
-  }
+  public void setValidationMessages(List<Message> messages) {
+    this.validationMessages = messages;
+    this.validationStatus = Status.getDefault();
 
-  /**
-   * Return error message associated with this element.
-   *
-   * @return Error message
-   */
-  public String getErrorMessage() {
-    return getValidationMessage(Status.UNACCEPTABLE);
+    for(Message message : messages ) {
+      this.validationStatus = Status.getWorstStatus(this.validationStatus, message.getStatus());
+    }
   }
 
   /**
-   * Set warning message for this element.
+   * Return validation message for given severity.
    *
-   * @param warnMsg Warning message
+   * Return either associated message for given severity or null in case
+   * that there is no message with given severity.
    */
-  public void setWarningMessage(String warnMsg) {
-    setValidationMessage(Status.ACCEPTABLE, warnMsg);
+  public List<Message> getValidationMessages() {
+    return this.validationMessages;
   }
 
   /**
-   * Retrieve warning message associated with this element.
-   * @return
+   * Return message validation status.
    */
-  public String getWarningMessage() {
-    return getValidationMessage(Status.ACCEPTABLE);
+  public Status getValidationStatus() {
+    return validationStatus;
   }
 
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/f89368e2/common/src/main/java/org/apache/sqoop/validation/Status.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/validation/Status.java b/common/src/main/java/org/apache/sqoop/validation/Status.java
index ae89e59..5a546bf 100644
--- a/common/src/main/java/org/apache/sqoop/validation/Status.java
+++ b/common/src/main/java/org/apache/sqoop/validation/Status.java
@@ -18,7 +18,21 @@
 package org.apache.sqoop.validation;
 
 /**
- * Status modes of a validation process.
+ * Severity of validation message.
+ *
+ * Fine:
+ * Everything is correct (default state).
+ *
+ * Warning:
+ * Warning is something suspicious, potentially wrong but something that
+ * can be ignored. For example in case of JDBC URL element, warning would
+ * be if specified host is not responding - it's warning because specified
+ * URL might be wrong. However at the same time URL might be right as only
+ * target host might be down.
+ *
+ * Error:
+ * Error represents unacceptable element content. For example in case of JDBC
+ * URL path, error would be empty element or element containing invalid URL.
  *
  * TODO: This should really be renamed to "severity"
  */

http://git-wip-us.apache.org/repos/asf/sqoop/blob/f89368e2/common/src/test/java/org/apache/sqoop/model/TestFormUtils.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/model/TestFormUtils.java b/common/src/test/java/org/apache/sqoop/model/TestFormUtils.java
index 41dddb7..9fa2b6b 100644
--- a/common/src/test/java/org/apache/sqoop/model/TestFormUtils.java
+++ b/common/src/test/java/org/apache/sqoop/model/TestFormUtils.java
@@ -129,23 +129,6 @@ public class TestFormUtils extends TestCase {
     assertNull(config.bForm.b2);
   }
 
-  public void testApplyValidation() {
-    Validation validation = getValidation();
-    List<MForm> forms = getForms();
-
-    FormUtils.applyValidation(forms, validation);
-
-    assertEquals(Status.ACCEPTABLE,
-      forms.get(0).getInputs().get(0).getValidationStatus());
-    assertEquals("e1",
-      forms.get(0).getInputs().get(0).getValidationMessage());
-
-    assertEquals(Status.UNACCEPTABLE,
-      forms.get(0).getInputs().get(1).getValidationStatus());
-    assertEquals("e2",
-      forms.get(0).getInputs().get(1).getValidationMessage());
-  }
-
   public void testJson() {
     Config config = new Config();
     config.aForm.a1 = "A";
@@ -179,20 +162,6 @@ public class TestFormUtils extends TestCase {
     assertEquals(Enumeration.X, targetConfig.cForm.enumeration);
   }
 
-  protected Validation getValidation() {
-    Map<Validation.FormInput, Validation.Message> messages
-      = new HashMap<Validation.FormInput, Validation.Message>();
-
-    messages.put(
-      new Validation.FormInput("aForm", "a1"),
-      new Validation.Message(Status.ACCEPTABLE, "e1"));
-    messages.put(
-      new Validation.FormInput("aForm", "a2"),
-      new Validation.Message(Status.UNACCEPTABLE, "e2"));
-
-    return new Validation(Status.UNACCEPTABLE, messages);
-  }
-
   /**
    * Form structure that corresponds to Config class declared below
    * @return Form structure

http://git-wip-us.apache.org/repos/asf/sqoop/blob/f89368e2/common/src/test/java/org/apache/sqoop/model/TestMValidatedElement.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/model/TestMValidatedElement.java b/common/src/test/java/org/apache/sqoop/model/TestMValidatedElement.java
index 3fd5a95..cf34983 100644
--- a/common/src/test/java/org/apache/sqoop/model/TestMValidatedElement.java
+++ b/common/src/test/java/org/apache/sqoop/model/TestMValidatedElement.java
@@ -19,6 +19,7 @@ package org.apache.sqoop.model;
 
 import static org.junit.Assert.*;
 
+import org.apache.sqoop.validation.Message;
 import org.apache.sqoop.validation.Status;
 import org.junit.Test;
 
@@ -41,30 +42,27 @@ public class TestMValidatedElement {
    * Test for validation message and status
    */
   @Test
-  public void testValidationMessageStatus() {
+  public void testVarious() {
     MValidatedElement input = new MIntegerInput("input", false);
+
     // Default status
     assertEquals(Status.FINE, input.getValidationStatus());
-    // Set status and user message
-    input.setValidationMessage(Status.ACCEPTABLE, "MY_MESSAGE");
+
+    // Add a message
+    input.addValidationMessage(new Message(Status.ACCEPTABLE, "MY_MESSAGE"));
     assertEquals(Status.ACCEPTABLE, input.getValidationStatus());
-    assertEquals("MY_MESSAGE", input.getValidationMessage());
-    // Check for null if status does not equal
-    assertNull(input.getValidationMessage(Status.FINE));
-    assertNull(input.getErrorMessage());
-    assertNotNull(input.getWarningMessage());
+    assertEquals(1, input.getValidationMessages().size());
+    assertEquals("MY_MESSAGE", input.getValidationMessages().get(0).getMessage());
+
+    // Reset
+    input.resetValidationMessages();
+    assertEquals(Status.FINE, input.getValidationStatus());
+    assertEquals(0, input.getValidationMessages().size());
+
     // Set unacceptable status
-    input.setValidationMessage(Status.UNACCEPTABLE, "MY_MESSAGE");
-    assertNotNull(input.getErrorMessage());
-    assertEquals("MY_MESSAGE", input.getErrorMessage());
-    assertNull(input.getWarningMessage());
-    // Set warning
-    input.setWarningMessage("WARN");
-    assertEquals(Status.ACCEPTABLE, input.getValidationStatus());
-    assertEquals("WARN", input.getValidationMessage());
-    // Unacceptable method
-    input.setErrorMessage("ERROR");
+    input.addValidationMessage(new Message(Status.UNACCEPTABLE, "MY_MESSAGE"));
     assertEquals(Status.UNACCEPTABLE, input.getValidationStatus());
-    assertEquals("ERROR", input.getValidationMessage());
+    assertEquals(1, input.getValidationMessages().size());
+    assertEquals("MY_MESSAGE", input.getValidationMessages().get(0).getMessage());
   }
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/f89368e2/shell/src/main/java/org/apache/sqoop/shell/utils/FormDisplayer.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/utils/FormDisplayer.java b/shell/src/main/java/org/apache/sqoop/shell/utils/FormDisplayer.java
index e04d7b5..bbad183 100644
--- a/shell/src/main/java/org/apache/sqoop/shell/utils/FormDisplayer.java
+++ b/shell/src/main/java/org/apache/sqoop/shell/utils/FormDisplayer.java
@@ -33,6 +33,7 @@ import org.apache.sqoop.model.MJob;
 import org.apache.sqoop.model.MMapInput;
 import org.apache.sqoop.model.MStringInput;
 import org.apache.sqoop.shell.core.Constants;
+import org.apache.sqoop.validation.Message;
 import org.apache.sqoop.validation.Status;
 
 import java.util.ArrayList;
@@ -163,7 +164,9 @@ public final class FormDisplayer {
           print("\n@|yellow %s|@\n", resourceString(Constants.RES_FORMDISPLAYER_FORM_WARNING));
           showMessage = false;
         }
-        FormFiller.warningMessage(form.getValidationMessage());
+        for(Message message : form.getValidationMessages()) {
+          FormFiller.warningMessage(message.getMessage());
+        }
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/f89368e2/shell/src/main/java/org/apache/sqoop/shell/utils/FormFiller.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/utils/FormFiller.java b/shell/src/main/java/org/apache/sqoop/shell/utils/FormFiller.java
index 9d63c6e..bbf7af7 100644
--- a/shell/src/main/java/org/apache/sqoop/shell/utils/FormFiller.java
+++ b/shell/src/main/java/org/apache/sqoop/shell/utils/FormFiller.java
@@ -33,6 +33,8 @@ import org.apache.sqoop.model.MJob;
 import org.apache.sqoop.model.MNamedElement;
 import org.apache.sqoop.model.MStringInput;
 import org.apache.sqoop.model.MValidatedElement;
+import org.apache.sqoop.validation.Message;
+import org.apache.sqoop.validation.Status;
 
 import java.io.IOException;
 import java.util.List;
@@ -849,19 +851,24 @@ public final class FormFiller {
    * @param element Validated element
    */
   public static void printValidationMessage(MValidatedElement element, boolean includeInputPrefix)
{
-    switch (element.getValidationStatus()) {
+    if(element.getValidationStatus() == Status.getDefault()) {
+      return;
+    }
+
+    for(Message message : element.getValidationMessages())
+    switch (message.getStatus()) {
       case UNACCEPTABLE:
         if (includeInputPrefix) {
-          errorMessage(element, element.getValidationMessage());
+          errorMessage(element, message.getMessage());
         } else {
-          errorMessage(element.getValidationMessage());
+          errorMessage(message.getMessage());
         }
         break;
       case ACCEPTABLE:
         if (includeInputPrefix) {
-          warningMessage(element, element.getValidationMessage());
+          warningMessage(element, message.getMessage());
         } else {
-          warningMessage(element.getValidationMessage());
+          warningMessage(message.getMessage());
         }
         break;
       default:


Mime
View raw message