View Javadoc
1   package org.eclipse.aether.connector.basic;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.net.URI;
27  import java.nio.ByteBuffer;
28  import java.nio.charset.StandardCharsets;
29  import java.security.NoSuchAlgorithmException;
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.eclipse.aether.internal.test.util.TestFileUtils;
35  import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  public class ChecksumCalculatorTest
40  {
41  
42      private static final String SHA1 = "SHA-1";
43  
44      private static final String MD5 = "MD5";
45  
46      private File file;
47  
48      private ChecksumCalculator newCalculator( String... algos )
49      {
50          List<RepositoryLayout.Checksum> checksums = new ArrayList<RepositoryLayout.Checksum>();
51          for ( String algo : algos )
52          {
53              checksums.add( new RepositoryLayout.Checksum( algo, URI.create( "irrelevant" ) ) );
54          }
55          return ChecksumCalculator.newInstance( file, checksums );
56      }
57  
58      private ByteBuffer toBuffer( String data )
59      {
60          return ByteBuffer.wrap( data.getBytes( StandardCharsets.UTF_8 ) );
61      }
62  
63      @Before
64      public void init()
65          throws Exception
66      {
67          file = TestFileUtils.createTempFile( "Hello World!" );
68      }
69  
70      @Test
71      public void testNoOffset()
72      {
73          ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
74          calculator.init( 0 );
75          calculator.update( toBuffer( "Hello World!" ) );
76          Map<String, Object> digests = calculator.get();
77          assertNotNull( digests );
78          assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
79          assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
80          assertEquals( 2, digests.size() );
81      }
82  
83      @Test
84      public void testWithOffset()
85      {
86          ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
87          calculator.init( 6 );
88          calculator.update( toBuffer( "World!" ) );
89          Map<String, Object> digests = calculator.get();
90          assertNotNull( digests );
91          assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
92          assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
93          assertEquals( 2, digests.size() );
94      }
95  
96      @Test
97      public void testWithExcessiveOffset()
98      {
99          ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
100         calculator.init( 100 );
101         calculator.update( toBuffer( "World!" ) );
102         Map<String, Object> digests = calculator.get();
103         assertNotNull( digests );
104         assertTrue( digests.get( SHA1 ) instanceof IOException );
105         assertTrue( digests.get( MD5 ) instanceof IOException );
106         assertEquals( 2, digests.size() );
107     }
108 
109     @Test
110     public void testUnknownAlgorithm()
111     {
112         ChecksumCalculator calculator = newCalculator( "unknown", SHA1 );
113         calculator.init( 0 );
114         calculator.update( toBuffer( "Hello World!" ) );
115         Map<String, Object> digests = calculator.get();
116         assertNotNull( digests );
117         assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
118         assertTrue( digests.get( "unknown" ) instanceof NoSuchAlgorithmException );
119         assertEquals( 2, digests.size() );
120     }
121 
122     @Test
123     public void testNoInitCall()
124     {
125         ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
126         calculator.update( toBuffer( "Hello World!" ) );
127         Map<String, Object> digests = calculator.get();
128         assertNotNull( digests );
129         assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
130         assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
131         assertEquals( 2, digests.size() );
132     }
133 
134     @Test
135     public void testRestart()
136     {
137         ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
138         calculator.init( 0 );
139         calculator.update( toBuffer( "Ignored" ) );
140         calculator.init( 0 );
141         calculator.update( toBuffer( "Hello World!" ) );
142         Map<String, Object> digests = calculator.get();
143         assertNotNull( digests );
144         assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
145         assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
146         assertEquals( 2, digests.size() );
147     }
148 
149     @Test
150     public void testRestartAfterError()
151     {
152         ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
153         calculator.init( 100 );
154         calculator.init( 0 );
155         calculator.update( toBuffer( "Hello World!" ) );
156         Map<String, Object> digests = calculator.get();
157         assertNotNull( digests );
158         assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
159         assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
160         assertEquals( 2, digests.size() );
161     }
162 
163 }