Author: etnu
Date: Mon Jun  9 00:31:50 2008
New Revision: 664645

URL: http://svn.apache.org/viewvc?rev=664645&view=rev
Log:
Added tests for and cleaned up Gadget class. The tests are trivial; this class 
may be able to be simplified further by removing JsLibraries from it.


Added:
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java
Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpUtil.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java?rev=664645&r1=664644&r2=664645&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
 Mon Jun  9 00:31:50 2008
@@ -19,8 +19,12 @@
 
 import org.apache.shindig.gadgets.http.HttpResponse;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
-import org.apache.shindig.gadgets.spec.MessageBundle;
+import org.apache.shindig.gadgets.spec.LocaleSpec;
 import org.apache.shindig.gadgets.spec.Preload;
+import org.apache.shindig.gadgets.spec.View;
+
+import org.json.JSONArray;
+import org.json.JSONException;
 
 import java.util.Collection;
 import java.util.HashMap;
@@ -30,11 +34,6 @@
 /**
  * Intermediary representation of all state associated with processing
  * of a single gadget request.
- *
- * This class is constructed by an immutable base [EMAIL PROTECTED] 
GadgetSpec},
- * and is modified in parallel by a number of [EMAIL PROTECTED] GadgetFeature}
- * processors, in an order defined by their dependencies, in
- * [EMAIL PROTECTED] GadgetServer}.
  */
 public class Gadget {
   private final GadgetContext context;
@@ -47,11 +46,6 @@
     return spec;
   }
 
-  private final MessageBundle messageBundle;
-  public MessageBundle getMessageBundle() {
-    return messageBundle;
-  }
-
   private final Collection<JsLibrary> jsLibraries;
   public Collection<JsLibrary> getJsLibraries() {
     return jsLibraries;
@@ -63,11 +57,54 @@
     return preloads;
   }
 
+  /**
+   * Convenience function for getting the locale spec for the current context.
+   *
+   * Identical to:
+   * Locale locale = gadget.getContext().getLocale();
+   * gadget.getSpec().getModulePrefs().getLocale(locale);
+   */
+  public LocaleSpec getLocale() {
+    return spec.getModulePrefs().getLocale(context.getLocale());
+  }
+
+  /**
+   * Attempts to extract the "current" view for this gadget.
+   *
+   * @param config The container configuration; used to look for any view name
+   *        aliases for the container specified in the context.
+   */
+  public View getView(ContainerConfig config) {
+    String viewName = context.getView();
+    View view = spec.getView(viewName);
+    if (view == null) {
+      JSONArray aliases = config.getJsonArray(context.getContainer(),
+          "gadgets.features/views/" + viewName + "/aliases");
+      if (aliases != null) {
+        try {
+          for (int i = 0, j = aliases.length(); i < j; ++i) {
+            viewName = aliases.getString(i);
+            view = spec.getView(viewName);
+            if (view != null) {
+              break;
+            }
+          }
+        } catch (JSONException e) {
+          view = null;
+        }
+      }
+
+      if (view == null) {
+        view = spec.getView(GadgetSpec.DEFAULT_VIEW);
+      }
+    }
+    return view;
+  }
+
   public Gadget(GadgetContext context, GadgetSpec spec,
-      MessageBundle messageBundle, Collection<JsLibrary> jsLibraries) {
+      Collection<JsLibrary> jsLibraries) {
     this.context = context;
     this.spec = spec;
-    this.messageBundle = messageBundle;
     this.jsLibraries = jsLibraries;
   }
 }
\ No newline at end of file

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java?rev=664645&r1=664644&r2=664645&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java
 Mon Jun  9 00:31:50 2008
@@ -31,6 +31,7 @@
 import org.apache.shindig.gadgets.GadgetServer;
 import org.apache.shindig.gadgets.JsLibrary;
 import org.apache.shindig.gadgets.LockedDomainService;
+import org.apache.shindig.gadgets.MessageBundleFactory;
 import org.apache.shindig.gadgets.http.HttpResponse;
 import org.apache.shindig.gadgets.spec.Feature;
 import org.apache.shindig.gadgets.spec.LocaleSpec;
@@ -77,6 +78,7 @@
   private HttpServletRequest request;
   private HttpServletResponse response;
   private final GadgetServer server;
+  private final MessageBundleFactory messageBundleFactory;
   private final GadgetFeatureRegistry registry;
   private final ContainerConfig containerConfig;
   private final UrlGenerator urlGenerator;
