Author: johnh
Date: Thu Nov 26 22:04:58 2009
New Revision: 884712

URL: http://svn.apache.org/viewvc?rev=884712&view=rev
Log:
Fix for beahvioral regression: <gadget> (or <container>) sections in 
feature.xml _without_ a container-filter attribute should apply only to 
container requests that have _no_ other match(es) in the feature.xml with a 
container match.


Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java?rev=884712&r1=884711&r2=884712&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java
 Thu Nov 26 22:04:58 2009
@@ -38,7 +38,6 @@
 
 import java.io.File;
 import java.io.IOException;
-import java.net.URI;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -202,13 +201,27 @@
         ctx.getRenderingContext() == RenderingContext.CONTAINER ? "container" 
: "gadget";
     
     for (FeatureNode entry : featureNodes) {
+      boolean specificContainerMatched = false;
+      List<FeatureBundle> noContainerBundles = Lists.newLinkedList();
       for (FeatureBundle bundle : entry.getBundles()) {
         if (bundle.getType().equals(targetBundleType)) {
-          if (containerMatch(bundle.getAttribs().get("container"), 
ctx.getContainer())) {
-            resources.addAll(bundle.getResources());
+          String containerAttrib = bundle.getAttribs().get("container");
+          if (containerAttrib != null) {
+            if (containerMatch(containerAttrib, ctx.getContainer())) {
+              resources.addAll(bundle.getResources());
+              specificContainerMatched = true;
+            }
+          } else {
+            // Applies only if there were no specific container matches.
+            noContainerBundles.add(bundle);
           }
         }
       }
+      if (!specificContainerMatched) {
+        for (FeatureBundle bundle : noContainerBundles) {
+          resources.addAll(bundle.getResources());
+        }
+      }
     }
     
     if (useCache != null && (unsupported == null || unsupported.isEmpty())) {
@@ -340,10 +353,6 @@
   }
   
   private boolean containerMatch(String containerAttrib, String container) {
-    if (containerAttrib == null || containerAttrib.length() == 0) {
-      // Nothing specified = all match.
-      return true;
-    }
     Set<String> containers = Sets.newHashSet();
     for (String attr : containerAttrib.split(",")) {
       containers.add(attr.trim());
@@ -409,10 +418,6 @@
       throw new GadgetException(GadgetException.Code.INVALID_PATH, e);
     }
   }
-  
-  private void loadFile(String filePath) throws GadgetException, IOException {
-    loadFile(getFile(filePath));
-  }
 
   private void loadFile(File file) throws GadgetException, IOException {
     if (!file.exists() || !file.canRead()) {

Modified: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java?rev=884712&r1=884711&r2=884712&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java
 Thu Nov 26 22:04:58 2009
@@ -428,6 +428,55 @@
     }
   }
   
+  @Test
+  public void returnOnlyContainerFilteredJs() throws Exception {
+    String feature = "thefeature";
+    String container =  "foo";
+    String containerContent = "content1();";
+    String defaultContent = "content2();";
+    Uri featureUri =
+        expectResource(
+          getContainerAndDefaultTpl(feature, container, containerContent, 
defaultContent));
+    registry.register(featureUri.toString());
+    List<String> needed = Lists.newArrayList(feature);
+    List<String> unsupported = Lists.newLinkedList();
+    List<FeatureResource> resources = 
+        registry.getFeatureResources(
+          getCtx(RenderingContext.GADGET, container), needed, unsupported);
+    assertEquals(1, resources.size());
+    assertEquals(containerContent, resources.get(0).getContent());
+  }
+  
+  @Test
+  public void returnDefaultMatchJs() throws Exception {
+    String feature = "thefeature";
+    String container =  "foo";
+    String containerContent = "content1();";
+    String defaultContent = "content2();";
+    Uri featureUri =
+        expectResource(
+          getContainerAndDefaultTpl(feature, container, containerContent, 
defaultContent));
+    registry.register(featureUri.toString());
+    List<String> needed = Lists.newArrayList(feature);
+    List<String> unsupported = Lists.newLinkedList();
+    List<FeatureResource> resources = 
+        registry.getFeatureResources(
+          getCtx(RenderingContext.GADGET, "othercontainer"), needed, 
unsupported);
+    assertEquals(1, resources.size());
+    assertEquals(defaultContent, resources.get(0).getContent());
+  }
+  
+  private String getContainerAndDefaultTpl(String name, String container, 
String c1, String c2) {
+    StringBuilder sb = new StringBuilder();
+    sb.append("<feature><name>").append(name).append("</name>");
+    sb.append("<gadget container=\"").append(container).append("\">");
+    sb.append("<script>").append(c1).append("</script></gadget>");
+    sb.append("<gadget>");
+    sb.append("<script>").append(c2).append("</script></gadget>");
+    sb.append("</feature>");
+    return sb.toString();
+  }
+  
   private GadgetContext getCtx(final RenderingContext rctx, final String 
container) {
     return new GadgetContext() {
       @Override


Reply via email to