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 import java.util.function.Function;
27 import java.util.function.Supplier;
28
29 import org.junit.Test;
30
31 public class DefaultSessionDataTest
32 {
33
34 private DefaultSessionData data = new DefaultSessionData();
35
36 private Object get( Object key )
37 {
38 return data.get( key );
39 }
40
41 private void set( Object key, Object value )
42 {
43 data.set( key, value );
44 }
45
46 private boolean set( Object key, Object oldValue, Object newValue )
47 {
48 return data.set( key, oldValue, newValue );
49 }
50
51 private Object computeIfAbsent( Object key, Supplier<Object> supplier )
52 {
53 return data.computeIfAbsent( key, supplier );
54 }
55
56 @Test( expected = RuntimeException.class )
57 public void testGet_NullKey()
58 {
59 get( null );
60 }
61
62 @Test( expected = RuntimeException.class )
63 public void testSet_NullKey()
64 {
65 set( null, "data" );
66 }
67
68 @Test
69 public void testGetSet()
70 {
71 Object key = "key";
72 assertNull( get( key ) );
73 set( key, "value" );
74 assertEquals( "value", get( key ) );
75 set( key, "changed" );
76 assertEquals( "changed", get( key ) );
77 set( key, null );
78 assertNull( get( key ) );
79 }
80
81 @Test
82 public void testGetSafeSet()
83 {
84 Object key = "key";
85 assertNull( get( key ) );
86 assertFalse( set( key, "wrong", "value" ) );
87 assertNull( get( key ) );
88 assertTrue( set( key, null, "value" ) );
89 assertEquals( "value", get( key ) );
90 assertTrue( set( key, "value", "value" ) );
91 assertEquals( "value", get( key ) );
92 assertFalse( set( key, "wrong", "changed" ) );
93 assertEquals( "value", get( key ) );
94 assertTrue( set( key, "value", "changed" ) );
95 assertEquals( "changed", get( key ) );
96 assertFalse( set( key, "wrong", null ) );
97 assertEquals( "changed", get( key ) );
98 assertTrue( set( key, "changed", null ) );
99 assertNull( get( key ) );
100 assertTrue( set( key, null, null ) );
101 assertNull( get( key ) );
102 }
103
104 @Test
105 public void testComputeIfAbsent()
106 {
107 Object key = "key";
108 assertNull( get( key ) );
109 assertEquals( "value", computeIfAbsent( key, () -> "value" ) );
110 assertEquals( "value", computeIfAbsent( key, () -> "changed" ) );
111 }
112
113 @Test( timeout = 10000L )
114 public void testConcurrency()
115 throws Exception
116 {
117 final AtomicReference<Throwable> error = new AtomicReference<>();
118 Thread[] threads = new Thread[20];
119 for ( int i = 0; i < threads.length; i++ )
120 {
121 threads[i] = new Thread()
122 {
123 @Override
124 public void run()
125 {
126 for ( int i = 0; i < 100; i++ )
127 {
128 String key = UUID.randomUUID().toString();
129 try
130 {
131 set( key, Boolean.TRUE );
132 assertEquals( Boolean.TRUE, get( key ) );
133 }
134 catch ( Throwable t )
135 {
136 error.compareAndSet( null, t );
137 t.printStackTrace();
138 }
139 }
140 }
141 };
142 }
143 for ( Thread thread : threads )
144 {
145 thread.start();
146 }
147 for ( Thread thread : threads )
148 {
149 thread.join();
150 }
151 assertNull( String.valueOf( error.get() ), error.get() );
152 }
153 }