AbstractAnnotationTests.java 12.8 KB
Newer Older
C
Costin Leau 已提交
1
/*
C
Costin Leau 已提交
2
 * Copyright 2010-2011 the original author or authors.
C
Costin Leau 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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;

C
Costin Leau 已提交
19
import static org.junit.Assert.*;
C
Costin Leau 已提交
20

C
Costin Leau 已提交
21
import java.util.Collection;
C
Costin Leau 已提交
22 23
import java.util.UUID;

C
Costin Leau 已提交
24 25
import org.junit.Before;
import org.junit.Test;
C
Costin Leau 已提交
26
import org.springframework.aop.framework.AopProxyUtils;
C
Costin Leau 已提交
27 28
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
C
Costin Leau 已提交
29 30 31 32 33
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Abstract annotation test (containing several reusable methods).
34
 *
C
Costin Leau 已提交
35 36
 * @author Costin Leau
 */
37
public abstract class AbstractAnnotationTests {
C
Costin Leau 已提交
38 39 40

	protected ApplicationContext ctx;

41
	protected CacheableService<?> cs;
C
Costin Leau 已提交
42

43
	protected CacheableService<?> ccs;
C
Costin Leau 已提交
44

C
Costin Leau 已提交
45 46
	protected CacheManager cm;

C
Costin Leau 已提交
47 48 49 50 51 52 53
	protected abstract String getConfig();

