Author: lindner
Date: Mon May 10 14:12:22 2010
New Revision: 942745

URL: http://svn.apache.org/viewvc?rev=942745&view=rev
Log:
SHINDIG-1320 | Patch from Mat Mannion | Perform hangman variable substitution 
on variables with later precedence

Added:
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/MessageSubstituter.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/ModuleSubstituter.java
      - copied, changed from r942321, 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substituter.java
      - copied, changed from r942321, 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/SubstituterModule.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/MessageSubstituterTest.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/ModuleSubstituterTest.java
Modified:
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/BidiSubstituter.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/VariableSubstituter.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/process/ProcessorTest.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/BidiSubstituterTest.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/UserPrefSubstituterTest.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/VariableSubstituterTest.java

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java?rev=942745&r1=942744&r2=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java
 Mon May 10 14:12:22 2010
@@ -35,6 +35,8 @@ import org.apache.shindig.gadgets.servle
 import org.apache.shindig.gadgets.templates.TemplateModule;
 import org.apache.shindig.gadgets.uri.UriModule;
 
+import org.apache.shindig.gadgets.variables.SubstituterModule;
+
 import java.util.Set;
 import java.util.concurrent.Executor;
 import java.util.concurrent.ExecutorService;
@@ -63,6 +65,7 @@ public class DefaultGuiceModule extends 
     install(new PreloadModule());
     install(new RenderModule());
     install(new RewriteModule());
+    install(new SubstituterModule());
     install(new TemplateModule());
     install(new UriModule());
 

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/BidiSubstituter.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/BidiSubstituter.java?rev=942745&r1=942744&r2=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/BidiSubstituter.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/BidiSubstituter.java
 Mon May 10 14:12:22 2010
@@ -17,11 +17,19 @@
  */
 package org.apache.shindig.gadgets.variables;
 
+import com.google.inject.Inject;
+
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.GadgetException;
+import org.apache.shindig.gadgets.MessageBundleFactory;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.apache.shindig.gadgets.spec.MessageBundle;
+
 /**
  * Provides static hangman substitutions for bidirectional language support.
  * Useful for generating internationalized layouts using CSS.
  */
