use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by google.
the class CacheLoadingTest method testLoadingExceptionWithCause.
/**
* Make sure LoadingCache correctly wraps ExecutionExceptions and UncheckedExecutionExceptions.
*/
public void testLoadingExceptionWithCause() {
final Exception cause = new Exception();
final UncheckedExecutionException uee = new UncheckedExecutionException(cause);
final ExecutionException ee = new ExecutionException(cause);
LoadingCache<Object, Object> cacheUnchecked = CacheBuilder.newBuilder().build(exceptionLoader(uee));
LoadingCache<Object, Object> cacheChecked = CacheBuilder.newBuilder().build(exceptionLoader(ee));
try {
cacheUnchecked.get(new Object());
fail();
} catch (ExecutionException e) {
fail();
} catch (UncheckedExecutionException caughtEe) {
assertSame(uee, caughtEe.getCause());
}
try {
cacheUnchecked.getUnchecked(new Object());
fail();
} catch (UncheckedExecutionException caughtUee) {
assertSame(uee, caughtUee.getCause());
}
cacheUnchecked.refresh(new Object());
checkLoggedCause(uee);
try {
cacheUnchecked.getAll(asList(new Object()));
fail();
} catch (ExecutionException e) {
fail();
} catch (UncheckedExecutionException caughtEe) {
assertSame(uee, caughtEe.getCause());
}
try {
cacheChecked.get(new Object());
fail();
} catch (ExecutionException caughtEe) {
assertSame(ee, caughtEe.getCause());
}
try {
cacheChecked.getUnchecked(new Object());
fail();
} catch (UncheckedExecutionException caughtUee) {
assertSame(ee, caughtUee.getCause());
}
cacheChecked.refresh(new Object());
checkLoggedCause(ee);
try {
cacheChecked.getAll(asList(new Object()));
fail();
} catch (ExecutionException caughtEe) {
assertSame(ee, caughtEe.getCause());
}
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by google.
the class AbstractLoadingCacheTest method testGetUnchecked_checked.
public void testGetUnchecked_checked() {
final Exception cause = new Exception();
final AtomicReference<Object> valueRef = new AtomicReference<Object>();
LoadingCache<Object, Object> cache = new AbstractLoadingCache<Object, Object>() {
@Override
public Object get(Object key) throws ExecutionException {
Object v = valueRef.get();
if (v == null) {
throw new ExecutionException(cause);
}
return v;
}
@Override
public Object getIfPresent(Object key) {
return valueRef.get();
}
};
try {
cache.getUnchecked(new Object());
fail();
} catch (UncheckedExecutionException expected) {
assertEquals(cause, expected.getCause());
}
Object newValue = new Object();
valueRef.set(newValue);
assertSame(newValue, cache.getUnchecked(new Object()));
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by hceylan.
the class CacheLoadingTest method testLoadingExceptionWithCause.
/**
* Make sure LoadingCache correctly wraps ExecutionExceptions and UncheckedExecutionExceptions.
*/
public void testLoadingExceptionWithCause() {
final Exception cause = new Exception();
final UncheckedExecutionException uee = new UncheckedExecutionException(cause);
final ExecutionException ee = new ExecutionException(cause);
LoadingCache<Object, Object> cacheUnchecked = CacheBuilder.newBuilder().build(exceptionLoader(uee));
LoadingCache<Object, Object> cacheChecked = CacheBuilder.newBuilder().build(exceptionLoader(ee));
try {
cacheUnchecked.get(new Object());
fail();
} catch (ExecutionException e) {
fail();
} catch (UncheckedExecutionException caughtEe) {
assertSame(uee, caughtEe.getCause());
}
try {
cacheUnchecked.getUnchecked(new Object());
fail();
} catch (UncheckedExecutionException caughtUee) {
assertSame(uee, caughtUee.getCause());
}
cacheUnchecked.refresh(new Object());
checkLoggedCause(uee);
try {
cacheUnchecked.getAll(asList(new Object()));
fail();
} catch (ExecutionException e) {
fail();
} catch (UncheckedExecutionException caughtEe) {
assertSame(uee, caughtEe.getCause());
}
try {
cacheChecked.get(new Object());
fail();
} catch (ExecutionException caughtEe) {
assertSame(ee, caughtEe.getCause());
}
try {
cacheChecked.getUnchecked(new Object());
fail();
} catch (UncheckedExecutionException caughtUee) {
assertSame(ee, caughtUee.getCause());
}
cacheChecked.refresh(new Object());
checkLoggedCause(ee);
try {
cacheChecked.getAll(asList(new Object()));
fail();
} catch (ExecutionException caughtEe) {
assertSame(ee, caughtEe.getCause());
}
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by hceylan.
the class CacheLoadingTest method testBulkLoadingExceptionWithCause.
public void testBulkLoadingExceptionWithCause() {
final Exception cause = new Exception();
final UncheckedExecutionException uee = new UncheckedExecutionException(cause);
final ExecutionException ee = new ExecutionException(cause);
LoadingCache<Object, Object> cacheUnchecked = CacheBuilder.newBuilder().build(bulkLoader(exceptionLoader(uee)));
LoadingCache<Object, Object> cacheChecked = CacheBuilder.newBuilder().build(bulkLoader(exceptionLoader(ee)));
try {
cacheUnchecked.getAll(asList(new Object()));
fail();
} catch (ExecutionException e) {
fail();
} catch (UncheckedExecutionException caughtEe) {
assertSame(uee, caughtEe.getCause());
}
try {
cacheChecked.getAll(asList(new Object()));
fail();
} catch (ExecutionException caughtEe) {
assertSame(ee, caughtEe.getCause());
}
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by hceylan.
the class CacheLoadingTest method testLoadCheckedException.
public void testLoadCheckedException() {
Exception e = new Exception();
CacheLoader<Object, Object> loader = exceptionLoader(e);
LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().build(loader);
CacheStats stats = cache.stats();
assertEquals(0, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.get(new Object());
fail();
} catch (ExecutionException expected) {
assertSame(e, expected.getCause());
}
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(1, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.getUnchecked(new Object());
fail();
} catch (UncheckedExecutionException expected) {
assertSame(e, expected.getCause());
}
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(2, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
cache.refresh(new Object());
checkLoggedCause(e);
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(3, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
Exception callableException = new Exception();
try {
cache.get(new Object(), throwing(callableException));
fail();
} catch (ExecutionException expected) {
assertSame(callableException, expected.getCause());
}
stats = cache.stats();
assertEquals(3, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(4, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.getAll(asList(new Object()));
fail();
} catch (ExecutionException expected) {
assertSame(e, expected.getCause());
}
stats = cache.stats();
assertEquals(4, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(5, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
}
Aggregations