1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether;
20
21 import java.util.UUID;
22 import java.util.concurrent.atomic.AtomicReference;
23
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.Timeout;
26
27 import static org.junit.jupiter.api.Assertions.*;
28 import static org.mockito.Mockito.mock;
29
30 public class DefaultRepositoryCacheTest {
31
32 private final DefaultRepositoryCache cache = new DefaultRepositoryCache();
33
34 private final RepositorySystemSession session = mock(RepositorySystemSession.class);
35
36 private Object get(Object key) {
37 return cache.get(session, key);
38 }
39
40 private void put(Object key, Object value) {
41 cache.put(session, key, value);
42 }
43
44 @Test
45 void testGet_NullKey() {
46 assertThrows(RuntimeException.class, () -> get(null));
47 }
48
49 @Test
50 void testPut_NullKey() {
51 assertThrows(RuntimeException.class, () -> put(null, "data"));
52 }
53
54 @Test
55 void testGetPut() {
56 Object key = "key";
57 assertNull(get(key));
58 put(key, "value");
59 assertEquals("value", get(key));
60 put(key, "changed");
61 assertEquals("changed", get(key));
62 put(key, null);
63 assertNull(get(key));
64 }
65
66 @Test
67 @Timeout(value = 10L)
68 public void testConcurrency() throws Exception {
69 final AtomicReference<Throwable> error = new AtomicReference<>();
70 Thread[] threads = new Thread[20];
71 for (int i = 0; i < threads.length; i++) {
72 threads[i] = new Thread() {
73 @Override
74 public void run() {
75 for (int i = 0; i < 100; i++) {
76 String key = UUID.randomUUID().toString();
77 try {
78 put(key, Boolean.TRUE);
79 assertEquals(Boolean.TRUE, get(key));
80 } catch (Throwable t) {
81 error.compareAndSet(null, t);
82 t.printStackTrace();
83 }
84 }
85 }
86 };
87 }
88 for (Thread thread : threads) {
89 thread.start();
90 }
91 for (Thread thread : threads) {
92 thread.join();
93 }
94 assertNull(error.get(), String.valueOf(error.get()));
95 }
96 }