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.lang.reflect.Method;
22  import java.util.Map;
23  
24  import org.eclipse.aether.repository.Authentication;
25  import org.eclipse.aether.repository.AuthenticationContext;
26  import org.eclipse.aether.repository.AuthenticationDigest;
27  import org.eclipse.aether.repository.Proxy;
28  import org.eclipse.aether.repository.RemoteRepository;
29  import org.junit.jupiter.api.Test;
30  import org.mockito.Mockito;
31  
32  import static org.junit.jupiter.api.Assertions.*;
33  
34  /**
35   */
36  public class DefaultRepositorySystemSessionTest {
37  
38      private DefaultRepositorySystemSession newSession() {
39          return new DefaultRepositorySystemSession(h -> false);
40      }
41  
42      @Test
43      void testDefaultProxySelectorUsesExistingProxy() {
44          DefaultRepositorySystemSession session = newSession();
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      void testDefaultAuthenticationSelectorUsesExistingAuth() {
56          DefaultRepositorySystemSession session = newSession();
57  
58          RemoteRepository repo = new RemoteRepository.Builder("id", "default", "void").build();
59          assertSame(null, session.getAuthenticationSelector().getAuthentication(repo));
60  
61          Authentication auth = new Authentication() {
62              public void fill(AuthenticationContext context, String key, Map<String, String> data) {}
63  
64              public void digest(AuthenticationDigest digest) {}
65          };
66          repo = new RemoteRepository.Builder(repo).setAuthentication(auth).build();
67          assertSame(auth, session.getAuthenticationSelector().getAuthentication(repo));
68      }
69  
70      @Test
71      void testCopyConstructorCopiesPropertiesDeep() {
72          DefaultRepositorySystemSession session1 = newSession();
73          session1.setUserProperties(System.getProperties());
74          session1.setSystemProperties(System.getProperties());
75          session1.setConfigProperties(System.getProperties());
76  
77          DefaultRepositorySystemSession session2 = new DefaultRepositorySystemSession(session1);
78          session2.setUserProperty("key", "test");
79          session2.setSystemProperty("key", "test");
80          session2.setConfigProperty("key", "test");
81  
82          assertNull(session1.getUserProperties().get("key"));
83          assertNull(session1.getSystemProperties().get("key"));
84          assertNull(session1.getConfigProperties().get("key"));
85      }
86  
87      @Test
88      void testReadOnlyProperties() {
89          DefaultRepositorySystemSession session = newSession();
90  
91          try {
92              session.getUserProperties().put("key", "test");
93              fail("user properties are modifiable");
94          } catch (UnsupportedOperationException e) {
95              // expected
96          }
97  
98          try {
99              session.getSystemProperties().put("key", "test");
100             fail("system properties are modifiable");
101         } catch (UnsupportedOperationException e) {
102             // expected
103         }
104 
105         try {
106             session.getConfigProperties().put("key", "test");
107             fail("config properties are modifiable");
108         } catch (UnsupportedOperationException e) {
109             // expected
110         }
111     }
112 
113     @Test
114     void testCopyRepositorySystemSession() throws Exception {
115         RepositorySystemSession session = Mockito.mock(RepositorySystemSession.class, Mockito.RETURNS_MOCKS);
116 
117         RepositorySystemSession newSession = new DefaultRepositorySystemSession(session);
118 
119         Method[] methods = RepositorySystemSession.class.getMethods();
120 
121         for (Method method : methods) {
122             if (method.getParameterCount() == 0) {
123                 assertEquals(method.invoke(session) == null, method.invoke(newSession) == null, method.getName());
124             }
125         }
126     }
127 }