• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.base.Supplier;
24 import com.google.common.base.Suppliers;
25 import java.security.Permission;
26 import java.util.concurrent.Callable;
27 import java.util.concurrent.ExecutionException;
28 import junit.framework.TestCase;
29 
30 /**
31  * Unit tests for {@link Callables}.
32  *
33  * @author Isaac Shum
34  */
35 @GwtCompatible(emulated = true)
36 public class CallablesTest extends TestCase {
37 
testReturning()38   public void testReturning() throws Exception {
39     assertNull(Callables.returning(null).call());
40 
41     Object value = new Object();
42     Callable<Object> callable = Callables.returning(value);
43     assertSame(value, callable.call());
44     // Expect the same value on subsequent calls
45     assertSame(value, callable.call());
46   }
47 
48   @GwtIncompatible
testAsAsyncCallable()49   public void testAsAsyncCallable() throws Exception {
50     final String expected = "MyCallableString";
51     Callable<String> callable =
52         new Callable<String>() {
53           @Override
54           public String call() throws Exception {
55             return expected;
56           }
57         };
58 
59     AsyncCallable<String> asyncCallable =
60         Callables.asAsyncCallable(callable, MoreExecutors.newDirectExecutorService());
61 
62     ListenableFuture<String> future = asyncCallable.call();
63     assertSame(expected, future.get());
64   }
65 
66   @GwtIncompatible
testAsAsyncCallable_exception()67   public void testAsAsyncCallable_exception() throws Exception {
68     final Exception expected = new IllegalArgumentException();
69     Callable<String> callable =
70         new Callable<String>() {
71           @Override
72           public String call() throws Exception {
73             throw expected;
74           }
75         };
76 
77     AsyncCallable<String> asyncCallable =
78         Callables.asAsyncCallable(callable, MoreExecutors.newDirectExecutorService());
79 
80     ListenableFuture<String> future = asyncCallable.call();
81     try {
82       future.get();
83       fail("Expected exception to be thrown");
84     } catch (ExecutionException e) {
85       assertThat(e).hasCauseThat().isSameInstanceAs(expected);
86     }
87   }
88 
89   @GwtIncompatible // threads
testRenaming()90   public void testRenaming() throws Exception {
91     String oldName = Thread.currentThread().getName();
92     final Supplier<String> newName = Suppliers.ofInstance("MyCrazyThreadName");
93     Callable<Void> callable =
94         new Callable<Void>() {
95           @Override
96           public Void call() throws Exception {
97             assertEquals(Thread.currentThread().getName(), newName.get());
98             return null;
99           }
100         };
101     Callables.threadRenaming(callable, newName).call();
102     assertEquals(oldName, Thread.currentThread().getName());
103   }
104 
105   @GwtIncompatible // threads
testRenaming_exceptionalReturn()106   public void testRenaming_exceptionalReturn() throws Exception {
107     String oldName = Thread.currentThread().getName();
108     final Supplier<String> newName = Suppliers.ofInstance("MyCrazyThreadName");
109     class MyException extends Exception {}
110     Callable<Void> callable =
111         new Callable<Void>() {
112           @Override
113           public Void call() throws Exception {
114             assertEquals(Thread.currentThread().getName(), newName.get());
115             throw new MyException();
116           }
117         };
118     try {
119       Callables.threadRenaming(callable, newName).call();
120       fail();
121     } catch (MyException expected) {
122     }
123     assertEquals(oldName, Thread.currentThread().getName());
124   }
125 
126   @GwtIncompatible // threads
127 
testRenaming_noPermissions()128   public void testRenaming_noPermissions() throws Exception {
129     System.setSecurityManager(
130         new SecurityManager() {
131           @Override
132           public void checkAccess(Thread t) {
133             throw new SecurityException();
134           }
135 
136           @Override
137           public void checkPermission(Permission perm) {
138             // Do nothing so we can clear the security manager at the end
139           }
140         });
141     try {
142       final String oldName = Thread.currentThread().getName();
143       Supplier<String> newName = Suppliers.ofInstance("MyCrazyThreadName");
144       Callable<Void> callable =
145           new Callable<Void>() {
146             @Override
147             public Void call() throws Exception {
148               assertEquals(Thread.currentThread().getName(), oldName);
149               return null;
150             }
151           };
152       Callables.threadRenaming(callable, newName).call();
153       assertEquals(oldName, Thread.currentThread().getName());
154     } finally {
155       System.setSecurityManager(null);
156     }
157   }
158 }
159