@@ -142,7 +144,7 @@
    * @throws GadgetException
    */
   private void outputGadget(Gadget gadget) throws IOException, GadgetException 
{
-    View view = HttpUtil.getView(gadget, containerConfig);
+    View view = gadget.getView(containerConfig);
     if (view == null) {
         throw new GadgetException(GadgetException.Code.UNKNOWN_VIEW_SPECIFIED,
             "No appropriate view could be found for gadget: " + 
gadget.getSpec().getUrl());
@@ -280,7 +282,8 @@
     appendJsConfig(gadget, libs, inlineJs);
 
     // message bundles for prefs object.
-    MessageBundle bundle = gadget.getMessageBundle();
+    MessageBundle bundle
+        = messageBundleFactory.getBundle(gadget.getLocale(), 
gadget.getContext());
 
     String msgs = new JSONObject(bundle.getMessages()).toString();
     inlineJs.append("gadgets.Prefs.setMessages_(").append(msgs).append(");");
@@ -447,7 +450,7 @@
       // Shouldn't be possible.
       throw new RuntimeException(e);
     }
-    
+
     js.append("gadgets.config.init(").append(json.toString()).append(");\n");
   }
 
@@ -544,13 +547,14 @@
 
   @Inject
   public GadgetRenderingTask(GadgetServer server,
+                             MessageBundleFactory messageBundleFactory,
                              GadgetFeatureRegistry registry,
                              ContainerConfig containerConfig,
                              UrlGenerator urlGenerator,
                              SecurityTokenDecoder tokenDecoder,
                              LockedDomainService lockedDomainService) {
-
     this.server = server;
+    this.messageBundleFactory = messageBundleFactory;
     this.registry = registry;
     this.containerConfig = containerConfig;
     this.urlGenerator = urlGenerator;

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpUtil.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpUtil.java?rev=664645&r1=664644&r2=664645&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpUtil.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpUtil.java
 Mon Jun  9 00:31:50 2008
@@ -19,12 +19,8 @@
 package org.apache.shindig.gadgets.servlet;
 
 import org.apache.shindig.gadgets.ContainerConfig;
-import org.apache.shindig.gadgets.Gadget;
 import org.apache.shindig.gadgets.GadgetContext;
-import org.apache.shindig.gadgets.spec.GadgetSpec;
-import org.apache.shindig.gadgets.spec.View;
 
-import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -94,41 +90,4 @@
     }
     return new JSONObject();
   }
-
-  /**
-   * Fetches the most appropriate view for the given gadget and container
-   * configuration.
-   *
-   * @param gadget
-   * @param config
-   * @return The most appropriate view for this request.
-   */
-  public static View getView(Gadget gadget, ContainerConfig config) {
-    GadgetContext context = gadget.getContext();
-    String viewName = context.getView();
-    GadgetSpec spec = gadget.getSpec();
-    View view = spec.getView(viewName);
-    if (view == null) {
-      JSONArray aliases = config.getJsonArray(context.getContainer(),
-          "gadgets.features/views/" + viewName + "/aliases");
-      if (aliases != null) {
-        try {
-          for (int i = 0, j = aliases.length(); i < j; ++i) {
-            viewName = aliases.getString(i);
-            view = spec.getView(viewName);
-            if (view != null) {
-              break;
-            }
-          }
-        } catch (JSONException e) {
-          view = null;
-        }
-      }
-
-      if (view == null) {
-        view = gadget.getSpec().getView(GadgetSpec.DEFAULT_VIEW);
-      }
-    }
-    return view;
-  }
 }

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java?rev=664645&r1=664644&r2=664645&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java
 Mon Jun  9 00:31:50 2008
