1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
92 }
93
94 try {
95 session.getSystemProperties().put("key", "test");
96 fail("system properties are modifiable");
97 } catch (UnsupportedOperationException e) {
98
99 }
100
101 try {
102 session.getConfigProperties().put("key", "test");
103 fail("config properties are modifiable");
104 } catch (UnsupportedOperationException e) {
105
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 }