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.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
95 }
96
97 try {
98 session.getSystemProperties().put("key", "test");
99 fail("system properties are modifiable");
100 } catch (UnsupportedOperationException e) {
101
102 }
103
104 try {
105 session.getConfigProperties().put("key", "test");
106 fail("config properties are modifiable");
107 } catch (UnsupportedOperationException e) {
108
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 }