001package org.eclipse.aether.util.repository; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.junit.Assert.*; 023 024import org.eclipse.aether.DefaultRepositorySystemSession; 025import org.eclipse.aether.RepositorySystemSession; 026import org.eclipse.aether.repository.Authentication; 027import org.eclipse.aether.repository.AuthenticationContext; 028import org.eclipse.aether.repository.AuthenticationDigest; 029import org.eclipse.aether.repository.RemoteRepository; 030import org.junit.Test; 031 032public class ComponentAuthenticationTest 033{ 034 035 private static class Component 036 { 037 } 038 039 private RepositorySystemSession newSession() 040 { 041 return new DefaultRepositorySystemSession(); 042 } 043 044 private RemoteRepository newRepo( Authentication auth ) 045 { 046 return new RemoteRepository.Builder( "test", "default", "http://localhost" ).setAuthentication( auth ).build(); 047 } 048 049 private AuthenticationContext newContext( Authentication auth ) 050 { 051 return AuthenticationContext.forRepository( newSession(), newRepo( auth ) ); 052 } 053 054 private String newDigest( Authentication auth ) 055 { 056 return AuthenticationDigest.forRepository( newSession(), newRepo( auth ) ); 057 } 058 059 @Test 060 public void testFill() 061 { 062 Component comp = new Component(); 063 Authentication auth = new ComponentAuthentication( "key", comp ); 064 AuthenticationContext context = newContext( auth ); 065 assertEquals( null, context.get( "another-key" ) ); 066 assertSame( comp, context.get( "key", Component.class ) ); 067 } 068 069 @Test 070 public void testDigest() 071 { 072 Authentication auth1 = new ComponentAuthentication( "key", new Component() ); 073 Authentication auth2 = new ComponentAuthentication( "key", new Component() ); 074 String digest1 = newDigest( auth1 ); 075 String digest2 = newDigest( auth2 ); 076 assertEquals( digest1, digest2 ); 077 078 Authentication auth3 = new ComponentAuthentication( "key", new Object() ); 079 String digest3 = newDigest( auth3 ); 080 assertFalse( digest3.equals( digest1 ) ); 081 082 Authentication auth4 = new ComponentAuthentication( "Key", new Component() ); 083 String digest4 = newDigest( auth4 ); 084 assertFalse( digest4.equals( digest1 ) ); 085 } 086 087 @Test 088 public void testEquals() 089 { 090 Authentication auth1 = new ComponentAuthentication( "key", new Component() ); 091 Authentication auth2 = new ComponentAuthentication( "key", new Component() ); 092 Authentication auth3 = new ComponentAuthentication( "key", new Object() ); 093 assertEquals( auth1, auth2 ); 094 assertFalse( auth1.equals( auth3 ) ); 095 assertFalse( auth1.equals( null ) ); 096 } 097 098 @Test 099 public void testHashCode() 100 { 101 Authentication auth1 = new ComponentAuthentication( "key", new Component() ); 102 Authentication auth2 = new ComponentAuthentication( "key", new Component() ); 103 assertEquals( auth1.hashCode(), auth2.hashCode() ); 104 } 105 106}