View Javadoc
1   package org.eclipse.aether;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertSame;
23  import static org.junit.Assert.fail;
24  
25  import java.lang.reflect.Method;
26  import java.util.Map;
27  
28  import org.eclipse.aether.repository.Authentication;
29  import org.eclipse.aether.repository.AuthenticationContext;
30  import org.eclipse.aether.repository.AuthenticationDigest;
31  import org.eclipse.aether.repository.Proxy;
32  import org.eclipse.aether.repository.RemoteRepository;
33  import org.junit.Test;
34  import org.mockito.Mockito;
35  
36  /**
37   */
38  public class DefaultRepositorySystemSessionTest
39  {
40  
41      @Test
42      public void testDefaultProxySelectorUsesExistingProxy()
43      {
44          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
45  
46          RemoteRepository repo = new RemoteRepository.Builder( "id", "default", "void" ).build();
47          assertSame( null, session.getProxySelector().getProxy( repo ) );
48  
49          Proxy proxy = new Proxy( "http", "localhost", 8080, null );
50          repo = new RemoteRepository.Builder( repo ).setProxy( proxy ).build();
51          assertSame( proxy, session.getProxySelector().getProxy( repo ) );
52      }
53  
54      @Test
55      public void testDefaultAuthenticationSelectorUsesExistingAuth()
56      {
57          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
58  
59          RemoteRepository repo = new RemoteRepository.Builder( "id", "default", "void" ).build();
60          assertSame( null, session.getAuthenticationSelector().getAuthentication( repo ) );
61  
62          Authentication auth = new Authentication()
63          {
64              public void fill( AuthenticationContext context, String key, Map<String, String> data )
65              {
66              }
67  
68              public void digest( AuthenticationDigest digest )
69              {
70              }
71          };
72          repo = new RemoteRepository.Builder( repo ).setAuthentication( auth ).build();
73          assertSame( auth, session.getAuthenticationSelector().getAuthentication( repo ) );
74      }
75  
76      @Test
77      public void testCopyConstructorCopiesPropertiesDeep()
78      {
79          DefaultRepositorySystemSession session1 = new DefaultRepositorySystemSession();
80          session1.setUserProperties( System.getProperties() );
81          session1.setSystemProperties( System.getProperties() );
82          session1.setConfigProperties( System.getProperties() );
83  
84          DefaultRepositorySystemSession session2 = new DefaultRepositorySystemSession( session1 );
85          session2.setUserProperty( "key", "test" );
86          session2.setSystemProperty( "key", "test" );
87          session2.setConfigProperty( "key", "test" );
88  
89          assertEquals( null, session1.getUserProperties().get( "key" ) );
90          assertEquals( null, session1.getSystemProperties().get( "key" ) );
91          assertEquals( null, session1.getConfigProperties().get( "key" ) );
92      }
93  
94      @Test
95      public void testReadOnlyProperties()
96      {
97          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
98  
99          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 }