@@ -98,7 +98,7 @@
     GadgetSpec spec = gadget.getSpec();
     try {
       String url = context.getUrl().toString();
-      View view = HttpUtil.getView(gadget, containerConfig);
+      View view = gadget.getView(containerConfig);
       View.ContentType type;
       if (view == null) {
         type = View.ContentType.HTML;

Added: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java?rev=664645&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java
 Mon Jun  9 00:31:50 2008
@@ -0,0 +1,189 @@
+/*
+ * 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;
+
+import static org.easymock.EasyMock.expect;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.junit.Assert.assertEquals;
+
+import org.apache.shindig.gadgets.http.HttpResponse;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.apache.shindig.gadgets.spec.LocaleSpec;
+import org.apache.shindig.gadgets.spec.Preload;
+import org.apache.shindig.gadgets.spec.View;
+
+import org.easymock.classextension.EasyMock;
+import org.json.JSONArray;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.net.URI;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Tests for Gadget
+ */
+public class GadgetTest {
+  private final static String SPEC_URL = "http://example.org/gadget.xml";;
+  private final static String SPEC_XML
+      = "<Module>" +
+        "<ModulePrefs title='title'>" +
+        "  <Preload href='http://example.org/foo'/>" +
+        "  <Locale>" +
+        "    <msg name='name'>VALUE</msg>" +
+        "  </Locale>" +
+        "</ModulePrefs>" +
+        "<Content type='html'>DEFAULT VIEW</Content>" +
+        "<Content view='one' type='html'>VIEW ONE</Content>" +
+        "<Content view='two' type='html'>VIEW TWO</Content>" +
+        "</Module>";
+
+  private final ContainerConfig config
+      = EasyMock.createNiceMock(ContainerConfig.class);
+  private final DummyContext context = new DummyContext();
+
+  private GadgetSpec spec;
+  private Gadget gadget;
+  private List<JsLibrary> libraries;
+
+  @Before
+  public void setUp() throws Exception {
+    JsLibrary lib
+        = JsLibrary.create(JsLibrary.Type.INLINE, "var foo='bar';", "core", 
null);
+
+    spec = new GadgetSpec(URI.create(SPEC_URL), SPEC_XML);
+    libraries = Arrays.asList(lib);
+    gadget = new Gadget(context, spec, libraries);
+  }
+
+  @Test
+  public void getView() {
+    context.view = "two";
+
+    replay(config);
+    View view = gadget.getView(config);
+
+    assertEquals("VIEW TWO", view.getContent());
+  }
+
+  @Test
+  public void getDefaultView() {
+    context.view = "unknown";
+
+    replay(config);
+    View view = gadget.getView(config);
+
+    assertEquals("DEFAULT VIEW", view.getContent());
+  }
+
+  @Test
+  public void getAliasedView() {
+    context.view = "unknown";
+    context.container = "foo";
+
+    String aliasStr = "gadgets.features/views/unknown/aliases";
+    JSONArray aliases = new JSONArray().put("blah").put("one");
+
+    expect(config.getJsonArray(context.container, 
aliasStr)).andReturn(aliases);
+    replay(config);
+
+    View view = gadget.getView(config);
+
+    assertEquals("VIEW ONE", view.getContent());
+  }
+
+  @Test
+  public void getLocale() {
+    LocaleSpec localeSpec = gadget.getLocale();
+    assertEquals("VALUE", 
localeSpec.getMessageBundle().getMessages().get("name"));
+  }
+
+  @Test
+  public void contextIsAPassThrough() {
+    assertEquals(context, gadget.getContext());
+  }
+
+  @Test
+  public void getJsLibrariesNotAltered() {
+    assertEquals(libraries, gadget.getJsLibraries());
+  }
+
+  @Test
+  public void getSpecNotAltered() {
+    assertEquals(spec.toString(), gadget.getSpec().toString());
+  }
+
+  @Test
+  public void preloadMapIsJustADummyMap() throws Exception {
+    HttpResponse response = HttpResponse.error();
+
+    Preload preload = spec.getModulePrefs().getPreloads().get(0);
+
+    gadget.getPreloadMap().put(preload, new 
DummyFuture<HttpResponse>(response));
+
+    assertEquals(response, gadget.getPreloadMap().get(preload).get());
+  }
+
+  private static class DummyContext extends GadgetContext {
+    public String view = super.getView();
+    public String container = super.getContainer();
+
+    @Override
+    public String getView() {
+      return view;
+    }
+
+    @Override
+    public String getContainer() {
+      return container;
+    }
+  }
+
+  private static class DummyFuture<T> implements Future<T> {
+
+    private final T value;
+
+    public T get() {
+      return value;
+    }
+
+    public T get(long timeout, TimeUnit unit) {
+      return value;
+    }
+
+    public boolean isDone() {
+      return true;
+    }
+
+    public boolean isCancelled() {
+      return false;
+    }
+
+    public boolean cancel(boolean ignore) {
+      return false;
+    }
+
+    public DummyFuture(T value) {
+      this.value = value;
+    }
+  }
+}


Reply via email to