/*
 *
 * (c) 2003-2009 ThoughtWorks Ltd
 *
 * See license.txt for license details
 */
package com.thoughtworks.proxy.toys.future;

import com.thoughtworks.proxy.AbstractProxyTest;
import static com.thoughtworks.proxy.toys.future.Future.typedFuture;
import static com.thoughtworks.proxy.toys.future.Future.future;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import org.junit.Test;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author Aslak Helles&oslash;y
 * @version $Revision: 1.3 $
 */
public class FutureTest extends AbstractProxyTest {
    public static interface Service {
        List getList();
        void methodReturnsVoid();
    }

    public static class SlowService implements Service {
        private final CountDownLatch latch;

        public SlowService(CountDownLatch latch) {
            this.latch = latch;
        }

        public List getList() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e.getMessage());
            }
            return Collections.singletonList("yo");
        }

        public void methodReturnsVoid() {
        }
    }

    @Test
    public void testShouldReturnNullObjectAsIntermediateResultAndSwapWhenMethodCompletesWithCast() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService = (Service) future(slowService).build(getFactory());

        List stuff = fastService.getList();
        assertTrue(stuff.isEmpty());
        // let slowService.getStuff() proceed!
        latch.countDown();
        assertEquals(0, stuff.size());
        Thread.sleep(100);
        assertEquals("yo", stuff.get(0));
    }

    @Test
    public void testShouldReturnNullObjectAsIntermediateResultAndSwapWhenMethodCompletesWithGenerics() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService = typedFuture(Service.class).with(slowService).build(getFactory());

        List stuff = fastService.getList();
        assertTrue(stuff.isEmpty());
        // let slowService.getStuff() proceed!
        latch.countDown();
        assertEquals(0, stuff.size());
        Thread.sleep(100);
        assertEquals("yo", stuff.get(0));
    }

    @Test
    public void testShouldHandleVoidMethodsWithCast() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService = (Service) future(slowService).build(getFactory());
        fastService.methodReturnsVoid();
    }

    @Test
    public void testShouldHandleVoidMethodsWithGenerics() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService =  typedFuture(Service.class).with(slowService).build(getFactory());
        fastService.methodReturnsVoid();
    }
}
@@ -139,16 +139,23 @@
 %0Aimport 
+static 
 com.thou
@@ -169,33 +169,41 @@
 s.proxy.
-AbstractProxyTest
+toys.future.Future.future
 ;%0D%0Aimpor
@@ -271,73 +271,8 @@
 e;%0D%0A
-import static com.thoughtworks.proxy.toys.future.Future.future;%0D%0A
 impo
@@ -372,32 +372,8 @@
 ue;%0D
-%0Aimport org.junit.Test;%0D
 %0A%0D%0Ai
@@ -426,16 +426,16 @@
 .List;%0D%0A
-
 import j
@@ -473,16 +473,94 @@
 tch;%0D%0A%0D%0A
+import com.thoughtworks.proxy.AbstractProxyTest;%0D%0A%0D%0Aimport org.junit.Test;%0D%0A%0D%0A
 /**%0D%0A * 
@@ -1812,32 +1812,60 @@
 h.countDown();%0D%0A
+        Thread.sleep(100);%0D%0A
         assertEq
@@ -1861,33 +1861,33 @@
    assertEquals(
-0
+1
 , stuff.size());
@@ -1892,36 +1892,8 @@
 );%0D%0A
-        Thread.sleep(100);%0D%0A
     
@@ -2445,32 +2445,60 @@
 h.countDown();%0D%0A
+        Thread.sleep(100);%0D%0A
         assertEq
@@ -2502,17 +2502,17 @@
 tEquals(
-0
+1
 , stuff.
@@ -2525,36 +2525,8 @@
 );%0D%0A
-        Thread.sleep(100);%0D%0A
     

Fix FutureToy tests.