Author: etnu
Date: Sun Jun 8 20:16:56 2008
New Revision: 664609
URL: http://svn.apache.org/viewvc?rev=664609&view=rev
Log:
- Added variable substitution for Preload, as required by 0.8.
- Now treating unknown auth types as "NONE" instead of rejecting them, as
required by 0.7 and 0.8.
- Added test cases for Preload (previously there weren't any).
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/PreloadTest.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Auth.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Preload.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Auth.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Auth.java?rev=664609&r1=664608&r2=664609&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Auth.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Auth.java
Sun Jun 8 20:16:56 2008
@@ -17,7 +17,6 @@
*/
package org.apache.shindig.gadgets.spec;
-import org.apache.shindig.gadgets.GadgetException;
/**
* The supported auth modes for Preload
@@ -29,15 +28,14 @@
* @param value
* @return The parsed value (defaults to NONE)
*/
- public static Auth parse(String value) throws GadgetException {
+ public static Auth parse(String value) {
if (value != null) {
value = value.trim();
if (value.length() == 0) return Auth.NONE;
try {
return Auth.valueOf(value.toUpperCase());
} catch (IllegalArgumentException iae) {
- throw new GadgetException(GadgetException.Code.UNSUPPORTED_FEATURE,
- Preload.AUTHZ_ATTR + "=\"" + value + "\" is not supported");
+ return Auth.NONE;
}
} else {
return Auth.NONE;
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java?rev=664609&r1=664608&r2=664609&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java
Sun Jun 8 20:16:56 2008
@@ -496,8 +496,11 @@
features = prefs.getFeatures();
locales = prefs.getLocales();
- // TODO: Preload should have substitutions performed as well.
- preloads = prefs.getPreloads();
+ List<Preload> preloads = new ArrayList<Preload>(prefs.preloads.size());
+ for (Preload preload : prefs.preloads) {
+ preloads.add(preload.substitute(substituter));
+ }
+ this.preloads = Collections.unmodifiableList(preloads);
List<Icon> icons = new ArrayList<Icon>(prefs.icons.size());
for (Icon icon : prefs.icons) {
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Preload.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Preload.java?rev=664609&r1=664608&r2=664609&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Preload.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/Preload.java
Sun Jun 8 20:16:56 2008
@@ -18,10 +18,13 @@
package org.apache.shindig.gadgets.spec;
import org.apache.shindig.common.xml.XmlUtil;
-import org.apache.shindig.gadgets.GadgetException;
+import org.apache.shindig.gadgets.Substitutions;
+
+import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Element;
import java.net.URI;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@@ -68,19 +71,26 @@
/**
* [EMAIL PROTECTED]
*/
- private final Set<String> views = new HashSet<String>();
+ private final Set<String> views;
public Set<String> getViews() {
return views;
}
+ public Preload substitute(Substitutions substituter) {
+ return new Preload(this, substituter);
+ }
+
/**
* Produces an xml representation of the Preload.
*/
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
- buf.append("<Preload href=\"").append(href).append("\" authz=\"")
- .append(auth.toString().toLowerCase()).append("\"/>");
+ buf.append("<Preload href='").append(href).append("'")
+ .append(" authz='").append(auth.toString().toLowerCase()).append("'")
+ .append(" sign_owner='").append(signOwner).append("'")
+ .append(" sign_viewer='").append(signViewer).append("'")
+ .append(" views='").append(StringUtils.join(views, ',')).append("'/>");
return buf.toString();
}
/**
@@ -94,23 +104,28 @@
signViewer = XmlUtil.getBoolAttribute(preload, "sign_viewer", true);
href = XmlUtil.getUriAttribute(preload, "href");
if (href == null) {
- throw new SpecParserException("[EMAIL PROTECTED] is required.");
+ throw new SpecParserException("Preload/@href is missing or invalid.");
}
// Record all the associated views
String viewNames = XmlUtil.getAttribute(preload, "views", "");
+ Set<String> views = new HashSet<String>();
for (String s: viewNames.split(",")) {
s = s.trim();
if (s.length() > 0) {
views.add(s.trim());
}
}
+ this.views = Collections.unmodifiableSet(views);
- String authAttr = XmlUtil.getAttribute(preload, AUTHZ_ATTR);
- try {
- auth = Auth.parse(authAttr);
- } catch (GadgetException ge) {
- throw new SpecParserException("Preload@" + ge.getMessage());
- }
+ auth = Auth.parse(XmlUtil.getAttribute(preload, AUTHZ_ATTR));
+ }
+
+ private Preload(Preload preload, Substitutions substituter) {
+ signOwner = preload.signOwner;
+ signViewer = preload.signViewer;
+ views = preload.views;
+ auth = preload.auth;
+ href = substituter.substituteUri(null, preload.href);
}
}
\ No newline at end of file
Modified:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java?rev=664609&r1=664608&r2=664609&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java
Sun Jun 8 20:16:56 2008
@@ -181,7 +181,7 @@
entry.optString("Summary", null));
}
- public void testParameters() throws Exception {
+ public void testFetchFeedParameters() throws Exception {
String entryTitle = "Feed title";
String entryLink = "http://example.org/entry/0/1";
String entrySummary = "This is the summary";
@@ -337,7 +337,7 @@
JSONObject json = readJSONResponse(baos.toString());
assertFalse(json.getJSONObject(URL_ONE).has("st"));
}
-
+
public void testChangeSecurityToken() throws Exception {
// Doesn't actually sign since it returns the standard fetcher.
// Signing tests are in SigningFetcherTest
@@ -360,19 +360,21 @@
assertEquals("updated", json.getJSONObject(URL_ONE).getString("st"));
}
- public void testInvalidSigningType() throws Exception {
- setupGetRequestMock(URL_ONE);
- expect(request.getParameter(ProxyHandler.SECURITY_TOKEN_PARAM))
- .andReturn("fake-token").atLeastOnce();
+ public void testInvalidSigningTypeTreatedAsNone() throws Exception {
+ setupGenericRequestMock("GET", URL_ONE);
+ expectGetAndReturnData(URL_ONE, DATA_ONE.getBytes());
expect(request.getParameter(Preload.AUTHZ_ATTR))
.andReturn("garbage").atLeastOnce();
replay();
- try {
- proxyHandler.fetchJson(request, response);
- fail("proxyHandler accepted invalid authz type");
- } catch (GadgetException e) {
- assertEquals(GadgetException.Code.UNSUPPORTED_FEATURE, e.getCode());
- }
+
+ proxyHandler.fetchJson(request, response);
+ verify();
+ writer.close();
+
+ JSONObject json = readJSONResponse(baos.toString());
+ JSONObject info = json.getJSONObject(URL_ONE);
+ assertEquals(200, info.getInt("rc"));
+ assertEquals(DATA_ONE, info.get("body"));
}
public void testValidateUrlNoPath() throws Exception {
Modified:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java?rev=664609&r1=664608&r2=664609&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java
Sun Jun 8 20:16:56 2008
@@ -154,27 +154,30 @@
@Test
public void doSubstitution() throws Exception {
String xml = "<ModulePrefs title='__MSG_title__'>" +
- " <Preload href='http://example.org' authz='signed'/>" +
" <Icon>__MSG_icon__</Icon>" +
- " <Link rel='__MSG_rel__' href='__MSG_href__'/>" +
+ " <Link rel='__MSG_rel__' href='__MSG_link_href__'/>" +
+ " <Preload href='__MSG_pre_href__'/>" +
"</ModulePrefs>";
String title = "blah";
String icon = "http://example.org/icon.gif";
String rel = "foo-bar";
- String href = "http://example.org/link.html";
+ String linkHref = "http://example.org/link.html";
+ String preHref = "http://example.org/preload.html";
ModulePrefs prefs = new ModulePrefs(XmlUtil.parse(xml), SPEC_URL);
- Substitutions substitutions = new Substitutions();
- substitutions.addSubstitution(Substitutions.Type.MESSAGE, "title", title);
- substitutions.addSubstitution(Substitutions.Type.MESSAGE, "icon", icon);
- substitutions.addSubstitution(Substitutions.Type.MESSAGE, "rel", rel);
- substitutions.addSubstitution(Substitutions.Type.MESSAGE, "href", href);
- prefs = prefs.substitute(substitutions);
+ Substitutions subst = new Substitutions();
+ subst.addSubstitution(Substitutions.Type.MESSAGE, "title", title);
+ subst.addSubstitution(Substitutions.Type.MESSAGE, "icon", icon);
+ subst.addSubstitution(Substitutions.Type.MESSAGE, "rel", rel);
+ subst.addSubstitution(Substitutions.Type.MESSAGE, "link_href", linkHref);
+ subst.addSubstitution(Substitutions.Type.MESSAGE, "pre_href", preHref);
+ prefs = prefs.substitute(subst);
assertEquals(title, prefs.getTitle());
assertEquals(icon, prefs.getIcons().get(0).getContent());
assertEquals(rel, prefs.getLinks().get(rel).getRel());
- assertEquals(href, prefs.getLinks().get(rel).getHref().toString());
+ assertEquals(linkHref, prefs.getLinks().get(rel).getHref().toString());
+ assertEquals(preHref, prefs.getPreloads().get(0).getHref().toString());
}
@Test(expected = SpecParserException.class)
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/PreloadTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/PreloadTest.java?rev=664609&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/PreloadTest.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/PreloadTest.java
Sun Jun 8 20:16:56 2008
@@ -0,0 +1,147 @@
+/*
+ * 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.spec;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.shindig.common.xml.XmlUtil;
+import org.apache.shindig.gadgets.Substitutions;
+
+import org.apache.commons.lang.StringUtils;
+import org.junit.Test;
+
+/**
+ * Tests for Preload
+ */
+public class PreloadTest {
+ private final static String HREF = "http://example.org/preload.xml";
+ private final static String[] VIEWS = new String[] {"view0", "view1"};
+
+ @Test
+ public void basicPreload() throws Exception {
+ String xml = "<Preload href='" + HREF + "'/>";
+
+ Preload preload = new Preload(XmlUtil.parse(xml));
+
+ assertEquals(HREF, preload.getHref().toString());
+ assertEquals(Auth.NONE, preload.getAuth());
+ assertTrue("Default value for sign_owner should be true.",
+ preload.isSignOwner());
+ assertTrue("Default value for sign_viewer should be true.",
+ preload.isSignViewer());
+ }
+
+ @Test
+ public void authzSigned() throws Exception {
+ String xml = "<Preload href='" + HREF + "' authz='signed'/>";
+
+ Preload preload = new Preload(XmlUtil.parse(xml));
+
+ assertEquals(Auth.SIGNED, preload.getAuth());
+ }
+
+ @Test
+ public void authzAuthenticated() throws Exception {
+ String xml = "<Preload href='" + HREF + "' authz='authenticated'/>";
+
+ Preload preload = new Preload(XmlUtil.parse(xml));
+
+ assertEquals(Auth.AUTHENTICATED, preload.getAuth());
+ }
+
+ @Test
+ public void authzUnknownTreatedAsNone() throws Exception {
+ String xml = "<Preload href='foo' authz='bad-bad-bad value!'/>";
+
+ Preload preload = new Preload(XmlUtil.parse(xml));
+
+ assertEquals(Auth.NONE, preload.getAuth());
+ }
+
+ @Test
+ public void multipleViews() throws Exception {
+ String xml = "<Preload href='" + HREF + "'" +
+ " views='" + StringUtils.join(VIEWS, ',') + "'/>";
+
+ Preload preload = new Preload(XmlUtil.parse(xml));
+
+ assertArrayEquals(VIEWS, preload.getViews().toArray());
+ }
+
+ @Test
+ public void signOwner() throws Exception {
+ String xml = "<Preload href='" + HREF + "' sign_owner='false'/>";
+
+ Preload preload = new Preload(XmlUtil.parse(xml));
+
+ assertFalse("sign_owner parsing is incorrect.", preload.isSignOwner());
+ }
+
+ @Test
+ public void signViewer() throws Exception {
+ String xml = "<Preload href='" + HREF + "' sign_viewer='false'/>";
+
+ Preload preload = new Preload(XmlUtil.parse(xml));
+
+ assertFalse("sign_viewer parsing is incorrect.", preload.isSignViewer());
+ }
+
+ @Test
+ public void substitutionsOk() throws Exception {
+ String xml = "<Preload href='__MSG_preload__'/>";
+
+ Preload preload = new Preload(XmlUtil.parse(xml));
+ Substitutions substituter = new Substitutions();
+ substituter.addSubstitution(Substitutions.Type.MESSAGE, "preload", HREF);
+ Preload substituted = preload.substitute(substituter);
+
+ assertEquals(HREF, substituted.getHref().toString());
+ }
+
+ @Test
+ public void toStringIsSane() throws Exception {
+ String xml = "<Preload" +
+ " href='" + HREF + "'" +
+ " sign_owner='false'" +
+ " views='" + StringUtils.join(VIEWS, ',') + "'/>";
+
+ Preload preload = new Preload(XmlUtil.parse(xml));
+ Preload preload2 = new Preload(XmlUtil.parse(preload.toString()));
+
+ assertTrue(preload2.isSignViewer());
+ assertFalse(preload2.isSignOwner());
+ assertArrayEquals(VIEWS, preload2.getViews().toArray());
+ assertEquals(HREF, preload2.getHref().toString());
+ }
+
+ @Test(expected = SpecParserException.class)
+ public void missingHrefThrows() throws Exception {
+ String xml = "<Preload/>";
+ new Preload(XmlUtil.parse(xml));
+ }
+
+ @Test(expected = SpecParserException.class)
+ public void malformedHrefThrows() throws Exception {
+ String xml = "<Preload href='@[EMAIL PROTECTED]'/>";
+ new Preload(XmlUtil.parse(xml));
+ }
+}