Author: doll
Date: Wed May 7 04:24:19 2008
New Revision: 654071
URL: http://svn.apache.org/viewvc?rev=654071&view=rev
Log:
SHINDIG-242
Patch from David Primmer. Adds some tests to the new social restful code.
Added:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JettyServer.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RouteManagerTest.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderTestFixture.java
Modified:
incubator/shindig/trunk/java/social-api/pom.xml
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/GadgetDataTest.java
incubator/shindig/trunk/pom.xml
Modified: incubator/shindig/trunk/java/social-api/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/pom.xml?rev=654071&r1=654070&r2=654071&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/pom.xml (original)
+++ incubator/shindig/trunk/java/social-api/pom.xml Wed May 7 04:24:19 2008
@@ -77,8 +77,12 @@
</dependency>
<dependency>
<artifactId>commons-betwixt</artifactId>
- <version>0.8</version>
<groupId>commons-betwixt</groupId>
</dependency>
+ <dependency>
+ <groupId>org.mortbay.jetty</groupId>
+ <artifactId>jetty</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
Added:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java?rev=654071&view=auto
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java
(added)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java
Wed May 7 04:24:19 2008
@@ -0,0 +1,79 @@
+/**
+ * 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.social;
+
+import junit.framework.TestCase;
+
+import org.easymock.classextension.EasyMock;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public abstract class EasyMockTestCase extends TestCase {
+ /** Tracks all EasyMock objects created for a test. */
+ private final List<Object> mocks = new ArrayList<Object>();
+
+ public EasyMockTestCase() {
+ super();
+ }
+
+ public EasyMockTestCase(String name) {
+ super(name);
+ }
+
+ /**
+ * Creates a strict mock object for the given class, adds it to the internal
+ * list of all mocks, and returns it.
+ *
+ * @param clazz Class to be mocked.
+ * @return A mock instance of the given type.
+ **/
+ protected <T> T mock(Class<T> clazz) {
+ return mock(clazz, false);
+ }
+
+ /**
+ * Creates a strict or nice mock object for the given class, adds it to the
internal
+ * list of all mocks, and returns it.
+ *
+ * @param clazz Class to be mocked.
+ * @param strict whether or not to make a strict mock
+ * @return A mock instance of the given type.
+ **/
+ protected <T> T mock(Class<T> clazz, boolean strict) {
+ T m = strict ? EasyMock.createMock(clazz) : EasyMock.createNiceMock(clazz);
+ mocks.add(m);
+ return m;
+ }
+
+ /**
+ * Sets each mock to replay mode in the order they were created. Call this
after setting
+ * all of the mock expectations for a test.
+ */
+ protected void replay() {
+ EasyMock.replay(mocks.toArray());
+ }
+
+ /**
+ * Verifies each mock in the order they were created. Call this at the end
of each test
+ * to verify the expectations were satisfied.
+ */
+ protected void verify() {
+ EasyMock.verify(mocks.toArray());
+ }
+}
Modified:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/GadgetDataTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/GadgetDataTest.java?rev=654071&r1=654070&r2=654071&view=diff
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/GadgetDataTest.java
(original)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/GadgetDataTest.java
Wed May 7 04:24:19 2008
@@ -53,7 +53,8 @@
activity = new Activity("activityId", johnDoe.getId());
List<MediaItem> mediaItems = new ArrayList<MediaItem>();
- mediaItems.add(new MediaItem("image/jpg", MediaItem.Type.IMAGE,
"http://foo.bar"));
+ mediaItems.add(new MediaItem("image/jpg", MediaItem.Type.IMAGE,
+ "http://foo.bar"));
activity.setMediaItems(mediaItems);
}
Added:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JettyServer.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JettyServer.java?rev=654071&view=auto
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JettyServer.java
(added)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/JettyServer.java
Wed May 7 04:24:19 2008
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. 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. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.shindig.social;
+
+import org.apache.shindig.gadgets.http.GuiceServletContextListener;
+
+import org.apache.abdera.protocol.server.Provider;
+import org.apache.abdera.protocol.server.ServiceManager;
+import org.apache.abdera.protocol.server.servlet.AbderaServlet;
+
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.servlet.Context;
+import org.mortbay.jetty.servlet.ServletHolder;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.Servlet;
+
+public class JettyServer {
+ //TODO such a hack. why have to specify the provider when using the Servlet?
+ public static final int DEFAULT_PORT = 9002;
+ public static final String PROVIDER_NAME =
+ "org.apache.shindig.social.abdera.SocialApiProvider";
+ public static final String GUICE_MODULES =
+ "org.apache.shindig.common.CommonGuiceModule:" +
+ "org.apache.shindig.gadgets.http.HttpGuiceModule:" +
+ "org.apache.shindig.social.SocialApiGuiceModule";
+
+ private final int port;
+ private Server server;
+
+ public JettyServer() {
+ this(DEFAULT_PORT);
+ }
+
+ public JettyServer(int port) {
+ this.port = port;
+ }
+
+ public void start(Class<? extends Provider> _class, String mapBase)
+ throws Exception {
+ server = new Server(port);
+ if (mapBase == null) {
+ mapBase = "/*";
+ }
+ Context context = new Context(server, "/", Context.SESSIONS);
+ ServletHolder servletHolder = new ServletHolder(new AbderaServlet());
+ servletHolder.setInitParameter(ServiceManager.PROVIDER, _class.getName());
+ context.addServlet(servletHolder, mapBase);
+ server.start();
+ }
+
+ public void start(Servlet servlet, String mapBase) throws Exception {
+ server = new Server(port);
+ if (mapBase == null) {
+ mapBase = "/*";
+ }
+ Context context = new Context(server, "/", Context.SESSIONS);
+ context.addEventListener(new GuiceServletContextListener());
+ Map<String, String> initParams = new HashMap<String, String>();
+ initParams.put(GuiceServletContextListener.MODULES_ATTRIBUTE,
+ GUICE_MODULES);
+ context.setInitParams(initParams);
+ ServletHolder servletHolder = new ServletHolder(servlet);
+ servletHolder.setInitParameter(ServiceManager.PROVIDER, PROVIDER_NAME);
+ context.addServlet(servletHolder, mapBase);
+ server.start();
+ }
+ public void stop() throws Exception {
+ server.stop();
+ }
+
+}
Added:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RouteManagerTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RouteManagerTest.java?rev=654071&view=auto
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RouteManagerTest.java
(added)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RouteManagerTest.java
Wed May 7 04:24:19 2008
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. 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. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.shindig.social.abdera;
+
+import static org.easymock.EasyMock.expect;
+
+import org.apache.abdera.protocol.server.impl.RouteManager;
+
+import org.junit.Test;
+
+public class RouteManagerTest extends SocialApiProviderTestFixture {
+
+ private RouteManager rm;
+
+ @Override
+ public void setUp(){
+ rm = provider.getRouteManager();
+ }
+
+ @Test
+ public void testRouteManagerResolve() {
+ mockAndResolve("people/x/@all");
+ mockAndResolve("people/x/@friends");
+ mockAndResolve("people/x/y");
+ mockAndResolve("people/x/@all/y");
+
+ mockAndResolve("activities/x/@self");
+ mockAndResolve("activities/x/@friends");
+ mockAndResolve("activities/x/y");
+ mockAndResolve("activities/x/@self/y");
+
+ mockAndResolve("appdata/x/friends/y");
+ mockAndResolve("appdata/x/self/y");
+ }
+
+ private void mockAndResolve(String path){
+ expect(request.getTargetPath()).andReturn(base + path);
+ org.easymock.EasyMock.replay(request);
+ assertNotNull("path = " + path, rm.resolve(request));
+ org.easymock.EasyMock.reset(request);
+ }
+}
Added:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java?rev=654071&view=auto
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java
(added)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java
Wed May 7 04:24:19 2008
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. 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. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.shindig.social.abdera;
+
+import junit.framework.Assert;
+
+import org.apache.shindig.social.JettyServer;
+import org.apache.shindig.social.RestServerServlet;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.protocol.Response.ResponseType;
+import org.apache.abdera.protocol.client.AbderaClient;
+import org.apache.abdera.protocol.client.ClientResponse;
+import org.apache.abdera.util.Constants;
+import org.apache.abdera.util.MimeTypeHelper;
+import org.apache.abdera.writer.Writer;
+import org.apache.abdera.writer.WriterFactory;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.IOException;
+
+
+public class SocialApiProviderLargeTest extends Assert {
+
+ private static JettyServer server;
+ private static Abdera abdera = Abdera.getInstance();
+ private static AbderaClient client = new AbderaClient();
+
+ private static String BASE = "http://localhost:9002/social/rest/";
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ try {
+ server = new JettyServer();
+ server.start(new RestServerServlet(), "/social/rest/*");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ @AfterClass
+ public static void tearDown() throws Exception {
+ server.stop();
+ }
+
+ @Test
+ public void testGetConnectionsForJohnDoe() throws IOException {
+ ClientResponse resp = client.get(BASE + "people/john.doe/@all");
+ checkForGoodAtomResponse(resp);
+ Document<Feed> doc = resp.getDocument();
+ Feed feed = doc.getRoot();
+ assertEquals(feed.getTitle(), "People Collection title");
+ //prettyPrint(doc);
+ resp.release();
+ }
+
+// TODO this test cannot pass without the gadget server started.
+// need to determine how to deal with this dependency xmlstatefilefetch.
+//
+// @Test
+// public void testGetJaneDoeProfileForJohnDoe() throws IOException {
+// ClientResponse resp = client.get(BASE + "people/john.doe/@all/jane.doe");
+// checkForGoodAtomResponse(resp);
+// Document<Entry> doc = resp.getDocument();
+// Entry entry = doc.getRoot();
+// assertEquals(entry.getTitle(), "Jane Doe");
+// prettyPrint(doc);
+// resp.release();
+// }
+
+ protected void checkForGoodAtomResponse(ClientResponse response){
+ assertNotNull(response);
+ assertEquals(ResponseType.SUCCESS, response.getType());
+ assertTrue(MimeTypeHelper.isMatch(response.getContentType().toString(),
+ Constants.ATOM_MEDIA_TYPE));
+ }
+
+ protected void prettyPrint(Base doc) throws IOException {
+ WriterFactory writerFactory = abdera.getWriterFactory();
+ Writer writer = writerFactory.getWriter("prettyxml");
+ writer.writeTo(doc, System.out);
+ System.out.println();
+ }
+
+}
Added:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderTestFixture.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderTestFixture.java?rev=654071&view=auto
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderTestFixture.java
(added)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderTestFixture.java
Wed May 7 04:24:19 2008
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. 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. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.shindig.social.abdera;
+
+import static org.easymock.EasyMock.expect;
+
+import com.google.inject.Provider;
+
+import org.apache.shindig.social.EasyMockTestCase;
+
+import org.apache.abdera.protocol.server.RequestContext;
+
[EMAIL PROTECTED]("unchecked")
+public class SocialApiProviderTestFixture extends EasyMockTestCase {
+
+ public final String base = "/social/rest/";
+ public final RequestContext request = mock(RequestContext.class);
+ public final Provider<ActivitiesServiceAdapter> activitiesProvider =
+ mock(Provider.class);
+ public final Provider<PeopleServiceAdapter> peopleProvider =
+ mock(Provider.class);
+ public final SocialApiProvider provider = new SocialApiProvider();
+
+ public SocialApiProviderTestFixture() {
+ provider.setActivitiesAdapter(activitiesProvider);
+ expect(activitiesProvider.get()).andReturn(null);
+ provider.setPeopleAdapter(peopleProvider);
+ expect(peopleProvider.get()).andReturn(null);
+ org.easymock.EasyMock.replay(activitiesProvider);
+ org.easymock.EasyMock.replay(peopleProvider);
+ provider.initialize();
+ }
+}
Modified: incubator/shindig/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/pom.xml?rev=654071&r1=654070&r2=654071&view=diff
==============================================================================
--- incubator/shindig/trunk/pom.xml (original)
+++ incubator/shindig/trunk/pom.xml Wed May 7 04:24:19 2008
@@ -694,6 +694,16 @@
<artifactId>abdera-client</artifactId>
<version>0.5.0-incubating-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <artifactId>commons-betwixt</artifactId>
+ <groupId>commons-betwixt</groupId>
+ <version>0.8</version>
+ </dependency>
+ <dependency>
+ <groupId>org.mortbay.jetty</groupId>
+ <artifactId>jetty</artifactId>
+ <version>6.1.9</version>
+ </dependency>
</dependencies>
</dependencyManagement>
</project>