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