001package org.eclipse.aether.internal.impl;
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.io.File;
023import java.io.IOException;
024import java.nio.charset.StandardCharsets;
025import java.nio.file.Files;
026import java.nio.file.Path;
027import java.util.Map;
028
029import org.eclipse.aether.DefaultRepositorySystemSession;
030import org.eclipse.aether.artifact.DefaultArtifact;
031import org.eclipse.aether.internal.impl.checksum.Sha1ChecksumAlgorithmFactory;
032import org.eclipse.aether.internal.test.util.TestFileProcessor;
033import org.eclipse.aether.internal.test.util.TestUtils;
034import org.eclipse.aether.repository.RemoteRepository;
035import org.eclipse.aether.repository.RepositoryPolicy;
036import org.eclipse.aether.spi.connector.ArtifactDownload;
037import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
038import org.eclipse.aether.transfer.NoRepositoryLayoutException;
039import org.junit.Before;
040import org.junit.Test;
041
042import static org.junit.Assert.assertEquals;
043import static org.junit.Assert.assertNotNull;
044import static org.junit.Assert.assertNull;
045
046public class FileProvidedChecksumsSourceTest
047{
048  private DefaultRepositorySystemSession session;
049
050  private RepositoryLayout repositoryLayout;
051
052  private FileProvidedChecksumsSource subject;
053
054  @Before
055  public void setup() throws NoRepositoryLayoutException, IOException
056  {
057    RemoteRepository repository = new RemoteRepository.Builder("test", "default", "https://irrelevant.com").build();
058    session = TestUtils.newSession();
059    repositoryLayout = new Maven2RepositoryLayoutFactory().newInstance(session, repository);
060    subject = new FileProvidedChecksumsSource(new TestFileProcessor(), new DefaultLocalPathComposer() );
061
062    // populate local repository
063    Path baseDir = session.getLocalRepository().getBasedir().toPath().resolve( FileProvidedChecksumsSource.LOCAL_REPO_PREFIX);
064
065    // artifact: test:test:2.0 => "foobar"
066    {
067      Path test = baseDir.resolve("test/test/2.0/test-2.0.jar.sha1");
068      Files.createDirectories(test.getParent());
069      Files.write(test, "foobar".getBytes(StandardCharsets.UTF_8));
070    }
071  }
072
073  @Test
074  public void noProvidedArtifactChecksum()
075  {
076    ArtifactDownload transfer = new ArtifactDownload(
077        new DefaultArtifact("test:test:1.0"),
078        "irrelevant",
079        new File("irrelevant"),
080        RepositoryPolicy.CHECKSUM_POLICY_FAIL
081    );
082    Map<String, String> providedChecksums = subject.getProvidedArtifactChecksums(
083        session,
084        transfer,
085        repositoryLayout.getChecksumAlgorithmFactories()
086    );
087    assertNull(providedChecksums);
088  }
089
090  @Test
091  public void haveProvidedArtifactChecksum()
092  {
093    ArtifactDownload transfer = new ArtifactDownload(
094        new DefaultArtifact("test:test:2.0"),
095        "irrelevant",
096        new File("irrelevant"),
097        RepositoryPolicy.CHECKSUM_POLICY_FAIL
098    );
099    Map<String, String> providedChecksums = subject.getProvidedArtifactChecksums(
100        session,
101        transfer,
102        repositoryLayout.getChecksumAlgorithmFactories()
103    );
104    assertNotNull(providedChecksums);
105    assertEquals(providedChecksums.get(Sha1ChecksumAlgorithmFactory.NAME), "foobar");
106  }
107}