shindig-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject svn commit: r1145758 - in /shindig/trunk/java/gadgets/src: main/java/org/apache/shindig/gadgets/js/ main/java/org/apache/shindig/gadgets/render/ test/java/org/apache/shindig/gadgets/js/ test/java/org/apache/shindig/gadgets/servlet/
Date Tue, 12 Jul 2011 20:40:39 GMT
Author: johnh
Date: Tue Jul 12 20:40:38 2011
New Revision: 1145758

URL: http://svn.apache.org/viewvc?rev=1145758&view=rev
Log:
End-to-end support for supportDefer="true" in ExportJsProcessor

Also includes all/new/loaded feature computation in JsRequest, as a useful convenience.

This CL adds support for the supportDefer="true" annotation in <api>. In its presence,
the exportJs() calls generated by ExportJsProcessor are augmented to request deferred symbol
binding when &jsload=1. This in turn allows support for synchronously enqueueing requests
to symbols whose full implementations haven't yet been loaded.



Modified:
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/ExportJsProcessor.java
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequest.java
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequestBuilder.java
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsServingPipelineModule.java
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java
    shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java
    shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsRequestBuilderTest.java
    shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerServiceTest.java
    shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerTest.java
    shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/JsServletTest.java

Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/ExportJsProcessor.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/ExportJsProcessor.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/ExportJsProcessor.java
(original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/ExportJsProcessor.java
Tue Jul 12 20:40:38 2011
@@ -49,40 +49,57 @@ public class ExportJsProcessor implement
 
   private final FeatureRegistryProvider featureRegistryProvider;
   private final Provider<GadgetContext> context;
+  private final boolean deferredMode;
 
   @Inject
   public ExportJsProcessor(FeatureRegistryProvider featureRegistryProvider,
       Provider<GadgetContext> context) {
+    this(featureRegistryProvider, context, false);
+  }
+  
+  private ExportJsProcessor(FeatureRegistryProvider featureRegistryProvider,
+      Provider<GadgetContext> context, boolean defer) {
     this.featureRegistryProvider = featureRegistryProvider;
     this.context = context;
+    this.deferredMode = defer;
+  }
+  
+  public ExportJsProcessor getDeferredInstance() {
+    return new ExportJsProcessor(featureRegistryProvider, context, true);
   }
 
   public boolean process(JsRequest jsRequest, JsResponseBuilder builder) throws JsException
{
     JsUri jsUri = jsRequest.getJsUri();
     ImmutableList.Builder<JsContent> resp = ImmutableList.builder();
 
+    FeatureRegistry featureRegistry;
+    try {
+      featureRegistry = featureRegistryProvider.get(jsUri.getRepository());
+    } catch (GadgetException e) {
+      throw new JsException(e.getHttpStatusCode(), e.getMessage());
+    }
+
     boolean neededExportJs = false;
     FeatureBundle last = null;
-    for (JsContent jsc : builder.build().getAllJsContent()) {
-      FeatureBundle current = jsc.getFeatureBundle();
-      if (last != null && current != last) {
+    if (!jsUri.isJsload()) {
+      for (JsContent jsc : builder.build().getAllJsContent()) {
+        FeatureBundle current = jsc.getFeatureBundle();
+        if (last != null && current != last) {
+          neededExportJs |= appendExportJsStatementsForFeature(resp, jsUri, last);
+        }
+        resp.add(jsc);
+        last = current;
+      }
+      if (last != null) {
         neededExportJs |= appendExportJsStatementsForFeature(resp, jsUri, last);
       }
-      resp.add(jsc);
-      last = current;
+    } else if (deferredMode) {
+      // append all exports for deferred symbols
+      neededExportJs = appendExportJsStatementsDeferred(featureRegistry, resp, jsRequest);
     }
-    if (last != null) {
-      neededExportJs |= appendExportJsStatementsForFeature(resp, jsUri, last);
-    }
-
+    
     builder.clearJs();
     if (neededExportJs) {
-      FeatureRegistry featureRegistry;
-      try {
-        featureRegistry = featureRegistryProvider.get(jsUri.getRepository());
-      } catch (GadgetException e) {
-        throw new JsException(e.getHttpStatusCode(), e.getMessage());
-      }
       builder.appendAllJs(getExportJsContents(featureRegistry));
     }
     builder.appendAllJs(resp.build());
@@ -103,11 +120,11 @@ public class ExportJsProcessor implement
         exports = bundle.getApis(ApiDirective.Type.JS, true);
       }
     }
-
+    
     if (!exports.isEmpty()) {
       StringBuilder sb = new StringBuilder();
       for (Input input : generateInputs(exports)) {
-        sb.append(input.toExportStatement());
+        sb.append(input.toExportStatement(jsUri.isJsload()));
       }
       builder.add(JsContent.fromFeature(sb.toString(),
           "[generated-symbol-exports]", bundle, null));
@@ -116,6 +133,19 @@ public class ExportJsProcessor implement
     return false;
   }
 
+  private boolean appendExportJsStatementsDeferred(FeatureRegistry registry,
+      ImmutableList.Builder<JsContent> builder, JsRequest jsRequest) {
+    LookupResult lookup = registry.getFeatureResources(context.get(),
+        jsRequest.getNewFeatures(), null, false);
+    
+    boolean neededExports = false;
+    for (FeatureBundle bundle : lookup.getBundles()) {
+      neededExports |= appendExportJsStatementsForFeature(builder, jsRequest.getJsUri(),
bundle);
+    }
+    
+    return neededExports;
+  }
+
   private List<JsContent> getExportJsContents(FeatureRegistry featureRegistry) {
     ImmutableList.Builder<JsContent> result = ImmutableList.builder();
     LookupResult lookup = featureRegistry.getFeatureResources(context.get(),
@@ -149,12 +179,13 @@ public class ExportJsProcessor implement
       return new Input(namespace, components);
     }
 
-    public String toExportStatement() {
+    public String toExportStatement(boolean isJsload) {
       StringBuilder result = new StringBuilder();
 
       // Local namespace.
       if (namespace != null) {
         result.append(FUNCTION_NAME).append("('").append(namespace).append("',[");
+        result.append(isJsload ? "window." : "");
         result.append(Joiner.on(',').join(components));
         result.append("],{");
         for (int i = 0; i < properties.size(); i++) {
@@ -162,14 +193,24 @@ public class ExportJsProcessor implement
           if (i > 0) result.append(",");
           result.append(prop).append(":'").append(prop).append("'");
         }
-        result.append("});");
+        result.append("}");
+        if (isJsload) {
+          result.append(",1");
+        }
+        result.append(");");
 
       // Global/window namespace.
       } else {
         for (String prop : properties) {
           result.append(FUNCTION_NAME).append("(");
-          result.append("'").append(prop).append("',[").append(prop);
-          result.append("]);");
+          result.append("'").append(prop).append("',[");
+          result.append(isJsload ? "window." : "");
+          result.append(prop);
+          result.append("]");
+          if (isJsload) {
+            result.append(",{},1");
+          }
+          result.append(");");
         }
       }
       return result.toString();

Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequest.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequest.java
(original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequest.java
Tue Jul 12 20:40:38 2011
@@ -19,8 +19,14 @@
 
 package org.apache.shindig.gadgets.js;
 
+import org.apache.shindig.gadgets.features.FeatureRegistry;
 import org.apache.shindig.gadgets.uri.JsUriManager.JsUri;
 
+import com.google.common.collect.Lists;
+
+import java.util.Iterator;
+import java.util.List;
+
 /**
  * Data about a JavaScript request.
  * 
@@ -31,31 +37,76 @@ public class JsRequest {
   private final JsUri jsUri;
   private final String host;
   private final boolean inCache;
+  private final FeatureRegistry registry;
+  private List<String> allFeatures;
+  private List<String> newFeatures;
+  private List<String> loadedFeatures;
 
-  JsRequest(JsUri jsUri, String host, boolean inCache) {
+  JsRequest(JsUri jsUri, String host, boolean inCache, FeatureRegistry registry) {
     this.jsUri = jsUri;
     this.host = host;
     this.inCache = inCache;
+    this.registry = registry;
   }
  
   /**
-   * Returns this request's {@link JsUri}.
+   * @return this request's {@link JsUri}.
    */
   public JsUri getJsUri() {
     return jsUri;
   }
 
   /**
-   * Returns the host this request was directed to.
+   * @return the host this request was directed to.
    */
   public String getHost() {
     return host;
   }
 
   /**
-   * Returns whether the client has this JS code in the cache.
+   * @return whether the client has this JS code in the cache.
    */
   public boolean isInCache() {
     return inCache;
   }
+  
+  /**
+   * @return All features encapsulated by this request, including deps, in dep order.
+   */
+  public List<String> getAllFeatures() {
+    initFeaturesLists();
+    return allFeatures;
+  }
+  
+  /**
+   * @return Features to be newly returned by this request (all - loaded), in dep order.
+   */
+  public List<String> getNewFeatures() {
+    initFeaturesLists();
+    return newFeatures;    
+  }
+  
+  /**
+   * @return Full list of all features previously loaded before this request, in dep order.
+   */
+  public List<String> getLoadedFeatures() {
+    initFeaturesLists();
+    return loadedFeatures;
+  }
+  
+  private void initFeaturesLists() {
+    if (allFeatures == null) {
+      // Lazy-initialize these, to avoid computation where not needed.
+      allFeatures = registry.getFeatures(jsUri.getLibs());
+      loadedFeatures = registry.getFeatures(jsUri.getLoadedLibs());
+      newFeatures = Lists.newLinkedList();
+      Iterator<String> allIt = allFeatures.iterator();
+      while (allIt.hasNext()) {
+        String candidate = allIt.next();
+        if (!loadedFeatures.contains(candidate)) {
+          newFeatures.add(candidate);
+        }
+      }
+    }
+  }
 }

Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequestBuilder.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequestBuilder.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequestBuilder.java
(original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsRequestBuilder.java
Tue Jul 12 20:40:38 2011
@@ -22,6 +22,7 @@ import com.google.inject.Inject;
 
 import org.apache.shindig.common.uri.UriBuilder;
 import org.apache.shindig.gadgets.GadgetException;
+import org.apache.shindig.gadgets.features.FeatureRegistry;
 import org.apache.shindig.gadgets.uri.JsUriManager;
 import org.apache.shindig.gadgets.uri.JsUriManager.JsUri;
 
@@ -33,10 +34,13 @@ import javax.servlet.http.HttpServletReq
 public class JsRequestBuilder {
 
   private final JsUriManager jsUriManager;
+  private final FeatureRegistry registry;
 
   @Inject
-  public JsRequestBuilder(JsUriManager jsUriManager) {
+  public JsRequestBuilder(JsUriManager jsUriManager,
+                          FeatureRegistry registry) {
     this.jsUriManager = jsUriManager; 
+    this.registry = registry;
   }
   
   /**
@@ -64,6 +68,6 @@ public class JsRequestBuilder {
   }
 
   protected JsRequest build(JsUri jsUri, String host, boolean inCache) {
-    return new JsRequest(jsUri, host, inCache);
+    return new JsRequest(jsUri, host, inCache, registry);
   }
 }

Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsServingPipelineModule.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsServingPipelineModule.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsServingPipelineModule.java
(original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsServingPipelineModule.java
Tue Jul 12 20:40:38 2011
@@ -50,8 +50,10 @@ public class JsServingPipelineModule ext
       ConfigInjectionProcessor configInjectionProcessor,
       AddJslLoadedVariableProcessor addJslLoadedVariableProcessor,
       AddOnloadFunctionProcessor addOnloadFunctionProcessor) {
+    jsLoaderGeneratorProcessor.setUseAsync(true);
     return ImmutableList.of(
         addJslInfoVariableProcessor,
+        exportJsProcessor.getDeferredInstance(),
         jsLoaderGeneratorProcessor,
         ifModifiedSinceProcessor,
         getJsContentProcessor,

Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java
(original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/RenderingGadgetRewriter.java
Tue Jul 12 20:40:38 2011
@@ -35,7 +35,6 @@ import org.apache.shindig.gadgets.Unsupp
 import org.apache.shindig.gadgets.config.ConfigProcessor;
 import org.apache.shindig.gadgets.features.FeatureRegistry;
 import org.apache.shindig.gadgets.features.FeatureRegistryProvider;
-import org.apache.shindig.gadgets.features.FeatureResource;
 import org.apache.shindig.gadgets.js.JsException;
 import org.apache.shindig.gadgets.js.JsRequest;
 import org.apache.shindig.gadgets.js.JsRequestBuilder;
@@ -336,9 +335,8 @@ public class RenderingGadgetRewriter imp
     // Get all resources requested by the gadget's requires/optional features.
     Map<String, Feature> featureMap = gadget.getViewFeatures();
     List<String> gadgetFeatureKeys = Lists.newLinkedList(gadget.getDirectFeatureDeps());
-    List<FeatureResource> gadgetResources =
-        featureRegistry.getFeatureResources(gadget.getContext(), gadgetFeatureKeys, unsupported)
-            .getResources();
+    featureRegistry.getFeatureResources(gadget.getContext(), gadgetFeatureKeys, unsupported)
+                   .getResources();
     if (!unsupported.isEmpty()) {
       List<String> requiredUnsupported = Lists.newLinkedList();
       for (String notThere : unsupported) {
@@ -354,11 +352,12 @@ public class RenderingGadgetRewriter imp
   }
 
   /**
-   * Get the js content for a request (JsUri)
+   * Get the JS content for a request (JsUri)
    */
   protected String getFeaturesContent(JsUri jsUri) throws GadgetException {
     // Inject js content, fetched from JsPipeline
-    JsRequest jsRequest = new JsRequestBuilder(jsUriManager).build(jsUri, null);
+    JsRequest jsRequest = new JsRequestBuilder(jsUriManager,
+        featureRegistryProvider.get(jsUri.getRepository())).build(jsUri, null);
     JsResponse jsResponse;
     try {
       jsResponse = jsServingPipeline.execute(jsRequest);

Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java
(original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java
Tue Jul 12 20:40:38 2011
@@ -18,6 +18,7 @@
 package org.apache.shindig.gadgets.js;
 
 import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.eq;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.isA;
 import static org.easymock.EasyMock.replay;
@@ -69,12 +70,21 @@ public class ExportJsProcessorTest {
       "exportJs('shindig',[shindig]);" +
       "exportJs('gadgets.rpc',[gadgets,gadgets.rpc],{call:'call'});" +
       "exportJs('shindig',[shindig],{random:'random'});";
+  
+  private final String EXPORT_STRING_1_DEFER =
+    "exportJs('gadgets',[window.gadgets],{},1);" +
+    "exportJs('shindig',[window.shindig],{},1);" +
+    "exportJs('gadgets.rpc',[window.gadgets,gadgets.rpc],{call:'call'},1);" +
+    "exportJs('shindig',[window.shindig],{random:'random'},1);";
 
   private final String EXPORT_STRING_2 =
       "exportJs('foo',[foo]);" +
       "exportJs('foo.prototype',[foo,foo.prototype],{bar:'bar'});";
 
   private final String EXPORT_STRING_3 = "";
+  
+  private final List<String> LIBS = Lists.newArrayList("lib1");
+  private final List<String> LOADED = Lists.newArrayList();
 
   private JsContent textJsContent1;
   private JsContent textJsContent2;
@@ -82,6 +92,7 @@ public class ExportJsProcessorTest {
   private JsContent featureJsContent2;
   private JsContent featureJsContent3;
   private ExportJsProcessor compiler;
+  private FeatureRegistry featureRegistry;
 
   @Before
   public void setUp() throws Exception {
@@ -91,6 +102,7 @@ public class ExportJsProcessorTest {
     FeatureRegistry.FeatureBundle bundle = mockExportJsBundle(resource);
     LookupResult lookupMock = mockLookupResult(bundle);
     final FeatureRegistry featureRegistryMock = mockRegistry(lookupMock);
+    featureRegistry = featureRegistryMock;
     FeatureRegistryProvider registryProvider = new FeatureRegistryProvider() {
       public FeatureRegistry get(String repository) {
         return featureRegistryMock;
@@ -111,14 +123,26 @@ public class ExportJsProcessorTest {
     expect(result.getFeatureResources(
         isA(GadgetContext.class), isA(List.class), EasyMock.isNull(List.class))).
         andReturn(lookupMock).anyTimes();
+    expect(result.getFeatureResources(
+        isA(GadgetContext.class), eq(LIBS), EasyMock.isNull(List.class), eq(false))).
+        andReturn(mockLookupResult(mockBundle(EXPORTS_1))).anyTimes();
+    expect(result.getFeatures(LIBS)).andReturn(LIBS).anyTimes();
+    expect(result.getFeatures(LOADED)).andReturn(LOADED).anyTimes();
     replay(result);
     return result;
   }
 
   private JsUri mockJsUri(JsCompileMode mode) {
+    return mockJsUri(mode, false);
+  }
+  
+  private JsUri mockJsUri(JsCompileMode mode, boolean isJsload) {
     JsUri result = createMock(JsUri.class);
     expect(result.getCompileMode()).andStubReturn(mode);
     expect(result.getRepository()).andStubReturn(null);
+    expect(result.isJsload()).andReturn(isJsload).anyTimes();
+    expect(result.getLibs()).andReturn(LIBS).anyTimes();
+    expect(result.getLoadedLibs()).andReturn(LOADED).anyTimes();
     replay(result);
     return result;
   }
@@ -143,6 +167,7 @@ public class ExportJsProcessorTest {
     for (String e : exports) apis.add(mockApiDirective(true, e));
     FeatureBundle result = createMock(FeatureBundle.class);
     expect(result.getApis(ApiDirective.Type.JS, true)).andReturn(exports).anyTimes();
+    expect(result.isSupportDefer()).andReturn(false).anyTimes();
     replay(result);
     return result;
   }
@@ -167,7 +192,7 @@ public class ExportJsProcessorTest {
   @Test
   public void testProcessEmpty() throws Exception {
     JsUri jsUri = mockJsUri(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL);
-    JsRequest jsRequest = new JsRequest(jsUri, null, false);
+    JsRequest jsRequest = new JsRequest(jsUri, null, false, null);
     JsResponseBuilder jsBuilder = new JsResponseBuilder();
     boolean actualReturnCode = compiler.process(jsRequest, jsBuilder);
     assertTrue(actualReturnCode);
@@ -177,7 +202,7 @@ public class ExportJsProcessorTest {
   @Test
   public void testProcessWithOneText() throws Exception {
     JsUri jsUri = mockJsUri(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL);
-    JsRequest jsRequest = new JsRequest(jsUri, null, false);
+    JsRequest jsRequest = new JsRequest(jsUri, null, false, null);
     JsResponseBuilder jsBuilder = new JsResponseBuilder();
     jsBuilder.appendJs(textJsContent1);
     boolean actualReturnCode = compiler.process(jsRequest, jsBuilder);
@@ -190,7 +215,7 @@ public class ExportJsProcessorTest {
   @Test
   public void testProcessWithOneNonEmptyFeature() throws Exception {
     JsUri jsUri = mockJsUri(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL);
-    JsRequest jsRequest = new JsRequest(jsUri, null, false);
+    JsRequest jsRequest = new JsRequest(jsUri, null, false, null);
     JsResponseBuilder jsBuilder = new JsResponseBuilder();
     jsBuilder.appendJs(featureJsContent1);
     boolean actualReturnCode = compiler.process(jsRequest, jsBuilder);
@@ -199,11 +224,35 @@ public class ExportJsProcessorTest {
         EXPORT_JS_DEB + FEATURE_CONTENT_1 + EXPORT_STRING_1,
         jsBuilder.build().toJsString());
   }
+  
+  @Test
+  public void testProcessWithOneNonEmptyFeatureDeferred() throws Exception {
+    JsUri jsUri = mockJsUri(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL, true);
+    JsRequest jsRequest = new JsRequest(jsUri, null, false, featureRegistry);
+    JsResponseBuilder jsBuilder = new JsResponseBuilder();
+    boolean actualReturnCode = compiler.getDeferredInstance().process(jsRequest, jsBuilder);
+    assertTrue(actualReturnCode);
+    assertEquals(
+        EXPORT_JS_DEB + EXPORT_STRING_1_DEFER,
+        jsBuilder.build().toJsString());
+  }
+  
+  @Test
+  public void testProcessWithOneNonEmptyFeatureNotDeferredMode() throws Exception {
+    JsUri jsUri = mockJsUri(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL, true);
+    JsRequest jsRequest = new JsRequest(jsUri, null, false, featureRegistry);
+    JsResponseBuilder jsBuilder = new JsResponseBuilder();
+    boolean actualReturnCode = compiler.process(jsRequest, jsBuilder);
+    assertTrue(actualReturnCode);
+    assertEquals(
+        "",
+        jsBuilder.build().toJsString());
+  }
 
   @Test
   public void testProcessWithOneEmptyFeature() throws Exception {
     JsUri jsUri = mockJsUri(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL);
-    JsRequest jsRequest = new JsRequest(jsUri, null, false);
+    JsRequest jsRequest = new JsRequest(jsUri, null, false, null);
     JsResponseBuilder jsBuilder = new JsResponseBuilder();
     jsBuilder.appendJs(featureJsContent3);
     boolean actualReturnCode = compiler.process(jsRequest, jsBuilder);
@@ -216,7 +265,7 @@ public class ExportJsProcessorTest {
   @Test
   public void testProcessWithFeaturesAndTexts() throws Exception {
     JsUri jsUri = mockJsUri(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL);
-    JsRequest jsRequest = new JsRequest(jsUri, null, false);
+    JsRequest jsRequest = new JsRequest(jsUri, null, false, null);
     JsResponseBuilder jsBuilder = new JsResponseBuilder();
     jsBuilder.appendJs(textJsContent1);
     jsBuilder.appendJs(featureJsContent1);

Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsRequestBuilderTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsRequestBuilderTest.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsRequestBuilderTest.java
(original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsRequestBuilderTest.java
Tue Jul 12 20:40:38 2011
@@ -21,9 +21,12 @@ package org.apache.shindig.gadgets.js;
 
 import static org.junit.Assert.*;
 
+import java.util.List;
+
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.gadgets.GadgetException;
 import org.apache.shindig.gadgets.GadgetException.Code;
+import org.apache.shindig.gadgets.features.FeatureRegistry;
 import org.apache.shindig.gadgets.uri.JsUriManager;
 import org.apache.shindig.gadgets.uri.JsUriManager.JsUri;
 import org.easymock.EasyMock;
@@ -31,6 +34,8 @@ import org.easymock.IMocksControl;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.caja.util.Lists;
+
 import javax.servlet.http.HttpServletRequest;
 
 
@@ -38,7 +43,6 @@ import javax.servlet.http.HttpServletReq
  * Tests for {@link JsRequestBuilder}.
  */
 public class JsRequestBuilderTest {
-
   private static final String HOST_HEADER_KEY = "Host";
   private static final String IMS_HEADER_KEY = "If-Modified-Since";
   private static final String HOST = "localhost";
@@ -48,6 +52,7 @@ public class JsRequestBuilderTest {
   private JsUri jsUri;
   private HttpServletRequest request;
   private JsRequestBuilder builder;
+  private FeatureRegistry registry;
 
   @Before
   public void setUp() {
@@ -55,7 +60,8 @@ public class JsRequestBuilderTest {
     jsUriManager = control.createMock(JsUriManager.class);
     jsUri = control.createMock(JsUri.class);
     request = control.createMock(HttpServletRequest.class);
-    builder = new JsRequestBuilder(jsUriManager);
+    registry = control.createMock(FeatureRegistry.class);
+    builder = new JsRequestBuilder(jsUriManager, registry);
     
     EasyMock.expect(request.getScheme()).andReturn("http");
     EasyMock.expect(request.getServerPort()).andReturn(80);
@@ -103,4 +109,40 @@ public class JsRequestBuilderTest {
     }
     control.verify();
   }
+  
+  @Test
+  public void testCreateRequestComputesDeps() throws Exception {
+    List<String> requested = Lists.newArrayList("req1", "req2");
+    List<String> loaded = Lists.newArrayList("load1", "load2");
+    List<String> fullClosure =
+        Lists.newArrayList("dep-s1", "dep1", "dep2", "dep-s2", "load1", "load2", "req1",
"req2");
+    List<String> loadedClosure =
+        Lists.newArrayList("dep-s1", "dep-s2", "load1", "load2");
+    EasyMock.expect(registry.getFeatures(requested)).andReturn(fullClosure);
+    EasyMock.expect(registry.getFeatures(loaded)).andReturn(loadedClosure);
+    EasyMock.expect(jsUri.getLibs()).andReturn(requested);
+    EasyMock.expect(jsUri.getLoadedLibs()).andReturn(loaded);
+    EasyMock.expect(jsUriManager.processExternJsUri(EasyMock.isA(Uri.class))).andReturn(jsUri);
+    EasyMock.expect(request.getHeader(IMS_HEADER_KEY)).andReturn(null);
+    EasyMock.expect(request.getHeader(HOST_HEADER_KEY)).andReturn(HOST);
+    control.replay();
+    JsRequest jsRequest = builder.build(request);
+    assertSame(jsUri, jsRequest.getJsUri());
+    assertEquals(HOST, jsRequest.getHost());
+    
+    List<String> allMatch =
+        Lists.newArrayList("dep-s1", "dep1", "dep2", "dep-s2", "load1", "load2", "req1",
"req2");
+    assertEquals(allMatch, jsRequest.getAllFeatures());
+
+    List<String> loadedMatch =
+        Lists.newArrayList("dep-s1", "dep-s2", "load1", "load2");
+    assertEquals(loadedMatch, jsRequest.getLoadedFeatures());
+    
+    List<String> newMatch =
+        Lists.newArrayList("dep1", "dep2", "req1", "req2");
+    assertEquals(newMatch, jsRequest.getNewFeatures());
+    
+    // Verify calls at the end, since they're made lazily in the context of .getFeatures()
calls.
+    control.verify();
+  }
 }

Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerServiceTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerServiceTest.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerServiceTest.java
(original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerServiceTest.java
Tue Jul 12 20:40:38 2011
@@ -87,7 +87,7 @@ public class GadgetsHandlerServiceTest e
   private final ProxyHandler proxyHandler = mock(ProxyHandler.class);
   private final CajaContentRewriter cajaContentRewriter = mock(CajaContentRewriter.class);
   private final JsServingPipeline jsPipeline = mock(JsServingPipeline.class);
-  private final JsRequestBuilder jsRequestBuilder = new JsRequestBuilder(jsUriManager);
+  private final JsRequestBuilder jsRequestBuilder = new JsRequestBuilder(jsUriManager, null);
 
   private FakeSecurityTokenCodec tokenCodec;
   private GadgetsHandlerService gadgetHandler;

Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerTest.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerTest.java
(original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetsHandlerTest.java
Tue Jul 12 20:40:38 2011
@@ -92,7 +92,7 @@ public class GadgetsHandlerTest extends 
   private final ProxyHandler proxyHandler = mock(ProxyHandler.class);
   private final CajaContentRewriter cajaContentRewriter = mock(CajaContentRewriter.class);
   private final JsServingPipeline jsPipeline = mock(JsServingPipeline.class);
-  private final JsRequestBuilder jsRequestBuilder = new JsRequestBuilder(jsUriManager);
+  private final JsRequestBuilder jsRequestBuilder = new JsRequestBuilder(jsUriManager, null);
 
   private Injector injector;
   private BeanJsonConverter converter;

Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/JsServletTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/JsServletTest.java?rev=1145758&r1=1145757&r2=1145758&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/JsServletTest.java
(original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/JsServletTest.java
Tue Jul 12 20:40:38 2011
@@ -69,7 +69,7 @@ public class JsServletTest extends Servl
     servlet.setCachingSetter(httpUtilMock);
 
     jsUriManagerMock = mock(JsUriManager.class);
-    servlet.setJsRequestBuilder(new JsRequestBuilder(jsUriManagerMock));
+    servlet.setJsRequestBuilder(new JsRequestBuilder(jsUriManagerMock, null));
 
     getJsProcessorMock = mock(GetJsContentProcessor.class);
   }



Mime
View raw message