	@Before
	public void setup() {
		ctx = new ClassPathXmlApplicationContext(getConfig());
		cs = ctx.getBean("service", CacheableService.class);
		ccs = ctx.getBean("classService", CacheableService.class);
C
Costin Leau 已提交
54
		cm = ctx.getBean(CacheManager.class);
C
Costin Leau 已提交
55 56 57 58
		Collection<String> cn = cm.getCacheNames();
		assertTrue(cn.contains("default"));
		assertTrue(cn.contains("secondary"));
		assertTrue(cn.contains("primary"));
C
Costin Leau 已提交
59 60
	}

61
	public void testCacheable(CacheableService<?> service) throws Exception {
C
Costin Leau 已提交
62 63 64 65 66 67 68 69 70 71
		Object o1 = new Object();

		Object r1 = service.cache(o1);
		Object r2 = service.cache(o1);
		Object r3 = service.cache(o1);

		assertSame(r1, r2);
		assertSame(r1, r3);
	}

72
	public void testInvalidate(CacheableService<?> service) throws Exception {
C
Costin Leau 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
		Object o1 = new Object();

		Object r1 = service.cache(o1);
		Object r2 = service.cache(o1);

		assertSame(r1, r2);
		service.invalidate(o1);
		Object r3 = service.cache(o1);
		Object r4 = service.cache(o1);
		assertNotSame(r1, r3);
		assertSame(r3, r4);
	}

86
	public void testInvalidateWKey(CacheableService<?> service) throws Exception {
C
Costin Leau 已提交
87 88 89 90 91 92
		Object o1 = new Object();

		Object r1 = service.cache(o1);
		Object r2 = service.cache(o1);

		assertSame(r1, r2);
93
		service.evict(o1, null);
C
Costin Leau 已提交
94 95 96 97 98 99
		Object r3 = service.cache(o1);
		Object r4 = service.cache(o1);
		assertNotSame(r1, r3);
		assertSame(r3, r4);
	}

100
	public void testConditionalExpression(CacheableService<?> service)
C
Costin Leau 已提交
101
			throws Exception {
C
Costin Leau 已提交
102 103 104 105 106 107 108 109 110 111 112
		Object r1 = service.conditional(4);
		Object r2 = service.conditional(4);

		assertNotSame(r1, r2);

		Object r3 = service.conditional(3);
		Object r4 = service.conditional(3);

		assertSame(r3, r4);
	}

113
	public void testKeyExpression(CacheableService<?> service) throws Exception {
C
Costin Leau 已提交
114 115 116 117 118 119 120 121 122 123 124
		Object r1 = service.key(5, 1);
		Object r2 = service.key(5, 2);

		assertSame(r1, r2);

		Object r3 = service.key(1, 5);
		Object r4 = service.key(2, 5);

		assertNotSame(r3, r4);
	}

125
	public void testNullValue(CacheableService<?> service) throws Exception {
C
Costin Leau 已提交
126 127 128 129 130 131 132 133 134
		Object key = new Object();
		assertNull(service.nullValue(key));
		int nr = service.nullInvocations().intValue();
		assertNull(service.nullValue(key));
		assertEquals(nr, service.nullInvocations().intValue());
		assertNull(service.nullValue(new Object()));
		assertEquals(nr + 1, service.nullInvocations().intValue());
	}

135
	public void testMethodName(CacheableService<?> service, String keyName)
C
Costin Leau 已提交
136 137 138 139
			throws Exception {
		Object key = new Object();
		Object r1 = service.name(key);
		assertSame(r1, service.name(key));
C
Costin Leau 已提交
140
		Cache cache = cm.getCache("default");
C
Costin Leau 已提交
141
		// assert the method name is used
C
Costin Leau 已提交
142
		assertNotNull(cache.get(keyName));
C
Costin Leau 已提交
143 144
	}

145
	public void testRootVars(CacheableService<?> service) {
C
Costin Leau 已提交
146 147 148
		Object key = new Object();
		Object r1 = service.rootVars(key);
		assertSame(r1, service.rootVars(key));
C
Costin Leau 已提交
149
		Cache cache = cm.getCache("default");
C
Costin Leau 已提交
150 151
		// assert the method name is used
		String expectedKey = "rootVarsrootVars" + AopProxyUtils.ultimateTargetClass(service) + service;
C
Costin Leau 已提交
152
		assertNotNull(cache.get(expectedKey));
C
Costin Leau 已提交
153 154
	}

155
	public void testCheckedThrowable(CacheableService<?> service) throws Exception {
C
Costin Leau 已提交
156 157 158 159 160 161 162 163 164
		String arg = UUID.randomUUID().toString();
		try {
			service.throwChecked(arg);
			fail("Excepted exception");
		} catch (Exception ex) {
			assertEquals(arg, ex.getMessage());
		}
	}

165
	public void testUncheckedThrowable(CacheableService<?> service) throws Exception {
C
Costin Leau 已提交
166 167 168 169 170 171 172 173 174 175
		try {
			service.throwUnchecked(Long.valueOf(1));
			fail("Excepted exception");
		} catch (RuntimeException ex) {
			assertTrue("Excepted different exception type and got " + ex.getClass(),
					ex instanceof UnsupportedOperationException);
			// expected
		}
	}

176
	public void testNullArg(CacheableService<?> service) {
C
Costin Leau 已提交
177 178 179 180
		Object r1 = service.cache(null);
		assertSame(r1, service.cache(null));
	}

181
	public void testCacheUpdate(CacheableService<?> service) {
C
Costin Leau 已提交
182 183 184 185 186 187 188 189 190 191 192 193
		Object o = new Object();
		Cache cache = cm.getCache("default");
		assertNull(cache.get(o));
		Object r1 = service.update(o);
		assertSame(r1, cache.get(o).get());

		o = new Object();
		assertNull(cache.get(o));
		Object r2 = service.update(o);
		assertSame(r2, cache.get(o).get());
	}

194
	public void testConditionalCacheUpdate(CacheableService<?> service) {
C
Costin Leau 已提交
195 196 197 198 199 200 201 202 203 204 205
		Integer one = Integer.valueOf(1);
		Integer three = Integer.valueOf(3);

		Cache cache = cm.getCache("default");
		assertEquals(one, Integer.valueOf(service.conditionalUpdate(one).toString()));
		assertNull(cache.get(one));

		assertEquals(three, Integer.valueOf(service.conditionalUpdate(three).toString()));
		assertEquals(three, Integer.valueOf(cache.get(three).get().toString()));
	}

206
	public void testMultiCache(CacheableService<?> service) {
C
Costin Leau 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		Object o1 = new Object();
		Object o2 = new Object();

		Cache primary = cm.getCache("primary");
		Cache secondary = cm.getCache("secondary");

		assertNull(primary.get(o1));
		assertNull(secondary.get(o1));
		Object r1 = service.multiCache(o1);
		assertSame(r1, primary.get(o1).get());
		assertSame(r1, secondary.get(o1).get());

		Object r2 = service.multiCache(o1);
		Object r3 = service.multiCache(o1);

		assertSame(r1, r2);
		assertSame(r1, r3);

		assertNull(primary.get(o2));
		assertNull(secondary.get(o2));
		Object r4 = service.multiCache(o2);
		assertSame(r4, primary.get(o2).get());
		assertSame(r4, secondary.get(o2).get());
	}

232
	public void testMultiEvict(CacheableService<?> service) {
C
Costin Leau 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		Object o1 = new Object();

		Object r1 = service.multiCache(o1);
		Object r2 = service.multiCache(o1);

		Cache primary = cm.getCache("primary");
		Cache secondary = cm.getCache("secondary");

		assertSame(r1, r2);
		assertSame(r1, primary.get(o1).get());
		assertSame(r1, secondary.get(o1).get());

		service.multiEvict(o1);
		assertNull(primary.get(o1));
		assertNull(secondary.get(o1));

		Object r3 = service.multiCache(o1);
		Object r4 = service.multiCache(o1);
		assertNotSame(r1, r3);
		assertSame(r3, r4);

		assertSame(r3, primary.get(o1).get());
		assertSame(r4, secondary.get(o1).get());
	}

258
	public void testMultiPut(CacheableService<?> service) {
C
Costin Leau 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
		Object o = Integer.valueOf(1);

		Cache primary = cm.getCache("primary");
		Cache secondary = cm.getCache("secondary");

		assertNull(primary.get(o));
		assertNull(secondary.get(o));
		Object r1 = service.multiUpdate(o);
		assertSame(r1, primary.get(o).get());
		assertSame(r1, secondary.get(o).get());

		o = Integer.valueOf(2);
		assertNull(primary.get(o));
		assertNull(secondary.get(o));
		Object r2 = service.multiUpdate(o);
		assertSame(r2, primary.get(o).get());
		assertSame(r2, secondary.get(o).get());
	}

278
	public void testMultiCacheAndEvict(CacheableService<?> service) {
C
Costin Leau 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
		String methodName = "multiCacheAndEvict";

		Cache primary = cm.getCache("primary");
		Cache secondary = cm.getCache("secondary");
		Object key = Integer.valueOf(1);

		secondary.put(key, key);

		assertNull(secondary.get(methodName));
		assertSame(key, secondary.get(key).get());

		Object r1 = service.multiCacheAndEvict(key);
		assertSame(r1, service.multiCacheAndEvict(key));

		// assert the method name is used
		assertSame(r1, primary.get(methodName).get());
		assertNull(secondary.get(methodName));
		assertNull(secondary.get(key));
	}

299
	public void testMultiConditionalCacheAndEvict(CacheableService<?> service) {
C
Costin Leau 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
		Cache primary = cm.getCache("primary");
		Cache secondary = cm.getCache("secondary");
		Object key = Integer.valueOf(1);

		secondary.put(key, key);

		assertNull(primary.get(key));
		assertSame(key, secondary.get(key).get());

		Object r1 = service.multiConditionalCacheAndEvict(key);
		Object r3 = service.multiConditionalCacheAndEvict(key);

		assertTrue(!r1.equals(r3));
		assertNull(primary.get(key));

		Object key2 = Integer.valueOf(3);
		Object r2 = service.multiConditionalCacheAndEvict(key2);
		assertSame(r2, service.multiConditionalCacheAndEvict(key2));

		// assert the method name is used
		assertSame(r2, primary.get(key2).get());
		assertNull(secondary.get(key2));
	}

C
Costin Leau 已提交
324 325 326 327 328 329 330 331 332 333
	@Test
	public void testCacheable() throws Exception {
		testCacheable(cs);
	}

