View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether;
20  
21  import java.util.UUID;
22  import java.util.concurrent.atomic.AtomicReference;
23  import java.util.function.Supplier;
24  
25  import org.junit.jupiter.api.Test;
26  import org.junit.jupiter.api.Timeout;
27  
28  import static org.junit.jupiter.api.Assertions.*;
29  
30  public class DefaultSessionDataTest {
31  
32      private final DefaultSessionData data = new DefaultSessionData();
33  
34      private Object get(Object key) {
35          return data.get(key);
36      }
37  
38      private void set(Object key, Object value) {
39          data.set(key, value);
40      }
41  
42      private boolean set(Object key, Object oldValue, Object newValue) {
43          return data.set(key, oldValue, newValue);
44      }
45  
46      private Object computeIfAbsent(Object key, Supplier<Object> supplier) {
47          return data.computeIfAbsent(key, supplier);
48      }
49  
50      @Test
51      void testGet_NullKey() {
52          assertThrows(RuntimeException.class, () -> get(null));
53      }
54  
55      @Test
56      void testSet_NullKey() {
57          assertThrows(RuntimeException.class, () -> set(null, "data"));
58      }
59  
60      @Test
61      void testGetSet() {
62          Object key = "key";
63          assertNull(get(key));
64          set(key, "value");
65          assertEquals("value", get(key));
66          set(key, "changed");
67          assertEquals("changed", get(key));
68          set(key, null);
69          assertNull(get(key));
70      }
71  
72      @Test
73      void testGetSafeSet() {
74          Object key = "key";
75          assertNull(get(key));
76          assertFalse(set(key, "wrong", "value"));
77          assertNull(get(key));
78          assertTrue(set(key, null, "value"));
79          assertEquals("value", get(key));
80          assertTrue(set(key, "value", "value"));
81          assertEquals("value", get(key));
82          assertFalse(set(key, "wrong", "changed"));
83          assertEquals("value", get(key));
84          assertTrue(set(key, "value", "changed"));
85          assertEquals("changed", get(key));
86          assertFalse(set(key, "wrong", null));
87          assertEquals("changed", get(key));
88          assertTrue(set(key, "changed", null));
89          assertNull(get(key));
90          assertTrue(set(key, null, null));
91          assertNull(get(key));
92      }
93  
94      @Test
95      void testComputeIfAbsent() {
96          Object key = "key";
97          assertNull(get(key));
98          assertEquals("value", computeIfAbsent(key, () -> "value"));
99          assertEquals("value", computeIfAbsent(key, () -> "changed"));
100     }
101 
102     @Test
103     @Timeout(10L)
104     public void testConcurrency() throws InterruptedException {
105         final AtomicReference<Throwable> error = new AtomicReference<>();
106         Thread[] threads = new Thread[20];
107         for (int i = 0; i < threads.length; i++) {
108             threads[i] = new Thread() {
109                 @Override
110                 public void run() {
111                     for (int i = 0; i < 100; i++) {
112                         String key = UUID.randomUUID().toString();
113                         try {
114                             set(key, Boolean.TRUE);
115                             assertEquals(Boolean.TRUE, get(key));
116                         } catch (Throwable t) {
117                             error.compareAndSet(null, t);
118                             t.printStackTrace();
119                         }
120                     }
121                 }
122             };
123         }
124         for (Thread thread : threads) {
125             thread.start();
126         }
127         for (Thread thread : threads) {
128             thread.join();
129         }
130         assertNull(error.get(), String.valueOf(error.get()));
131     }
132 }