Author: beaton
Date: Sat Jun 14 14:58:16 2008
New Revision: 667888
URL: http://svn.apache.org/viewvc?rev=667888&view=rev
Log:
Parse <OAuth> elements in gadget spec.
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthService.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthSpec.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/AuthTest.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthServiceTest.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthSpecTest.java
Modified:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/xml/XmlUtil.java
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/xml/XmlUtilTest.java
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/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java
Modified:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/xml/XmlUtil.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/xml/XmlUtil.java?rev=667888&r1=667887&r2=667888&view=diff
==============================================================================
---
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/xml/XmlUtil.java
(original)
+++
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/xml/XmlUtil.java
Sat Jun 14 14:58:16 2008
@@ -91,6 +91,38 @@
public static URI getUriAttribute(Node node, String attr) {
return getUriAttribute(node, attr, null);
}
+
+ /**
+ * Retrieves an attribute as a URI, and verifies that the URI is an http
+ * or https URI.
+ * @param node
+ * @param attr
+ * @param def
+ * @return the parsed uri, or def if the attribute is not a valid http or
+ * https URI.
+ */
+ public static URI getHttpUriAttribute(Node node, String attr, URI def) {
+ URI uri = getUriAttribute(node, attr, def);
+ if (uri == null) {
+ return def;
+ }
+ if (!"http".equals(uri.getScheme()) && !"https".equals(uri.getScheme())) {
+ return def;
+ }
+ return uri;
+ }
+
+ /**
+ * Retrieves an attribute as a URI, and verifies that the URI is an http
+ * or https URI.
+ * @param node
+ * @param attr
+ * @return the parsed uri, or null if the attribute is not a valid http or
+ * https URI.
+ */
+ public static URI getHttpUriAttribute(Node node, String attr) {
+ return getHttpUriAttribute(node, attr, null);
+ }
/**
* Retrieves an attribute as a boolean.
Modified:
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/xml/XmlUtilTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/xml/XmlUtilTest.java?rev=667888&r1=667887&r2=667888&view=diff
==============================================================================
---
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/xml/XmlUtilTest.java
(original)
+++
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/xml/XmlUtilTest.java
Sat Jun 14 14:58:16 2008
@@ -43,6 +43,10 @@
private final static URI URI_VALUE = URI.create("http://example.org/file");
private final static String URI_MALFORMED_ATTR = "uri-malformed";
private final static String FAKE_ATTR = "fake";
+ private final static String HTTPS_URI_ATTR = "httpsuri";
+ private final static URI HTTPS_URI_VALUE = URI.create("https://example.org");
+ private final static String FTP_URI_ATTR = "ftpuri";
+ private final static URI FTP_URI_VALUE = URI.create("ftp://ftp.example.org");
private final static String XML
= "<Element " +
@@ -51,7 +55,9 @@
BOOL_TRUE_ATTR + "='true' " +
BOOL_FALSE_ATTR + "='false' " +
URI_ATTR + "='" + URI_VALUE + "' " +
- URI_MALFORMED_ATTR + "='$#%$^$^$^$%$%!! '" +
+ URI_MALFORMED_ATTR + "='$#%$^$^$^$%$%!! ' " +
+ HTTPS_URI_ATTR + "='" + HTTPS_URI_VALUE + "' " +
+ FTP_URI_ATTR + "='" + FTP_URI_VALUE + "' " +
"/>";
private Element node;
@@ -95,6 +101,14 @@
assertEquals(URI_VALUE, XmlUtil.getUriAttribute(node, URI_MALFORMED_ATTR,
URI_VALUE));
assertNull("getUriAttribute must return null for undefined attributes.",
XmlUtil.getUriAttribute(node, FAKE_ATTR));
+ assertEquals(FTP_URI_VALUE, XmlUtil.getUriAttribute(node, FTP_URI_ATTR));
+ }
+
+ @Test
+ public void testHttpUriAttribute() {
+ assertEquals(HTTPS_URI_VALUE, XmlUtil.getHttpUriAttribute(node,
HTTPS_URI_ATTR));
+ assertNull(XmlUtil.getHttpUriAttribute(node, FTP_URI_ATTR));
+ assertEquals(HTTPS_URI_VALUE, XmlUtil.getHttpUriAttribute(node,
FTP_URI_ATTR, HTTPS_URI_VALUE));
}
@Test(expected=XmlException.class)
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=667888&r1=667887&r2=667888&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
Sat Jun 14 14:58:16 2008
@@ -22,7 +22,8 @@
* The supported auth modes for Preload
*/
public enum Auth {
- NONE, SIGNED, AUTHENTICATED;
+ // TODO(beaton) remove AUTHENTICATED once we've renamed everything to OAuth
+ NONE, SIGNED, AUTHENTICATED, OAUTH;
/**
* @param value
@@ -31,7 +32,9 @@
public static Auth parse(String value) {
if (value != null) {
value = value.trim();
- if (value.length() == 0) return Auth.NONE;
+ if (value.length() == 0) {
+ return Auth.NONE;
+ }
try {
return Auth.valueOf(value.toUpperCase());
} catch (IllegalArgumentException iae) {
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=667888&r1=667887&r2=667888&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
Sat Jun 14 14:58:16 2008
@@ -359,6 +359,14 @@
public Map<String, LinkSpec> getLinks() {
return links;
}
+
+ /**
+ * ModuleSpec/OAuthSpec
+ */
+ private final OAuthSpec oauth;
+ public OAuthSpec getOAuthSpec() {
+ return oauth;
+ }
/**
* Attempts to retrieve a valid LocaleSpec for the given Locale.
@@ -440,6 +448,9 @@
for (LinkSpec link : links.values()) {
buf.append(link).append('\n');
}
+ if (oauth != null) {
+ buf.append(oauth).append('\n');
+ }
buf.append("</ModulePrefs>");
return buf.toString();
}
@@ -466,6 +477,7 @@
// Child elements
PreloadVisitor preloadVisitor = new PreloadVisitor();
FeatureVisitor featureVisitor = new FeatureVisitor();
+ OAuthVisitor oauthVisitor = new OAuthVisitor();
IconVisitor iconVisitor = new IconVisitor();
LocaleVisitor localeVisitor = new LocaleVisitor(specUrl);
LinkVisitor linkVisitor = new LinkVisitor();
@@ -475,6 +487,7 @@
visitors.put("Preload", preloadVisitor);
visitors.put("Optional", featureVisitor);
visitors.put("Require", featureVisitor);
+ visitors.put("OAuth", oauthVisitor);
visitors.put("Icon", iconVisitor);
visitors.put("Locale", localeVisitor);
visitors.put("Link", linkVisitor);
@@ -486,6 +499,7 @@
icons = Collections.unmodifiableList(iconVisitor.icons);
locales = Collections.unmodifiableMap(localeVisitor.locales);
links = Collections.unmodifiableMap(linkVisitor.links);
+ oauth = oauthVisitor.oauth;
}
/**
@@ -495,6 +509,7 @@
categories = prefs.getCategories();
features = prefs.getFeatures();
locales = prefs.getLocales();
+ oauth = prefs.oauth;
List<Preload> preloads = new ArrayList<Preload>(prefs.preloads.size());
for (Preload preload : prefs.preloads) {
@@ -541,6 +556,22 @@
}
/**
+ * Process ModulePrefs/OAuth
+ */
+class OAuthVisitor implements ElementVisitor {
+ OAuthSpec oauth;
+ public void visit(Element element) throws SpecParserException {
+ if (oauth != null) {
+ throw new SpecParserException("ModulePrefs/OAuth may only occur once.");
+ }
+ oauth = new OAuthSpec(element);
+ }
+ public OAuthVisitor() {
+ this.oauth = null;
+ }
+}
+
+/**
* Processes ModulePrefs/Require and ModulePrefs/Optional
*/
class FeatureVisitor implements ElementVisitor {
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthService.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthService.java?rev=667888&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthService.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthService.java
Sat Jun 14 14:58:16 2008
@@ -0,0 +1,168 @@
+/*
+ * 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 java.net.URI;
+
+import org.apache.shindig.common.xml.XmlUtil;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * Information about an OAuth service that a gadget wants to use.
+ *
+ * Instances are immutable.
+ */
+public class OAuthService {
+ private EndPoint requestUrl;
+ private EndPoint accessUrl;
+ private URI authorizationUrl;
+ private String name;
+
+ /**
+ * Represents /OAuth/Service/Request elements.
+ */
+ public EndPoint getRequestUrl() {
+ return requestUrl;
+ }
+
+ /**
+ * Represents /OAuth/Service/Access elements.
+ */
+ public EndPoint getAccessUrl() {
+ return accessUrl;
+ }
+ /**
+ * Represents /OAuth/Service/Authorization elements.
+ */
+ public URI getAuthorizationUrl() {
+ return authorizationUrl;
+ }
+
+ /**
+ * Represents /OAuth/[EMAIL PROTECTED]
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Method to use for requests to an OAuth request token or access token URL.
+ */
+ public enum Method {
+ GET, POST;
+ }
+
+ /**
+ * Location for OAuth parameters in requests to an OAuth request token,
+ * access token, or resource URL. (Lowercase to match gadget spec schema)
+ */
+ public enum Location {
+ header, url, body;
+ }
+
+ private static final String URL_ATTR = "url";
+ private static final String PARAM_LOCATION_ATTR = "param_location";
+ private static final String METHOD_ATTR = "method";
+
+ /**
+ * Description of an OAuth request token or access token URL.
+ */
+ public static class EndPoint {
+ public final URI url;
+ public final Method method;
+ public final Location location;
+
+ public EndPoint(URI url, Method method, Location location) {
+ this.url = url;
+ this.method = method;
+ this.location = location;
+ }
+
+ public String toString(String element) {
+ return "<" + element + " url='" + url.toString() + "' " +
+ "method='" + method + "' param_location='" + location + "'/>";
+ }
+ }
+
+ public OAuthService(Element serviceElement) throws SpecParserException {
+ name = serviceElement.getAttribute("name");
+ NodeList children = serviceElement.getChildNodes();
+ for (int i=0; i < children.getLength(); ++i) {
+ Node child = children.item(i);
+ if (child.getNodeType() != Element.ELEMENT_NODE) {
+ continue;
+ }
+ String childName = child.getNodeName();
+ if (childName.equals("Request")) {
+ if (requestUrl != null) {
+ throw new SpecParserException("Multiple OAuth/Service/Request
elements");
+ }
+ requestUrl = parseEndPoint("OAuth/Service/Request", (Element)child);
+ } else if (childName.equals("Authorization")) {
+ if (authorizationUrl != null) {
+ throw new SpecParserException("Multiple OAuth/Service/Authorization
elements");
+ }
+ authorizationUrl = parseAuthorizationUrl((Element)child);
+ } else if (childName.equals("Access")) {
+ if (accessUrl != null) {
+ throw new SpecParserException("Multiple OAuth/Service/Access
elements");
+ }
+ accessUrl = parseEndPoint("OAuth/Service/Access", (Element)child);
+ }
+ }
+ }
+
+ /**
+ * Constructor for testing only.
+ */
+ OAuthService() {
+ }
+
+ URI parseAuthorizationUrl(Element child) throws SpecParserException {
+ URI url = XmlUtil.getHttpUriAttribute(child, URL_ATTR);
+ if (url == null) {
+ throw new SpecParserException("OAuth/Service/Authorization @url is not
valid: " +
+ child.getAttribute(URL_ATTR));
+ }
+ return url;
+ }
+
+
+ EndPoint parseEndPoint(String where, Element child) throws
SpecParserException {
+ URI url = XmlUtil.getHttpUriAttribute(child, URL_ATTR);
+ if (url == null) {
+ throw new SpecParserException("Not an HTTP url: " +
child.getAttribute(URL_ATTR));
+ }
+
+ Location location = Location.header;
+ String locationString = child.getAttribute(PARAM_LOCATION_ATTR);
+ if (!"".equals(locationString)) {
+ location = Location.valueOf(locationString);
+ }
+
+ Method method = Method.POST;
+ String methodString = child.getAttribute(METHOD_ATTR);
+ if (!"".equals(methodString)) {
+ method = Method.valueOf(methodString);
+ }
+ return new EndPoint(url, method, location);
+ }
+}
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthSpec.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthSpec.java?rev=667888&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthSpec.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/OAuthSpec.java
Sat Jun 14 14:58:16 2008
@@ -0,0 +1,77 @@
+/*
+ * 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 java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * Wraps an <OAuth> element from the gadget spec.
+ *
+ * Instances are immutable.
+ */
+public class OAuthSpec {
+
+ /** Keys are service names, values are service descriptors */
+ private final Map<String, OAuthService> serviceMap;
+
+ public OAuthSpec(Element element) throws SpecParserException {
+ serviceMap = new HashMap<String, OAuthService>();
+ NodeList services = element.getElementsByTagName("Service");
+ for (int i=0; i < services.getLength(); ++i) {
+ Node node = services.item(i);
+ if (node.getNodeType() == Element.ELEMENT_NODE) {
+ parseService((Element)node);
+ }
+ }
+ }
+
+ private void parseService(Element serviceElement) throws SpecParserException
{
+ OAuthService service = new OAuthService(serviceElement);
+ serviceMap.put(service.getName(), service);
+ }
+
+ public Map<String, OAuthService> getServices() {
+ return Collections.unmodifiableMap(serviceMap);
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("<OAuth>");
+ for (String name : serviceMap.keySet()) {
+ sb.append("<Service name='");
+ sb.append(name);
+ sb.append("'>");
+ OAuthService service = serviceMap.get(name);
+ sb.append(service.getRequestUrl().toString("Request"));
+ sb.append(service.getAccessUrl().toString("Access"));
+ sb.append("<Authorization url='" +
+ service.getAuthorizationUrl().toString() + "'/>");
+ sb.append("</Service>");
+ }
+ sb.append("</OAuth>");
+ return sb.toString();
+ }
+
+}
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/AuthTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/AuthTest.java?rev=667888&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/AuthTest.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/AuthTest.java
Sat Jun 14 14:58:16 2008
@@ -0,0 +1,41 @@
+/*
+ * 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.assertEquals;
+
+import org.junit.Test;
+
+public class AuthTest {
+
+ @Test
+ public void testAuth() {
+ assertEquals(Auth.AUTHENTICATED, Auth.parse("Authenticated"));
+ assertEquals(Auth.OAUTH, Auth.parse("oauth"));
+ assertEquals(Auth.OAUTH, Auth.parse(" oauth "));
+ assertEquals(Auth.SIGNED, Auth.parse("SiGnEd"));
+ assertEquals(Auth.NONE, Auth.parse("NONE"));
+ assertEquals(Auth.NONE, Auth.parse(""));
+ assertEquals(Auth.NONE, Auth.parse(null));
+ assertEquals(Auth.NONE, Auth.parse("foo"));
+ assertEquals("none", Auth.NONE.toString());
+ assertEquals("oauth", Auth.OAUTH.toString());
+ }
+}
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=667888&r1=667887&r2=667888&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
Sat Jun 14 14:58:16 2008
@@ -64,6 +64,15 @@
" <Icon/>" +
" <Locale/>" +
" <Link rel='link' href='http://example.org/link'/>" +
+ " <OAuth>" +
+ " <Service name='serviceOne'>" +
+ " <Request url='http://www.example.com/request'" +
+ " method='GET' param_location='header' />" +
+ " <Authorization url='http://www.example.com/authorize'/>" +
+ " <Access url='http://www.example.com/access' method='GET'" +
+ " param_location='header' />" +
+ " </Service>" +
+ " </OAuth>" +
"</ModulePrefs>";
private void doAsserts(ModulePrefs prefs) {
@@ -102,6 +111,13 @@
assertEquals(URI.create("http://example.org/link"),
prefs.getLinks().get("link").getHref());
+
+ OAuthService oauth = prefs.getOAuthSpec().getServices().get("serviceOne");
+ assertEquals(URI.create("http://www.example.com/request"),
oauth.getRequestUrl().url);
+ assertEquals(OAuthService.Method.GET, oauth.getRequestUrl().method);
+ assertEquals(OAuthService.Method.GET, oauth.getAccessUrl().method);
+ assertEquals(OAuthService.Location.header, oauth.getAccessUrl().location);
+ assertEquals(URI.create("http://www.example.com/authorize"),
oauth.getAuthorizationUrl());
}
@Test
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthServiceTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthServiceTest.java?rev=667888&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthServiceTest.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthServiceTest.java
Sat Jun 14 14:58:16 2008
@@ -0,0 +1,167 @@
+/*
+ * 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.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.net.URI;
+
+import org.apache.shindig.common.xml.XmlUtil;
+import org.junit.Before;
+import org.junit.Test;
+
+public class OAuthServiceTest {
+
+ private OAuthService service;
+
+ @Before
+ public void setUp() {
+ service = new OAuthService();
+ }
+
+ @Test
+ public void testParseAuthorizeUrl() throws Exception {
+ String xml = "<Authorization url='http://azn.example.com'/>";
+ URI url = service.parseAuthorizationUrl(XmlUtil.parse(xml));
+ assertEquals("http://azn.example.com", url.toString());
+ }
+
+ @Test
+ public void testParseAuthorizeUrl_nourl() throws Exception {
+ String xml = "<Authorization/>";
+ try {
+ service.parseAuthorizationUrl(XmlUtil.parse(xml));
+ fail("Should have rejected malformed Authorization element");
+ } catch (SpecParserException e) {
+ assertEquals("OAuth/Service/Authorization @url is not valid: ",
e.getMessage());
+ }
+ }
+
+ @Test
+ public void testParseAuthorizeUrl_extraAttr() throws Exception {
+ String xml = "<Authorization url='http://www.example.com' foo='bar'/>";
+ URI url = service.parseAuthorizationUrl(XmlUtil.parse(xml));
+ assertEquals("http://www.example.com", url.toString());
+ }
+
+ @Test
+ public void testParseAuthorizeUrl_notHttp() throws Exception {
+ OAuthService service = new OAuthService();
+ String xml = "<Authorization url='ftp://www.example.com'/>";
+ try {
+ service.parseAuthorizationUrl(XmlUtil.parse(xml));
+ fail("Should have rejected malformed Authorization element");
+ } catch (SpecParserException e) {
+ assertEquals("OAuth/Service/Authorization @url is not valid:
ftp://www.example.com", e.getMessage());
+ }
+ }
+
+ @Test
+ public void testParseEndPoint() throws Exception {
+ String xml = "<Request url='http://www.example.com'/>";
+ OAuthService.EndPoint ep = service.parseEndPoint("Request",
XmlUtil.parse(xml));
+ assertEquals("http://www.example.com", ep.url.toString());
+ assertEquals(OAuthService.Location.header, ep.location);
+ assertEquals(OAuthService.Method.POST, ep.method);
+ }
+
+ @Test
+ public void testParseEndPoint_nodefaults() throws Exception {
+ String xml = "<Request url='http://www.example.com' method='GET'
param_location='body'/>";
+ OAuthService.EndPoint ep = service.parseEndPoint("Request",
XmlUtil.parse(xml));
+ assertEquals("http://www.example.com", ep.url.toString());
+ assertEquals(OAuthService.Location.body, ep.location);
+ assertEquals(OAuthService.Method.GET, ep.method);
+ }
+
+ @Test(expected=SpecParserException.class)
+ public void testParseEndPoint_nourl() throws Exception {
+ String xml = "<Request method='GET' param_location='body'/>";
+ service.parseEndPoint("Request", XmlUtil.parse(xml));
+ }
+
+ @Test(expected=SpecParserException.class)
+ public void testParseEndPoint_badurl() throws Exception {
+ String xml = "<Request url='www.example.com' />";
+ service.parseEndPoint("Request", XmlUtil.parse(xml));
+ }
+
+ @Test
+ public void testParseService() throws Exception {
+ String xml = "" +
+ "<Service name='thename'>" +
+ " <Request url='http://request.example.com/foo'/>" +
+ " <Access url='http://access.example.com/bar'/>" +
+ " <Authorization url='http://azn.example.com/quux'/>" +
+ "</Service>";
+ OAuthService s = new OAuthService(XmlUtil.parse(xml));
+ assertEquals("thename", s.getName());
+ assertEquals(OAuthService.Location.header, s.getAccessUrl().location);
+ assertEquals("http://azn.example.com/quux",
s.getAuthorizationUrl().toString());
+ }
+
+ @Test
+ public void testParseService_noname() throws Exception {
+ String xml = "" +
+ "<Service>" +
+ " <Request url='http://request.example.com/foo'/>" +
+ " <Access url='http://access.example.com/bar'/>" +
+ " <Authorization url='http://azn.example.com/quux'/>" +
+ "</Service>";
+ OAuthService s = new OAuthService(XmlUtil.parse(xml));
+ assertEquals("", s.getName());
+ assertEquals(OAuthService.Location.header, s.getAccessUrl().location);
+ assertEquals("http://azn.example.com/quux",
s.getAuthorizationUrl().toString());
+ }
+
+ @Test
+ public void testParseService_nodata() throws Exception {
+ String xml = "<Service/>";
+ OAuthService s = new OAuthService(XmlUtil.parse(xml));
+ assertEquals("", s.getName());
+ assertNull(s.getAccessUrl());
+ assertNull(s.getAuthorizationUrl());
+ assertNull(s.getRequestUrl());
+ }
+
+ @Test
+ public void testParseService_nameonly() throws Exception {
+ String xml = "<Service name='foo'/>";
+ OAuthService s = new OAuthService(XmlUtil.parse(xml));
+ assertEquals("foo", s.getName());
+ assertNull(s.getAccessUrl());
+ assertNull(s.getAuthorizationUrl());
+ assertNull(s.getRequestUrl());
+ }
+
+ @Test
+ public void testParseService_reqonly() throws Exception {
+ String xml = "<Service>" +
+ "<Request url='http://www.example.com/request'/>" +
+ "</Service>";
+ OAuthService s = new OAuthService(XmlUtil.parse(xml));
+ assertEquals("", s.getName());
+ assertNull(s.getAccessUrl());
+ assertNull(s.getAuthorizationUrl());
+ assertEquals("http://www.example.com/request",
s.getRequestUrl().url.toString());
+ }
+}
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthSpecTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthSpecTest.java?rev=667888&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthSpecTest.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/OAuthSpecTest.java
Sat Jun 14 14:58:16 2008
@@ -0,0 +1,73 @@
+/*
+ * 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.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.apache.shindig.common.xml.XmlUtil;
+import org.junit.Test;
+
+/**
+ * Tests for OAuthSpec
+ */
+public class OAuthSpecTest {
+
+ @Test
+ public void testOAuthSpec() throws Exception {
+ String xml = "<OAuth><Service>" +
+ "<Request url='http://www.example.com/request'/>" +
+ "</Service></OAuth>";
+ OAuthSpec oauth = new OAuthSpec(XmlUtil.parse(xml));
+ assertEquals(1, oauth.getServices().size());
+ }
+
+ @Test
+ public void testOAuthSpec_noservice() throws Exception {
+ String xml = "<OAuth/>";
+ OAuthSpec oauth = new OAuthSpec(XmlUtil.parse(xml));
+ assertEquals(0, oauth.getServices().size());
+ }
+
+
+ @Test
+ public void testOAuthSpec_threeservice() throws Exception {
+ String xml = "<OAuth>" +
+ "<Service name='one'>" +
+ " <Request url='http://req.example.com' param_location='url'
method='POST'/>" +
+ "</Service>" +
+ "<Service name='two'>" +
+ " <Access url='http://two.example.com'/>" +
+ "</Service>" +
+ "<Service name='three'>" +
+ " <Request url='http://three.example.com' param_location='url'
method='POST'/>" +
+ "</Service>" +
+ "</OAuth>";
+ OAuthSpec oauth = new OAuthSpec(XmlUtil.parse(xml));
+ assertEquals("http://req.example.com",
+ oauth.getServices().get("one").getRequestUrl().url.toString());
+ assertEquals(OAuthService.Location.url,
+ oauth.getServices().get("one").getRequestUrl().location);
+ assertEquals("http://two.example.com",
+ oauth.getServices().get("two").getAccessUrl().url.toString());
+ assertEquals(OAuthService.Method.POST,
+ oauth.getServices().get("three").getRequestUrl().method);
+ assertNull(oauth.getServices().get("three").getAccessUrl());
+ assertNull(oauth.getServices().get("three").getAuthorizationUrl());
+ }
+}