	@Test
	public void testInvalidate() throws Exception {
		testInvalidate(cs);
	}

C
Costin Leau 已提交
334 335 336 337 338
	@Test
	public void testInvalidateWithKey() throws Exception {
		testInvalidateWKey(cs);
	}

C
Costin Leau 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	@Test
	public void testConditionalExpression() throws Exception {
		testConditionalExpression(cs);
	}

	@Test
	public void testKeyExpression() throws Exception {
		testKeyExpression(cs);
	}

	@Test
	public void testClassCacheCacheable() throws Exception {
		testCacheable(ccs);
	}

	@Test
	public void testClassCacheInvalidate() throws Exception {
		testInvalidate(ccs);
	}

C
Costin Leau 已提交
359 360 361 362 363
	@Test
	public void testClassCacheInvalidateWKey() throws Exception {
		testInvalidateWKey(ccs);
	}

C
Costin Leau 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	@Test
	public void testNullValue() throws Exception {
		testNullValue(cs);
	}

	@Test
	public void testClassNullValue() throws Exception {
		Object key = new Object();
		assertNull(ccs.nullValue(key));
		int nr = ccs.nullInvocations().intValue();
		assertNull(ccs.nullValue(key));
		assertEquals(nr, ccs.nullInvocations().intValue());
		assertNull(ccs.nullValue(new Object()));
		// the check method is also cached
		assertEquals(nr, ccs.nullInvocations().intValue());
		assertEquals(nr + 1, AnnotatedClassCacheableService.nullInvocations
				.intValue());
	}
C
Costin Leau 已提交
382 383 384 385 386 387 388 389 390 391

