Convert CRLF (dos) to LF (unix)
Prior to this change, roughly 5% (~300 out of 6000+) of files under the source tree had CRLF line endings as opposed to the majority which have LF endings. This change normalizes these files to LF for consistency going forward. Command used: $ git ls-files | xargs file | grep CRLF | cut -d":" -f1 | xargs dos2unix Issue: SPR-5608
This commit is contained in:
@@ -1,61 +1,61 @@
|
||||
/*
|
||||
* Copyright 2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.support.NoOpCacheManager;
|
||||
|
||||
public class NoOpCacheManagerTests {
|
||||
|
||||
private CacheManager manager;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
manager = new NoOpCacheManager();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCache() throws Exception {
|
||||
Cache cache = manager.getCache("bucket");
|
||||
assertNotNull(cache);
|
||||
assertSame(cache, manager.getCache("bucket"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoOpCache() throws Exception {
|
||||
String name = UUID.randomUUID().toString();
|
||||
Cache cache = manager.getCache(name);
|
||||
assertEquals(name, cache.getName());
|
||||
Object key = new Object();
|
||||
cache.put(key, new Object());
|
||||
assertNull(cache.get(key));
|
||||
assertNull(cache.getNativeCache());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheName() throws Exception {
|
||||
String name = "bucket";
|
||||
assertFalse(manager.getCacheNames().contains(name));
|
||||
manager.getCache(name);
|
||||
assertTrue(manager.getCacheNames().contains(name));
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.support.NoOpCacheManager;
|
||||
|
||||
public class NoOpCacheManagerTests {
|
||||
|
||||
private CacheManager manager;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
manager = new NoOpCacheManager();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCache() throws Exception {
|
||||
Cache cache = manager.getCache("bucket");
|
||||
assertNotNull(cache);
|
||||
assertSame(cache, manager.getCache("bucket"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoOpCache() throws Exception {
|
||||
String name = UUID.randomUUID().toString();
|
||||
Cache cache = manager.getCache(name);
|
||||
assertEquals(name, cache.getName());
|
||||
Object key = new Object();
|
||||
cache.put(key, new Object());
|
||||
assertNull(cache.get(key));
|
||||
assertNull(cache.getNativeCache());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheName() throws Exception {
|
||||
String name = "bucket";
|
||||
assertFalse(manager.getCacheNames().contains(name));
|
||||
manager.getCache(name);
|
||||
assertTrue(manager.getCacheNames().contains(name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +1,39 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.concurrent;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.vendor.AbstractNativeCacheTests;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class ConcurrentCacheTests extends AbstractNativeCacheTests<ConcurrentMap<Object, Object>> {
|
||||
|
||||
@Override
|
||||
protected Cache createCache(ConcurrentMap<Object, Object> nativeCache) {
|
||||
return new ConcurrentMapCache(CACHE_NAME, nativeCache, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConcurrentMap<Object, Object> createNativeCache() throws Exception {
|
||||
return new ConcurrentHashMap<Object, Object>();
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.concurrent;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.vendor.AbstractNativeCacheTests;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class ConcurrentCacheTests extends AbstractNativeCacheTests<ConcurrentMap<Object, Object>> {
|
||||
|
||||
@Override
|
||||
protected Cache createCache(ConcurrentMap<Object, Object> nativeCache) {
|
||||
return new ConcurrentMapCache(CACHE_NAME, nativeCache, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConcurrentMap<Object, Object> createNativeCache() throws Exception {
|
||||
return new ConcurrentHashMap<Object, Object>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,63 +1,63 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.concurrent;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ConcurrentMapCacheManagerTests {
|
||||
|
||||
@Test
|
||||
public void testDynamicMode() {
|
||||
CacheManager cm = new ConcurrentMapCacheManager();
|
||||
Cache cache1 = cm.getCache("c1");
|
||||
assertTrue(cache1 instanceof ConcurrentMapCache);
|
||||
Cache cache1again = cm.getCache("c1");
|
||||
assertSame(cache1again, cache1);
|
||||
Cache cache2 = cm.getCache("c2");
|
||||
assertTrue(cache2 instanceof ConcurrentMapCache);
|
||||
Cache cache2again = cm.getCache("c2");
|
||||
assertSame(cache2again, cache2);
|
||||
Cache cache3 = cm.getCache("c3");
|
||||
assertTrue(cache3 instanceof ConcurrentMapCache);
|
||||
Cache cache3again = cm.getCache("c3");
|
||||
assertSame(cache3again, cache3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticMode() {
|
||||
ConcurrentMapCacheManager cm = new ConcurrentMapCacheManager("c1", "c2");
|
||||
Cache cache1 = cm.getCache("c1");
|
||||
assertTrue(cache1 instanceof ConcurrentMapCache);
|
||||
Cache cache1again = cm.getCache("c1");
|
||||
assertSame(cache1again, cache1);
|
||||
Cache cache2 = cm.getCache("c2");
|
||||
assertTrue(cache2 instanceof ConcurrentMapCache);
|
||||
Cache cache2again = cm.getCache("c2");
|
||||
assertSame(cache2again, cache2);
|
||||
Cache cache3 = cm.getCache("c3");
|
||||
assertNull(cache3);
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.concurrent;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ConcurrentMapCacheManagerTests {
|
||||
|
||||
@Test
|
||||
public void testDynamicMode() {
|
||||
CacheManager cm = new ConcurrentMapCacheManager();
|
||||
Cache cache1 = cm.getCache("c1");
|
||||
assertTrue(cache1 instanceof ConcurrentMapCache);
|
||||
Cache cache1again = cm.getCache("c1");
|
||||
assertSame(cache1again, cache1);
|
||||
Cache cache2 = cm.getCache("c2");
|
||||
assertTrue(cache2 instanceof ConcurrentMapCache);
|
||||
Cache cache2again = cm.getCache("c2");
|
||||
assertSame(cache2again, cache2);
|
||||
Cache cache3 = cm.getCache("c3");
|
||||
assertTrue(cache3 instanceof ConcurrentMapCache);
|
||||
Cache cache3again = cm.getCache("c3");
|
||||
assertSame(cache3again, cache3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticMode() {
|
||||
ConcurrentMapCacheManager cm = new ConcurrentMapCacheManager("c1", "c2");
|
||||
Cache cache1 = cm.getCache("c1");
|
||||
assertTrue(cache1 instanceof ConcurrentMapCache);
|
||||
Cache cache1again = cm.getCache("c1");
|
||||
assertSame(cache1again, cache1);
|
||||
Cache cache2 = cm.getCache("c2");
|
||||
assertTrue(cache2 instanceof ConcurrentMapCache);
|
||||
Cache cache2again = cm.getCache("c2");
|
||||
assertSame(cache2again, cache2);
|
||||
Cache cache3 = cm.getCache("c3");
|
||||
assertNull(cache3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,138 +1,138 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@Cacheable("default")
|
||||
public class AnnotatedClassCacheableService implements CacheableService<Object> {
|
||||
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
public static final AtomicLong nullInvocations = new AtomicLong();
|
||||
|
||||
public Object cache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
public Object conditional(int field) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void invalidate(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void evictWithException(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should NOT occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", allEntries = true)
|
||||
public void evictAll(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", beforeInvocation = true)
|
||||
public void evictEarly(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0")
|
||||
public void evict(Object arg1, Object arg2) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0", beforeInvocation = true)
|
||||
public void invalidateEarly(Object arg1, Object arg2) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#p0")
|
||||
public Object key(Object arg1, Object arg2) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#root.methodName + #root.caches[0].name")
|
||||
public Object name(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
|
||||
public Object rootVars(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CachePut("default")
|
||||
public Object update(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CachePut(value = "default", condition = "#arg.equals(3)")
|
||||
public Object conditionalUpdate(Object arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
public Object nullValue(Object arg1) {
|
||||
nullInvocations.incrementAndGet();
|
||||
return null;
|
||||
}
|
||||
|
||||
public Number nullInvocations() {
|
||||
return nullInvocations.get();
|
||||
}
|
||||
|
||||
public Long throwChecked(Object arg1) throws Exception {
|
||||
throw new UnsupportedOperationException(arg1.toString());
|
||||
}
|
||||
|
||||
public Long throwUnchecked(Object arg1) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
// multi annotations
|
||||
|
||||
@Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
|
||||
public Object multiCache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0"), @CacheEvict(value = "primary", key = "#p0 + 'A'") })
|
||||
public Object multiEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(cacheable = { @Cacheable(value = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
|
||||
public Object multiCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(cacheable = { @Cacheable(value = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
|
||||
public Object multiConditionalCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(put = { @CachePut("primary"), @CachePut("secondary") })
|
||||
public Object multiUpdate(Object arg1) {
|
||||
return arg1;
|
||||
}
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@Cacheable("default")
|
||||
public class AnnotatedClassCacheableService implements CacheableService<Object> {
|
||||
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
public static final AtomicLong nullInvocations = new AtomicLong();
|
||||
|
||||
public Object cache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
public Object conditional(int field) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void invalidate(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void evictWithException(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should NOT occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", allEntries = true)
|
||||
public void evictAll(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", beforeInvocation = true)
|
||||
public void evictEarly(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0")
|
||||
public void evict(Object arg1, Object arg2) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0", beforeInvocation = true)
|
||||
public void invalidateEarly(Object arg1, Object arg2) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#p0")
|
||||
public Object key(Object arg1, Object arg2) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#root.methodName + #root.caches[0].name")
|
||||
public Object name(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
|
||||
public Object rootVars(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CachePut("default")
|
||||
public Object update(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CachePut(value = "default", condition = "#arg.equals(3)")
|
||||
public Object conditionalUpdate(Object arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
public Object nullValue(Object arg1) {
|
||||
nullInvocations.incrementAndGet();
|
||||
return null;
|
||||
}
|
||||
|
||||
public Number nullInvocations() {
|
||||
return nullInvocations.get();
|
||||
}
|
||||
|
||||
public Long throwChecked(Object arg1) throws Exception {
|
||||
throw new UnsupportedOperationException(arg1.toString());
|
||||
}
|
||||
|
||||
public Long throwUnchecked(Object arg1) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
// multi annotations
|
||||
|
||||
@Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
|
||||
public Object multiCache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0"), @CacheEvict(value = "primary", key = "#p0 + 'A'") })
|
||||
public Object multiEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(cacheable = { @Cacheable(value = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
|
||||
public Object multiCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(cacheable = { @Cacheable(value = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
|
||||
public Object multiConditionalCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(put = { @CachePut("primary"), @CachePut("secondary") })
|
||||
public Object multiUpdate(Object arg1) {
|
||||
return arg1;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +1,44 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.interceptor.CacheInterceptor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class AnnotationNamespaceDrivenTests extends AbstractAnnotationTests {
|
||||
|
||||
@Override
|
||||
protected ApplicationContext getApplicationContext() {
|
||||
return new GenericXmlApplicationContext(
|
||||
"/org/springframework/cache/config/annotationDrivenCacheNamespace.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyStrategy() throws Exception {
|
||||
CacheInterceptor ci = ctx.getBean("org.springframework.cache.interceptor.CacheInterceptor#0",
|
||||
CacheInterceptor.class);
|
||||
Assert.assertSame(ctx.getBean("keyGenerator"), ci.getKeyGenerator());
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.interceptor.CacheInterceptor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class AnnotationNamespaceDrivenTests extends AbstractAnnotationTests {
|
||||
|
||||
@Override
|
||||
protected ApplicationContext getApplicationContext() {
|
||||
return new GenericXmlApplicationContext(
|
||||
"/org/springframework/cache/config/annotationDrivenCacheNamespace.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyStrategy() throws Exception {
|
||||
CacheInterceptor ci = ctx.getBean("org.springframework.cache.interceptor.CacheInterceptor#0",
|
||||
CacheInterceptor.class);
|
||||
Assert.assertSame(ctx.getBean("keyGenerator"), ci.getKeyGenerator());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class AnnotationTests extends AbstractAnnotationTests {
|
||||
|
||||
@Override
|
||||
protected ApplicationContext getApplicationContext() {
|
||||
return new GenericXmlApplicationContext(
|
||||
"/org/springframework/cache/config/annotationDrivenCacheConfig.xml");
|
||||
}
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class AnnotationTests extends AbstractAnnotationTests {
|
||||
|
||||
@Override
|
||||
protected ApplicationContext getApplicationContext() {
|
||||
return new GenericXmlApplicationContext(
|
||||
"/org/springframework/cache/config/annotationDrivenCacheConfig.xml");
|
||||
}
|
||||
}
|
||||
@@ -1,42 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.interceptor.CacheInterceptor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class CacheAdviceNamespaceTests extends AbstractAnnotationTests {
|
||||
|
||||
@Override
|
||||
protected ApplicationContext getApplicationContext() {
|
||||
return new GenericXmlApplicationContext(
|
||||
"/org/springframework/cache/config/cache-advice.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyStrategy() throws Exception {
|
||||
CacheInterceptor bean = ctx.getBean("cacheAdviceClass", CacheInterceptor.class);
|
||||
Assert.assertSame(ctx.getBean("keyGenerator"), bean.getKeyGenerator());
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.interceptor.CacheInterceptor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class CacheAdviceNamespaceTests extends AbstractAnnotationTests {
|
||||
|
||||
@Override
|
||||
protected ApplicationContext getApplicationContext() {
|
||||
return new GenericXmlApplicationContext(
|
||||
"/org/springframework/cache/config/cache-advice.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyStrategy() throws Exception {
|
||||
CacheInterceptor bean = ctx.getBean("cacheAdviceClass", CacheInterceptor.class);
|
||||
Assert.assertSame(ctx.getBean("keyGenerator"), bean.getKeyGenerator());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,70 +1,70 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
/**
|
||||
* Basic service interface.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public interface CacheableService<T> {
|
||||
|
||||
T cache(Object arg1);
|
||||
|
||||
void invalidate(Object arg1);
|
||||
|
||||
void evictEarly(Object arg1);
|
||||
|
||||
void evictAll(Object arg1);
|
||||
|
||||
void evictWithException(Object arg1);
|
||||
|
||||
void evict(Object arg1, Object arg2);
|
||||
|
||||
void invalidateEarly(Object arg1, Object arg2);
|
||||
|
||||
T conditional(int field);
|
||||
|
||||
T key(Object arg1, Object arg2);
|
||||
|
||||
T name(Object arg1);
|
||||
|
||||
T nullValue(Object arg1);
|
||||
|
||||
T update(Object arg1);
|
||||
|
||||
T conditionalUpdate(Object arg2);
|
||||
|
||||
Number nullInvocations();
|
||||
|
||||
T rootVars(Object arg1);
|
||||
|
||||
T throwChecked(Object arg1) throws Exception;
|
||||
|
||||
T throwUnchecked(Object arg1);
|
||||
|
||||
// multi annotations
|
||||
T multiCache(Object arg1);
|
||||
|
||||
T multiEvict(Object arg1);
|
||||
|
||||
T multiCacheAndEvict(Object arg1);
|
||||
|
||||
T multiConditionalCacheAndEvict(Object arg1);
|
||||
|
||||
T multiUpdate(Object arg1);
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
/**
|
||||
* Basic service interface.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public interface CacheableService<T> {
|
||||
|
||||
T cache(Object arg1);
|
||||
|
||||
void invalidate(Object arg1);
|
||||
|
||||
void evictEarly(Object arg1);
|
||||
|
||||
void evictAll(Object arg1);
|
||||
|
||||
void evictWithException(Object arg1);
|
||||
|
||||
void evict(Object arg1, Object arg2);
|
||||
|
||||
void invalidateEarly(Object arg1, Object arg2);
|
||||
|
||||
T conditional(int field);
|
||||
|
||||
T key(Object arg1, Object arg2);
|
||||
|
||||
T name(Object arg1);
|
||||
|
||||
T nullValue(Object arg1);
|
||||
|
||||
T update(Object arg1);
|
||||
|
||||
T conditionalUpdate(Object arg2);
|
||||
|
||||
Number nullInvocations();
|
||||
|
||||
T rootVars(Object arg1);
|
||||
|
||||
T throwChecked(Object arg1) throws Exception;
|
||||
|
||||
T throwUnchecked(Object arg1);
|
||||
|
||||
// multi annotations
|
||||
T multiCache(Object arg1);
|
||||
|
||||
T multiEvict(Object arg1);
|
||||
|
||||
T multiCacheAndEvict(Object arg1);
|
||||
|
||||
T multiConditionalCacheAndEvict(Object arg1);
|
||||
|
||||
T multiUpdate(Object arg1);
|
||||
}
|
||||
@@ -1,144 +1,144 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
|
||||
/**
|
||||
* Simple cacheable service
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class DefaultCacheableService implements CacheableService<Long> {
|
||||
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
private final AtomicLong nullInvocations = new AtomicLong();
|
||||
|
||||
@Cacheable("default")
|
||||
public Long cache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void invalidate(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void evictWithException(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should NOT occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", allEntries = true)
|
||||
public void evictAll(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", beforeInvocation = true)
|
||||
public void evictEarly(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0")
|
||||
public void evict(Object arg1, Object arg2) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0", beforeInvocation = true)
|
||||
public void invalidateEarly(Object arg1, Object arg2) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", condition = "#classField == 3")
|
||||
public Long conditional(int classField) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#p0")
|
||||
public Long key(Object arg1, Object arg2) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#root.methodName")
|
||||
public Long name(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
|
||||
public Long rootVars(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CachePut("default")
|
||||
public Long update(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CachePut(value = "default", condition = "#arg.equals(3)")
|
||||
public Long conditionalUpdate(Object arg) {
|
||||
return Long.valueOf(arg.toString());
|
||||
}
|
||||
|
||||
@Cacheable("default")
|
||||
public Long nullValue(Object arg1) {
|
||||
nullInvocations.incrementAndGet();
|
||||
return null;
|
||||
}
|
||||
|
||||
public Number nullInvocations() {
|
||||
return nullInvocations.get();
|
||||
}
|
||||
|
||||
@Cacheable("default")
|
||||
public Long throwChecked(Object arg1) throws Exception {
|
||||
throw new Exception(arg1.toString());
|
||||
}
|
||||
|
||||
@Cacheable("default")
|
||||
public Long throwUnchecked(Object arg1) {
|
||||
throw new UnsupportedOperationException(arg1.toString());
|
||||
}
|
||||
|
||||
// multi annotations
|
||||
|
||||
@Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
|
||||
public Long multiCache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0"), @CacheEvict(value = "primary", key = "#p0 + 'A'") })
|
||||
public Long multiEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(cacheable = { @Cacheable(value = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
|
||||
public Long multiCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(cacheable = { @Cacheable(value = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
|
||||
public Long multiConditionalCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(put = { @CachePut("primary"), @CachePut("secondary") })
|
||||
public Long multiUpdate(Object arg1) {
|
||||
return Long.valueOf(arg1.toString());
|
||||
}
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
|
||||
/**
|
||||
* Simple cacheable service
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class DefaultCacheableService implements CacheableService<Long> {
|
||||
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
private final AtomicLong nullInvocations = new AtomicLong();
|
||||
|
||||
@Cacheable("default")
|
||||
public Long cache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void invalidate(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void evictWithException(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should NOT occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", allEntries = true)
|
||||
public void evictAll(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", beforeInvocation = true)
|
||||
public void evictEarly(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0")
|
||||
public void evict(Object arg1, Object arg2) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0", beforeInvocation = true)
|
||||
public void invalidateEarly(Object arg1, Object arg2) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", condition = "#classField == 3")
|
||||
public Long conditional(int classField) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#p0")
|
||||
public Long key(Object arg1, Object arg2) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#root.methodName")
|
||||
public Long name(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
|
||||
public Long rootVars(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CachePut("default")
|
||||
public Long update(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CachePut(value = "default", condition = "#arg.equals(3)")
|
||||
public Long conditionalUpdate(Object arg) {
|
||||
return Long.valueOf(arg.toString());
|
||||
}
|
||||
|
||||
@Cacheable("default")
|
||||
public Long nullValue(Object arg1) {
|
||||
nullInvocations.incrementAndGet();
|
||||
return null;
|
||||
}
|
||||
|
||||
public Number nullInvocations() {
|
||||
return nullInvocations.get();
|
||||
}
|
||||
|
||||
@Cacheable("default")
|
||||
public Long throwChecked(Object arg1) throws Exception {
|
||||
throw new Exception(arg1.toString());
|
||||
}
|
||||
|
||||
@Cacheable("default")
|
||||
public Long throwUnchecked(Object arg1) {
|
||||
throw new UnsupportedOperationException(arg1.toString());
|
||||
}
|
||||
|
||||
// multi annotations
|
||||
|
||||
@Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
|
||||
public Long multiCache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0"), @CacheEvict(value = "primary", key = "#p0 + 'A'") })
|
||||
public Long multiEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(cacheable = { @Cacheable(value = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
|
||||
public Long multiCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(cacheable = { @Cacheable(value = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
|
||||
public Long multiConditionalCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Caching(put = { @CachePut("primary"), @CachePut("secondary") })
|
||||
public Long multiUpdate(Object arg1) {
|
||||
return Long.valueOf(arg1.toString());
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,23 @@
|
||||
/*
|
||||
* Copyright 2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import org.springframework.cache.interceptor.DefaultKeyGenerator;
|
||||
|
||||
public class SomeKeyGenerator extends DefaultKeyGenerator {
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import org.springframework.cache.interceptor.DefaultKeyGenerator;
|
||||
|
||||
public class SomeKeyGenerator extends DefaultKeyGenerator {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,62 +1,62 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.ehcache;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import net.sf.ehcache.Ehcache;
|
||||
import net.sf.ehcache.Element;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.vendor.AbstractNativeCacheTests;
|
||||
|
||||
/**
|
||||
* Integration test for EhCache cache.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class EhCacheCacheTests extends AbstractNativeCacheTests<Ehcache> {
|
||||
|
||||
@Override
|
||||
protected Ehcache createNativeCache() throws Exception {
|
||||
EhCacheFactoryBean fb = new EhCacheFactoryBean();
|
||||
fb.setBeanName(CACHE_NAME);
|
||||
fb.setCacheName(CACHE_NAME);
|
||||
fb.afterPropertiesSet();
|
||||
return fb.getObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Cache createCache(Ehcache nativeCache) {
|
||||
return new EhCacheCache(nativeCache);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExpiredElements() throws Exception {
|
||||
String key = "brancusi";
|
||||
String value = "constantin";
|
||||
Element brancusi = new Element(key, value);
|
||||
// ttl = 10s
|
||||
brancusi.setTimeToLive(3);
|
||||
nativeCache.put(brancusi);
|
||||
|
||||
assertEquals(value, cache.get(key).get());
|
||||
// wait for the entry to expire
|
||||
Thread.sleep(5 * 1000);
|
||||
assertNull(cache.get(key));
|
||||
}
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.ehcache;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import net.sf.ehcache.Ehcache;
|
||||
import net.sf.ehcache.Element;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.vendor.AbstractNativeCacheTests;
|
||||
|
||||
/**
|
||||
* Integration test for EhCache cache.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class EhCacheCacheTests extends AbstractNativeCacheTests<Ehcache> {
|
||||
|
||||
@Override
|
||||
protected Ehcache createNativeCache() throws Exception {
|
||||
EhCacheFactoryBean fb = new EhCacheFactoryBean();
|
||||
fb.setBeanName(CACHE_NAME);
|
||||
fb.setCacheName(CACHE_NAME);
|
||||
fb.afterPropertiesSet();
|
||||
return fb.getObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Cache createCache(Ehcache nativeCache) {
|
||||
return new EhCacheCache(nativeCache);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExpiredElements() throws Exception {
|
||||
String key = "brancusi";
|
||||
String value = "constantin";
|
||||
Element brancusi = new Element(key, value);
|
||||
// ttl = 10s
|
||||
brancusi.setTimeToLive(3);
|
||||
nativeCache.put(brancusi);
|
||||
|
||||
assertEquals(value, cache.get(key).get());
|
||||
// wait for the entry to expire
|
||||
Thread.sleep(5 * 1000);
|
||||
assertNull(cache.get(key));
|
||||
}
|
||||
}
|
||||
@@ -1,88 +1,88 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.vendor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.Cache;
|
||||
|
||||
/**
|
||||
* Test for native cache implementations.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class AbstractNativeCacheTests<T> {
|
||||
|
||||
protected T nativeCache;
|
||||
protected Cache cache;
|
||||
protected final static String CACHE_NAME = "testCache";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
nativeCache = createNativeCache();
|
||||
cache = createCache(nativeCache);
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
|
||||
protected abstract T createNativeCache() throws Exception;
|
||||
|
||||
protected abstract Cache createCache(T nativeCache);
|
||||
|
||||
|
||||
@Test
|
||||
public void testCacheName() throws Exception {
|
||||
assertEquals(CACHE_NAME, cache.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNativeCache() throws Exception {
|
||||
assertSame(nativeCache, cache.getNativeCache());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCachePut() throws Exception {
|
||||
Object key = "enescu";
|
||||
Object value = "george";
|
||||
|
||||
assertNull(cache.get(key));
|
||||
cache.put(key, value);
|
||||
assertEquals(value, cache.get(key).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheRemove() throws Exception {
|
||||
Object key = "enescu";
|
||||
Object value = "george";
|
||||
|
||||
assertNull(cache.get(key));
|
||||
cache.put(key, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheClear() throws Exception {
|
||||
assertNull(cache.get("enescu"));
|
||||
cache.put("enescu", "george");
|
||||
assertNull(cache.get("vlaicu"));
|
||||
cache.put("vlaicu", "aurel");
|
||||
cache.clear();
|
||||
assertNull(cache.get("vlaicu"));
|
||||
assertNull(cache.get("enescu"));
|
||||
}
|
||||
/*
|
||||
* Copyright 2010-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.vendor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.Cache;
|
||||
|
||||
/**
|
||||
* Test for native cache implementations.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class AbstractNativeCacheTests<T> {
|
||||
|
||||
protected T nativeCache;
|
||||
protected Cache cache;
|
||||
protected final static String CACHE_NAME = "testCache";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
nativeCache = createNativeCache();
|
||||
cache = createCache(nativeCache);
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
|
||||
protected abstract T createNativeCache() throws Exception;
|
||||
|
||||
protected abstract Cache createCache(T nativeCache);
|
||||
|
||||
|
||||
@Test
|
||||
public void testCacheName() throws Exception {
|
||||
assertEquals(CACHE_NAME, cache.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNativeCache() throws Exception {
|
||||
assertSame(nativeCache, cache.getNativeCache());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCachePut() throws Exception {
|
||||
Object key = "enescu";
|
||||
Object value = "george";
|
||||
|
||||
assertNull(cache.get(key));
|
||||
cache.put(key, value);
|
||||
assertEquals(value, cache.get(key).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheRemove() throws Exception {
|
||||
Object key = "enescu";
|
||||
Object value = "george";
|
||||
|
||||
assertNull(cache.get(key));
|
||||
cache.put(key, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheClear() throws Exception {
|
||||
assertNull(cache.get("enescu"));
|
||||
cache.put("enescu", "george");
|
||||
assertNull(cache.get("vlaicu"));
|
||||
cache.put("vlaicu", "aurel");
|
||||
cache.clear();
|
||||
assertNull(cache.get("vlaicu"));
|
||||
assertNull(cache.get("enescu"));
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
|
||||
@Target({ElementType.METHOD, ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Qualifier
|
||||
public @interface BeanAge {
|
||||
int value();
|
||||
}
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
|
||||
@Target({ElementType.METHOD, ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Qualifier
|
||||
public @interface BeanAge {
|
||||
int value();
|
||||
}
|
||||
|
||||
@@ -1,95 +1,95 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.aop.scope.ScopedObject;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.config.SimpleMapScope;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation4.DependencyBean;
|
||||
import org.springframework.context.annotation4.FactoryMethodComponent;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Mark Pollack
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ClassPathFactoryBeanDefinitionScannerTests extends TestCase {
|
||||
|
||||
private static final String BASE_PACKAGE = FactoryMethodComponent.class.getPackage().getName();
|
||||
|
||||
|
||||
public void testSingletonScopedFactoryMethod() {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
|
||||
|
||||
context.getBeanFactory().registerScope("request", new SimpleMapScope());
|
||||
|
||||
scanner.scan(BASE_PACKAGE);
|
||||
context.registerBeanDefinition("clientBean", new RootBeanDefinition(QualifiedClientBean.class));
|
||||
context.refresh();
|
||||
|
||||
FactoryMethodComponent fmc = context.getBean("factoryMethodComponent", FactoryMethodComponent.class);
|
||||
assertFalse(fmc.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR));
|
||||
|
||||
TestBean tb = (TestBean)context.getBean("publicInstance"); //2
|
||||
assertEquals("publicInstance", tb.getName());
|
||||
TestBean tb2 = (TestBean)context.getBean("publicInstance"); //2
|
||||
assertEquals("publicInstance", tb2.getName());
|
||||
assertSame(tb2, tb);
|
||||
|
||||
tb = (TestBean)context.getBean("protectedInstance"); //3
|
||||
assertEquals("protectedInstance", tb.getName());
|
||||
assertSame(tb, context.getBean("protectedInstance"));
|
||||
assertEquals("0", tb.getCountry());
|
||||
tb2 = context.getBean("protectedInstance", TestBean.class); //3
|
||||
assertEquals("protectedInstance", tb2.getName());
|
||||
assertSame(tb2, tb);
|
||||
|
||||
tb = context.getBean("privateInstance", TestBean.class); //4
|
||||
assertEquals("privateInstance", tb.getName());
|
||||
assertEquals(1, tb.getAge());
|
||||
tb2 = context.getBean("privateInstance", TestBean.class); //4
|
||||
assertEquals(2, tb2.getAge());
|
||||
assertNotSame(tb2, tb);
|
||||
|
||||
Object bean = context.getBean("requestScopedInstance"); //5
|
||||
assertTrue(AopUtils.isCglibProxy(bean));
|
||||
assertTrue(bean instanceof ScopedObject);
|
||||
|
||||
QualifiedClientBean clientBean = context.getBean("clientBean", QualifiedClientBean.class);
|
||||
assertSame(clientBean.testBean, context.getBean("publicInstance"));
|
||||
assertSame(clientBean.dependencyBean, context.getBean("dependencyBean"));
|
||||
}
|
||||
|
||||
|
||||
public static class QualifiedClientBean {
|
||||
|
||||
@Autowired @Qualifier("public")
|
||||
public TestBean testBean;
|
||||
|
||||
@Autowired
|
||||
public DependencyBean dependencyBean;
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.aop.scope.ScopedObject;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.config.SimpleMapScope;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation4.DependencyBean;
|
||||
import org.springframework.context.annotation4.FactoryMethodComponent;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Mark Pollack
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ClassPathFactoryBeanDefinitionScannerTests extends TestCase {
|
||||
|
||||
private static final String BASE_PACKAGE = FactoryMethodComponent.class.getPackage().getName();
|
||||
|
||||
|
||||
public void testSingletonScopedFactoryMethod() {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
|
||||
|
||||
context.getBeanFactory().registerScope("request", new SimpleMapScope());
|
||||
|
||||
scanner.scan(BASE_PACKAGE);
|
||||
context.registerBeanDefinition("clientBean", new RootBeanDefinition(QualifiedClientBean.class));
|
||||
context.refresh();
|
||||
|
||||
FactoryMethodComponent fmc = context.getBean("factoryMethodComponent", FactoryMethodComponent.class);
|
||||
assertFalse(fmc.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR));
|
||||
|
||||
TestBean tb = (TestBean)context.getBean("publicInstance"); //2
|
||||
assertEquals("publicInstance", tb.getName());
|
||||
TestBean tb2 = (TestBean)context.getBean("publicInstance"); //2
|
||||
assertEquals("publicInstance", tb2.getName());
|
||||
assertSame(tb2, tb);
|
||||
|
||||
tb = (TestBean)context.getBean("protectedInstance"); //3
|
||||
assertEquals("protectedInstance", tb.getName());
|
||||
assertSame(tb, context.getBean("protectedInstance"));
|
||||
assertEquals("0", tb.getCountry());
|
||||
tb2 = context.getBean("protectedInstance", TestBean.class); //3
|
||||
assertEquals("protectedInstance", tb2.getName());
|
||||
assertSame(tb2, tb);
|
||||
|
||||
tb = context.getBean("privateInstance", TestBean.class); //4
|
||||
assertEquals("privateInstance", tb.getName());
|
||||
assertEquals(1, tb.getAge());
|
||||
tb2 = context.getBean("privateInstance", TestBean.class); //4
|
||||
assertEquals(2, tb2.getAge());
|
||||
assertNotSame(tb2, tb);
|
||||
|
||||
Object bean = context.getBean("requestScopedInstance"); //5
|
||||
assertTrue(AopUtils.isCglibProxy(bean));
|
||||
assertTrue(bean instanceof ScopedObject);
|
||||
|
||||
QualifiedClientBean clientBean = context.getBean("clientBean", QualifiedClientBean.class);
|
||||
assertSame(clientBean.testBean, context.getBean("publicInstance"));
|
||||
assertSame(clientBean.dependencyBean, context.getBean("dependencyBean"));
|
||||
}
|
||||
|
||||
|
||||
public static class QualifiedClientBean {
|
||||
|
||||
@Autowired @Qualifier("public")
|
||||
public TestBean testBean;
|
||||
|
||||
@Autowired
|
||||
public DependencyBean dependencyBean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import example.scannable.FooService;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class FooServiceDependentConverter implements Converter<String, org.springframework.beans.TestBean> {
|
||||
|
||||
private FooService fooService;
|
||||
|
||||
public void setFooService(FooService fooService) {
|
||||
this.fooService = fooService;
|
||||
}
|
||||
|
||||
public org.springframework.beans.TestBean convert(String source) {
|
||||
return new org.springframework.beans.TestBean(source);
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import example.scannable.FooService;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class FooServiceDependentConverter implements Converter<String, org.springframework.beans.TestBean> {
|
||||
|
||||
private FooService fooService;
|
||||
|
||||
public void setFooService(FooService fooService) {
|
||||
this.fooService = fooService;
|
||||
}
|
||||
|
||||
public org.springframework.beans.TestBean convert(String source) {
|
||||
return new org.springframework.beans.TestBean(source);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,63 +1,63 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation.jsr330;
|
||||
|
||||
import junit.framework.Test;
|
||||
import org.atinject.tck.Tck;
|
||||
import org.atinject.tck.auto.Car;
|
||||
import org.atinject.tck.auto.Convertible;
|
||||
import org.atinject.tck.auto.Drivers;
|
||||
import org.atinject.tck.auto.DriversSeat;
|
||||
import org.atinject.tck.auto.FuelTank;
|
||||
import org.atinject.tck.auto.Seat;
|
||||
import org.atinject.tck.auto.Tire;
|
||||
import org.atinject.tck.auto.V8Engine;
|
||||
import org.atinject.tck.auto.accessories.Cupholder;
|
||||
import org.atinject.tck.auto.accessories.SpareTire;
|
||||
|
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.context.annotation.Jsr330ScopeMetadataResolver;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class SpringAtInjectTck {
|
||||
|
||||
public static Test suite() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotatedBeanDefinitionReader bdr = new AnnotatedBeanDefinitionReader(ac);
|
||||
bdr.setScopeMetadataResolver(new Jsr330ScopeMetadataResolver());
|
||||
|
||||
bdr.registerBean(Convertible.class);
|
||||
bdr.registerBean(DriversSeat.class, Drivers.class);
|
||||
bdr.registerBean(Seat.class, Primary.class);
|
||||
bdr.registerBean(V8Engine.class);
|
||||
bdr.registerBean(SpareTire.class, "spare");
|
||||
bdr.registerBean(Cupholder.class);
|
||||
bdr.registerBean(Tire.class, Primary.class);
|
||||
bdr.registerBean(FuelTank.class);
|
||||
|
||||
ac.refresh();
|
||||
Car car = ac.getBean(Car.class);
|
||||
|
||||
return Tck.testsFor(car, false, true);
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation.jsr330;
|
||||
|
||||
import junit.framework.Test;
|
||||
import org.atinject.tck.Tck;
|
||||
import org.atinject.tck.auto.Car;
|
||||
import org.atinject.tck.auto.Convertible;
|
||||
import org.atinject.tck.auto.Drivers;
|
||||
import org.atinject.tck.auto.DriversSeat;
|
||||
import org.atinject.tck.auto.FuelTank;
|
||||
import org.atinject.tck.auto.Seat;
|
||||
import org.atinject.tck.auto.Tire;
|
||||
import org.atinject.tck.auto.V8Engine;
|
||||
import org.atinject.tck.auto.accessories.Cupholder;
|
||||
import org.atinject.tck.auto.accessories.SpareTire;
|
||||
|
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.context.annotation.Jsr330ScopeMetadataResolver;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class SpringAtInjectTck {
|
||||
|
||||
public static Test suite() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotatedBeanDefinitionReader bdr = new AnnotatedBeanDefinitionReader(ac);
|
||||
bdr.setScopeMetadataResolver(new Jsr330ScopeMetadataResolver());
|
||||
|
||||
bdr.registerBean(Convertible.class);
|
||||
bdr.registerBean(DriversSeat.class, Drivers.class);
|
||||
bdr.registerBean(Seat.class, Primary.class);
|
||||
bdr.registerBean(V8Engine.class);
|
||||
bdr.registerBean(SpareTire.class, "spare");
|
||||
bdr.registerBean(Cupholder.class);
|
||||
bdr.registerBean(Tire.class, Primary.class);
|
||||
bdr.registerBean(FuelTank.class);
|
||||
|
||||
ac.refresh();
|
||||
Car car = ac.getBean(Car.class);
|
||||
|
||||
return Tck.testsFor(car, false, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation4;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@Component
|
||||
public class DependencyBean {
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation4;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@Component
|
||||
public class DependencyBean {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,77 +1,77 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation4;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.BeanAge;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Class used to test the functionality of factory method bean definitions
|
||||
* declared inside a Spring component class.
|
||||
*
|
||||
* @author Mark Pollack
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@Component
|
||||
public final class FactoryMethodComponent {
|
||||
|
||||
private int i;
|
||||
|
||||
public static TestBean nullInstance() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Bean @Qualifier("public")
|
||||
public TestBean publicInstance() {
|
||||
return new TestBean("publicInstance");
|
||||
}
|
||||
|
||||
// to be ignored
|
||||
public TestBean publicInstance(boolean doIt) {
|
||||
return new TestBean("publicInstance");
|
||||
}
|
||||
|
||||
@Bean @BeanAge(1)
|
||||
protected TestBean protectedInstance(@Qualifier("public") TestBean spouse, @Value("#{privateInstance.age}") String country) {
|
||||
TestBean tb = new TestBean("protectedInstance", 1);
|
||||
tb.setSpouse(tb);
|
||||
tb.setCountry(country);
|
||||
return tb;
|
||||
}
|
||||
|
||||
@Bean @Scope("prototype")
|
||||
private TestBean privateInstance() {
|
||||
return new TestBean("privateInstance", i++);
|
||||
}
|
||||
|
||||
@Bean @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
public TestBean requestScopedInstance() {
|
||||
return new TestBean("requestScopedInstance", 3);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DependencyBean secondInstance() {
|
||||
return new DependencyBean();
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation4;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.BeanAge;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Class used to test the functionality of factory method bean definitions
|
||||
* declared inside a Spring component class.
|
||||
*
|
||||
* @author Mark Pollack
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@Component
|
||||
public final class FactoryMethodComponent {
|
||||
|
||||
private int i;
|
||||
|
||||
public static TestBean nullInstance() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Bean @Qualifier("public")
|
||||
public TestBean publicInstance() {
|
||||
return new TestBean("publicInstance");
|
||||
}
|
||||
|
||||
// to be ignored
|
||||
public TestBean publicInstance(boolean doIt) {
|
||||
return new TestBean("publicInstance");
|
||||
}
|
||||
|
||||
@Bean @BeanAge(1)
|
||||
protected TestBean protectedInstance(@Qualifier("public") TestBean spouse, @Value("#{privateInstance.age}") String country) {
|
||||
TestBean tb = new TestBean("protectedInstance", 1);
|
||||
tb.setSpouse(tb);
|
||||
tb.setCountry(country);
|
||||
return tb;
|
||||
}
|
||||
|
||||
@Bean @Scope("prototype")
|
||||
private TestBean privateInstance() {
|
||||
return new TestBean("privateInstance", i++);
|
||||
}
|
||||
|
||||
@Bean @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
public TestBean requestScopedInstance() {
|
||||
return new TestBean("requestScopedInstance", 3);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DependencyBean secondInstance() {
|
||||
return new DependencyBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation4;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Class to test that @FactoryMethods are detected only when inside a class with an @Component
|
||||
* class annotation.
|
||||
*
|
||||
* @author Mark Pollack
|
||||
*/
|
||||
public class SimpleBean {
|
||||
|
||||
// This should *not* recognized as a bean since it does not reside inside an @Component
|
||||
@Bean
|
||||
public TestBean getPublicInstance() {
|
||||
return new TestBean("publicInstance");
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation4;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Class to test that @FactoryMethods are detected only when inside a class with an @Component
|
||||
* class annotation.
|
||||
*
|
||||
* @author Mark Pollack
|
||||
*/
|
||||
public class SimpleBean {
|
||||
|
||||
// This should *not* recognized as a bean since it does not reside inside an @Component
|
||||
@Bean
|
||||
public TestBean getPublicInstance() {
|
||||
return new TestBean("publicInstance");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation5;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Repository
|
||||
@Primary
|
||||
@Lazy
|
||||
public @interface MyRepository {
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation5;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Repository
|
||||
@Primary
|
||||
@Lazy
|
||||
public @interface MyRepository {
|
||||
}
|
||||
|
||||
@@ -1,432 +1,432 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.expression;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.security.AccessControlException;
|
||||
import java.security.Permission;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||
import org.springframework.beans.factory.config.Scope;
|
||||
import org.springframework.beans.factory.config.TypedStringValue;
|
||||
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation.AnnotationConfigUtils;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ApplicationContextExpressionTests {
|
||||
|
||||
private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class);
|
||||
|
||||
@Test
|
||||
public void genericApplicationContext() throws Exception {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
|
||||
|
||||
ac.getBeanFactory().registerScope("myScope", new Scope() {
|
||||
public Object get(String name, ObjectFactory objectFactory) {
|
||||
return objectFactory.getObject();
|
||||
}
|
||||
public Object remove(String name) {
|
||||
return null;
|
||||
}
|
||||
public void registerDestructionCallback(String name, Runnable callback) {
|
||||
}
|
||||
public Object resolveContextualObject(String key) {
|
||||
if (key.equals("mySpecialAttr")) {
|
||||
return "42";
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public String getConversationId() {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
||||
Properties placeholders = new Properties();
|
||||
placeholders.setProperty("code", "123");
|
||||
ppc.setProperties(placeholders);
|
||||
ac.addBeanFactoryPostProcessor(ppc);
|
||||
|
||||
GenericBeanDefinition bd0 = new GenericBeanDefinition();
|
||||
bd0.setBeanClass(TestBean.class);
|
||||
bd0.getPropertyValues().add("name", "myName");
|
||||
bd0.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "original"));
|
||||
ac.registerBeanDefinition("tb0", bd0);
|
||||
|
||||
GenericBeanDefinition bd1 = new GenericBeanDefinition();
|
||||
bd1.setBeanClass(TestBean.class);
|
||||
bd1.setScope("myScope");
|
||||
bd1.getConstructorArgumentValues().addGenericArgumentValue("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ");
|
||||
bd1.getConstructorArgumentValues().addGenericArgumentValue("#{mySpecialAttr}");
|
||||
ac.registerBeanDefinition("tb1", bd1);
|
||||
|
||||
GenericBeanDefinition bd2 = new GenericBeanDefinition();
|
||||
bd2.setBeanClass(TestBean.class);
|
||||
bd2.setScope("myScope");
|
||||
bd2.getPropertyValues().add("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }");
|
||||
bd2.getPropertyValues().add("age", "#{mySpecialAttr}");
|
||||
bd2.getPropertyValues().add("country", "${code} #{systemProperties.country}");
|
||||
ac.registerBeanDefinition("tb2", bd2);
|
||||
|
||||
GenericBeanDefinition bd3 = new GenericBeanDefinition();
|
||||
bd3.setBeanClass(ValueTestBean.class);
|
||||
bd3.setScope("myScope");
|
||||
ac.registerBeanDefinition("tb3", bd3);
|
||||
|
||||
GenericBeanDefinition bd4 = new GenericBeanDefinition();
|
||||
bd4.setBeanClass(ConstructorValueTestBean.class);
|
||||
bd4.setScope("myScope");
|
||||
ac.registerBeanDefinition("tb4", bd4);
|
||||
|
||||
GenericBeanDefinition bd5 = new GenericBeanDefinition();
|
||||
bd5.setBeanClass(MethodValueTestBean.class);
|
||||
bd5.setScope("myScope");
|
||||
ac.registerBeanDefinition("tb5", bd5);
|
||||
|
||||
GenericBeanDefinition bd6 = new GenericBeanDefinition();
|
||||
bd6.setBeanClass(PropertyValueTestBean.class);
|
||||
bd6.setScope("myScope");
|
||||
ac.registerBeanDefinition("tb6", bd6);
|
||||
|
||||
System.getProperties().put("country", "UK");
|
||||
try {
|
||||
ac.refresh();
|
||||
|
||||
TestBean tb0 = ac.getBean("tb0", TestBean.class);
|
||||
|
||||
TestBean tb1 = ac.getBean("tb1", TestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb1.getName());
|
||||
assertEquals(42, tb1.getAge());
|
||||
|
||||
TestBean tb2 = ac.getBean("tb2", TestBean.class);
|
||||
assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName());
|
||||
assertEquals(42, tb2.getAge());
|
||||
assertEquals("123 UK", tb2.getCountry());
|
||||
|
||||
ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb3.name);
|
||||
assertEquals(42, tb3.age);
|
||||
assertEquals("123 UK", tb3.country);
|
||||
assertEquals("123 UK", tb3.countryFactory.getObject());
|
||||
System.getProperties().put("country", "US");
|
||||
assertEquals("123 UK", tb3.country);
|
||||
assertEquals("123 US", tb3.countryFactory.getObject());
|
||||
System.getProperties().put("country", "UK");
|
||||
assertEquals("123 UK", tb3.country);
|
||||
assertEquals("123 UK", tb3.countryFactory.getObject());
|
||||
assertSame(tb0, tb3.tb);
|
||||
|
||||
tb3 = (ValueTestBean) SerializationTestUtils.serializeAndDeserialize(tb3);
|
||||
assertEquals("123 UK", tb3.countryFactory.getObject());
|
||||
|
||||
ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb4.name);
|
||||
assertEquals(42, tb4.age);
|
||||
assertEquals("123 UK", tb4.country);
|
||||
assertSame(tb0, tb4.tb);
|
||||
|
||||
MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb5.name);
|
||||
assertEquals(42, tb5.age);
|
||||
assertEquals("123 UK", tb5.country);
|
||||
assertSame(tb0, tb5.tb);
|
||||
|
||||
PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb6.name);
|
||||
assertEquals(42, tb6.age);
|
||||
assertEquals("123 UK", tb6.country);
|
||||
assertSame(tb0, tb6.tb);
|
||||
}
|
||||
finally {
|
||||
System.getProperties().remove("country");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prototypeCreationReevaluatesExpressions() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
|
||||
RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class);
|
||||
rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
rbd.getPropertyValues().add("country", "#{systemProperties.country}");
|
||||
rbd.getPropertyValues().add("country2", new TypedStringValue("#{systemProperties.country}"));
|
||||
ac.registerBeanDefinition("test", rbd);
|
||||
ac.refresh();
|
||||
|
||||
try {
|
||||
System.getProperties().put("name", "juergen1");
|
||||
System.getProperties().put("country", "UK1");
|
||||
PrototypeTestBean tb = (PrototypeTestBean) ac.getBean("test");
|
||||
assertEquals("juergen1", tb.getName());
|
||||
assertEquals("UK1", tb.getCountry());
|
||||
assertEquals("UK1", tb.getCountry2());
|
||||
|
||||
System.getProperties().put("name", "juergen2");
|
||||
System.getProperties().put("country", "UK2");
|
||||
tb = (PrototypeTestBean) ac.getBean("test");
|
||||
assertEquals("juergen2", tb.getName());
|
||||
assertEquals("UK2", tb.getCountry());
|
||||
assertEquals("UK2", tb.getCountry2());
|
||||
}
|
||||
finally {
|
||||
System.getProperties().remove("name");
|
||||
System.getProperties().remove("country");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prototypeCreationIsFastEnough() {
|
||||
if (factoryLog.isTraceEnabled() || factoryLog.isDebugEnabled()) {
|
||||
// Skip this test: Trace logging blows the time limit.
|
||||
return;
|
||||
}
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
|
||||
rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
rbd.getConstructorArgumentValues().addGenericArgumentValue("#{systemProperties.name}");
|
||||
rbd.getPropertyValues().add("country", "#{systemProperties.country}");
|
||||
ac.registerBeanDefinition("test", rbd);
|
||||
ac.refresh();
|
||||
StopWatch sw = new StopWatch();
|
||||
sw.start("prototype");
|
||||
System.getProperties().put("name", "juergen");
|
||||
System.getProperties().put("country", "UK");
|
||||
try {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
TestBean tb = (TestBean) ac.getBean("test");
|
||||
assertEquals("juergen", tb.getName());
|
||||
assertEquals("UK", tb.getCountry());
|
||||
}
|
||||
sw.stop();
|
||||
}
|
||||
finally {
|
||||
System.getProperties().remove("country");
|
||||
System.getProperties().remove("name");
|
||||
}
|
||||
assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void systemPropertiesSecurityManager() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
|
||||
|
||||
GenericBeanDefinition bd = new GenericBeanDefinition();
|
||||
bd.setBeanClass(TestBean.class);
|
||||
bd.getPropertyValues().add("country", "#{systemProperties.country}");
|
||||
ac.registerBeanDefinition("tb", bd);
|
||||
|
||||
SecurityManager oldSecurityManager = System.getSecurityManager();
|
||||
try {
|
||||
System.setProperty("country", "NL");
|
||||
|
||||
SecurityManager securityManager = new SecurityManager() {
|
||||
@Override
|
||||
public void checkPropertiesAccess() {
|
||||
throw new AccessControlException("Not Allowed");
|
||||
}
|
||||
@Override
|
||||
public void checkPermission(Permission perm) {
|
||||
// allow everything else
|
||||
}
|
||||
};
|
||||
System.setSecurityManager(securityManager);
|
||||
ac.refresh();
|
||||
|
||||
TestBean tb = ac.getBean("tb", TestBean.class);
|
||||
assertEquals("NL", tb.getCountry());
|
||||
|
||||
}
|
||||
finally {
|
||||
System.setSecurityManager(oldSecurityManager);
|
||||
System.getProperties().remove("country");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringConcatenationWithDebugLogging() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
|
||||
|
||||
GenericBeanDefinition bd = new GenericBeanDefinition();
|
||||
bd.setBeanClass(String.class);
|
||||
bd.getConstructorArgumentValues().addGenericArgumentValue("test-#{ T(java.lang.System).currentTimeMillis() }");
|
||||
ac.registerBeanDefinition("str", bd);
|
||||
ac.refresh();
|
||||
|
||||
String str = ac.getBean("str", String.class);
|
||||
assertTrue(str.startsWith("test-"));
|
||||
}
|
||||
|
||||
|
||||
public static class ValueTestBean implements Serializable {
|
||||
|
||||
@Autowired @Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ")
|
||||
public String name;
|
||||
|
||||
@Autowired @Value("#{mySpecialAttr}")
|
||||
public int age;
|
||||
|
||||
@Value("${code} #{systemProperties.country}")
|
||||
public String country;
|
||||
|
||||
@Value("${code} #{systemProperties.country}")
|
||||
public ObjectFactory<String> countryFactory;
|
||||
|
||||
@Autowired @Qualifier("original")
|
||||
public transient TestBean tb;
|
||||
}
|
||||
|
||||
|
||||
public static class ConstructorValueTestBean {
|
||||
|
||||
public String name;
|
||||
|
||||
public int age;
|
||||
|
||||
public String country;
|
||||
|
||||
public TestBean tb;
|
||||
|
||||
@Autowired
|
||||
public ConstructorValueTestBean(
|
||||
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
|
||||
@Value("#{mySpecialAttr}") int age,
|
||||
@Qualifier("original") TestBean tb,
|
||||
@Value("${code} #{systemProperties.country}") String country) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.country = country;
|
||||
this.tb = tb;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MethodValueTestBean {
|
||||
|
||||
public String name;
|
||||
|
||||
public int age;
|
||||
|
||||
public String country;
|
||||
|
||||
public TestBean tb;
|
||||
|
||||
@Autowired
|
||||
public void configure(
|
||||
@Qualifier("original") TestBean tb,
|
||||
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
|
||||
@Value("#{mySpecialAttr}") int age,
|
||||
@Value("${code} #{systemProperties.country}") String country) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.country = country;
|
||||
this.tb = tb;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class PropertyValueTestBean {
|
||||
|
||||
public String name;
|
||||
|
||||
public int age;
|
||||
|
||||
public String country;
|
||||
|
||||
public TestBean tb;
|
||||
|
||||
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ")
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Value("#{mySpecialAttr}")
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Value("${code} #{systemProperties.country}")
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
@Autowired @Qualifier("original")
|
||||
public void setTb(TestBean tb) {
|
||||
this.tb = tb;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class PrototypeTestBean {
|
||||
|
||||
public String name;
|
||||
|
||||
public String country;
|
||||
|
||||
public String country2;
|
||||
|
||||
@Value("#{systemProperties.name}")
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry2(String country2) {
|
||||
this.country2 = country2;
|
||||
}
|
||||
|
||||
public String getCountry2() {
|
||||
return country2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.expression;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.security.AccessControlException;
|
||||
import java.security.Permission;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||
import org.springframework.beans.factory.config.Scope;
|
||||
import org.springframework.beans.factory.config.TypedStringValue;
|
||||
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation.AnnotationConfigUtils;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ApplicationContextExpressionTests {
|
||||
|
||||
private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class);
|
||||
|
||||
@Test
|
||||
public void genericApplicationContext() throws Exception {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
|
||||
|
||||
ac.getBeanFactory().registerScope("myScope", new Scope() {
|
||||
public Object get(String name, ObjectFactory objectFactory) {
|
||||
return objectFactory.getObject();
|
||||
}
|
||||
public Object remove(String name) {
|
||||
return null;
|
||||
}
|
||||
public void registerDestructionCallback(String name, Runnable callback) {
|
||||
}
|
||||
public Object resolveContextualObject(String key) {
|
||||
if (key.equals("mySpecialAttr")) {
|
||||
return "42";
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public String getConversationId() {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
||||
Properties placeholders = new Properties();
|
||||
placeholders.setProperty("code", "123");
|
||||
ppc.setProperties(placeholders);
|
||||
ac.addBeanFactoryPostProcessor(ppc);
|
||||
|
||||
GenericBeanDefinition bd0 = new GenericBeanDefinition();
|
||||
bd0.setBeanClass(TestBean.class);
|
||||
bd0.getPropertyValues().add("name", "myName");
|
||||
bd0.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "original"));
|
||||
ac.registerBeanDefinition("tb0", bd0);
|
||||
|
||||
GenericBeanDefinition bd1 = new GenericBeanDefinition();
|
||||
bd1.setBeanClass(TestBean.class);
|
||||
bd1.setScope("myScope");
|
||||
bd1.getConstructorArgumentValues().addGenericArgumentValue("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ");
|
||||
bd1.getConstructorArgumentValues().addGenericArgumentValue("#{mySpecialAttr}");
|
||||
ac.registerBeanDefinition("tb1", bd1);
|
||||
|
||||
GenericBeanDefinition bd2 = new GenericBeanDefinition();
|
||||
bd2.setBeanClass(TestBean.class);
|
||||
bd2.setScope("myScope");
|
||||
bd2.getPropertyValues().add("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }");
|
||||
bd2.getPropertyValues().add("age", "#{mySpecialAttr}");
|
||||
bd2.getPropertyValues().add("country", "${code} #{systemProperties.country}");
|
||||
ac.registerBeanDefinition("tb2", bd2);
|
||||
|
||||
GenericBeanDefinition bd3 = new GenericBeanDefinition();
|
||||
bd3.setBeanClass(ValueTestBean.class);
|
||||
bd3.setScope("myScope");
|
||||
ac.registerBeanDefinition("tb3", bd3);
|
||||
|
||||
GenericBeanDefinition bd4 = new GenericBeanDefinition();
|
||||
bd4.setBeanClass(ConstructorValueTestBean.class);
|
||||
bd4.setScope("myScope");
|
||||
ac.registerBeanDefinition("tb4", bd4);
|
||||
|
||||
GenericBeanDefinition bd5 = new GenericBeanDefinition();
|
||||
bd5.setBeanClass(MethodValueTestBean.class);
|
||||
bd5.setScope("myScope");
|
||||
ac.registerBeanDefinition("tb5", bd5);
|
||||
|
||||
GenericBeanDefinition bd6 = new GenericBeanDefinition();
|
||||
bd6.setBeanClass(PropertyValueTestBean.class);
|
||||
bd6.setScope("myScope");
|
||||
ac.registerBeanDefinition("tb6", bd6);
|
||||
|
||||
System.getProperties().put("country", "UK");
|
||||
try {
|
||||
ac.refresh();
|
||||
|
||||
TestBean tb0 = ac.getBean("tb0", TestBean.class);
|
||||
|
||||
TestBean tb1 = ac.getBean("tb1", TestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb1.getName());
|
||||
assertEquals(42, tb1.getAge());
|
||||
|
||||
TestBean tb2 = ac.getBean("tb2", TestBean.class);
|
||||
assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName());
|
||||
assertEquals(42, tb2.getAge());
|
||||
assertEquals("123 UK", tb2.getCountry());
|
||||
|
||||
ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb3.name);
|
||||
assertEquals(42, tb3.age);
|
||||
assertEquals("123 UK", tb3.country);
|
||||
assertEquals("123 UK", tb3.countryFactory.getObject());
|
||||
System.getProperties().put("country", "US");
|
||||
assertEquals("123 UK", tb3.country);
|
||||
assertEquals("123 US", tb3.countryFactory.getObject());
|
||||
System.getProperties().put("country", "UK");
|
||||
assertEquals("123 UK", tb3.country);
|
||||
assertEquals("123 UK", tb3.countryFactory.getObject());
|
||||
assertSame(tb0, tb3.tb);
|
||||
|
||||
tb3 = (ValueTestBean) SerializationTestUtils.serializeAndDeserialize(tb3);
|
||||
assertEquals("123 UK", tb3.countryFactory.getObject());
|
||||
|
||||
ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb4.name);
|
||||
assertEquals(42, tb4.age);
|
||||
assertEquals("123 UK", tb4.country);
|
||||
assertSame(tb0, tb4.tb);
|
||||
|
||||
MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb5.name);
|
||||
assertEquals(42, tb5.age);
|
||||
assertEquals("123 UK", tb5.country);
|
||||
assertSame(tb0, tb5.tb);
|
||||
|
||||
PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class);
|
||||
assertEquals("XXXmyNameYYY42ZZZ", tb6.name);
|
||||
assertEquals(42, tb6.age);
|
||||
assertEquals("123 UK", tb6.country);
|
||||
assertSame(tb0, tb6.tb);
|
||||
}
|
||||
finally {
|
||||
System.getProperties().remove("country");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prototypeCreationReevaluatesExpressions() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
|
||||
RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class);
|
||||
rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
rbd.getPropertyValues().add("country", "#{systemProperties.country}");
|
||||
rbd.getPropertyValues().add("country2", new TypedStringValue("#{systemProperties.country}"));
|
||||
ac.registerBeanDefinition("test", rbd);
|
||||
ac.refresh();
|
||||
|
||||
try {
|
||||
System.getProperties().put("name", "juergen1");
|
||||
System.getProperties().put("country", "UK1");
|
||||
PrototypeTestBean tb = (PrototypeTestBean) ac.getBean("test");
|
||||
assertEquals("juergen1", tb.getName());
|
||||
assertEquals("UK1", tb.getCountry());
|
||||
assertEquals("UK1", tb.getCountry2());
|
||||
|
||||
System.getProperties().put("name", "juergen2");
|
||||
System.getProperties().put("country", "UK2");
|
||||
tb = (PrototypeTestBean) ac.getBean("test");
|
||||
assertEquals("juergen2", tb.getName());
|
||||
assertEquals("UK2", tb.getCountry());
|
||||
assertEquals("UK2", tb.getCountry2());
|
||||
}
|
||||
finally {
|
||||
System.getProperties().remove("name");
|
||||
System.getProperties().remove("country");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prototypeCreationIsFastEnough() {
|
||||
if (factoryLog.isTraceEnabled() || factoryLog.isDebugEnabled()) {
|
||||
// Skip this test: Trace logging blows the time limit.
|
||||
return;
|
||||
}
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
|
||||
rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
rbd.getConstructorArgumentValues().addGenericArgumentValue("#{systemProperties.name}");
|
||||
rbd.getPropertyValues().add("country", "#{systemProperties.country}");
|
||||
ac.registerBeanDefinition("test", rbd);
|
||||
ac.refresh();
|
||||
StopWatch sw = new StopWatch();
|
||||
sw.start("prototype");
|
||||
System.getProperties().put("name", "juergen");
|
||||
System.getProperties().put("country", "UK");
|
||||
try {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
TestBean tb = (TestBean) ac.getBean("test");
|
||||
assertEquals("juergen", tb.getName());
|
||||
assertEquals("UK", tb.getCountry());
|
||||
}
|
||||
sw.stop();
|
||||
}
|
||||
finally {
|
||||
System.getProperties().remove("country");
|
||||
System.getProperties().remove("name");
|
||||
}
|
||||
assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void systemPropertiesSecurityManager() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
|
||||
|
||||
GenericBeanDefinition bd = new GenericBeanDefinition();
|
||||
bd.setBeanClass(TestBean.class);
|
||||
bd.getPropertyValues().add("country", "#{systemProperties.country}");
|
||||
ac.registerBeanDefinition("tb", bd);
|
||||
|
||||
SecurityManager oldSecurityManager = System.getSecurityManager();
|
||||
try {
|
||||
System.setProperty("country", "NL");
|
||||
|
||||
SecurityManager securityManager = new SecurityManager() {
|
||||
@Override
|
||||
public void checkPropertiesAccess() {
|
||||
throw new AccessControlException("Not Allowed");
|
||||
}
|
||||
@Override
|
||||
public void checkPermission(Permission perm) {
|
||||
// allow everything else
|
||||
}
|
||||
};
|
||||
System.setSecurityManager(securityManager);
|
||||
ac.refresh();
|
||||
|
||||
TestBean tb = ac.getBean("tb", TestBean.class);
|
||||
assertEquals("NL", tb.getCountry());
|
||||
|
||||
}
|
||||
finally {
|
||||
System.setSecurityManager(oldSecurityManager);
|
||||
System.getProperties().remove("country");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringConcatenationWithDebugLogging() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
|
||||
|
||||
GenericBeanDefinition bd = new GenericBeanDefinition();
|
||||
bd.setBeanClass(String.class);
|
||||
bd.getConstructorArgumentValues().addGenericArgumentValue("test-#{ T(java.lang.System).currentTimeMillis() }");
|
||||
ac.registerBeanDefinition("str", bd);
|
||||
ac.refresh();
|
||||
|
||||
String str = ac.getBean("str", String.class);
|
||||
assertTrue(str.startsWith("test-"));
|
||||
}
|
||||
|
||||
|
||||
public static class ValueTestBean implements Serializable {
|
||||
|
||||
@Autowired @Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ")
|
||||
public String name;
|
||||
|
||||
@Autowired @Value("#{mySpecialAttr}")
|
||||
public int age;
|
||||
|
||||
@Value("${code} #{systemProperties.country}")
|
||||
public String country;
|
||||
|
||||
@Value("${code} #{systemProperties.country}")
|
||||
public ObjectFactory<String> countryFactory;
|
||||
|
||||
@Autowired @Qualifier("original")
|
||||
public transient TestBean tb;
|
||||
}
|
||||
|
||||
|
||||
public static class ConstructorValueTestBean {
|
||||
|
||||
public String name;
|
||||
|
||||
public int age;
|
||||
|
||||
public String country;
|
||||
|
||||
public TestBean tb;
|
||||
|
||||
@Autowired
|
||||
public ConstructorValueTestBean(
|
||||
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
|
||||
@Value("#{mySpecialAttr}") int age,
|
||||
@Qualifier("original") TestBean tb,
|
||||
@Value("${code} #{systemProperties.country}") String country) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.country = country;
|
||||
this.tb = tb;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MethodValueTestBean {
|
||||
|
||||
public String name;
|
||||
|
||||
public int age;
|
||||
|
||||
public String country;
|
||||
|
||||
public TestBean tb;
|
||||
|
||||
@Autowired
|
||||
public void configure(
|
||||
@Qualifier("original") TestBean tb,
|
||||
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
|
||||
@Value("#{mySpecialAttr}") int age,
|
||||
@Value("${code} #{systemProperties.country}") String country) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.country = country;
|
||||
this.tb = tb;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class PropertyValueTestBean {
|
||||
|
||||
public String name;
|
||||
|
||||
public int age;
|
||||
|
||||
public String country;
|
||||
|
||||
public TestBean tb;
|
||||
|
||||
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ")
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Value("#{mySpecialAttr}")
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Value("${code} #{systemProperties.country}")
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
@Autowired @Qualifier("original")
|
||||
public void setTb(TestBean tb) {
|
||||
this.tb = tb;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class PrototypeTestBean {
|
||||
|
||||
public String name;
|
||||
|
||||
public String country;
|
||||
|
||||
public String country2;
|
||||
|
||||
@Value("#{systemProperties.name}")
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry2(String country2) {
|
||||
this.country2 = country2;
|
||||
}
|
||||
|
||||
public String getCountry2() {
|
||||
return country2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.support;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class GenericApplicationContextTests {
|
||||
|
||||
@Test
|
||||
public void nullBeanRegistration() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.registerSingleton("nullBean", null);
|
||||
new GenericApplicationContext(bf).refresh();
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.support;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class GenericApplicationContextTests {
|
||||
|
||||
@Test
|
||||
public void nullBeanRegistration() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.registerSingleton("nullBean", null);
|
||||
new GenericApplicationContext(bf).refresh();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.support;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ResourceConverter implements Converter<String, Resource> {
|
||||
|
||||
public Resource convert(String source) {
|
||||
return new FileSystemResource(source + ".xml");
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.support;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ResourceConverter implements Converter<String, Resource> {
|
||||
|
||||
public Resource convert(String source) {
|
||||
return new FileSystemResource(source + ".xml");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.support;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Scott Andrews
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class Spr7283Tests {
|
||||
|
||||
@Test
|
||||
public void testListWithInconsistentElementType() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spr7283.xml", getClass());
|
||||
List list = ctx.getBean("list", List.class);
|
||||
assertEquals(2, list.size());
|
||||
assertTrue(list.get(0) instanceof A);
|
||||
assertTrue(list.get(1) instanceof B);
|
||||
}
|
||||
|
||||
|
||||
public static class A {
|
||||
public A() {}
|
||||
}
|
||||
|
||||
public static class B {
|
||||
public B() {}
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.support;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Scott Andrews
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class Spr7283Tests {
|
||||
|
||||
@Test
|
||||
public void testListWithInconsistentElementType() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spr7283.xml", getClass());
|
||||
List list = ctx.getBean("list", List.class);
|
||||
assertEquals(2, list.size());
|
||||
assertTrue(list.get(0) instanceof A);
|
||||
assertTrue(list.get(1) instanceof B);
|
||||
}
|
||||
|
||||
|
||||
public static class A {
|
||||
public A() {}
|
||||
}
|
||||
|
||||
public static class B {
|
||||
public B() {}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,45 +1,45 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.jmx.export.annotation;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.jmx.IJmxTestBean;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class AnnotationTestBeanFactory implements FactoryBean<IJmxTestBean> {
|
||||
|
||||
private final FactoryCreatedAnnotationTestBean instance = new FactoryCreatedAnnotationTestBean();
|
||||
|
||||
public AnnotationTestBeanFactory() {
|
||||
this.instance.setName("FACTORY");
|
||||
}
|
||||
|
||||
public IJmxTestBean getObject() throws Exception {
|
||||
return this.instance;
|
||||
}
|
||||
|
||||
public Class<? extends IJmxTestBean> getObjectType() {
|
||||
return FactoryCreatedAnnotationTestBean.class;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.jmx.export.annotation;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.jmx.IJmxTestBean;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class AnnotationTestBeanFactory implements FactoryBean<IJmxTestBean> {
|
||||
|
||||
private final FactoryCreatedAnnotationTestBean instance = new FactoryCreatedAnnotationTestBean();
|
||||
|
||||
public AnnotationTestBeanFactory() {
|
||||
this.instance.setName("FACTORY");
|
||||
}
|
||||
|
||||
public IJmxTestBean getObject() throws Exception {
|
||||
return this.instance;
|
||||
}
|
||||
|
||||
public Class<? extends IJmxTestBean> getObjectType() {
|
||||
return FactoryCreatedAnnotationTestBean.class;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.jmx.export.annotation;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@ManagedResource(objectName = "bean:name=testBean5")
|
||||
public class FactoryCreatedAnnotationTestBean extends AnnotationTestBean {
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.jmx.export.annotation;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@ManagedResource(objectName = "bean:name=testBean5")
|
||||
public class FactoryCreatedAnnotationTestBean extends AnnotationTestBean {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,261 +1,261 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.scheduling.annotation;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.scheduling.annotation.AsyncResult;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class AsyncExecutionTests {
|
||||
|
||||
private static String originalThreadName;
|
||||
|
||||
private static int listenerCalled = 0;
|
||||
|
||||
private static int listenerConstructed = 0;
|
||||
|
||||
|
||||
@Test
|
||||
public void asyncMethods() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodBean.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
AsyncMethodBean asyncTest = context.getBean("asyncTest", AsyncMethodBean.class);
|
||||
asyncTest.doNothing(5);
|
||||
asyncTest.doSomething(10);
|
||||
Future<String> future = asyncTest.returnSomething(20);
|
||||
assertEquals("20", future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncClass() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassBean.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
AsyncClassBean asyncTest = context.getBean("asyncTest", AsyncClassBean.class);
|
||||
asyncTest.doSomething(10);
|
||||
Future<String> future = asyncTest.returnSomething(20);
|
||||
assertEquals("20", future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncInterface() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncInterfaceBean.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
AsyncInterface asyncTest = context.getBean("asyncTest", AsyncInterface.class);
|
||||
asyncTest.doSomething(10);
|
||||
Future<String> future = asyncTest.returnSomething(20);
|
||||
assertEquals("20", future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncMethodsInInterface() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodsInterfaceBean.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
AsyncMethodsInterface asyncTest = context.getBean("asyncTest", AsyncMethodsInterface.class);
|
||||
asyncTest.doNothing(5);
|
||||
asyncTest.doSomething(10);
|
||||
Future<String> future = asyncTest.returnSomething(20);
|
||||
assertEquals("20", future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncMethodListener() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
listenerCalled = 0;
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodListener.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
Thread.sleep(1000);
|
||||
assertEquals(1, listenerCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncClassListener() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
listenerCalled = 0;
|
||||
listenerConstructed = 0;
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassListener.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
context.close();
|
||||
Thread.sleep(1000);
|
||||
assertEquals(2, listenerCalled);
|
||||
assertEquals(1, listenerConstructed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncPrototypeClassListener() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
listenerCalled = 0;
|
||||
listenerConstructed = 0;
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
RootBeanDefinition listenerDef = new RootBeanDefinition(AsyncClassListener.class);
|
||||
listenerDef.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
context.registerBeanDefinition("asyncTest", listenerDef);
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
context.close();
|
||||
Thread.sleep(1000);
|
||||
assertEquals(2, listenerCalled);
|
||||
assertEquals(2, listenerConstructed);
|
||||
}
|
||||
|
||||
|
||||
public static class AsyncMethodBean {
|
||||
|
||||
public void doNothing(int i) {
|
||||
assertTrue(Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
@Async
|
||||
public void doSomething(int i) {
|
||||
System.out.println(Thread.currentThread().getName() + ": " + i);
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
@Async
|
||||
public Future<String> returnSomething(int i) {
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
return new AsyncResult<String>(Integer.toString(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Async
|
||||
public static class AsyncClassBean {
|
||||
|
||||
public void doSomething(int i) {
|
||||
System.out.println(Thread.currentThread().getName() + ": " + i);
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
public Future<String> returnSomething(int i) {
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
return new AsyncResult<String>(Integer.toString(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Async
|
||||
public interface AsyncInterface {
|
||||
|
||||
void doSomething(int i);
|
||||
|
||||
Future<String> returnSomething(int i);
|
||||
}
|
||||
|
||||
|
||||
public static class AsyncInterfaceBean implements AsyncInterface {
|
||||
|
||||
public void doSomething(int i) {
|
||||
System.out.println(Thread.currentThread().getName() + ": " + i);
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
public Future<String> returnSomething(int i) {
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
return new AsyncResult<String>(Integer.toString(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface AsyncMethodsInterface {
|
||||
|
||||
void doNothing(int i);
|
||||
|
||||
@Async
|
||||
void doSomething(int i);
|
||||
|
||||
@Async
|
||||
Future<String> returnSomething(int i);
|
||||
}
|
||||
|
||||
|
||||
public static class AsyncMethodsInterfaceBean implements AsyncMethodsInterface {
|
||||
|
||||
public void doNothing(int i) {
|
||||
assertTrue(Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
public void doSomething(int i) {
|
||||
System.out.println(Thread.currentThread().getName() + ": " + i);
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
public Future<String> returnSomething(int i) {
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
return new AsyncResult<String>(Integer.toString(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class AsyncMethodListener implements ApplicationListener {
|
||||
|
||||
@Async
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
listenerCalled++;
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Async
|
||||
public static class AsyncClassListener implements ApplicationListener {
|
||||
|
||||
public AsyncClassListener() {
|
||||
listenerConstructed++;
|
||||
}
|
||||
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
listenerCalled++;
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.scheduling.annotation;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.scheduling.annotation.AsyncResult;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class AsyncExecutionTests {
|
||||
|
||||
private static String originalThreadName;
|
||||
|
||||
private static int listenerCalled = 0;
|
||||
|
||||
private static int listenerConstructed = 0;
|
||||
|
||||
|
||||
@Test
|
||||
public void asyncMethods() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodBean.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
AsyncMethodBean asyncTest = context.getBean("asyncTest", AsyncMethodBean.class);
|
||||
asyncTest.doNothing(5);
|
||||
asyncTest.doSomething(10);
|
||||
Future<String> future = asyncTest.returnSomething(20);
|
||||
assertEquals("20", future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncClass() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassBean.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
AsyncClassBean asyncTest = context.getBean("asyncTest", AsyncClassBean.class);
|
||||
asyncTest.doSomething(10);
|
||||
Future<String> future = asyncTest.returnSomething(20);
|
||||
assertEquals("20", future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncInterface() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncInterfaceBean.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
AsyncInterface asyncTest = context.getBean("asyncTest", AsyncInterface.class);
|
||||
asyncTest.doSomething(10);
|
||||
Future<String> future = asyncTest.returnSomething(20);
|
||||
assertEquals("20", future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncMethodsInInterface() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodsInterfaceBean.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
AsyncMethodsInterface asyncTest = context.getBean("asyncTest", AsyncMethodsInterface.class);
|
||||
asyncTest.doNothing(5);
|
||||
asyncTest.doSomething(10);
|
||||
Future<String> future = asyncTest.returnSomething(20);
|
||||
assertEquals("20", future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncMethodListener() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
listenerCalled = 0;
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodListener.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
Thread.sleep(1000);
|
||||
assertEquals(1, listenerCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncClassListener() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
listenerCalled = 0;
|
||||
listenerConstructed = 0;
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassListener.class));
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
context.close();
|
||||
Thread.sleep(1000);
|
||||
assertEquals(2, listenerCalled);
|
||||
assertEquals(1, listenerConstructed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncPrototypeClassListener() throws Exception {
|
||||
originalThreadName = Thread.currentThread().getName();
|
||||
listenerCalled = 0;
|
||||
listenerConstructed = 0;
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
RootBeanDefinition listenerDef = new RootBeanDefinition(AsyncClassListener.class);
|
||||
listenerDef.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
context.registerBeanDefinition("asyncTest", listenerDef);
|
||||
context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
|
||||
context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
|
||||
context.refresh();
|
||||
context.close();
|
||||
Thread.sleep(1000);
|
||||
assertEquals(2, listenerCalled);
|
||||
assertEquals(2, listenerConstructed);
|
||||
}
|
||||
|
||||
|
||||
public static class AsyncMethodBean {
|
||||
|
||||
public void doNothing(int i) {
|
||||
assertTrue(Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
@Async
|
||||
public void doSomething(int i) {
|
||||
System.out.println(Thread.currentThread().getName() + ": " + i);
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
@Async
|
||||
public Future<String> returnSomething(int i) {
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
return new AsyncResult<String>(Integer.toString(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Async
|
||||
public static class AsyncClassBean {
|
||||
|
||||
public void doSomething(int i) {
|
||||
System.out.println(Thread.currentThread().getName() + ": " + i);
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
public Future<String> returnSomething(int i) {
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
return new AsyncResult<String>(Integer.toString(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Async
|
||||
public interface AsyncInterface {
|
||||
|
||||
void doSomething(int i);
|
||||
|
||||
Future<String> returnSomething(int i);
|
||||
}
|
||||
|
||||
|
||||
public static class AsyncInterfaceBean implements AsyncInterface {
|
||||
|
||||
public void doSomething(int i) {
|
||||
System.out.println(Thread.currentThread().getName() + ": " + i);
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
public Future<String> returnSomething(int i) {
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
return new AsyncResult<String>(Integer.toString(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface AsyncMethodsInterface {
|
||||
|
||||
void doNothing(int i);
|
||||
|
||||
@Async
|
||||
void doSomething(int i);
|
||||
|
||||
@Async
|
||||
Future<String> returnSomething(int i);
|
||||
}
|
||||
|
||||
|
||||
public static class AsyncMethodsInterfaceBean implements AsyncMethodsInterface {
|
||||
|
||||
public void doNothing(int i) {
|
||||
assertTrue(Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
public void doSomething(int i) {
|
||||
System.out.println(Thread.currentThread().getName() + ": " + i);
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
|
||||
public Future<String> returnSomething(int i) {
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
return new AsyncResult<String>(Integer.toString(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class AsyncMethodListener implements ApplicationListener {
|
||||
|
||||
@Async
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
listenerCalled++;
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Async
|
||||
public static class AsyncClassListener implements ApplicationListener {
|
||||
|
||||
public AsyncClassListener() {
|
||||
listenerConstructed++;
|
||||
}
|
||||
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
listenerCalled++;
|
||||
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,100 +1,100 @@
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class GroovyAspectIntegrationTests {
|
||||
|
||||
private GenericXmlApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaBean() {
|
||||
|
||||
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml");
|
||||
|
||||
TestService bean = context.getBean("javaBean", TestService.class);
|
||||
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("TestServiceImpl", e.getMessage());
|
||||
}
|
||||
assertEquals(1, logAdvice.getCountThrows());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroovyBeanInterface() {
|
||||
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-interface-context.xml");
|
||||
|
||||
TestService bean = context.getBean("groovyBean", TestService.class);
|
||||
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("GroovyServiceImpl", e.getMessage());
|
||||
}
|
||||
assertEquals(1, logAdvice.getCountThrows());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGroovyBeanDynamic() {
|
||||
|
||||
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-dynamic-context.xml");
|
||||
|
||||
TestService bean = context.getBean("groovyBean", TestService.class);
|
||||
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("GroovyServiceImpl", e.getMessage());
|
||||
}
|
||||
// No proxy here because the pointcut only applies to the concrete class, not the interface
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
assertEquals(0, logAdvice.getCountBefore());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroovyBeanProxyTargetClass() {
|
||||
|
||||
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml");
|
||||
|
||||
TestService bean = context.getBean("groovyBean", TestService.class);
|
||||
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (TestException e) {
|
||||
assertEquals("GroovyServiceImpl", e.getMessage());
|
||||
}
|
||||
assertEquals(1, logAdvice.getCountBefore());
|
||||
assertEquals(1, logAdvice.getCountThrows());
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class GroovyAspectIntegrationTests {
|
||||
|
||||
private GenericXmlApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaBean() {
|
||||
|
||||
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml");
|
||||
|
||||
TestService bean = context.getBean("javaBean", TestService.class);
|
||||
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("TestServiceImpl", e.getMessage());
|
||||
}
|
||||
assertEquals(1, logAdvice.getCountThrows());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroovyBeanInterface() {
|
||||
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-interface-context.xml");
|
||||
|
||||
TestService bean = context.getBean("groovyBean", TestService.class);
|
||||
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("GroovyServiceImpl", e.getMessage());
|
||||
}
|
||||
assertEquals(1, logAdvice.getCountThrows());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGroovyBeanDynamic() {
|
||||
|
||||
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-dynamic-context.xml");
|
||||
|
||||
TestService bean = context.getBean("groovyBean", TestService.class);
|
||||
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("GroovyServiceImpl", e.getMessage());
|
||||
}
|
||||
// No proxy here because the pointcut only applies to the concrete class, not the interface
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
assertEquals(0, logAdvice.getCountBefore());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroovyBeanProxyTargetClass() {
|
||||
|
||||
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml");
|
||||
|
||||
TestService bean = context.getBean("groovyBean", TestService.class);
|
||||
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (TestException e) {
|
||||
assertEquals("GroovyServiceImpl", e.getMessage());
|
||||
}
|
||||
assertEquals(1, logAdvice.getCountBefore());
|
||||
assertEquals(1, logAdvice.getCountThrows());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,101 +1,101 @@
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.scripting.groovy.GroovyScriptFactory;
|
||||
import org.springframework.scripting.support.ResourceScriptSource;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class GroovyAspectTests {
|
||||
|
||||
@Test
|
||||
public void testManualGroovyBeanWithUnconditionalPointcut() throws Exception {
|
||||
|
||||
LogUserAdvice logAdvice = new LogUserAdvice();
|
||||
|
||||
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
|
||||
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
|
||||
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
|
||||
|
||||
testAdvice(new DefaultPointcutAdvisor(logAdvice), logAdvice, target, "GroovyServiceImpl");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManualGroovyBeanWithStaticPointcut() throws Exception {
|
||||
LogUserAdvice logAdvice = new LogUserAdvice();
|
||||
|
||||
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
|
||||
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
|
||||
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
|
||||
|
||||
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
|
||||
pointcut.setExpression(String.format("execution(* %s.TestService+.*(..))", ClassUtils.getPackageName(getClass())));
|
||||
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManualGroovyBeanWithDynamicPointcut() throws Exception {
|
||||
|
||||
LogUserAdvice logAdvice = new LogUserAdvice();
|
||||
|
||||
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
|
||||
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
|
||||
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
|
||||
|
||||
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
|
||||
pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass())));
|
||||
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", false);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManualGroovyBeanWithDynamicPointcutProxyTargetClass() throws Exception {
|
||||
|
||||
LogUserAdvice logAdvice = new LogUserAdvice();
|
||||
|
||||
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
|
||||
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
|
||||
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
|
||||
|
||||
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
|
||||
pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass())));
|
||||
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", true);
|
||||
|
||||
}
|
||||
|
||||
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message)
|
||||
throws Exception {
|
||||
testAdvice(advisor, logAdvice, target, message, false);
|
||||
}
|
||||
|
||||
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message,
|
||||
boolean proxyTargetClass) throws Exception {
|
||||
|
||||
logAdvice.reset();
|
||||
|
||||
ProxyFactory factory = new ProxyFactory(target);
|
||||
factory.setProxyTargetClass(proxyTargetClass);
|
||||
factory.addAdvisor(advisor);
|
||||
TestService bean = (TestService) factory.getProxy();
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (TestException e) {
|
||||
assertEquals(message, e.getMessage());
|
||||
}
|
||||
assertEquals(1, logAdvice.getCountThrows());
|
||||
}
|
||||
}
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.scripting.groovy.GroovyScriptFactory;
|
||||
import org.springframework.scripting.support.ResourceScriptSource;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class GroovyAspectTests {
|
||||
|
||||
@Test
|
||||
public void testManualGroovyBeanWithUnconditionalPointcut() throws Exception {
|
||||
|
||||
LogUserAdvice logAdvice = new LogUserAdvice();
|
||||
|
||||
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
|
||||
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
|
||||
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
|
||||
|
||||
testAdvice(new DefaultPointcutAdvisor(logAdvice), logAdvice, target, "GroovyServiceImpl");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManualGroovyBeanWithStaticPointcut() throws Exception {
|
||||
LogUserAdvice logAdvice = new LogUserAdvice();
|
||||
|
||||
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
|
||||
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
|
||||
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
|
||||
|
||||
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
|
||||
pointcut.setExpression(String.format("execution(* %s.TestService+.*(..))", ClassUtils.getPackageName(getClass())));
|
||||
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManualGroovyBeanWithDynamicPointcut() throws Exception {
|
||||
|
||||
LogUserAdvice logAdvice = new LogUserAdvice();
|
||||
|
||||
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
|
||||
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
|
||||
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
|
||||
|
||||
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
|
||||
pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass())));
|
||||
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", false);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManualGroovyBeanWithDynamicPointcutProxyTargetClass() throws Exception {
|
||||
|
||||
LogUserAdvice logAdvice = new LogUserAdvice();
|
||||
|
||||
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
|
||||
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
|
||||
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
|
||||
|
||||
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
|
||||
pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass())));
|
||||
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", true);
|
||||
|
||||
}
|
||||
|
||||
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message)
|
||||
throws Exception {
|
||||
testAdvice(advisor, logAdvice, target, message, false);
|
||||
}
|
||||
|
||||
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message,
|
||||
boolean proxyTargetClass) throws Exception {
|
||||
|
||||
logAdvice.reset();
|
||||
|
||||
ProxyFactory factory = new ProxyFactory(target);
|
||||
factory.setProxyTargetClass(proxyTargetClass);
|
||||
factory.addAdvisor(advisor);
|
||||
TestService bean = (TestService) factory.getProxy();
|
||||
|
||||
assertEquals(0, logAdvice.getCountThrows());
|
||||
try {
|
||||
bean.sayHello();
|
||||
fail("Expected exception");
|
||||
} catch (TestException e) {
|
||||
assertEquals(message, e.getMessage());
|
||||
}
|
||||
assertEquals(1, logAdvice.getCountThrows());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
@Log
|
||||
public class GroovyServiceImpl implements TestService {
|
||||
|
||||
public String sayHello() {
|
||||
throw new TestException("GroovyServiceImpl");
|
||||
}
|
||||
|
||||
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
@Log
|
||||
public class GroovyServiceImpl implements TestService {
|
||||
|
||||
public String sayHello() {
|
||||
throw new TestException("GroovyServiceImpl");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Documented;
|
||||
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface Log {
|
||||
}
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Documented;
|
||||
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface Log {
|
||||
}
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.MethodBeforeAdvice;
|
||||
import org.springframework.aop.ThrowsAdvice;
|
||||
|
||||
public class LogUserAdvice implements MethodBeforeAdvice, ThrowsAdvice {
|
||||
|
||||
private int countBefore = 0;
|
||||
|
||||
private int countThrows = 0;
|
||||
|
||||
public void before(Method method, Object[] objects, Object o) throws Throwable {
|
||||
countBefore++;
|
||||
System.out.println("Method:"+method.getName());
|
||||
}
|
||||
|
||||
public void afterThrowing(Exception e) throws Throwable {
|
||||
countThrows++;
|
||||
System.out.println("***********************************************************************************");
|
||||
System.out.println("Exception caught:");
|
||||
System.out.println("***********************************************************************************");
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
|
||||
public int getCountBefore() {
|
||||
return countBefore;
|
||||
}
|
||||
|
||||
public int getCountThrows() {
|
||||
return countThrows;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
countThrows = 0;
|
||||
countBefore = 0;
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.MethodBeforeAdvice;
|
||||
import org.springframework.aop.ThrowsAdvice;
|
||||
|
||||
public class LogUserAdvice implements MethodBeforeAdvice, ThrowsAdvice {
|
||||
|
||||
private int countBefore = 0;
|
||||
|
||||
private int countThrows = 0;
|
||||
|
||||
public void before(Method method, Object[] objects, Object o) throws Throwable {
|
||||
countBefore++;
|
||||
System.out.println("Method:"+method.getName());
|
||||
}
|
||||
|
||||
public void afterThrowing(Exception e) throws Throwable {
|
||||
countThrows++;
|
||||
System.out.println("***********************************************************************************");
|
||||
System.out.println("Exception caught:");
|
||||
System.out.println("***********************************************************************************");
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
|
||||
public int getCountBefore() {
|
||||
return countBefore;
|
||||
}
|
||||
|
||||
public int getCountThrows() {
|
||||
return countThrows;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
countThrows = 0;
|
||||
countBefore = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
public interface TestService {
|
||||
public String sayHello();
|
||||
}
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
public interface TestService {
|
||||
public String sayHello();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
@Log
|
||||
public class TestServiceImpl implements TestService{
|
||||
public String sayHello() {
|
||||
throw new TestException("TestServiceImpl");
|
||||
}
|
||||
}
|
||||
package org.springframework.scripting.groovy;
|
||||
|
||||
@Log
|
||||
public class TestServiceImpl implements TestService{
|
||||
public String sayHello() {
|
||||
throw new TestException("TestServiceImpl");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,156 +1,156 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.validation.beanvalidation;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class BeanValidationPostProcessorTests {
|
||||
|
||||
@Test
|
||||
public void testNotNullConstraint() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
|
||||
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
|
||||
ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
assertTrue(ex.getRootCause().getMessage().contains("testBean"));
|
||||
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotNullConstraintSatisfied() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
|
||||
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
|
||||
RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
|
||||
bd.getPropertyValues().add("testBean", new TestBean());
|
||||
ac.registerBeanDefinition("bean", bd);
|
||||
ac.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotNullConstraintAfterInitialization() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
|
||||
bvpp.getPropertyValues().add("afterInitialization", true);
|
||||
ac.registerBeanDefinition("bvpp", bvpp);
|
||||
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
|
||||
ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
|
||||
ac.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSizeConstraint() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
|
||||
RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
|
||||
bd.getPropertyValues().add("testBean", new TestBean());
|
||||
bd.getPropertyValues().add("stringValue", "s");
|
||||
ac.registerBeanDefinition("bean", bd);
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
assertTrue(ex.getRootCause().getMessage().contains("stringValue"));
|
||||
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSizeConstraintSatisfied() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
|
||||
RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
|
||||
bd.getPropertyValues().add("testBean", new TestBean());
|
||||
bd.getPropertyValues().add("stringValue", "ss");
|
||||
ac.registerBeanDefinition("bean", bd);
|
||||
ac.refresh();
|
||||
}
|
||||
|
||||
|
||||
public static class NotNullConstrainedBean {
|
||||
|
||||
@NotNull
|
||||
private TestBean testBean;
|
||||
|
||||
@Size(min = 2)
|
||||
private String stringValue;
|
||||
|
||||
public TestBean getTestBean() {
|
||||
return testBean;
|
||||
}
|
||||
|
||||
public void setTestBean(TestBean testBean) {
|
||||
this.testBean = testBean;
|
||||
}
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
assertNotNull("Shouldn't be here after constraint checking", this.testBean);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class AfterInitConstraintBean {
|
||||
|
||||
@NotNull
|
||||
private TestBean testBean;
|
||||
|
||||
public TestBean getTestBean() {
|
||||
return testBean;
|
||||
}
|
||||
|
||||
public void setTestBean(TestBean testBean) {
|
||||
this.testBean = testBean;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.testBean = new TestBean();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.validation.beanvalidation;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class BeanValidationPostProcessorTests {
|
||||
|
||||
@Test
|
||||
public void testNotNullConstraint() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
|
||||
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
|
||||
ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
assertTrue(ex.getRootCause().getMessage().contains("testBean"));
|
||||
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotNullConstraintSatisfied() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
|
||||
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
|
||||
RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
|
||||
bd.getPropertyValues().add("testBean", new TestBean());
|
||||
ac.registerBeanDefinition("bean", bd);
|
||||
ac.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotNullConstraintAfterInitialization() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
|
||||
bvpp.getPropertyValues().add("afterInitialization", true);
|
||||
ac.registerBeanDefinition("bvpp", bvpp);
|
||||
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
|
||||
ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
|
||||
ac.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSizeConstraint() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
|
||||
RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
|
||||
bd.getPropertyValues().add("testBean", new TestBean());
|
||||
bd.getPropertyValues().add("stringValue", "s");
|
||||
ac.registerBeanDefinition("bean", bd);
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
assertTrue(ex.getRootCause().getMessage().contains("stringValue"));
|
||||
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSizeConstraintSatisfied() {
|
||||
GenericApplicationContext ac = new GenericApplicationContext();
|
||||
ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
|
||||
RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
|
||||
bd.getPropertyValues().add("testBean", new TestBean());
|
||||
bd.getPropertyValues().add("stringValue", "ss");
|
||||
ac.registerBeanDefinition("bean", bd);
|
||||
ac.refresh();
|
||||
}
|
||||
|
||||
|
||||
public static class NotNullConstrainedBean {
|
||||
|
||||
@NotNull
|
||||
private TestBean testBean;
|
||||
|
||||
@Size(min = 2)
|
||||
private String stringValue;
|
||||
|
||||
public TestBean getTestBean() {
|
||||
return testBean;
|
||||
}
|
||||
|
||||
public void setTestBean(TestBean testBean) {
|
||||
this.testBean = testBean;
|
||||
}
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
assertNotNull("Shouldn't be here after constraint checking", this.testBean);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class AfterInitConstraintBean {
|
||||
|
||||
@NotNull
|
||||
private TestBean testBean;
|
||||
|
||||
public TestBean getTestBean() {
|
||||
return testBean;
|
||||
}
|
||||
|
||||
public void setTestBean(TestBean testBean) {
|
||||
this.testBean = testBean;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.testBean = new TestBean();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,270 +1,270 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.validation.beanvalidation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.hibernate.validator.HibernateValidator;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.ObjectError;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ValidatorFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testSimpleValidation() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
|
||||
assertEquals(2, result.size());
|
||||
for (ConstraintViolation<ValidPerson> cv : result) {
|
||||
String path = cv.getPropertyPath().toString();
|
||||
if ("name".equals(path) || "address.street".equals(path)) {
|
||||
assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull);
|
||||
}
|
||||
else {
|
||||
fail("Invalid constraint violation with path '" + path + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleValidationWithCustomProvider() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.setProviderClass(HibernateValidator.class);
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
|
||||
assertEquals(2, result.size());
|
||||
for (ConstraintViolation<ValidPerson> cv : result) {
|
||||
String path = cv.getPropertyPath().toString();
|
||||
if ("name".equals(path) || "address.street".equals(path)) {
|
||||
assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull);
|
||||
}
|
||||
else {
|
||||
fail("Invalid constraint violation with path '" + path + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleValidationWithClassLevel() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
person.setName("Juergen");
|
||||
person.getAddress().setStreet("Juergen's Street");
|
||||
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
|
||||
assertEquals(1, result.size());
|
||||
Iterator<ConstraintViolation<ValidPerson>> iterator = result.iterator();
|
||||
ConstraintViolation cv = iterator.next();
|
||||
assertEquals("", cv.getPropertyPath().toString());
|
||||
assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NameAddressValid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringValidation() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
assertEquals(2, result.getErrorCount());
|
||||
FieldError fieldError = result.getFieldError("name");
|
||||
assertEquals("name", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("address.street");
|
||||
assertEquals("address.street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringValidationWithClassLevel() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
person.setName("Juergen");
|
||||
person.getAddress().setStreet("Juergen's Street");
|
||||
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
assertEquals(1, result.getErrorCount());
|
||||
ObjectError globalError = result.getGlobalError();
|
||||
System.out.println(Arrays.asList(globalError.getCodes()));
|
||||
System.out.println(globalError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringValidationWithErrorInListElement() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
person.getAddressList().add(new ValidAddress());
|
||||
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
assertEquals(3, result.getErrorCount());
|
||||
FieldError fieldError = result.getFieldError("name");
|
||||
assertEquals("name", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("address.street");
|
||||
assertEquals("address.street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("addressList[0].street");
|
||||
assertEquals("addressList[0].street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringValidationWithErrorInSetElement() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
person.getAddressSet().add(new ValidAddress());
|
||||
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
assertEquals(3, result.getErrorCount());
|
||||
FieldError fieldError = result.getFieldError("name");
|
||||
assertEquals("name", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("address.street");
|
||||
assertEquals("address.street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("addressSet[].street");
|
||||
assertEquals("addressSet[].street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
}
|
||||
|
||||
|
||||
@NameAddressValid
|
||||
public static class ValidPerson {
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Valid
|
||||
private ValidAddress address = new ValidAddress();
|
||||
|
||||
@Valid
|
||||
private List<ValidAddress> addressList = new LinkedList<ValidAddress>();
|
||||
|
||||
@Valid
|
||||
private Set<ValidAddress> addressSet = new LinkedHashSet<ValidAddress>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ValidAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(ValidAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public List<ValidAddress> getAddressList() {
|
||||
return addressList;
|
||||
}
|
||||
|
||||
public void setAddressList(List<ValidAddress> addressList) {
|
||||
this.addressList = addressList;
|
||||
}
|
||||
|
||||
public Set<ValidAddress> getAddressSet() {
|
||||
return addressSet;
|
||||
}
|
||||
|
||||
public void setAddressSet(Set<ValidAddress> addressSet) {
|
||||
this.addressSet = addressSet;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ValidAddress {
|
||||
|
||||
@NotNull
|
||||
private String street;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = NameAddressValidator.class)
|
||||
public @interface NameAddressValid {
|
||||
|
||||
String message() default "Street must not contain name";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<?>[] payload() default {};
|
||||
}
|
||||
|
||||
|
||||
public static class NameAddressValidator implements ConstraintValidator<NameAddressValid, ValidPerson> {
|
||||
|
||||
public void initialize(NameAddressValid constraintAnnotation) {
|
||||
}
|
||||
|
||||
public boolean isValid(ValidPerson value, ConstraintValidatorContext constraintValidatorContext) {
|
||||
return (value.name == null || !value.address.street.contains(value.name));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.validation.beanvalidation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.hibernate.validator.HibernateValidator;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.ObjectError;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ValidatorFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testSimpleValidation() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
|
||||
assertEquals(2, result.size());
|
||||
for (ConstraintViolation<ValidPerson> cv : result) {
|
||||
String path = cv.getPropertyPath().toString();
|
||||
if ("name".equals(path) || "address.street".equals(path)) {
|
||||
assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull);
|
||||
}
|
||||
else {
|
||||
fail("Invalid constraint violation with path '" + path + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleValidationWithCustomProvider() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.setProviderClass(HibernateValidator.class);
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
|
||||
assertEquals(2, result.size());
|
||||
for (ConstraintViolation<ValidPerson> cv : result) {
|
||||
String path = cv.getPropertyPath().toString();
|
||||
if ("name".equals(path) || "address.street".equals(path)) {
|
||||
assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull);
|
||||
}
|
||||
else {
|
||||
fail("Invalid constraint violation with path '" + path + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleValidationWithClassLevel() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
person.setName("Juergen");
|
||||
person.getAddress().setStreet("Juergen's Street");
|
||||
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
|
||||
assertEquals(1, result.size());
|
||||
Iterator<ConstraintViolation<ValidPerson>> iterator = result.iterator();
|
||||
ConstraintViolation cv = iterator.next();
|
||||
assertEquals("", cv.getPropertyPath().toString());
|
||||
assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NameAddressValid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringValidation() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
assertEquals(2, result.getErrorCount());
|
||||
FieldError fieldError = result.getFieldError("name");
|
||||
assertEquals("name", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("address.street");
|
||||
assertEquals("address.street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringValidationWithClassLevel() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
person.setName("Juergen");
|
||||
person.getAddress().setStreet("Juergen's Street");
|
||||
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
assertEquals(1, result.getErrorCount());
|
||||
ObjectError globalError = result.getGlobalError();
|
||||
System.out.println(Arrays.asList(globalError.getCodes()));
|
||||
System.out.println(globalError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringValidationWithErrorInListElement() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
person.getAddressList().add(new ValidAddress());
|
||||
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
assertEquals(3, result.getErrorCount());
|
||||
FieldError fieldError = result.getFieldError("name");
|
||||
assertEquals("name", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("address.street");
|
||||
assertEquals("address.street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("addressList[0].street");
|
||||
assertEquals("addressList[0].street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringValidationWithErrorInSetElement() throws Exception {
|
||||
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
||||
validator.afterPropertiesSet();
|
||||
ValidPerson person = new ValidPerson();
|
||||
person.getAddressSet().add(new ValidAddress());
|
||||
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
assertEquals(3, result.getErrorCount());
|
||||
FieldError fieldError = result.getFieldError("name");
|
||||
assertEquals("name", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("address.street");
|
||||
assertEquals("address.street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("addressSet[].street");
|
||||
assertEquals("addressSet[].street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
}
|
||||
|
||||
|
||||
@NameAddressValid
|
||||
public static class ValidPerson {
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Valid
|
||||
private ValidAddress address = new ValidAddress();
|
||||
|
||||
@Valid
|
||||
private List<ValidAddress> addressList = new LinkedList<ValidAddress>();
|
||||
|
||||
@Valid
|
||||
private Set<ValidAddress> addressSet = new LinkedHashSet<ValidAddress>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ValidAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(ValidAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public List<ValidAddress> getAddressList() {
|
||||
return addressList;
|
||||
}
|
||||
|
||||
public void setAddressList(List<ValidAddress> addressList) {
|
||||
this.addressList = addressList;
|
||||
}
|
||||
|
||||
public Set<ValidAddress> getAddressSet() {
|
||||
return addressSet;
|
||||
}
|
||||
|
||||
public void setAddressSet(Set<ValidAddress> addressSet) {
|
||||
this.addressSet = addressSet;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ValidAddress {
|
||||
|
||||
@NotNull
|
||||
private String street;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = NameAddressValidator.class)
|
||||
public @interface NameAddressValid {
|
||||
|
||||
String message() default "Street must not contain name";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<?>[] payload() default {};
|
||||
}
|
||||
|
||||
|
||||
public static class NameAddressValidator implements ConstraintValidator<NameAddressValid, ValidPerson> {
|
||||
|
||||
public void initialize(NameAddressValid constraintAnnotation) {
|
||||
}
|
||||
|
||||
public boolean isValid(ValidPerson value, ConstraintValidatorContext constraintValidatorContext) {
|
||||
return (value.name == null || !value.address.street.contains(value.name));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user