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 SecretAuthenticationTest
033{
034
035    private RepositorySystemSession newSession()
036    {
037        return new DefaultRepositorySystemSession();
038    }
039
040    private RemoteRepository newRepo( Authentication auth )
041    {
042        return new RemoteRepository.Builder( "test", "default", "http://localhost" ).setAuthentication( auth ).build();
043    }
044
045    private AuthenticationContext newContext( Authentication auth )
046    {
047        return AuthenticationContext.forRepository( newSession(), newRepo( auth ) );
048    }
049
050    private String newDigest( Authentication auth )
051    {
052        return AuthenticationDigest.forRepository( newSession(), newRepo( auth ) );
053    }
054
055    @Test
056    public void testConstructor_CopyChars()
057    {
058        char[] value = { 'v', 'a', 'l' };
059        new SecretAuthentication( "key", value );
060        assertArrayEquals( new char[] { 'v', 'a', 'l' }, value );
061    }
062
063    @Test
064    public void testFill()
065    {
066        Authentication auth = new SecretAuthentication( "key", "value" );
067        AuthenticationContext context = newContext( auth );
068        assertNull( context.get( "another-key" ) );
069        assertEquals( "value", context.get( "key" ) );
070    }
071
072    @Test
073    public void testDigest()
074    {
075        Authentication auth1 = new SecretAuthentication( "key", "value" );
076        Authentication auth2 = new SecretAuthentication( "key", "value" );
077        String digest1 = newDigest( auth1 );
078        String digest2 = newDigest( auth2 );
079        assertEquals( digest1, digest2 );
080
081        Authentication auth3 = new SecretAuthentication( "key", "Value" );
082        String digest3 = newDigest( auth3 );
083        assertNotEquals( digest3, digest1 );
084
085        Authentication auth4 = new SecretAuthentication( "Key", "value" );
086        String digest4 = newDigest( auth4 );
087        assertNotEquals( digest4, digest1 );
088    }
089
090    @Test
091    public void testEquals()
092    {
093        Authentication auth1 = new SecretAuthentication( "key", "value" );
094        Authentication auth2 = new SecretAuthentication( "key", "value" );
095        Authentication auth3 = new SecretAuthentication( "key", "Value" );
096        assertEquals( auth1, auth2 );
097        assertNotEquals( auth1, auth3 );
098        assertNotEquals( null, auth1 );
099    }
100
101    @Test
102    public void testHashCode()
103    {
104        Authentication auth1 = new SecretAuthentication( "key", "value" );
105        Authentication auth2 = new SecretAuthentication( "key", "value" );
106        assertEquals( auth1.hashCode(), auth2.hashCode() );
107    }
108
109}