001package org.eclipse.aether; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.junit.Assert.*; 023 024import java.util.UUID; 025import java.util.concurrent.atomic.AtomicReference; 026 027import org.junit.Test; 028 029public class DefaultSessionDataTest 030{ 031 032 private DefaultSessionData data = new DefaultSessionData(); 033 034 private Object get( Object key ) 035 { 036 return data.get( key ); 037 } 038 039 private void set( Object key, Object value ) 040 { 041 data.set( key, value ); 042 } 043 044 private boolean set( Object key, Object oldValue, Object newValue ) 045 { 046 return data.set( key, oldValue, newValue ); 047 } 048 049 @Test( expected = RuntimeException.class ) 050 public void testGet_NullKey() 051 { 052 get( null ); 053 } 054 055 @Test( expected = RuntimeException.class ) 056 public void testSet_NullKey() 057 { 058 set( null, "data" ); 059 } 060 061 @Test 062 public void testGetSet() 063 { 064 Object key = "key"; 065 assertNull( get( key ) ); 066 set( key, "value" ); 067 assertEquals( "value", get( key ) ); 068 set( key, "changed" ); 069 assertEquals( "changed", get( key ) ); 070 set( key, null ); 071 assertNull( get( key ) ); 072 } 073 074 @Test 075 public void testGetSafeSet() 076 { 077 Object key = "key"; 078 assertNull( get( key ) ); 079 assertFalse( set( key, "wrong", "value" ) ); 080 assertNull( get( key ) ); 081 assertTrue( set( key, null, "value" ) ); 082 assertEquals( "value", get( key ) ); 083 assertTrue( set( key, "value", "value" ) ); 084 assertEquals( "value", get( key ) ); 085 assertFalse( set( key, "wrong", "changed" ) ); 086 assertEquals( "value", get( key ) ); 087 assertTrue( set( key, "value", "changed" ) ); 088 assertEquals( "changed", get( key ) ); 089 assertFalse( set( key, "wrong", null ) ); 090 assertEquals( "changed", get( key ) ); 091 assertTrue( set( key, "changed", null ) ); 092 assertNull( get( key ) ); 093 assertTrue( set( key, null, null ) ); 094 assertNull( get( key ) ); 095 } 096 097 @Test( timeout = 10000L ) 098 public void testConcurrency() 099 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}