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