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