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