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
29 public class DefaultRepositoryCacheTest {
30
31 private final DefaultRepositoryCache cache = new DefaultRepositoryCache();
32
33 private final RepositorySystemSession session = new DefaultRepositorySystemSession();
34
35 private Object get(Object key) {
36 return cache.get(session, key);
37 }
38
39 private void put(Object key, Object value) {
40 cache.put(session, key, value);
41 }
42
43 @Test
44 void testGet_NullKey() {
45 assertThrows(RuntimeException.class, () -> get(null));
46 }
47
48 @Test
49 void testPut_NullKey() {
50 assertThrows(RuntimeException.class, () -> put(null, "data"));
51 }
52
53 @Test
54 void testGetPut() {
55 Object key = "key";
56 assertNull(get(key));
57 put(key, "value");
58 assertEquals("value", get(key));
59 put(key, "changed");
60 assertEquals("changed", get(key));
61 put(key, null);
62 assertNull(get(key));
63 }
64
65 @Test
66 @Timeout(value = 10L)
67 public void testConcurrency() throws Exception {
68 final AtomicReference<Throwable> error = new AtomicReference<>();
69 Thread[] threads = new Thread[20];
70 for (int i = 0; i < threads.length; i++) {
71 threads[i] = new Thread() {
72 @Override
73 public void run() {
74 for (int i = 0; i < 100; i++) {
75 String key = UUID.randomUUID().toString();
76 try {
77 put(key, Boolean.TRUE);
78 assertEquals(Boolean.TRUE, get(key));
79 } catch (Throwable t) {
80 error.compareAndSet(null, t);
81 t.printStackTrace();
82 }
83 }
84 }
85 };
86 }
87 for (Thread thread : threads) {
88 thread.start();
89 }
90 for (Thread thread : threads) {
91 thread.join();
92 }
93 assertNull(error.get(), String.valueOf(error.get()));
94 }
95 }