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  
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  }