Home | History | Annotate | Download | only in concurrent
      1 /*
      2  * Copyright (C) 2009 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.util.concurrent;
     18 
     19 import junit.framework.TestCase;
     20 
     21 import java.util.concurrent.TimeUnit;
     22 import java.util.concurrent.TimeoutException;
     23 
     24 /**
     25  * Test cases for {@link SettableFuture}.
     26  *
     27  * @author Sven Mawson
     28  */
     29 public class SettableFutureTest extends TestCase {
     30 
     31   private SettableFuture<String> future;
     32   private ListenableFutureTester tester;
     33 
     34   @Override
     35   protected void setUp() throws Exception {
     36     super.setUp();
     37 
     38     future = SettableFuture.create();
     39     tester = new ListenableFutureTester(future);
     40     tester.setUp();
     41   }
     42 
     43   public void testDefaultState() throws Exception {
     44     try {
     45       future.get(5, TimeUnit.MILLISECONDS);
     46       fail();
     47     } catch (TimeoutException expected) {}
     48   }
     49 
     50   public void testSetValue() throws Exception {
     51     assertTrue(future.set("value"));
     52     tester.testCompletedFuture("value");
     53   }
     54 
     55   public void testSetFailure() throws Exception {
     56     assertTrue(future.setException(new Exception("failure")));
     57     tester.testFailedFuture("failure");
     58   }
     59 
     60   public void testSetFailureNull() throws Exception {
     61     try {
     62       future.setException(null);
     63       fail();
     64     } catch (NullPointerException expected) {
     65     }
     66     assertFalse(future.isDone());
     67     assertTrue(future.setException(new Exception("failure")));
     68     tester.testFailedFuture("failure");
     69   }
     70 
     71   public void testCancel() throws Exception {
     72     assertTrue(future.cancel(true));
     73     tester.testCancelledFuture();
     74   }
     75 }
     76