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