001package org.eclipse.aether.connector.basic;
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 java.nio.ByteBuffer;
023import java.security.MessageDigest;
024import java.security.NoSuchAlgorithmException;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.List;
028import java.util.Locale;
029import java.util.Set;
030
031import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithm;
032import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
033import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
034import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySupport;
035import org.eclipse.aether.util.ChecksumUtils;
036
037import static java.util.stream.Collectors.toList;
038
039/**
040 * Test implementation of {@link ChecksumAlgorithmFactorySelector}.
041 */
042public class TestChecksumAlgorithmSelector
043        implements ChecksumAlgorithmFactorySelector
044{
045    public static final String SHA512 = "SHA-512";
046
047    public static final String SHA256 = "SHA-256";
048
049    public static final String SHA1 = "SHA-1";
050
051    public static final String MD5 = "MD5";
052
053    public static final String TEST_CHECKSUM = "test";
054
055    public static final String TEST_CHECKSUM_VALUE = "01020304";
056
057    @Override
058    public Set<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories()
059    {
060        return Collections.emptySet(); // irrelevant
061    }
062
063    @Override
064    public ChecksumAlgorithmFactory select( final String algorithm )
065    {
066        if ( TEST_CHECKSUM.equals( algorithm ) )
067        {
068            return new ChecksumAlgorithmFactorySupport( TEST_CHECKSUM, "test" )
069            {
070                @Override
071                public ChecksumAlgorithm getAlgorithm()
072                {
073                    return new ChecksumAlgorithm()
074                    {
075                        @Override
076                        public void update( final ByteBuffer input )
077                        {
078
079                        }
080
081                        @Override
082                        public String checksum()
083                        {
084                            return TEST_CHECKSUM_VALUE;
085                        }
086                    };
087                }
088            };
089        }
090        return new MessageDigestChecksumAlgorithmFactory( algorithm );
091    }
092
093    @Override
094    public List<ChecksumAlgorithmFactory> selectList( Collection<String> algorithmNames )
095    {
096        return algorithmNames.stream()
097                .map( this::select )
098                .collect( toList() );
099    }
100
101    private static class MessageDigestChecksumAlgorithmFactory
102            extends ChecksumAlgorithmFactorySupport
103    {
104        public MessageDigestChecksumAlgorithmFactory( String name )
105        {
106            super( name, name.replace( "-", "" ).toLowerCase( Locale.ENGLISH ) );
107        }
108
109        @Override
110        public ChecksumAlgorithm getAlgorithm()
111        {
112            try
113            {
114                MessageDigest messageDigest = MessageDigest.getInstance( getName() );
115                return new ChecksumAlgorithm()
116                {
117                    @Override
118                    public void update( final ByteBuffer input )
119                    {
120                        messageDigest.update( input );
121                    }
122
123                    @Override
124                    public String checksum()
125                    {
126                        return ChecksumUtils.toHexString( messageDigest.digest() );
127                    }
128                };
129            }
130            catch ( NoSuchAlgorithmException e )
131            {
132                throw new IllegalArgumentException( "Algorithm '" + getName() + "' not supported." );
133            }
134        }
135    }
136}