	@Test
	public void testMethodName() throws Exception {
		testMethodName(cs, "name");
	}

	@Test
	public void testClassMethodName() throws Exception {
		testMethodName(ccs, "namedefault");
	}
C
Costin Leau 已提交
392 393 394 395 396 397 398 399 400 401

	@Test
	public void testRootVars() throws Exception {
		testRootVars(cs);
	}

	@Test
	public void testClassRootVars() throws Exception {
		testRootVars(ccs);
	}
C
Costin Leau 已提交
402 403 404 405 406 407 408 409 410 411

	@Test
	public void testNullArg() throws Exception {
		testNullArg(cs);
	}

	@Test
	public void testClassNullArg() throws Exception {
		testNullArg(ccs);
	}
C
Costin Leau 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

	@Test
	public void testCheckedException() throws Exception {
		testCheckedThrowable(cs);
	}

	@Test
	public void testClassCheckedException() throws Exception {
		testCheckedThrowable(ccs);
	}

	@Test
	public void testUncheckedException() throws Exception {
		testUncheckedThrowable(cs);
	}

	@Test
	public void testClassUncheckedException() throws Exception {
		testUncheckedThrowable(ccs);
	}
C
Costin Leau 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

	@Test
	public void testUpdate() {
		testCacheUpdate(cs);
	}

	@Test
	public void testClassUpdate() {
		testCacheUpdate(ccs);
	}

	@Test
	public void testConditionalUpdate() {
		testConditionalCacheUpdate(cs);
	}

	@Test
	public void testClassConditionalUpdate() {
		testConditionalCacheUpdate(ccs);
	}
C
Costin Leau 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

	@Test
	public void testMultiCache() {
		testMultiCache(cs);
	}

	@Test
	public void testClassMultiCache() {
		testMultiCache(ccs);
	}

	@Test
	public void testMultiEvict() {
		testMultiEvict(cs);
	}

	@Test
	public void testClassMultiEvict() {
		testMultiEvict(ccs);
	}

	@Test
	public void testMultiPut() {
		testMultiPut(cs);
	}

	@Test
	public void testClassMultiPut() {
		testMultiPut(ccs);
	}

	@Test
	public void testMultiCacheAndEvict() {
		testMultiCacheAndEvict(cs);
	}

	@Test
	public void testClassMultiCacheAndEvict() {
		testMultiCacheAndEvict(ccs);
	}

	@Test
	public void testMultiConditionalCacheAndEvict() {
		testMultiConditionalCacheAndEvict(cs);
	}

	@Test
	public void testClassMultiConditionalCacheAndEvict() {
		testMultiConditionalCacheAndEvict(ccs);
	}
C
Costin Leau 已提交
502
}