[seam-commits] Seam SVN: r10681 - 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 17:23:52 EDT 2009


Author: dan.j.allen
Date: 2009-04-28 17:23:52 -0400 (Tue, 28 Apr 2009)
New Revision: 10681

Added:
   modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesTest.java
Modified:
   modules/trunk/international/src/main/java/org/jboss/seam/international/Interpolator.java
   modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessages.java
   modules/trunk/international/src/test/java/org/jboss/seam/international/InterpolatorTest.java
   modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesUnitTest.java
Log:
standalone integration test for StatusMessages
add initializer to StatusMessages


Modified: modules/trunk/international/src/main/java/org/jboss/seam/international/Interpolator.java
===================================================================
--- modules/trunk/international/src/main/java/org/jboss/seam/international/Interpolator.java	2009-04-28 20:29:58 UTC (rev 10680)
+++ modules/trunk/international/src/main/java/org/jboss/seam/international/Interpolator.java	2009-04-28 21:23:52 UTC (rev 10681)
@@ -1,5 +1,6 @@
 package org.jboss.seam.international;
 
+import java.io.Serializable;
 import org.jboss.seam.el.*;
 import java.text.MessageFormat;
 import java.util.Locale;
@@ -17,7 +18,7 @@
  */
 public
 @Dependent
-class Interpolator
+class Interpolator implements Serializable
 {
    private static final LogProvider log = Logging.getLogProvider(Interpolator.class);
 

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 20:29:58 UTC (rev 10680)
+++ modules/trunk/international/src/main/java/org/jboss/seam/international/StatusMessages.java	2009-04-28 21:23:52 UTC (rev 10681)
@@ -34,8 +34,7 @@
 
 import javax.annotation.Named;
 import javax.context.ConversationScoped;
-import javax.inject.Current;
-import javax.inject.manager.Manager;
+import javax.inject.Initializer;
 import javax.validation.ConstraintViolation;
 import org.jboss.seam.international.StatusMessage.Severity;
 
@@ -62,16 +61,23 @@
 @ConversationScoped
 class StatusMessages implements Serializable
 {
-   private static final long serialVersionUID = -5395975397632138270L;
+   private static final long serialVersionUID = -5396789975397138270L;
 
    private List<StatusMessage> messages = new ArrayList<StatusMessage>();
 
    private Map<String, List<StatusMessage>> keyedMessages = new HashMap<String, List<StatusMessage>>();
 
    private transient List<Runnable> tasks;
+
+   private Interpolator interpolator;
+
+   public StatusMessages() {}
+
+   public @Initializer StatusMessages(Interpolator interpolator)
+   {
+      this.interpolator = interpolator;
+   }
    
-   @Current Interpolator interpolator;
-
    /**
     * Get a list of StatusMessage objects that are not associated with a "client id".
     *
@@ -153,14 +159,7 @@
       if (!message.isEmpty())
       {
          messages.add(message);
-         getTasks().add(
-            new Runnable()
-            {
-               public void run()
-               {
-                  message.interpolate(interpolator, params);
-               }
-            });
+         getTasks().add(new InterpolateMessageTask(message, params));
       }
    }
 
@@ -191,14 +190,7 @@
             list.add(message);
             keyedMessages.put(id, list);
          }
-         getTasks().add(
-            new Runnable()
-            {
-               public void run()
-               {
-                  message.interpolate(interpolator, params);
-               }
-            });
+         getTasks().add(new InterpolateMessageTask(message, params));
       }
    }
 
@@ -433,6 +425,16 @@
       addToControl(id, WARN, cv.getMessage());
    }
 
+   /**
+    * Run the tasks on each message that interpolate the message templates. View layer
+    * modules are expected to invoke this message. The view layer implementation may override
+    * this method to transpose the messages into the view layer-specific messages.
+    */
+   public void onBeforeRender()
+   {
+      doRunTasks();
+   }
+
    private List<Runnable> getTasks()
    {
       if (tasks == null)
@@ -453,4 +455,21 @@
          tasks.clear();
       }
    }
+
+   private class InterpolateMessageTask implements Runnable
+   {
+      private final StatusMessage message;
+      private final Object[] params;
+
+      public InterpolateMessageTask(StatusMessage message, Object[] params)
+      {
+         this.message = message;
+         this.params = params;
+      }
+
+      public void run()
+      {
+         message.interpolate(interpolator, params);
+      }
+   }
 }

Modified: modules/trunk/international/src/test/java/org/jboss/seam/international/InterpolatorTest.java
===================================================================
--- modules/trunk/international/src/test/java/org/jboss/seam/international/InterpolatorTest.java	2009-04-28 20:29:58 UTC (rev 10680)
+++ modules/trunk/international/src/test/java/org/jboss/seam/international/InterpolatorTest.java	2009-04-28 21:23:52 UTC (rev 10681)
@@ -59,7 +59,7 @@
    @Test
    public void testMultipleValueExpressionsAndParameters()
    {
-      String result = getInterpolatorInstance().interpolate("#{name}, meet {0}. {0}, meet #{name}. Everyone, meet {1}.", "Pete", "Gavin");
+      String result = getInterpolatorInstance().interpolate("#{name}, meet {0}. #0, meet #{name}. Everyone, meet {1}.", "Pete", "Gavin");
       assertEquals(result, "Dan, meet Pete. Pete, meet Dan. Everyone, meet Gavin.");
    }
 

