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.apache.maven.internal.impl;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.nio.ByteBuffer;
24  import java.nio.charset.StandardCharsets;
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.apache.maven.api.services.ChecksumAlgorithmService;
31  import org.eclipse.aether.internal.impl.checksum.*;
32  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.*;
36  
37  public class DefaultChecksumAlgorithmServiceTest {
38      private static Map<String, ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() {
39          HashMap<String, ChecksumAlgorithmFactory> result = new HashMap<>();
40          result.put(Sha512ChecksumAlgorithmFactory.NAME, new Sha512ChecksumAlgorithmFactory());
41          result.put(Sha256ChecksumAlgorithmFactory.NAME, new Sha256ChecksumAlgorithmFactory());
42          result.put(Sha1ChecksumAlgorithmFactory.NAME, new Sha1ChecksumAlgorithmFactory());
43          result.put(Md5ChecksumAlgorithmFactory.NAME, new Md5ChecksumAlgorithmFactory());
44          return result;
45      }
46  
47      private final DefaultChecksumAlgorithmService service = new DefaultChecksumAlgorithmService(
48              new DefaultChecksumAlgorithmFactorySelector(getChecksumAlgorithmFactories()));
49  
50      @Test
51      void smokeTest() {
52          Collection<String> algNames = service.getChecksumAlgorithmNames();
53          assertEquals(4, algNames.size());
54      }
55  
56      @Test
57      void emptySha1Calculator() {
58          ChecksumAlgorithmService.ChecksumCalculator calculator =
59                  service.select("SHA-1").getCalculator();
60          calculator.update(ByteBuffer.allocate(0));
61          assertEquals(calculator.checksum(), "da39a3ee5e6b4b0d3255bfef95601890afd80709");
62      }
63  
64      @Test
65      void calculateByte() throws IOException {
66          Map<ChecksumAlgorithmService.ChecksumAlgorithm, String> checksums = service.calculate(
67                  "test".getBytes(StandardCharsets.UTF_8), service.select(Arrays.asList("SHA-1", "MD5")));
68          assertNotNull(checksums);
69          assertEquals(2, checksums.size());
70          assertEquals("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", checksums.get(service.select("SHA-1")));
71          assertEquals("098f6bcd4621d373cade4e832627b4f6", checksums.get(service.select("MD5")));
72      }
73  
74      @Test
75      void calculateByteBuffer() throws IOException {
76          Map<ChecksumAlgorithmService.ChecksumAlgorithm, String> checksums = service.calculate(
77                  ByteBuffer.wrap("test".getBytes(StandardCharsets.UTF_8)),
78                  service.select(Arrays.asList("SHA-1", "MD5")));
79          assertNotNull(checksums);
80          assertEquals(2, checksums.size());
81          assertEquals("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", checksums.get(service.select("SHA-1")));
82          assertEquals("098f6bcd4621d373cade4e832627b4f6", checksums.get(service.select("MD5")));
83      }
84  
85      @Test
86      void calculateStream() throws IOException {
87          Map<ChecksumAlgorithmService.ChecksumAlgorithm, String> checksums = service.calculate(
88                  new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)),
89                  service.select(Arrays.asList("SHA-1", "MD5")));
90          assertNotNull(checksums);
91          assertEquals(2, checksums.size());
92          assertEquals("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", checksums.get(service.select("SHA-1")));
93          assertEquals("098f6bcd4621d373cade4e832627b4f6", checksums.get(service.select("MD5")));
94      }
95  }