1 package org.eclipse.aether.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.*;
23
24 import java.util.Map;
25
26 import org.eclipse.aether.DefaultRepositorySystemSession;
27 import org.eclipse.aether.RepositorySystemSession;
28 import org.junit.Test;
29
30 public class AuthenticationDigestTest
31 {
32
33 private RepositorySystemSession newSession()
34 {
35 return new DefaultRepositorySystemSession();
36 }
37
38 private RemoteRepository newRepo( Authentication auth, Proxy proxy )
39 {
40 return new RemoteRepository.Builder( "test", "default", "http://localhost" ) //
41 .setAuthentication( auth ).setProxy( proxy ).build();
42 }
43
44 private Proxy newProxy( Authentication auth )
45 {
46 return new Proxy( Proxy.TYPE_HTTP, "localhost", 8080, auth );
47 }
48
49 @Test
50 public void testForRepository()
51 {
52 final RepositorySystemSession session = newSession();
53 final RemoteRepository[] repos = { null };
54
55 Authentication auth = new Authentication()
56 {
57 public void fill( AuthenticationContext context, String key, Map<String, String> data )
58 {
59 fail( "AuthenticationDigest should not call fill()" );
60 }
61
62 public void digest( AuthenticationDigest digest )
63 {
64 assertNotNull( digest );
65 assertSame( session, digest.getSession() );
66 assertNotNull( digest.getRepository() );
67 assertNull( digest.getProxy() );
68 assertNull( "digest() should only be called once", repos[0] );
69 repos[0] = digest.getRepository();
70
71 digest.update( (byte[]) null );
72 digest.update( (char[]) null );
73 digest.update( (String[]) null );
74 digest.update( null, null );
75 }
76 };
77
78 RemoteRepository repo = newRepo( auth, newProxy( null ) );
79
80 String digest = AuthenticationDigest.forRepository( session, repo );
81 assertSame( repo, repos[0] );
82 assertNotNull( digest );
83 assertTrue( digest.length() > 0 );
84 }
85
86 @Test
87 public void testForRepository_NoAuth()
88 {
89 RemoteRepository repo = newRepo( null, null );
90
91 String digest = AuthenticationDigest.forRepository( newSession(), repo );
92 assertEquals( "", digest );
93 }
94
95 @Test
96 public void testForProxy()
97 {
98 final RepositorySystemSession session = newSession();
99 final Proxy[] proxies = { null };
100
101 Authentication auth = new Authentication()
102 {
103 public void fill( AuthenticationContext context, String key, Map<String, String> data )
104 {
105 fail( "AuthenticationDigest should not call fill()" );
106 }
107
108 public void digest( AuthenticationDigest digest )
109 {
110 assertNotNull( digest );
111 assertSame( session, digest.getSession() );
112 assertNotNull( digest.getRepository() );
113 assertNotNull( digest.getProxy() );
114 assertNull( "digest() should only be called once", proxies[0] );
115 proxies[0] = digest.getProxy();
116
117 digest.update( (byte[]) null );
118 digest.update( (char[]) null );
119 digest.update( (String[]) null );
120 digest.update( null, null );
121 }
122 };
123
124 Proxy proxy = newProxy( auth );
125
126 String digest = AuthenticationDigest.forProxy( session, newRepo( null, proxy ) );
127 assertSame( proxy, proxies[0] );
128 assertNotNull( digest );
129 assertTrue( digest.length() > 0 );
130 }
131
132 @Test
133 public void testForProxy_NoProxy()
134 {
135 RemoteRepository repo = newRepo( null, null );
136
137 String digest = AuthenticationDigest.forProxy( newSession(), repo );
138 assertEquals( "", digest );
139 }
140
141 @Test
142 public void testForProxy_NoProxyAuth()
143 {
144 RemoteRepository repo = newRepo( null, newProxy( null ) );
145
146 String digest = AuthenticationDigest.forProxy( newSession(), repo );
147 assertEquals( "", digest );
148 }
149
150 }