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