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.Map;
025
026import org.eclipse.aether.repository.Authentication;
027import org.eclipse.aether.repository.AuthenticationContext;
028import org.eclipse.aether.repository.AuthenticationDigest;
029import org.eclipse.aether.repository.Proxy;
030import org.eclipse.aether.repository.RemoteRepository;
031import org.junit.Test;
032
033/**
034 */
035public class DefaultRepositorySystemSessionTest
036{
037
038    @Test
039    public void testDefaultProxySelectorUsesExistingProxy()
040    {
041        DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
042
043        RemoteRepository repo = new RemoteRepository.Builder( "id", "default", "void" ).build();
044        assertSame( null, session.getProxySelector().getProxy( repo ) );
045
046        Proxy proxy = new Proxy( "http", "localhost", 8080, null );
047        repo = new RemoteRepository.Builder( repo ).setProxy( proxy ).build();
048        assertSame( proxy, session.getProxySelector().getProxy( repo ) );
049    }
050
051    @Test
052    public void testDefaultAuthenticationSelectorUsesExistingAuth()
053    {
054        DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
055
056        RemoteRepository repo = new RemoteRepository.Builder( "id", "default", "void" ).build();
057        assertSame( null, session.getAuthenticationSelector().getAuthentication( repo ) );
058
059        Authentication auth = new Authentication()
060        {
061            public void fill( AuthenticationContext context, String key, Map<String, String> data )
062            {
063            }
064
065            public void digest( AuthenticationDigest digest )
066            {
067            }
068        };
069        repo = new RemoteRepository.Builder( repo ).setAuthentication( auth ).build();
070        assertSame( auth, session.getAuthenticationSelector().getAuthentication( repo ) );
071    }
072
073    @Test
074    public void testCopyConstructorCopiesPropertiesDeep()
075    {
076        DefaultRepositorySystemSession session1 = new DefaultRepositorySystemSession();
077        session1.setUserProperties( System.getProperties() );
078        session1.setSystemProperties( System.getProperties() );
079        session1.setConfigProperties( System.getProperties() );
080
081        DefaultRepositorySystemSession session2 = new DefaultRepositorySystemSession( session1 );
082        session2.setUserProperty( "key", "test" );
083        session2.setSystemProperty( "key", "test" );
084        session2.setConfigProperty( "key", "test" );
085
086        assertEquals( null, session1.getUserProperties().get( "key" ) );
087        assertEquals( null, session1.getSystemProperties().get( "key" ) );
088        assertEquals( null, session1.getConfigProperties().get( "key" ) );
089    }
090
091    @Test
092    public void testReadOnlyProperties()
093    {
094        DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
095
096        try
097        {
098            session.getUserProperties().put( "key", "test" );
099            fail( "user properties are modifiable" );
100        }
101        catch ( UnsupportedOperationException e )
102        {
103            // expected
104        }
105
106        try
107        {
108            session.getSystemProperties().put( "key", "test" );
109            fail( "system properties are modifiable" );
110        }
111        catch ( UnsupportedOperationException e )
112        {
113            // expected
114        }
115
116        try
117        {
118            session.getConfigProperties().put( "key", "test" );
119            fail( "config properties are modifiable" );
120        }
121        catch ( UnsupportedOperationException e )
122        {
123            // expected
124        }
125    }
126
127}