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