-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternTest.java
More file actions
34 lines (30 loc) · 1.2 KB
/
Copy pathPatternTest.java
File metadata and controls
34 lines (30 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.hanserwei.patterns.proxy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Map;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.Test;
/** 验证本模式的协作契约与边界行为. */
public final class PatternTest {
/** 命中缓存省掉读取,显式失效后重新读取. */
@Test
void cachesAndInvalidates() {
MemoryArticleSource source = new MemoryArticleSource(Map.of(1L, "Java"));
CachingArticleProxy proxy = new CachingArticleProxy(source);
assertEquals("Java", proxy.find(1));
assertEquals("Java", proxy.find(1));
assertEquals(1, source.reads());
proxy.invalidate(1);
proxy.find(1);
assertEquals(2, source.reads());
}
/** 读取失败必须保持原异常语义且不缓存失败. */
@Test
void preservesMissingArticleFailure() {
MemoryArticleSource source = new MemoryArticleSource(Map.of());
ArticleSource proxy = new CachingArticleProxy(source);
assertThrows(NoSuchElementException.class, () -> proxy.find(99));
assertThrows(NoSuchElementException.class, () -> proxy.find(99));
assertEquals(2, source.reads());
}
}