[java-metadata-aggregator] branch master updated: MDA-200 - Extend BaseValidator with an error message mechanism

Ian Young ian at iay.org.uk
Tue Jan 30 09:43:14 EST 2018


This is an automated email from the git hooks/post-receive script.

iay pushed a commit to branch master
in repository java-metadata-aggregator.

View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=5f45bcc2826529c7fc2185df0ab91b95736488eb

The following commit(s) were added to refs/heads/master by this push:
       new  5f45bcc   MDA-200 - Extend BaseValidator with an error message mechanism
5f45bcc is described below

commit 5f45bcc2826529c7fc2185df0ab91b95736488eb
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Tue Jan 30 14:43:09 2018 +0000

    MDA-200 - Extend BaseValidator with an error message mechanism
---
 .../metadata/validate/BaseValidator.java           | 73 ++++++++++++++++++----
 .../metadata/validate/BaseValidatorTest.java       | 54 ++++++++++++++++
 2 files changed, 116 insertions(+), 11 deletions(-)

diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java
index 114b4ea..0d7ce22 100644
--- a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java
@@ -23,6 +23,8 @@ import net.shibboleth.metadata.ErrorStatus;
 import net.shibboleth.metadata.Item;
 import net.shibboleth.metadata.WarningStatus;
 import net.shibboleth.utilities.java.support.component.AbstractIdentifiableInitializableComponent;
+import net.shibboleth.utilities.java.support.component.ComponentSupport;
+import net.shibboleth.utilities.java.support.logic.Constraint;
 
 /**
  * Base class for validator implementations.
@@ -33,6 +35,39 @@ import net.shibboleth.utilities.java.support.component.AbstractIdentifiableIniti
 public abstract class BaseValidator extends AbstractIdentifiableInitializableComponent {
 
     /**
+     * Message format string.
+     *
+     * The generated message is formatted using this with the object being validated passed
+     * as an argument.
+     *
+     * Defaults to <code>"value rejected: '%s'"</code>.
+     */
+    @Nonnull
+    private String message = "value rejected: '%s'";
+
+    /**
+     * Returns the message format string.
+     *
+     * @return the message format string
+     */
+    @Nonnull
+    public String getMessage() {
+        return message;
+    }
+
+    /**
+     * Set the message format string.
+     * 
+     * @param newMessage the new message format string
+     */
+    public void setMessage(@Nonnull final String newMessage) {
+        ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+        message = Constraint.isNotNull(newMessage, "message format string may not be null");
+    }
+
+    /**
      * Construct a modified component identifier from the stage identifier and the
      * validator identifier.
      * 
@@ -52,42 +87,58 @@ public abstract class BaseValidator extends AbstractIdentifiableInitializableCom
     /**
      * Add an {@link ErrorStatus} to the given {@link Item}.
      * 
-     * @param message message to include in the status metadata
+     * @param mess message to include in the status metadata
      * @param item {@link Item} to add the status metadata to
      * @param stageId component identifier for the calling stage
      */
-    protected void addError(@Nonnull final String message, @Nonnull final Item<?> item,
+    protected void addError(@Nonnull final String mess, @Nonnull final Item<?> item,
             @Nonnull final String stageId) {
-        item.getItemMetadata().put(new ErrorStatus(makeComponentId(stageId), message));
+        item.getItemMetadata().put(new ErrorStatus(makeComponentId(stageId), mess));
     }
     
     /**
      * Add a {@link WarningStatus} to the given {@link Item}.
      * 
-     * @param message message to include in the status metadata
+     * @param mess message to include in the status metadata
      * @param item {@link Item} to add the status metadata to
      * @param stageId component identifier for the calling stage
      */
-    protected void addWarning(@Nonnull final String message, @Nonnull final Item<?> item,
+    protected void addWarning(@Nonnull final String mess, @Nonnull final Item<?> item,
             @Nonnull final String stageId) {
-        item.getItemMetadata().put(new WarningStatus(makeComponentId(stageId), message));
+        item.getItemMetadata().put(new WarningStatus(makeComponentId(stageId), mess));
     }
     
     /**
      * Add a {@link WarningStatus} or {@link ErrorStatus} to the given {@link Item}.
      * 
      * @param error <code>true</code> if an {@link ErrorStatus} should be added
-     * @param message message to include in the status metadata
+     * @param mess message to include in the status metadata
      * @param item {@link Item} to add the status metadata to
      * @param stageId component identifier for the calling stage
      */
-    protected void addStatus(final boolean error, @Nonnull final String message, @Nonnull final Item<?> item,
+    protected void addStatus(final boolean error, @Nonnull final String mess, @Nonnull final Item<?> item,
             @Nonnull final String stageId) {
         if (error) {
-            addError(message, item, stageId);
+            addError(mess, item, stageId);
         } else {
-            addWarning(message, item, stageId);
+            addWarning(mess, item, stageId);
         }
     }
-    
+
+    /**
+     * Add an {@link ErrorStatus} to the given {@link Item}.
+     *
+     * The status message included in the {@link ErrorStatus} is generated
+     * by formatting the provided value with the {@link #message} field.
+     *
+     * @param extra extra value to include in the status metadata
+     * @param item {@link Item} to add the status metadata to
+     * @param stageId component identifier for the calling stage
+     */
+    protected void addErrorMessage(@Nonnull final Object extra, @Nonnull final Item<?> item,
+            @Nonnull final String stageId) {
+        final String mess = String.format(getMessage(), extra);
+        addError(mess, item, stageId);
+    }
+
 }
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/validate/BaseValidatorTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/validate/BaseValidatorTest.java
new file mode 100644
index 0000000..2e711f6
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/validate/BaseValidatorTest.java
@@ -0,0 +1,54 @@
+
+package net.shibboleth.metadata.validate;
+
+import java.util.List;
+
+import org.testng.annotations.Test;
+
+import junit.framework.Assert;
+import net.shibboleth.metadata.ErrorStatus;
+import net.shibboleth.metadata.Item;
+import net.shibboleth.metadata.MockItem;
+import net.shibboleth.metadata.validate.Validator.Action;
+
+public class BaseValidatorTest {
+
+    /**
+     * Test String validator which always rejects.
+     */
+    private class BoomValidator extends BaseValidator implements Validator<String> {
+
+        public Action validate(String e, Item<?> item, String stageId) {
+            addErrorMessage(e, item, stageId);
+            return Action.DONE;
+        }
+
+    }
+
+    @Test
+    public void getMessage() throws Exception {
+        final BoomValidator b = new BoomValidator();
+        b.setId("test");
+        b.initialize();
+        Assert.assertEquals(b.getMessage(), "value rejected: '%s'");
+        final Item<String> item = new MockItem("test");
+        Assert.assertEquals(Action.DONE, b.validate("foo", item, "stage"));
+        final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class);
+        Assert.assertEquals(1, errs.size());
+        Assert.assertEquals("value rejected: 'foo'", errs.get(0).getStatusMessage());
+    }
+
+    @Test
+    public void setMessage() throws Exception {
+        final BoomValidator b = new BoomValidator();
+        b.setId("test");
+        b.setMessage("%s is bad");
+        b.initialize();
+        Assert.assertEquals(b.getMessage(), "%s is bad");
+        final Item<String> item = new MockItem("test");
+        Assert.assertEquals(Action.DONE, b.validate("foo", item, "stage"));
+        final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class);
+        Assert.assertEquals(1, errs.size());
+        Assert.assertEquals("foo is bad", errs.get(0).getStatusMessage());
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list