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