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