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.util.repository;
20  
21  import org.eclipse.aether.DefaultRepositorySystemSession;
22  import org.eclipse.aether.RepositorySystemSession;
23  import org.eclipse.aether.repository.Authentication;
24  import org.eclipse.aether.repository.AuthenticationContext;
25  import org.eclipse.aether.repository.AuthenticationDigest;
26  import org.eclipse.aether.repository.RemoteRepository;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.*;
30  
31  public class SecretAuthenticationTest {
32  
33      private RepositorySystemSession newSession() {
34          return new DefaultRepositorySystemSession();
35      }
36  
37      private RemoteRepository newRepo(Authentication auth) {
38          return new RemoteRepository.Builder("test", "default", "http://localhost")
39                  .setAuthentication(auth)
40                  .build();
41      }
42  
43      private AuthenticationContext newContext(Authentication auth) {
44          return AuthenticationContext.forRepository(newSession(), newRepo(auth));
45      }
46  
47      private String newDigest(Authentication auth) {
48          return AuthenticationDigest.forRepository(newSession(), newRepo(auth));
49      }
50  
51      @Test
52      public void testConstructor_CopyChars() {
53          char[] value = {'v', 'a', 'l'};
54          new SecretAuthentication("key", value);
55          assertArrayEquals(new char[] {'v', 'a', 'l'}, value);
56      }
57  
58      @Test
59      public void testFill() {
60          Authentication auth = new SecretAuthentication("key", "value");
61          AuthenticationContext context = newContext(auth);
62          assertNull(context.get("another-key"));
63          assertEquals("value", context.get("key"));
64      }
65  
66      @Test
67      public void testDigest() {
68          Authentication auth1 = new SecretAuthentication("key", "value");
69          Authentication auth2 = new SecretAuthentication("key", "value");
70          String digest1 = newDigest(auth1);
71          String digest2 = newDigest(auth2);
72          assertEquals(digest1, digest2);
73  
74          Authentication auth3 = new SecretAuthentication("key", "Value");
75          String digest3 = newDigest(auth3);
76          assertNotEquals(digest3, digest1);
77  
78          Authentication auth4 = new SecretAuthentication("Key", "value");
79          String digest4 = newDigest(auth4);
80          assertNotEquals(digest4, digest1);
81      }
82  
83      @Test
84      public void testEquals() {
85          Authentication auth1 = new SecretAuthentication("key", "value");
86          Authentication auth2 = new SecretAuthentication("key", "value");
87          Authentication auth3 = new SecretAuthentication("key", "Value");
88          assertEquals(auth1, auth2);
89          assertNotEquals(auth1, auth3);
90          assertNotEquals(null, auth1);
91      }
92  
93      @Test
94      public void testHashCode() {
95          Authentication auth1 = new SecretAuthentication("key", "value");
96          Authentication auth2 = new SecretAuthentication("key", "value");
97          assertEquals(auth1.hashCode(), auth2.hashCode());
98      }
99  }