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