Added: modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesTest.java
===================================================================
--- modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesTest.java	                        (rev 0)
+++ modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesTest.java	2009-04-28 21:23:52 UTC (rev 10681)
@@ -0,0 +1,50 @@
+package org.jboss.seam.international;
+
+import org.jboss.seam.el.Expressions;
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.testharness.impl.packaging.Classes;
+import org.jboss.webbeans.context.ConversationContext;
+import org.jboss.webbeans.context.api.helpers.ConcurrentHashMapBeanStore;
+import org.jboss.webbeans.test.AbstractWebBeansTest;
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+/**
+ * Integration test the StatusMessages bean to ensure that it finds
+ * and uses the interpolator and interpolates the message templates.
+ *
+ * @author Dan Allen
+ */
+ at Artifact(addCurrentPackage = false)
+ at Classes(
+{
+   StatusMessages.class, Interpolator.class, Expressions.class, LocaleProducer.class
+})
+public class StatusMessagesTest extends AbstractWebBeansTest
+{
+   @Override
+   public void beforeMethod()
+   {
+      super.beforeMethod();
+      ConversationContext.instance().setBeanStore(new ConcurrentHashMapBeanStore());
+      ConversationContext.instance().setActive(true);
+   }
+
+   @Test
+   public void testAddGlobalMessageFromTemplateWithInterpolations()
+   {
+      StatusMessages statusMessages = getStatusMessagesInstance();
+      statusMessages.add("The hole numbers of Amen Corner at #{'Augusta National'} are {0}, {1} and {2}.", 11, 12, 13);
+      statusMessages.onBeforeRender();
+      assertEquals(statusMessages.getGlobalMessages().size(), 1);
+      StatusMessage message = statusMessages.getGlobalMessages().get(0);
+      assertEquals(message.getSummary(), "The hole numbers of Amen Corner at Augusta National are 11, 12 and 13.");
+   }
+
+   // TODO test loading messages from resource bundle when resource bundle is ready
+
+   private StatusMessages getStatusMessagesInstance()
+   {
+      return getCurrentManager().getInstanceByType(StatusMessages.class);
+   }
+}

Modified: modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesUnitTest.java
===================================================================
--- modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesUnitTest.java	2009-04-28 20:29:58 UTC (rev 10680)
+++ modules/trunk/international/src/test/java/org/jboss/seam/international/StatusMessagesUnitTest.java	2009-04-28 21:23:52 UTC (rev 10681)
@@ -16,8 +16,6 @@
 @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.
@@ -33,7 +31,7 @@
       assertSame(message.getSeverity(), StatusMessage.Severity.INFO);
       assertFalse(message.isEmpty());
       assertNull(message.getSummary());
-      message.interpolate(identityInterpolator);
+      statusMessages.onBeforeRender();
       assertEquals(message.getSummary(), "Simple message");
       assertNull(message.getDetail());
    }
@@ -49,7 +47,7 @@
       assertEquals(statusMessages.getGlobalMessages().size(), 1);
       StatusMessage message = statusMessages.getGlobalMessages().get(0);
       assertSame(message.getSeverity(), StatusMessage.Severity.WARN);
-      message.interpolate(identityInterpolator);
+      statusMessages.onBeforeRender();
       assertEquals(message.getSummary(), "Summary template");
       assertEquals(message.getDetail(), "Detail template");
    }
@@ -81,7 +79,7 @@
       StatusMessage message = messages.get(0);
       assertSame(message.getSeverity(), StatusMessage.Severity.INFO);
       assertNull(message.getSummary());
-      message.interpolate(identityInterpolator);
+      statusMessages.onBeforeRender();
       assertEquals(message.getSummary(), "Available!");
       assertNull(message.getDetail());
       statusMessages.addToControl("username", "Nice choice");
@@ -101,7 +99,7 @@
       assertEquals(statusMessages.getKeyedMessages().size(), 0);
       StatusMessage message = statusMessages.getGlobalMessages().get(0);
       assertSame(message.getSeverity(), StatusMessage.Severity.WARN);
-      message.interpolate(identityInterpolator);
+      statusMessages.onBeforeRender();
       assertEquals(message.getSummary(), "Invalid number");
    }
 
@@ -187,9 +185,10 @@
 
    private StatusMessages getStatusMessagesInstance()
    {
-      return new StatusMessages();
+      return new StatusMessages(new IdentityInterpolator());
    }
 
+   // QUESTION should I make this a @Mock Interpolator and use Web Beans to test?
    private class IdentityInterpolator extends Interpolator
    {
       @Override




More information about the seam-commits mailing list