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.RepositorySystemSession;
22  import org.eclipse.aether.internal.test.util.TestUtils;
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.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.*;
30  
31  public class ComponentAuthenticationTest {
32  
33      private static class Component {}
34  
35      private RepositorySystemSession newSession() {
36          return TestUtils.newSession();
37      }
38  
39      private RemoteRepository newRepo(Authentication auth) {
40          return new RemoteRepository.Builder("test", "default", "http://localhost")
41                  .setAuthentication(auth)
42                  .build();
43      }
44  
45      private AuthenticationContext newContext(Authentication auth) {
46          return AuthenticationContext.forRepository(newSession(), newRepo(auth));
47      }
48  
49      private String newDigest(Authentication auth) {
50          return AuthenticationDigest.forRepository(newSession(), newRepo(auth));
51      }
52  
53      @Test
54      void testFill() {
55          Component comp = new Component();
56          Authentication auth = new ComponentAuthentication("key", comp);
57          AuthenticationContext context = newContext(auth);
58          assertNull(context.get("another-key"));
59          assertSame(comp, context.get("key", Component.class));
60      }
61  
62      @Test
63      void testDigest() {
64          Authentication auth1 = new ComponentAuthentication("key", new Component());
65          Authentication auth2 = new ComponentAuthentication("key", new Component());
66          String digest1 = newDigest(auth1);
67          String digest2 = newDigest(auth2);
68          assertEquals(digest1, digest2);
69  
70          Authentication auth3 = new ComponentAuthentication("key", new Object());
71          String digest3 = newDigest(auth3);
72          assertNotEquals(digest3, digest1);
73  
74          Authentication auth4 = new ComponentAuthentication("Key", new Component());
75          String digest4 = newDigest(auth4);
76          assertNotEquals(digest4, digest1);
77      }
78  
79      @Test
80      void testEquals() {
81          Authentication auth1 = new ComponentAuthentication("key", new Component());
82          Authentication auth2 = new ComponentAuthentication("key", new Component());
83          Authentication auth3 = new ComponentAuthentication("key", new Object());
84          assertEquals(auth1, auth2);
85          assertNotEquals(auth1, auth3);
86          assertNotEquals(null, auth1);
87      }
88  
89      @Test
90      void testHashCode() {
91          Authentication auth1 = new ComponentAuthentication("key", new Component());
92          Authentication auth2 = new ComponentAuthentication("key", new Component());
93          assertEquals(auth1.hashCode(), auth2.hashCode());
94      }
95  }