[seam-commits] Seam SVN: r10680 - in modules/trunk/international/src: test/java/org/jboss/seam/international and 1 other directory.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Tue Apr 28 16:29:58 EDT 2009
Author: dan.j.allen
Date: 2009-04-28 16:29:58 -0400 (Tue, 28 Apr 2009)
New Revision: 10680
Added:
modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesUnitTest.java
Modified:
modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessage.java
modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessages.java
Log:
add additional JavaDoc and unit tests for StatusMessages
Modified: modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessage.java
===================================================================
--- modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessage.java 2009-04-28 18:24:05 UTC (rev 10679)
+++ modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessage.java 2009-04-28 20:29:58 UTC (rev 10680)
@@ -1,3 +1,26 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * $Id$
+ */
package org.jboss.seam.international;
import java.io.Serializable;
Modified: modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessages.java
===================================================================
--- modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessages.java 2009-04-28 18:24:05 UTC (rev 10679)
+++ modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessages.java 2009-04-28 20:29:58 UTC (rev 10680)
@@ -1,3 +1,26 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * $Id$
+ */
package org.jboss.seam.international;
import static org.jboss.seam.international.StatusMessage.Severity.INFO;
@@ -17,10 +40,22 @@
import org.jboss.seam.international.StatusMessage.Severity;
/**
- * Abstract base class for providing status messages. View layers should provide
- * a concrete implementation.
+ * <p>{@link StatusMessages} is a technology agnostic repository for holding
+ * status messages intended to be displayed to an end user. View layer modules
+ * should provide a concrete implementation that handles tranposing the generic
+ * status messages into status messages specific to the view techology (e.g.,
+ * JSF, Wicket).</p>
+ *
+ * <p>A status message can either be global or it can be keyed to a "client id", which
+ * is the id of a cooresponding user interface component on the page.</p>
+ *
+ * <p>Seam will interpolate value expressions and positional parameters in
+ * message templates. Interpolation is deferred until just before the next view
+ * in the conversation is rendered.</p>
*
* @author Pete Muir
+ * @author Dan Allen
+ * @see StatusMessage
*/
public
@Named("org.jboss.seam.international.statusMessages")
@@ -35,20 +70,43 @@
private transient List<Runnable> tasks;
- protected @Current Manager manager;
+ @Current Interpolator interpolator;
- protected List<StatusMessage> getMessages()
+ /**
+ * Get a list of StatusMessage objects that are not associated with a "client id".
+ *
+ * @return A list of StatusMessage objects not associated with a "client id"
+ */
+ public List<StatusMessage> getGlobalMessages()
{
return messages;
}
+
+ /**
+ * Get a list of StatusMessage objects that are associated with the given "client id".
+ *
+ * @param id The "client id" of the user interface component to which the messages apply
+ *
+ * @return A list of StatusMessage objects associated with the given "client id"
+ */
+ public List<StatusMessage> getKeyedMessages(String id)
+ {
+ return keyedMessages.get(id);
+ }
- protected Map<String, List<StatusMessage>> getKeyedMessages()
+ /**
+ * Get a map of StatusMessage objects associated with a "client id". The keys in the map are
+ * the "client id" values and the values for each key are the messages.
+ *
+ * @return A map of StatusMessage objects keyed by "client id"
+ */
+ public Map<String, List<StatusMessage>> getKeyedMessages()
{
return keyedMessages;
}
/**
- * Clear all status messages
+ * Clear all status messages.
*/
public void clear()
{
@@ -56,11 +114,27 @@
keyedMessages.clear();
}
+ /**
+ * Clear all status messages associated with a "client id".
+ */
+ public void clearKeyedMessages()
+ {
+ keyedMessages.clear();
+ }
+
+ /**
+ * Clear all status messages associated with a given "client id".
+ *
+ * @param id The "client id" of the user interface component to which the messages apply
+ */
public void clearKeyedMessages(String id)
{
keyedMessages.remove(id);
}
+ /**
+ * Clear all status messages not associated with a "client id".
+ */
public void clearGlobalMessages()
{
messages.clear();
@@ -84,7 +158,7 @@
{
public void run()
{
- message.interpolate(manager.getInstanceByType(Interpolator.class), params);
+ message.interpolate(interpolator, params);
}
});
}
@@ -122,7 +196,7 @@
{
public void run()
{
- message.interpolate(manager.getInstanceByType(Interpolator.class), params);
+ message.interpolate(interpolator, params);
}
});
}
Added: modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesUnitTest.java
===================================================================
--- modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesUnitTest.java (rev 0)
+++ modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesUnitTest.java 2009-04-28 20:29:58 UTC (rev 10680)
@@ -0,0 +1,249 @@
+package org.jboss.seam.international;
+
+import java.util.List;
+import javax.validation.ConstraintDescriptor;
+import javax.validation.ConstraintViolation;
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+/**
+ * A set of tests which exercise the StatusMessages as a unit (no external dependencies).
+ * These tests focus primarily on the creation and storage of the StatusMessage.
+ *
+ * @author Dan Allen
+ * @see StatusMessages
+ */
+ at Test(groups = "unit")
+public class StatusMessagesUnitTest
+{
+ private Interpolator identityInterpolator = new IdentityInterpolator();
+
+ /**
+ * Check that the add() method appends a global message, that the
+ * default severity is INFO, and that interpolation is deferred.
+ */
+ @Test
+ public void testAddGlobalMessageFromTemplateWithDefaultSeverity()
+ {
+ StatusMessages statusMessages = getStatusMessagesInstance();
+ statusMessages.add("Simple message");
+ assertEquals(statusMessages.getGlobalMessages().size(), 1);
+ assertEquals(statusMessages.getKeyedMessages().size(), 0);
+ StatusMessage message = statusMessages.getGlobalMessages().get(0);
+ assertSame(message.getSeverity(), StatusMessage.Severity.INFO);
+ assertFalse(message.isEmpty());
+ assertNull(message.getSummary());
+ message.interpolate(identityInterpolator);
+ assertEquals(message.getSummary(), "Simple message");
+ assertNull(message.getDetail());
+ }
+
+ /**
+ * Test that separate summary and detail templates can be specified.
+ */
+ @Test
+ public void testAddMessageWithSummaryAndDetail()
+ {
+ StatusMessages statusMessages = getStatusMessagesInstance();
+ statusMessages.add(StatusMessage.Severity.WARN, null, null, "Summary template", "Detail template");
+ assertEquals(statusMessages.getGlobalMessages().size(), 1);
+ StatusMessage message = statusMessages.getGlobalMessages().get(0);
+ assertSame(message.getSeverity(), StatusMessage.Severity.WARN);
+ message.interpolate(identityInterpolator);
+ assertEquals(message.getSummary(), "Summary template");
+ assertEquals(message.getDetail(), "Detail template");
+ }
+
+ /**
+ * Test that the severity is processed correctly when adding a global message.
+ */
+ @Test
+ public void testAddGlobalWarningMessageFromTemplate()
+ {
+ StatusMessages statusMessages = getStatusMessagesInstance();
+ statusMessages.add(StatusMessage.Severity.WARN, "Achtung!");
+ assertEquals(statusMessages.getGlobalMessages().size(), 1);
+ assertSame(statusMessages.getGlobalMessages().get(0).getSeverity(), StatusMessage.Severity.WARN);
+ }
+
+ /**
+ * Test that the addToControl() associates a message with a client id, that the
+ * default severity is INFO, and that interpolation is deferred.
+ */
+ @Test
+ public void testAddMessageToControlFromTemplateWithDefaultSeverity()
+ {
+ StatusMessages statusMessages = getStatusMessagesInstance();
+ statusMessages.addToControl("username", "Available!");
+ List<StatusMessage> messages = statusMessages.getKeyedMessages("username");
+ assertNotNull(messages);
+ assertEquals(messages.size(), 1);
+ StatusMessage message = messages.get(0);
+ assertSame(message.getSeverity(), StatusMessage.Severity.INFO);
+ assertNull(message.getSummary());
+ message.interpolate(identityInterpolator);
+ assertEquals(message.getSummary(), "Available!");
+ assertNull(message.getDetail());
+ statusMessages.addToControl("username", "Nice choice");
+ assertEquals(statusMessages.getKeyedMessages("username").size(), 2);
+ }
+
+ /**
+ * Test that add() can create a global message with severity WARN from the contraint violation message.
+ */
+ @Test
+ public void testAddGlobalMessageFromConstraintViolation()
+ {
+ StatusMessages statusMessages = getStatusMessagesInstance();
+ ConstraintViolation violation = new ConstraintViolationStub("Invalid number", "creditCardNumber");
+ statusMessages.add(violation);
+ assertEquals(statusMessages.getGlobalMessages().size(), 1);
+ assertEquals(statusMessages.getKeyedMessages().size(), 0);
+ StatusMessage message = statusMessages.getGlobalMessages().get(0);
+ assertSame(message.getSeverity(), StatusMessage.Severity.WARN);
+ message.interpolate(identityInterpolator);
+ assertEquals(message.getSummary(), "Invalid number");
+ }
+
+ /**
+ * Test that addToControl() can create a message associated with the "client id" that is equivalent to the
+ * last path segment of the property path with severity WARN from the contraint violation message.
+ */
+ @Test
+ public void testAddMessageToControlsDynamicallyFromConstraintViolation()
+ {
+ StatusMessages statusMessages = getStatusMessagesInstance();
+ ConstraintViolation firstNameViolation = new ConstraintViolationStub("Too short", "firstName");
+ ConstraintViolation lastNameViolation = new ConstraintViolationStub("Too long", "lastName");
+ ConstraintViolation usernameViolation = new ConstraintViolationStub("Already taken", "account.username");
+ statusMessages.addToControl(firstNameViolation);
+ statusMessages.addToControls(new ConstraintViolation[] { lastNameViolation, usernameViolation });
+ assertEquals(statusMessages.getGlobalMessages().size(), 0);
+ assertEquals(statusMessages.getKeyedMessages().size(), 3);
+ List<StatusMessage> messages = statusMessages.getKeyedMessages("lastName");
+ assertNotNull(messages);
+ assertEquals(messages.size(), 1);
+ assertSame(messages.get(0).getSeverity(), StatusMessage.Severity.WARN);
+ assertNotNull(statusMessages.getKeyedMessages("lastName"));
+ assertNotNull(statusMessages.getKeyedMessages("username"));
+ }
+
+ /**
+ * Test that the serverity is processed correctly when adding a message to a client id.
+ */
+ @Test
+ public void testAddWarningMessageToControlFromTemplate()
+ {
+ StatusMessages statusMessages = getStatusMessagesInstance();
+ statusMessages.addToControl("password", StatusMessage.Severity.WARN, "Too short");
+ List<StatusMessage> messages = statusMessages.getKeyedMessages("password");
+ assertNotNull(messages);
+ assertEquals(messages.size(), 1);
+ assertSame(StatusMessage.Severity.WARN, messages.get(0).getSeverity());
+ }
+
+ /**
+ * Test that global and keyed messages can be cleared separately.
+ */
+ @Test
+ public void testClearMessages()
+ {
+ StatusMessages statusMessages = getStatusMessagesInstance();
+
+ statusMessages.add("An info message");
+ statusMessages.add(StatusMessage.Severity.ERROR, "An error message");
+ statusMessages.addToControl("username", "Available!");
+ statusMessages.addToControl("username", "Nice choice");
+ statusMessages.addToControl("password", StatusMessage.Severity.WARN, "Too short");
+
+ assertEquals(statusMessages.getGlobalMessages().size(), 2);
+ assertEquals(statusMessages.getKeyedMessages().size(), 2);
+
+ statusMessages.clearGlobalMessages();
+
+ assertEquals(statusMessages.getGlobalMessages().size(), 0);
+ assertEquals(statusMessages.getKeyedMessages().size(), 2);
+
+ statusMessages.clearKeyedMessages("username");
+
+ assertEquals(statusMessages.getKeyedMessages().size(), 1);
+ assertNull(statusMessages.getKeyedMessages("username"));
+
+ statusMessages.clearKeyedMessages();
+
+ assertEquals(statusMessages.getKeyedMessages().size(), 0);
+
+ statusMessages.add("An info message");
+ statusMessages.addToControl("username", "Nice choice");
+
+ assertEquals(statusMessages.getGlobalMessages().size(), 1);
+ assertEquals(statusMessages.getKeyedMessages().size(), 1);
+
+ statusMessages.clear();
+
+ assertEquals(statusMessages.getGlobalMessages().size(), 0);
+ assertEquals(statusMessages.getKeyedMessages().size(), 0);
+ }
+
+ private StatusMessages getStatusMessagesInstance()
+ {
+ return new StatusMessages();
+ }
+
+ private class IdentityInterpolator extends Interpolator
+ {
+ @Override
+ public String interpolate(String string, Object... params)
+ {
+ return string;
+ }
+ }
+
+ private class ConstraintViolationStub implements ConstraintViolation
+ {
+ private String message;
+ private String propertyPath;
+
+ public ConstraintViolationStub(String message, String propertyPath)
+ {
+ this.message = message;
+ this.propertyPath = propertyPath;
+ }
+
+ public String getMessage()
+ {
+ return message;
+ }
+
+ public String getMessageTemplate()
+ {
+ throw new UnsupportedOperationException("Not supported by stub.");
+ }
+
+ public Object getRootBean()
+ {
+ throw new UnsupportedOperationException("Not supported by stub.");
+ }
+
+ public Object getLeafBean()
+ {
+ throw new UnsupportedOperationException("Not supported by stub.");
+ }
+
+ public String getPropertyPath()
+ {
+ return propertyPath;
+ }
+
+ public Object getInvalidValue()
+ {
+ throw new UnsupportedOperationException("Not supported by stub.");
+ }
+
+ public ConstraintDescriptor getConstraintDescriptor()
+ {
+ throw new UnsupportedOperationException("Not supported by stub.");
+ }
+
+ }
+}
More information about the seam-commits
mailing list