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      @Test
39      void testDefaultProxySelectorUsesExistingProxy() {
40          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
41  
42          RemoteRepository repo = new RemoteRepository.Builder("id", "default", "void").build();
43          assertSame(null, session.getProxySelector().getProxy(repo));
44  
45          Proxy proxy = new Proxy("http", "localhost", 8080, null);
46          repo = new RemoteRepository.Builder(repo).setProxy(proxy).build();
47          assertSame(proxy, session.getProxySelector().getProxy(repo));
48      }
49  
50      @Test
51      void testDefaultAuthenticationSelectorUsesExistingAuth() {
52          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
53  
54          RemoteRepository repo = new RemoteRepository.Builder("id", "default", "void").build();
55          assertSame(null, session.getAuthenticationSelector().getAuthentication(repo));
56  
57          Authentication auth = new Authentication() {
58              public void fill(AuthenticationContext context, String key, Map<String, String> data) {}
59  
60              public void digest(AuthenticationDigest digest) {}
61          };
62          repo = new RemoteRepository.Builder(repo).setAuthentication(auth).build();
63          assertSame(auth, session.getAuthenticationSelector().getAuthentication(repo));
64      }
65  
66      @Test
67      void testCopyConstructorCopiesPropertiesDeep() {
68          DefaultRepositorySystemSession session1 = new DefaultRepositorySystemSession();
69          session1.setUserProperties(System.getProperties());
70          session1.setSystemProperties(System.getProperties());
71          session1.setConfigProperties(System.getProperties());
72  
73          DefaultRepositorySystemSession session2 = new DefaultRepositorySystemSession(session1);
74          session2.setUserProperty("key", "test");
75          session2.setSystemProperty("key", "test");
76          session2.setConfigProperty("key", "test");
77  
78          assertNull(session1.getUserProperties().get("key"));
79          assertNull(session1.getSystemProperties().get("key"));
80          assertNull(session1.getConfigProperties().get("key"));
81      }
82  
83      @Test
84      void testReadOnlyProperties() {
85          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
86  
87          try {
88              session.getUserProperties().put("key", "test");
89              fail("user properties are modifiable");
90          } catch (UnsupportedOperationException e) {
91              // expected
92          }
93  
94          try {
95              session.getSystemProperties().put("key", "test");
96              fail("system properties are modifiable");
97          } catch (UnsupportedOperationException e) {
98              // expected
99          }
100 
101         try {
102             session.getConfigProperties().put("key", "test");
103             fail("config properties are modifiable");
104         } catch (UnsupportedOperationException e) {
105             // expected
106         }
107     }
108 
109     @Test
110     void testCopyRepositorySystemSession() throws Exception {
111         RepositorySystemSession session = Mockito.mock(RepositorySystemSession.class, Mockito.RETURNS_MOCKS);
112 
113         RepositorySystemSession newSession = new DefaultRepositorySystemSession(session);
114 
115         Method[] methods = RepositorySystemSession.class.getMethods();
116 
117         for (Method method : methods) {
118             if (method.getParameterCount() == 0) {
119                 assertEquals(method.invoke(session) == null, method.invoke(newSession) == null, method.getName());
120             }
121         }
122     }
123 }