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 DefaultSessionDataTest
30  {
31  
32      private DefaultSessionData data = new DefaultSessionData();
33  
34      private Object get( Object key )
35      {
36          return data.get( key );
37      }
38  
39      private void set( Object key, Object value )
40      {
41          data.set( key, value );
42      }
43  
44      private boolean set( Object key, Object oldValue, Object newValue )
45      {
46          return data.set( key, oldValue, newValue );
47      }
48  
49      @Test( expected = RuntimeException.class )
50      public void testGet_NullKey()
51      {
52          get( null );
53      }
54  
55      @Test( expected = RuntimeException.class )
56      public void testSet_NullKey()
57      {
58          set( null, "data" );
59      }
60  
61      @Test
62      public void testGetSet()
63      {
64          Object key = "key";
65          assertNull( get( key ) );
66          set( key, "value" );
67          assertEquals( "value", get( key ) );
68          set( key, "changed" );
69          assertEquals( "changed", get( key ) );
70          set( key, null );
71          assertNull( get( key ) );
72      }
73  
74      @Test
75      public void testGetSafeSet()
76      {
77          Object key = "key";
78          assertNull( get( key ) );
79          assertFalse( set( key, "wrong", "value" ) );
80          assertNull( get( key ) );
81          assertTrue( set( key, null, "value" ) );
82          assertEquals( "value", get( key ) );
83          assertTrue( set( key, "value", "value" ) );
84          assertEquals( "value", get( key ) );
85          assertFalse( set( key, "wrong", "changed" ) );
86          assertEquals( "value", get( key ) );
87          assertTrue( set( key, "value", "changed" ) );
88          assertEquals( "changed", get( key ) );
89          assertFalse( set( key, "wrong", null ) );
90          assertEquals( "changed", get( key ) );
91          assertTrue( set( key, "changed", null ) );
92          assertNull( get( key ) );
93          assertTrue( set( key, null, null ) );
94          assertNull( get( key ) );
95      }
96  
97      @Test( timeout = 10000L )
98      public void testConcurrency()
99          throws Exception
100     {
101         final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
102         Thread threads[] = new Thread[20];
103         for ( int i = 0; i < threads.length; i++ )
104         {
105             threads[i] = new Thread()
106             {
107                 @Override
108                 public void run()
109                 {
110                     for ( int i = 0; i < 100; i++ )
111                     {
112                         String key = UUID.randomUUID().toString();
113                         try
114                         {
115                             set( key, Boolean.TRUE );
116                             assertEquals( Boolean.TRUE, get( key ) );
117                         }
118                         catch ( Throwable t )
119                         {
120                             error.compareAndSet( null, t );
121                             t.printStackTrace();
122                         }
123                     }
124                 }
125             };
126         }
127         for ( Thread thread : threads )
128         {
129             thread.start();
130         }
131         for ( Thread thread : threads )
132         {
133             thread.join();
134         }
135         assertNull( String.valueOf( error.get() ), error.get() );
136     }
137 }