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 DefaultRepositoryCacheTest
030{
031
032    private DefaultRepositoryCache cache = new DefaultRepositoryCache();
033
034    private RepositorySystemSession session = new DefaultRepositorySystemSession();
035
036    private Object get( Object key )
037    {
038        return cache.get( session, key );
039    }
040
041    private void put( Object key, Object value )
042    {
043        cache.put( session, key, value );
044    }
045
046    @Test( expected = RuntimeException.class )
047    public void testGet_NullKey()
048    {
049        get( null );
050    }
051
052    @Test( expected = RuntimeException.class )
053    public void testPut_NullKey()
054    {
055        put( null, "data" );
056    }
057
058    @Test
059    public void testGetPut()
060    {
061        Object key = "key";
062        assertNull( get( key ) );
063        put( key, "value" );
064        assertEquals( "value", get( key ) );
065        put( key, "changed" );
066        assertEquals( "changed", get( key ) );
067        put( key, null );
068        assertNull( get( key ) );
069    }
070
071    @Test( timeout = 10000L )
072    public void testConcurrency()
073        throws Exception
074    {
075        final AtomicReference<Throwable> error = new AtomicReference<>();
076        Thread[] threads = new Thread[20];
077        for ( int i = 0; i < threads.length; i++ )
078        {
079            threads[i] = new Thread()
080            {
081                @Override
082                public void run()
083                {
084                    for ( int i = 0; i < 100; i++ )
085                    {
086                        String key = UUID.randomUUID().toString();
087                        try
088                        {
089                            put( key, Boolean.TRUE );
090                            assertEquals( Boolean.TRUE, get( key ) );
091                        }
092                        catch ( Throwable t )
093                        {
094                            error.compareAndSet( null, t );
095                            t.printStackTrace();
096                        }
097                    }
098                }
099            };
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}