Revisit Assert to avoid single-arg assert methods (with refined messages)

Issue: SPR-15196
This commit is contained in:
Juergen Hoeller
2017-01-30 22:15:53 +01:00
parent 768802fa96
commit 1b2dc3638f
118 changed files with 708 additions and 828 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,14 +44,15 @@ class DefaultCacheInvocationContext<A extends Annotation>
private final CacheInvocationParameter[] allParameters;
public DefaultCacheInvocationContext(JCacheOperation<A> operation,
Object target, Object[] args) {
public DefaultCacheInvocationContext(JCacheOperation<A> operation, Object target, Object[] args) {
this.operation = operation;
this.target = target;
this.args = args;
this.allParameters = operation.getAllParameters(args);
}
@Override
public JCacheOperation<A> getOperation() {
return this.operation;
@@ -94,17 +95,19 @@ class DefaultCacheInvocationContext<A extends Annotation>
@Override
public <T> T unwrap(Class<T> cls) {
throw new IllegalArgumentException("Could not unwrap to '" + cls.getName() + "'");
throw new IllegalArgumentException("Cannot unwrap to " + cls);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("CacheInvocationContext{");
sb.append("operation=").append(operation);
sb.append(", target=").append(target);
sb.append(", args=").append(Arrays.toString(args));
sb.append(", allParameters=").append(Arrays.toString(allParameters));
StringBuilder sb = new StringBuilder("CacheInvocationContext{");
sb.append("operation=").append(this.operation);
sb.append(", target=").append(this.target);
sb.append(", args=").append(Arrays.toString(this.args));
sb.append(", allParameters=").append(Arrays.toString(this.allParameters));
sb.append('}');
return sb.toString();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -66,7 +66,7 @@ public class JCacheAspectSupport extends AbstractCacheInvoker implements Initial
public void setCacheOperationSource(JCacheOperationSource cacheOperationSource) {
Assert.notNull(cacheOperationSource);
Assert.notNull(cacheOperationSource, "JCacheOperationSource must not be null");
this.cacheOperationSource = cacheOperationSource;
}
@@ -80,7 +80,7 @@ public class JCacheAspectSupport extends AbstractCacheInvoker implements Initial
public void afterPropertiesSet() {
Assert.state(getCacheOperationSource() != null, "The 'cacheOperationSource' property is required: " +
"If there are no cacheable methods, then don't use a cache aspect.");
Assert.state(getErrorHandler() != null, "The 'errorHandler' is required");
Assert.state(getErrorHandler() != null, "The 'errorHandler' property is required");
this.cacheResultInterceptor = new CacheResultInterceptor(getErrorHandler());
this.cachePutInterceptor = new CachePutInterceptor(getErrorHandler());
@@ -128,23 +128,23 @@ public class JCacheAspectSupport extends AbstractCacheInvoker implements Initial
BasicOperation operation = context.getOperation();
if (operation instanceof CacheResultOperation) {
return cacheResultInterceptor.invoke(
return this.cacheResultInterceptor.invoke(
(CacheOperationInvocationContext<CacheResultOperation>) context, adapter);
}
else if (operation instanceof CachePutOperation) {
return cachePutInterceptor.invoke(
return this.cachePutInterceptor.invoke(
(CacheOperationInvocationContext<CachePutOperation>) context, adapter);
}
else if (operation instanceof CacheRemoveOperation) {
return cacheRemoveEntryInterceptor.invoke(
return this.cacheRemoveEntryInterceptor.invoke(
(CacheOperationInvocationContext<CacheRemoveOperation>) context, adapter);
}
else if (operation instanceof CacheRemoveAllOperation) {
return cacheRemoveAllInterceptor.invoke(
return this.cacheRemoveAllInterceptor.invoke(
(CacheOperationInvocationContext<CacheRemoveAllOperation>) context, adapter);
}
else {
throw new IllegalArgumentException("Could not handle " + operation);
throw new IllegalArgumentException("Cannot handle " + operation);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,7 +43,6 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.scheduling.SchedulingException;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
@@ -105,6 +104,7 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe
private static final ThreadLocal<DataSource> configTimeNonTransactionalDataSourceHolder =
new ThreadLocal<>();
/**
* Return the ResourceLoader for the currently configured Quartz Scheduler,
* to be used by ResourceLoaderClassLoadHelper.
@@ -210,7 +210,6 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe
* @see #setQuartzProperties
*/
public void setSchedulerFactoryClass(Class<? extends SchedulerFactory> schedulerFactoryClass) {
Assert.isAssignable(SchedulerFactory.class, schedulerFactoryClass);
this.schedulerFactoryClass = schedulerFactoryClass;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,8 +30,6 @@ import org.junit.rules.ExpectedException;
import org.springframework.cache.Cache;
import org.springframework.cache.jcache.AbstractJCacheTests;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
@@ -46,7 +44,7 @@ public class CacheResolverAdapterTests extends AbstractJCacheTests {
@Test
public void resolveSimpleCache() {
public void resolveSimpleCache() throws Exception {
DefaultCacheInvocationContext<?> dummyContext = createDummyContext();
CacheResolverAdapter adapter = new CacheResolverAdapter(getCacheResolver(dummyContext, "testCache"));
Collection<? extends Cache> caches = adapter.resolveCaches(dummyContext);
@@ -56,7 +54,7 @@ public class CacheResolverAdapterTests extends AbstractJCacheTests {
}
@Test
public void resolveUnknownCache() {
public void resolveUnknownCache() throws Exception {
DefaultCacheInvocationContext<?> dummyContext = createDummyContext();
CacheResolverAdapter adapter = new CacheResolverAdapter(getCacheResolver(dummyContext, null));
@@ -66,7 +64,7 @@ public class CacheResolverAdapterTests extends AbstractJCacheTests {
protected CacheResolver getCacheResolver(CacheInvocationContext<? extends Annotation> context, String cacheName) {
CacheResolver cacheResolver = mock(CacheResolver.class);
final javax.cache.Cache cache;
javax.cache.Cache cache;
if (cacheName == null) {
cache = null;
}
@@ -78,9 +76,8 @@ public class CacheResolverAdapterTests extends AbstractJCacheTests {
return cacheResolver;
}
protected DefaultCacheInvocationContext<?> createDummyContext() {
Method method = ReflectionUtils.findMethod(Sample.class, "get", String.class);
Assert.notNull(method);
protected DefaultCacheInvocationContext<?> createDummyContext() throws Exception {
Method method = Sample.class.getMethod("get", String.class);
CacheResult cacheAnnotation = method.getAnnotation(CacheResult.class);
CacheMethodDetails<CacheResult> methodDetails =
new DefaultCacheMethodDetails<>(method, cacheAnnotation, "test");
@@ -93,7 +90,7 @@ public class CacheResolverAdapterTests extends AbstractJCacheTests {
static class Sample {
@CacheResult
private Object get(String id) {
public Object get(String id) {
return null;
}
}