Author: etnu
Date: Thu Aug 28 19:06:57 2008
New Revision: 690070

URL: http://svn.apache.org/viewvc?rev=690070&view=rev
Log:
More work for SHINDIG-523.

Migrated all preloading to the preload package and extracted a few interfaces 
for the base services.

Due to a screw up in the rename / moves, this is delete + add instead of move. 
There isn't much history here anyway though so it's OK.


Added:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderService.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloads.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadException.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadedData.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloader.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloaderService.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloads.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderServiceTest.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloadsTest.java
Removed:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Preload.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Preloader.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/PreloaderService.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/PreloaderServiceTest.java
Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java?rev=690070&r1=690069&r2=690070&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java
 Thu Aug 28 19:06:57 2008
@@ -35,7 +35,8 @@
   private final HttpFetcher httpFetcher;
 
   @Inject
-  public Renderer(GadgetSpecFactory gadgetSpecFactory, HttpFetcher 
httpFetcher) {
+  public Renderer(GadgetSpecFactory gadgetSpecFactory,
+                  HttpFetcher httpFetcher) {
     this.gadgetSpecFactory = gadgetSpecFactory;
     this.httpFetcher = httpFetcher;
   }

Added: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderService.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderService.java?rev=690070&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderService.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderService.java
 Thu Aug 28 19:06:57 2008
@@ -0,0 +1,54 @@
+/**
+ * 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.preload;
+
+import com.google.inject.Inject;
+
+import org.apache.shindig.gadgets.Gadget;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+
+/**
+ * Preloads will be fetched concurrently using the injected ExecutorService, 
and they can be read
+ * lazily using the returned map of futures.
+ */
+public class ConcurrentPreloaderService implements PreloaderService {
+  private final ExecutorService executor;
+  private final List<? extends Preloader> preloaders;
+
+  @Inject
+  public ConcurrentPreloaderService(ExecutorService executor,
+      List<? extends Preloader> preloaders) {
+    this.executor = executor;
+    this.preloaders = preloaders;
+  }
+
+  public Preloads preload(Gadget gadget) {
+    ConcurrentPreloads preloads = new ConcurrentPreloads();
+    for (Preloader preloader : preloaders) {
+      Map<String, Callable<PreloadedData>> tasks = 
preloader.createPreloadTasks(gadget);
+      for (Map.Entry<String, Callable<PreloadedData>> entry : 
tasks.entrySet()) {
+        preloads.add(entry.getKey(), executor.submit(entry.getValue()));
+      }
+    }
+    return preloads;
+  }
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloads.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloads.java?rev=690070&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloads.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/ConcurrentPreloads.java
 Thu Aug 28 19:06:57 2008
@@ -0,0 +1,76 @@
+/*
+ * 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.preload;
+
+import com.google.common.collect.Maps;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+/**
+ * Preloads data by processing all Runnables concurrently.
+ */
+class ConcurrentPreloads implements Preloads {
+  private final Map<String, Future<PreloadedData>> preloads;
+
+  ConcurrentPreloads() {
+    preloads = Maps.newHashMap();
+  }
+
+  /**
+   * Add an active preloading process.
+   *
+   * @param key The key that this preload will be stored under.
+   * @param futureData A future that will return the preloaded data.
+   */
+  ConcurrentPreloads add(String key, Future<PreloadedData> futureData) {
+    preloads.put(key, futureData);
+    return this;
+  }
+
+  public Set<String> getKeys() {
+    return preloads.keySet();
+  }
+
+  public PreloadedData getData(String key) throws PreloadException {
+    Future<PreloadedData> future = preloads.get(key);
+
+    if (future == null) {
+      return null;
+    }
+
+    try {
+      // TODO: Determine if timeouts should be supported.
+      return future.get();
+    } catch (InterruptedException e) {
+      // Thread was interrupted. We might want to throw a RTE here, but this 
is probably only going
+      // to happen if we're shutting down the server anyway.
+      throw new PreloadException("Preloading was interrupted by thread 
termination.", e);
+    } catch (ExecutionException e) {
+      // Callable threw an exception. Throw the original.
+      Throwable cause = e.getCause();
+      if (cause instanceof PreloadException) {
+        throw (PreloadException) cause;
+      }
+      throw new PreloadException(cause);
+    }
+  }
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadException.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadException.java?rev=690070&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadException.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadException.java
 Thu Aug 28 19:06:57 2008
@@ -0,0 +1,36 @@
+/*
+ * 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.preload;
+
+/**
+ * Exceptions thrown when preloading data.
+ */
+public class PreloadException extends Exception {
+  public PreloadException(String msg) {
+    super(msg);
+  }
+
+  public PreloadException(Throwable t) {
+    super(t);
+  }
+
+  public PreloadException(String msg, Throwable t) {
+    super(msg, t);
+  }
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadedData.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadedData.java?rev=690070&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadedData.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloadedData.java
 Thu Aug 28 19:06:57 2008
@@ -0,0 +1,31 @@
+/**
+ * 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.preload;
+
+/**
+ * Contains preloaded data and methods for manipulating it.
+ */
+public interface PreloadedData {
+
+  /**
+   * Serialize the preloaded data into json.
+   *
+   * @return A JSON object suitable for passing to 
org.json.JSONObject.put(String, Object).
+   */
+  Object toJson();
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloader.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloader.java?rev=690070&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloader.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloader.java
 Thu Aug 28 19:06:57 2008
@@ -0,0 +1,37 @@
+/**
+ * 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.preload;
+
+import org.apache.shindig.gadgets.Gadget;
+
+import java.util.Map;
+import java.util.concurrent.Callable;
+
+/**
+ * Performs an individual preloading operation.
+ */
+public interface Preloader {
+  /**
+   * Create new preload tasks for the provided gadget.
+   *
+   * @param gadget The gadget that the operations will be performed for.
+   * @return Preloading tasks that will be executed by
+   *  [EMAIL PROTECTED] PreloaderService#preload(Gadget)}.
+   */
+  Map<String, Callable<PreloadedData>> createPreloadTasks(Gadget gadget);
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloaderService.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloaderService.java?rev=690070&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloaderService.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PreloaderService.java
 Thu Aug 28 19:06:57 2008
@@ -0,0 +1,37 @@
+/**
+ * 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.preload;
+
+import com.google.inject.ImplementedBy;
+
+import org.apache.shindig.gadgets.Gadget;
+
+/**
+ * Handles preloading operations, such as HTTP fetches, social data retrieval, 
or anything else that
+ * would benefit from preloading on the server instead of incurring a network 
request for users.
+ */
[EMAIL PROTECTED](ConcurrentPreloaderService.class)
+public interface PreloaderService {
+  /**
+   * Begin all preload operations.
+   *
+   * @param gadget The gadget to perform preloading for.
+   * @return The preloads for the gadget.
+   */
+  Preloads preload(Gadget gadget);
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloads.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloads.java?rev=690070&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloads.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/Preloads.java
 Thu Aug 28 19:06:57 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.preload;
+
+import java.util.Set;
+
+/**
+ * Container that holds preload operations, which are actually contained as 
futures.
+ */
+public interface Preloads {
+  /**
+   * @return Keys for all preloaded data.
+   */
+  Set<String> getKeys();
+
+  /**
+   * Retrieve a single preload.
+   * 
+   * @param key The key that the preload is stored under.
+   * @return The preloaded data, or null if there is no preload under the 
specified key (including
+   * failure to preload).
+   * @throws PreloadException If there was any issue while preloading.
+   */
+  PreloadedData getData(String key) throws PreloadException;
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderServiceTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderServiceTest.java?rev=690070&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderServiceTest.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloaderServiceTest.java
 Thu Aug 28 19:06:57 2008
@@ -0,0 +1,122 @@
+/**
+ * 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.preload;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.shindig.common.testing.TestExecutorService;
+import org.apache.shindig.gadgets.Gadget;
+
+import com.google.common.collect.Maps;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Map;
+import java.util.concurrent.Callable;
+
+/**
+ * Tests for FuturePreloaderService.
+ */
+public class ConcurrentPreloaderServiceTest {
+  private static final String PRELOAD_STRING_KEY = "key a";
+  private static final String PRELOAD_NUMERIC_KEY = "key b";
+  private static final String PRELOAD_MAP_KEY = "key c";
+  private static final String PRELOAD_STRING_VALUE = "Some random string";
+  private static final Integer PRELOAD_NUMERIC_VALUE = 5;
+  private static final Map<String, String> PRELOAD_MAP_VALUE
+      = Maps.immutableMap("foo", "bar", "baz", "blah");
+
+  private final TestPreloader preloader = new TestPreloader();
+  private final TestPreloader preloader2 = new TestPreloader();
+
+  @Test
+  public void preloadSingleService() throws PreloadException {
+    preloader.tasks.put(PRELOAD_STRING_KEY,
+        new TestPreloadCallable(new DataPreload(PRELOAD_STRING_VALUE)));
+
+    PreloaderService service
+        = new ConcurrentPreloaderService(new TestExecutorService(), 
Arrays.asList(preloader));
+
+    assertEquals(PRELOAD_STRING_VALUE, 
service.preload(null).getData(PRELOAD_STRING_KEY).toJson());
+  }
+
+  @Test
+  public void preloadMultipleServices() throws PreloadException {
+    preloader.tasks.put(PRELOAD_STRING_KEY,
+        new TestPreloadCallable(new DataPreload(PRELOAD_STRING_VALUE)));
+
+    preloader.tasks.put(PRELOAD_NUMERIC_KEY,
+        new TestPreloadCallable(new DataPreload(PRELOAD_NUMERIC_VALUE)));
+
+    preloader2.tasks.put(PRELOAD_MAP_KEY,
+        new TestPreloadCallable(new DataPreload(PRELOAD_MAP_VALUE)));
+
+    PreloaderService service = new ConcurrentPreloaderService(new 
TestExecutorService(),
+        Arrays.asList(preloader, preloader2));
+
+    Preloads preloads = service.preload(null);
+
+    assertEquals(PRELOAD_STRING_VALUE, 
preloads.getData(PRELOAD_STRING_KEY).toJson());
+    assertEquals(PRELOAD_NUMERIC_VALUE, 
preloads.getData(PRELOAD_NUMERIC_KEY).toJson());
+    assertEquals(PRELOAD_MAP_VALUE, 
preloads.getData(PRELOAD_MAP_KEY).toJson());
+  }
+
+  @Test(expected = PreloadException.class)
+  public void exceptionsArePropagated() throws PreloadException {
+    preloader.tasks.put(PRELOAD_STRING_KEY, new TestPreloadCallable(null));
+    PreloaderService service
+        = new ConcurrentPreloaderService(new TestExecutorService(), 
Arrays.asList(preloader));
+    service.preload(null).getData(PRELOAD_STRING_KEY);
+  }
+
+  private static class TestPreloader implements Preloader {
+    private final Map<String, Callable<PreloadedData>> tasks = 
Maps.newHashMap();
+
+    public Map<String, Callable<PreloadedData>> createPreloadTasks(Gadget 
gadget) {
+      return tasks;
+    }
+  }
+
+  private static class TestPreloadCallable implements Callable<PreloadedData> {
+    private final PreloadedData preload;
+
+    public TestPreloadCallable(PreloadedData preload) {
+      this.preload = preload;
+    }
+
+    public PreloadedData call() throws Exception {
+      if (preload == null) {
+        throw new PreloadException("No preload for this test.");
+      }
+      return preload;
+    }
+  }
+
+  private static class DataPreload implements PreloadedData {
+    private final Object data;
+
+    public DataPreload(Object data) {
+      this.data = data;
+    }
+
+    public Object toJson() {
+      return data;
+    }
+  }
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloadsTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloadsTest.java?rev=690070&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloadsTest.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/preload/ConcurrentPreloadsTest.java
 Thu Aug 28 19:06:57 2008
@@ -0,0 +1,151 @@
+/*
+ * 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.preload;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import com.google.common.collect.Sets;
+
+import org.junit.Test;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Tests for ConcurrentPreloads.
+ */
+public class ConcurrentPreloadsTest {
+
+  @Test
+  public void getKeys() {
+    ConcurrentPreloads preloads = new ConcurrentPreloads();
+    preloads.add("foo", TestFuture.returnsNormal());
+    preloads.add("throwsInterrupted", TestFuture.throwsInterrupted());
+
+    assertEquals(Sets.newHashSet("foo", "throwsInterrupted"), 
preloads.getKeys());
+  }
+
+  @Test
+  public void getPreloadedDataNormal() throws Exception {
+    ConcurrentPreloads preloads = new ConcurrentPreloads();
+    preloads.add("foo", TestFuture.returnsNormal());
+
+    assertNotNull(preloads.getData("foo"));
+  }
+
+  @Test
+  public void getPreloadedDataNull() throws Exception {
+    ConcurrentPreloads preloads = new ConcurrentPreloads();
+    preloads.add("foo", null);
+    assertNull(preloads.getData("foo"));
+  }
+
+  @Test(expected = PreloadException.class)
+  public void getPreloadedDataThrowsInterrupted() throws Exception {
+    ConcurrentPreloads preloads = new ConcurrentPreloads();
+    preloads.add("foo", TestFuture.throwsInterrupted());
+    preloads.getData("foo");
+  }
+
+  @Test(expected = PreloadException.class)
+  public void getPreloadedThrowsExecution() throws Exception {
+    ConcurrentPreloads preloads = new ConcurrentPreloads();
+    preloads.add("foo", TestFuture.throwsExecution());
+    preloads.getData("foo");
+  }
+
+  @Test(expected = PreloadException.class)
+  public void getPreloadedThrowsExecutionWrapped() throws Exception {
+    ConcurrentPreloads preloads = new ConcurrentPreloads();
+    preloads.add("foo", TestFuture.throwsExecutionWrapped());
+    preloads.getData("foo");
+  }
+
+  private static class TestFuture implements Future<PreloadedData> {
+    private boolean throwsInterrupted;
+    private boolean throwsExecution;
+    private boolean throwsExecutionWrapped;
+
+    public static TestFuture returnsNormal() {
+      return new TestFuture();
+    }
+
+    public static TestFuture returnsNull() {
+      return new TestFuture();
+    }
+
+    public static TestFuture throwsInterrupted() {
+      TestFuture future = new TestFuture();
+      future.throwsInterrupted = true;
+      return future;
+    }
+
+    public static TestFuture throwsExecution() {
+      TestFuture future = new TestFuture();
+      future.throwsExecution = true;
+      return future;
+    }
+
+    public static TestFuture throwsExecutionWrapped() {
+      TestFuture future = new TestFuture();
+      future.throwsExecutionWrapped = true;
+      return future;
+    }
+
+    public PreloadedData get() throws InterruptedException, ExecutionException 
{
+      if (throwsInterrupted) {
+        throw new InterruptedException("Interrupted!");
+      }
+
+      if (throwsExecution) {
+        throw new ExecutionException(new RuntimeException("Fail"));
+      }
+
+      if (throwsExecutionWrapped) {
+        throw new ExecutionException(new PreloadException("Preload failed."));
+      }
+
+      return new PreloadedData() {
+        public Object toJson() {
+          return "Preloaded";
+        }
+      };
+    }
+
+    public boolean cancel(boolean mayInterruptIfRunning) {
+      return false;
+    }
+
+    public PreloadedData get(long timeout, TimeUnit unit) throws 
InterruptedException,
+        ExecutionException  {
+      return get();
+    }
+
+    public boolean isCancelled() {
+      return false;
+    }
+
+    public boolean isDone() {
+      return false;
+    }
+  }
+}


Reply via email to