View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl.checksum;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.eclipse.aether.DefaultRepositorySystemSession;
26  import org.eclipse.aether.artifact.Artifact;
27  import org.eclipse.aether.artifact.DefaultArtifact;
28  import org.eclipse.aether.impl.RepositorySystemLifecycle;
29  import org.eclipse.aether.internal.impl.DefaultRepositorySystemLifecycle;
30  import org.eclipse.aether.internal.test.util.TestUtils;
31  import org.eclipse.aether.spi.checksums.TrustedChecksumsSource;
32  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  import static org.hamcrest.MatcherAssert.assertThat;
37  import static org.hamcrest.Matchers.aMapWithSize;
38  import static org.hamcrest.Matchers.anEmptyMap;
39  import static org.hamcrest.Matchers.hasEntry;
40  import static org.hamcrest.Matchers.is;
41  import static org.hamcrest.Matchers.notNullValue;
42  import static org.hamcrest.Matchers.nullValue;
43  import static org.junit.Assume.assumeThat;
44  
45  public abstract class FileTrustedChecksumsSourceTestSupport {
46      private static final Artifact ARTIFACT_WITHOUT_CHECKSUM = new DefaultArtifact("test:test:1.0");
47  
48      private static final Artifact ARTIFACT_WITH_CHECKSUM = new DefaultArtifact("test:test:2.0");
49  
50      private static final String ARTIFACT_TRUSTED_CHECKSUM = "trustedChecksum";
51  
52      private DefaultRepositorySystemSession session;
53  
54      private ChecksumAlgorithmFactory checksumAlgorithmFactory;
55  
56      private RepositorySystemLifecycle repositorySystemLifecycle;
57  
58      private FileTrustedChecksumsSourceSupport subject;
59  
60      private boolean checksumWritten;
61  
62      @Before
63      public void before() throws Exception {
64          session = TestUtils.newSession();
65          // populate local repository
66          checksumAlgorithmFactory = new Sha1ChecksumAlgorithmFactory();
67          repositorySystemLifecycle = new DefaultRepositorySystemLifecycle();
68          subject = prepareSubject(repositorySystemLifecycle);
69          checksumWritten = false;
70  
71          DefaultRepositorySystemSession prepareSession = new DefaultRepositorySystemSession(session);
72          enableSource(prepareSession);
73          TrustedChecksumsSource.Writer writer = subject.getTrustedArtifactChecksumsWriter(prepareSession);
74          if (writer != null) {
75              HashMap<String, String> checksums = new HashMap<>();
76              checksums.put(checksumAlgorithmFactory.getName(), ARTIFACT_TRUSTED_CHECKSUM);
77              writer.addTrustedArtifactChecksums(
78                      ARTIFACT_WITH_CHECKSUM,
79                      prepareSession.getLocalRepository(),
80                      Collections.singletonList(checksumAlgorithmFactory),
81                      checksums);
82              checksumWritten = true;
83          }
84      }
85  
86      protected abstract FileTrustedChecksumsSourceSupport prepareSubject(RepositorySystemLifecycle lifecycle);
87  
88      protected abstract void enableSource(DefaultRepositorySystemSession session);
89  
90      @Test
91      public void notEnabled() {
92          assertThat(
93                  subject.getTrustedArtifactChecksums(
94                          session,
95                          ARTIFACT_WITH_CHECKSUM,
96                          session.getLocalRepository(),
97                          Collections.singletonList(checksumAlgorithmFactory)),
98                  nullValue());
99      }
100 
101     @Test
102     public void noProvidedArtifactChecksum() {
103         enableSource(session);
104         Map<String, String> providedChecksums = subject.getTrustedArtifactChecksums(
105                 session,
106                 ARTIFACT_WITHOUT_CHECKSUM,
107                 session.getLocalRepository(),
108                 Collections.singletonList(checksumAlgorithmFactory));
109         assertThat(providedChecksums, notNullValue());
110         assertThat(providedChecksums, anEmptyMap());
111     }
112 
113     @Test
114     public void haveProvidedArtifactChecksum() {
115         assumeThat(checksumWritten, is(true));
116         enableSource(session);
117         Map<String, String> providedChecksums = subject.getTrustedArtifactChecksums(
118                 session,
119                 ARTIFACT_WITH_CHECKSUM,
120                 session.getLocalRepository(),
121                 Collections.singletonList(checksumAlgorithmFactory));
122         assertThat(providedChecksums, notNullValue());
123         assertThat(providedChecksums, aMapWithSize(1));
124         assertThat(providedChecksums, hasEntry(checksumAlgorithmFactory.getName(), ARTIFACT_TRUSTED_CHECKSUM));
125     }
126 }