-public class BidiSubstituter {
+public class BidiSubstituter implements Substituter {
   public static final String START_EDGE = "START_EDGE";
   public static final String END_EDGE = "END_EDGE";
   public static final String DIR = "DIR";
@@ -32,15 +40,24 @@ public class BidiSubstituter {
   public static final String RTL = "rtl";
   public static final String LTR = "ltr";
 
-  public static void addSubstitutions(Substitutions substituter, String dir) {
+  private final MessageBundleFactory messageBundleFactory;
+
+  @Inject
+  public BidiSubstituter(MessageBundleFactory messageBundleFactory) {
+    this.messageBundleFactory = messageBundleFactory;
+  }
+
+  public void addSubstitutions(Substitutions substituter, GadgetContext 
context, GadgetSpec spec)
+      throws GadgetException {
+    MessageBundle bundle =
+        messageBundleFactory.getBundle(spec, context.getLocale(), 
context.getIgnoreCache(),
+                    context.getContainer());
+    String dir = bundle.getLanguageDirection();
+
     boolean rtl = RTL.equals(dir);
-    substituter.addSubstitution(Substitutions.Type.BIDI, START_EDGE,
-        rtl ? RIGHT : LEFT);
-    substituter.addSubstitution(Substitutions.Type.BIDI, END_EDGE,
-        rtl ? LEFT : RIGHT);
-    substituter.addSubstitution(Substitutions.Type.BIDI, DIR,
-        rtl ? RTL : LTR);
-    substituter.addSubstitution(Substitutions.Type.BIDI, REVERSE_DIR,
-        rtl ? LTR : RTL);
+    substituter.addSubstitution(Substitutions.Type.BIDI, START_EDGE, rtl ? 
RIGHT : LEFT);
+    substituter.addSubstitution(Substitutions.Type.BIDI, END_EDGE, rtl ? LEFT 
: RIGHT);
+    substituter.addSubstitution(Substitutions.Type.BIDI, DIR, rtl ? RTL : LTR);
+    substituter.addSubstitution(Substitutions.Type.BIDI, REVERSE_DIR, rtl ? 
LTR : RTL);
   }
 }

Added: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/MessageSubstituter.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/MessageSubstituter.java?rev=942745&view=auto
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/MessageSubstituter.java
 (added)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/MessageSubstituter.java
 Mon May 10 14:12:22 2010
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.shindig.gadgets.variables;
+
+import com.google.inject.Inject;
+
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.GadgetException;
+import org.apache.shindig.gadgets.MessageBundleFactory;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.apache.shindig.gadgets.spec.MessageBundle;
+
+/**
+ * Provides static hangman substitutions for message bundles.
+ */
+public class MessageSubstituter implements Substituter {
+  private final MessageBundleFactory messageBundleFactory;
+
+  @Inject
+  public MessageSubstituter(MessageBundleFactory messageBundleFactory) {
+    this.messageBundleFactory = messageBundleFactory;
+  }
+
+  public void addSubstitutions(Substitutions substituter, GadgetContext 
context, GadgetSpec spec)
+          throws GadgetException {
+    MessageBundle bundle = messageBundleFactory.getBundle(spec, 
context.getLocale(),
+        context.getIgnoreCache(), context.getContainer());
+        
+    substituter.addSubstitutions(Substitutions.Type.MESSAGE, 
bundle.getMessages());
+  }
+}

Copied: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/ModuleSubstituter.java
 (from r942321, 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java)
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/ModuleSubstituter.java?p2=shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/ModuleSubstituter.java&p1=shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java&r1=942321&r2=942745&rev=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/ModuleSubstituter.java
 Mon May 10 14:12:22 2010
@@ -17,29 +17,16 @@
  */
 package org.apache.shindig.gadgets.variables;
 
-import org.apache.shindig.gadgets.UserPrefs;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.GadgetException;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
-import org.apache.shindig.gadgets.spec.UserPref;
-
-import org.apache.commons.lang.StringEscapeUtils;
 
 /**
- * Substitutes user prefs into the spec.
+ * Provides hangman substitution variables related to the Module (i.e. 
__MODULE_ID__)
  */
-public class UserPrefSubstituter {
-  public static void addSubstitutions(Substitutions substituter,
-      GadgetSpec spec, UserPrefs values) {
-    for (UserPref pref : spec.getUserPrefs()) {
-      String name = pref.getName();
-      String value = values.getPref(name);
-      if (value == null) {
-        value = pref.getDefaultValue();
-        if (value == null) {
-          value = "";
-        }
-      }
-      substituter.addSubstitution(Substitutions.Type.USER_PREF, name,
-          StringEscapeUtils.escapeHtml(value));
-    }
+public class ModuleSubstituter implements Substituter {
+  public void addSubstitutions(Substitutions substituter, GadgetContext 
context, GadgetSpec spec)
+        throws GadgetException {
+    substituter.addSubstitution(Substitutions.Type.MODULE, "ID", 
Integer.toString(context.getModuleId()));
   }
 }

Copied: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substituter.java
 (from r942321, 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java)
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substituter.java?p2=shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substituter.java&p1=shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java&r1=942321&r2=942745&rev=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substituter.java
 Mon May 10 14:12:22 2010
@@ -17,29 +17,24 @@
  */
 package org.apache.shindig.gadgets.variables;
 
-import org.apache.shindig.gadgets.UserPrefs;
-import org.apache.shindig.gadgets.spec.GadgetSpec;
-import org.apache.shindig.gadgets.spec.UserPref;
+import org.apache.shindig.gadgets.GadgetException;
 
-import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
 
 /**
- * Substitutes user prefs into the spec.
+ * Substituter that provides variables to {...@link VariableSubstituter}.
  */
-public class UserPrefSubstituter {
-  public static void addSubstitutions(Substitutions substituter,
-      GadgetSpec spec, UserPrefs values) {
-    for (UserPref pref : spec.getUserPrefs()) {
-      String name = pref.getName();
-      String value = values.getPref(name);
-      if (value == null) {
-        value = pref.getDefaultValue();
-        if (value == null) {
-          value = "";
-        }
-      }
-      substituter.addSubstitution(Substitutions.Type.USER_PREF, name,
-          StringEscapeUtils.escapeHtml(value));
-    }
-  }
+public interface Substituter {
+
+  /**
+   * Add the substitutions from this Substituter to the {...@link 
Substitutions}.
+   * 
+   * @param substituter container for the new substitutions, containing any 
existing substitutions
+   * @param context the context in which this gadget is being rendered
+   * @param spec the gadget specification being substituted
+   * @throws GadgetException when there has been a general error adding 
substitutions
+   */
+  void addSubstitutions(Substitutions substituter, GadgetContext context, 
GadgetSpec spec) throws GadgetException;
+
 }

Added: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/SubstituterModule.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/SubstituterModule.java?rev=942745&view=auto
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/SubstituterModule.java
 (added)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/SubstituterModule.java
 Mon May 10 14:12:22 2010
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.shindig.gadgets.variables;
+
+import java.util.List;
+
+import com.google.common.collect.Lists;
+import com.google.inject.AbstractModule;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.TypeLiteral;
+import com.google.inject.name.Names;
+
+/**
+ * Guice bindings for the variables package.
+ */
+public class SubstituterModule extends AbstractModule {
+
+  @Override
+  protected void configure() {
+    bind(new TypeLiteral<List<Substituter>>(){})
+        .annotatedWith(Names.named("shindig.substituters.gadget"))
+        .toProvider(SubstitutersProvider.class);
+  }
+
+  public static class SubstitutersProvider implements 
Provider<List<Substituter>> {
+    private final List<Substituter> substituters;
+
+    @Inject
+    public SubstitutersProvider(MessageSubstituter messageSubstituter,
+        UserPrefSubstituter prefSubstituter,
+        ModuleSubstituter moduleSubstituter,
+        BidiSubstituter bidiSubstituter) {
+      substituters = Lists.newArrayList();
+      substituters.add(messageSubstituter);
+      substituters.add(prefSubstituter);
+      substituters.add(moduleSubstituter);
+      substituters.add(bidiSubstituter);
+    }
+
+    public List<Substituter> get() {
+      return substituters;
+    }
+  }
+}

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java?rev=942745&r1=942744&r2=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java
 Mon May 10 14:12:22 2010
@@ -41,19 +41,19 @@ public class Substitutions {
     MESSAGE("MSG"),
 
     /**
-     * Bi-directional text transformations.
-     */
-    BIDI("BIDI"),
-
-    /**
      * User preferences.
      */
     USER_PREF("UP"),
-
+    
     /**
      * MODULE_ variables (i.e. MODULE_ID)
      */
-    MODULE("MODULE");
+    MODULE("MODULE"),
+
+    /**
+     * Bi-directional text transformations.
+     */
+    BIDI("BIDI");
 
     private final String prefix;
 
@@ -83,7 +83,7 @@ public class Substitutions {
    * @param value
    */
   public void addSubstitution(Type type, String key, String value) {
-    substitutions.put(type.prefix + key, value);
+    substitutions.put(type.prefix + key, substituteString(value));
   }
 
   /**

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java?rev=942745&r1=942744&r2=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/UserPrefSubstituter.java
 Mon May 10 14:12:22 2010
@@ -17,18 +17,20 @@
  */
 package org.apache.shindig.gadgets.variables;
 
+import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.shindig.gadgets.GadgetContext;
 import org.apache.shindig.gadgets.UserPrefs;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
 import org.apache.shindig.gadgets.spec.UserPref;
 
-import org.apache.commons.lang.StringEscapeUtils;
-
 /**
  * Substitutes user prefs into the spec.
  */
-public class UserPrefSubstituter {
-  public static void addSubstitutions(Substitutions substituter,
-      GadgetSpec spec, UserPrefs values) {
+public class UserPrefSubstituter implements Substituter {
+
+  public void addSubstitutions(Substitutions substituter, GadgetContext 
context, GadgetSpec spec) {
+    UserPrefs values = context.getUserPrefs();
+    
     for (UserPref pref : spec.getUserPrefs()) {
       String name = pref.getName();
       String value = values.getPref(name);
@@ -38,8 +40,8 @@ public class UserPrefSubstituter {
           value = "";
         }
       }
-      substituter.addSubstitution(Substitutions.Type.USER_PREF, name,
-          StringEscapeUtils.escapeHtml(value));
+      substituter.addSubstitution(Substitutions.Type.USER_PREF, name, 
StringEscapeUtils
+            .escapeHtml(value));
     }
   }
 }

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/VariableSubstituter.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/VariableSubstituter.java?rev=942745&r1=942744&r2=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/VariableSubstituter.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/VariableSubstituter.java
 Mon May 10 14:12:22 2010
@@ -18,23 +18,25 @@
  */
 package org.apache.shindig.gadgets.variables;
 
+import java.util.List;
+
+import com.google.common.collect.ImmutableList;
+import com.google.inject.Inject;
+import com.google.inject.name.Named;
+
 import org.apache.shindig.gadgets.GadgetContext;
 import org.apache.shindig.gadgets.GadgetException;
-import org.apache.shindig.gadgets.MessageBundleFactory;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
-import org.apache.shindig.gadgets.spec.MessageBundle;
-
-import com.google.inject.Inject;
 
 /**
  * Performs variable substitution on a gadget spec.
  */
 public class VariableSubstituter {
-  private final MessageBundleFactory messageBundleFactory;
+  private final List<Substituter> substituters;
 
   @Inject
-  public VariableSubstituter(MessageBundleFactory messageBundleFactory) {
-    this.messageBundleFactory = messageBundleFactory;
+  public VariableSubstituter(@Named("shindig.substituters.gadget") 
List<Substituter> substituters) {
+    this.substituters = ImmutableList.copyOf(substituters);
   }
 
   /**
@@ -42,18 +44,13 @@ public class VariableSubstituter {
    *
    * @return A new GadgetSpec, with all fields substituted as needed.
    */
-  public GadgetSpec substitute(GadgetContext context, GadgetSpec spec) throws 
GadgetException {
-    MessageBundle bundle =
-        messageBundleFactory.getBundle(spec, context.getLocale(), 
context.getIgnoreCache(), context.getContainer());
-    String dir = bundle.getLanguageDirection();
-
-    Substitutions substituter = new Substitutions();
-    substituter.addSubstitutions(Substitutions.Type.MESSAGE, 
bundle.getMessages());
-    BidiSubstituter.addSubstitutions(substituter, dir);
-    substituter.addSubstitution(Substitutions.Type.MODULE, "ID",
-        Integer.toString(context.getModuleId()));
-    UserPrefSubstituter.addSubstitutions(substituter, spec, 
context.getUserPrefs());
-
-    return spec.substitute(substituter);
+  public GadgetSpec substitute(GadgetContext context, GadgetSpec spec) throws 
GadgetException {   
+    Substitutions substitutions = new Substitutions();
+    
+    for (Substituter substituter : substituters) {
+        substituter.addSubstitutions(substitutions, context, spec);
+    }
+    
+    return spec.substitute(substitutions);
   }
 }

Modified: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/process/ProcessorTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/process/ProcessorTest.java?rev=942745&r1=942744&r2=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/process/ProcessorTest.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/process/ProcessorTest.java
 Mon May 10 14:12:22 2010
@@ -40,6 +40,10 @@ import org.junit.Test;
 
 import javax.servlet.http.HttpServletResponse;
 
+import com.google.common.collect.Lists;
+
+import org.apache.shindig.gadgets.variables.Substituter;
+
 public class ProcessorTest {
   private static final Uri SPEC_URL = 
Uri.parse("http://example.org/gadget.xml";);
   private static final Uri TYPE_URL_HREF = 
Uri.parse("http://example.org/gadget.php";);
@@ -196,7 +200,7 @@ public class ProcessorTest {
     protected boolean wasSubstituted;
 
     protected FakeVariableSubstituter() {
-      super(null);
+      super(Lists.<Substituter>newArrayList());
     }
 
     @Override

Modified: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/BidiSubstituterTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/BidiSubstituterTest.java?rev=942745&r1=942744&r2=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/BidiSubstituterTest.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/BidiSubstituterTest.java
 Mon May 10 14:12:22 2010
@@ -17,48 +17,64 @@
  */
 package org.apache.shindig.gadgets.variables;
 
-import org.apache.shindig.gadgets.variables.BidiSubstituter;
-import org.apache.shindig.gadgets.variables.Substitutions;
-
+import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.render.FakeMessageBundleFactory;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
 import org.junit.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 
 public class BidiSubstituterTest extends Assert {
 
   @Test
-  public void testBidiWithRtl() {
+  public void testBidiWithRtl() throws Exception {
     assertRightToLeft(BidiSubstituter.RTL);
   }
 
   @Test
-  public void testBidiWithLtr() {
+  public void testBidiWithLtr() throws Exception {
     assertLeftToRight(BidiSubstituter.LTR);
   }
 
   @Test
-  public void testBidiWithEmpty() {
+  @Ignore("Is this valid behaviour?")
+  public void testBidiWithEmpty() throws Exception {
     assertLeftToRight("");
   }
 
   @Test
-  public void testBidiWithNull() {
+  @Ignore("Is this valid behaviour?")
+  public void testBidiWithNull() throws Exception {
     assertLeftToRight(null);
   }
 
-  private void assertRightToLeft(String direction) {
+  private void assertRightToLeft(String direction) throws Exception {
     assertSubstitutions(direction, BidiSubstituter.RIGHT,
         BidiSubstituter.LEFT, BidiSubstituter.RTL, BidiSubstituter.LTR);
   }
 
-  private void assertLeftToRight(String direction) {
+  private void assertLeftToRight(String direction) throws Exception {
     assertSubstitutions(direction, BidiSubstituter.LEFT,
         BidiSubstituter.RIGHT, BidiSubstituter.LTR, BidiSubstituter.RTL);
   }
 
   private void assertSubstitutions(String direction,
-      String startEdge, String endEdge, String dir, String reverseDir) {
+      String startEdge, String endEdge, String dir, String reverseDir) throws 
Exception {
+    String xml = 
+        "<Module><ModulePrefs title=''>" +
+        "  <Locale language_direction='" + direction + "' />" +
+        "</ModulePrefs>" +
+        "<Content />" +
+        "</Module>";
+    
+    GadgetSpec spec = new GadgetSpec(Uri.parse("#"), xml);
+    GadgetContext context = new GadgetContext();
+
+    BidiSubstituter substituter = new BidiSubstituter(new 
FakeMessageBundleFactory());  
+      
     Substitutions substitutions = new Substitutions();
-    BidiSubstituter.addSubstitutions(substitutions, direction);
+    substituter.addSubstitutions(substitutions, context, spec);
 
     assertEquals(startEdge, substitutions.getSubstitution(
         Substitutions.Type.BIDI, BidiSubstituter.START_EDGE));

Added: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/MessageSubstituterTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/MessageSubstituterTest.java?rev=942745&view=auto
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/MessageSubstituterTest.java
 (added)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/MessageSubstituterTest.java
 Mon May 10 14:12:22 2010
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.shindig.gadgets.variables;
+
+import org.apache.shindig.gadgets.variables.Substitutions.Type;
+
+import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.render.FakeMessageBundleFactory;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MessageSubstituterTest extends Assert {
+  private final FakeMessageBundleFactory messageBundleFactory = new 
FakeMessageBundleFactory();
+  private final MessageSubstituter substituter = new 
MessageSubstituter(messageBundleFactory);
+
+  private final GadgetContext context = new GadgetContext();
+
+  @Test
+  public void testMessageReplacements() throws Exception {
+    String xml = 
+        "<Module>" +
+        " <ModulePrefs title=''>" +
+        "  <Locale>" +
+        "    <msg name='foo'>bar</msg>" +
+        "    <msg name='bar'>baz</msg>" +
+        "  </Locale>" +
+        " </ModulePrefs>" +
+        " <Content />" +
+        "</Module>";
+      
+    Substitutions substitutions = new Substitutions();
+    substituter.addSubstitutions(substitutions, context, new 
GadgetSpec(Uri.parse("#"), xml));
+    
+    assertEquals("bar", substitutions.getSubstitution(Type.MESSAGE, "foo"));
+    assertEquals("baz", substitutions.getSubstitution(Type.MESSAGE, "bar"));
+  }
+}

Added: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/ModuleSubstituterTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/ModuleSubstituterTest.java?rev=942745&view=auto
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/ModuleSubstituterTest.java
 (added)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/ModuleSubstituterTest.java
 Mon May 10 14:12:22 2010
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.shindig.gadgets.variables;
+
+import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.apache.shindig.gadgets.variables.Substitutions.Type;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ModuleSubstituterTest extends Assert {
+  private final Substitutions substitutions = new Substitutions();
+  private GadgetSpec spec;
+
+  @Before
+  public void setUp() throws Exception {
+    spec = new GadgetSpec(Uri.parse("#"), "<Module><ModulePrefs title='' 
/><Content /></Module>");
+  }
+
+  @Test
+  public void testDefault() throws Exception {
+    ModuleSubstituter substituter = new ModuleSubstituter();
+    substituter.addSubstitutions(substitutions, new GadgetContext(), spec);
+
+    assertEquals("0",
+        substitutions.getSubstitution(Type.MODULE, "ID"));
+  }
+
+  @Test
+  public void testSpecific() throws Exception {
+    final int moduleId = 12345678;  
+      
+    ModuleSubstituter substituter = new ModuleSubstituter();
+    substituter.addSubstitutions(substitutions, new GadgetContext() {
+        @Override
+        public int getModuleId() {
+            return moduleId;
+        }
+    }, spec);
+
+    assertEquals(Integer.toString(moduleId),
+        substitutions.getSubstitution(Type.MODULE, "ID"));
+  }
+}

Modified: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/UserPrefSubstituterTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/UserPrefSubstituterTest.java?rev=942745&r1=942744&r2=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/UserPrefSubstituterTest.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/UserPrefSubstituterTest.java
 Mon May 10 14:12:22 2010
@@ -19,19 +19,19 @@
 
 package org.apache.shindig.gadgets.variables;
 
+import java.util.Map;
+
 import com.google.common.collect.ImmutableMap;
 
 import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.gadgets.GadgetContext;
 import org.apache.shindig.gadgets.UserPrefs;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
 import org.apache.shindig.gadgets.variables.Substitutions.Type;
-
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
-import java.util.Map;
-
 public class UserPrefSubstituterTest extends Assert {
   private final Substitutions substituter = new Substitutions();
   private final static String DEFAULT_NAME = "default";
@@ -63,8 +63,15 @@ public class UserPrefSubstituterTest ext
   @Test
   public void testSubstitutions() throws Exception {
     Map<String, String> map = ImmutableMap.of(USER_NAME, USER_VALUE, 
OVERRIDE_NAME, OVERRIDE_VALUE);
-    UserPrefs prefs = new UserPrefs(map);
-    UserPrefSubstituter.addSubstitutions(substituter, spec, prefs);
+    final UserPrefs prefs = new UserPrefs(map);
+    GadgetContext context = new GadgetContext() {
+        @Override
+        public UserPrefs getUserPrefs() {
+            return prefs;
+        }
+    };
+    
+    new UserPrefSubstituter().addSubstitutions(substituter, context, spec);
 
     assertEquals(DEFAULT_VALUE,
         substituter.getSubstitution(Type.USER_PREF, DEFAULT_NAME));
@@ -77,8 +84,15 @@ public class UserPrefSubstituterTest ext
   @Test
   public void testEscaping() throws Exception {
     Map<String, String> map = ImmutableMap.of(USER_NAME, UNESCAPED_USER_VALUE);
-    UserPrefs prefs = new UserPrefs(map);
-    UserPrefSubstituter.addSubstitutions(substituter, spec, prefs);
+    final UserPrefs prefs = new UserPrefs(map);
+    GadgetContext context = new GadgetContext() {
+        @Override
+        public UserPrefs getUserPrefs() {
+            return prefs;
+        }
+    };
+    
+    new UserPrefSubstituter().addSubstitutions(substituter, context, spec);
     assertEquals(ESCAPED_USER_VALUE,
         substituter.getSubstitution(Type.USER_PREF, USER_NAME));
   }

Modified: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/VariableSubstituterTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/VariableSubstituterTest.java?rev=942745&r1=942744&r2=942745&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/VariableSubstituterTest.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/VariableSubstituterTest.java
 Mon May 10 14:12:22 2010
@@ -30,13 +30,20 @@ import org.apache.shindig.gadgets.spec.L
 import org.apache.shindig.gadgets.spec.MessageBundle;
 
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+
 import org.junit.Test;
 
 import java.util.Locale;
 
 public class VariableSubstituterTest {
   private final FakeMessageBundleFactory messageBundleFactory = new 
FakeMessageBundleFactory();
-  private final VariableSubstituter substituter = new 
VariableSubstituter(messageBundleFactory);
+  private final VariableSubstituter substituter = new 
VariableSubstituter(Lists.newArrayList(
+    new MessageSubstituter(messageBundleFactory),
+    new UserPrefSubstituter(),
+    new ModuleSubstituter(),
+    new BidiSubstituter(messageBundleFactory)
+  ));
 
   private GadgetSpec substitute(String xml) throws Exception {
     return substituter.substitute(new GadgetContext(), new 
GadgetSpec(Uri.parse("#"), xml));
@@ -93,6 +100,24 @@ public class VariableSubstituterTest {
 
     assertEquals("I heart shindig", spec.getModulePrefs().getTitle());
   }
+  
+  @Test
+  public void nestedMessageBundleInUserPrefSubstituted() throws Exception {
+    String xml = 
+      "<Module>" + 
+      " <ModulePrefs title='__UP_title__ for __MODULE_ID__'>" +
+      "  <Locale>" +
+      "   <msg name='title'>Gadget title</msg>" +
+      "  </Locale>" +
+      " </ModulePrefs>" +
+      " <UserPref name='title' default_value='__MSG_title__' />" +
+      " <Content />" +
+      "</Module>";
+
+    GadgetSpec spec = substitute(xml);
+
+    assertEquals("Gadget title for 0", spec.getModulePrefs().getTitle());
+  }
 
   private static class FakeMessageBundleFactory implements 
MessageBundleFactory {
 


Reply via email to