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