1 package org.eclipse.aether.connector.basic;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.*;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.UnsupportedEncodingException;
27 import java.net.URI;
28 import java.nio.ByteBuffer;
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 try
61 {
62 return ByteBuffer.wrap( data.getBytes( "UTF-8" ) );
63 }
64 catch ( UnsupportedEncodingException e )
65 {
66 throw new IllegalStateException( e );
67 }
68 }
69
70 @Before
71 public void init()
72 throws Exception
73 {
74 file = TestFileUtils.createTempFile( "Hello World!" );
75 }
76
77 @Test
78 public void testNoOffset()
79 {
80 ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
81 calculator.init( 0 );
82 calculator.update( toBuffer( "Hello World!" ) );
83 Map<String, Object> digests = calculator.get();
84 assertNotNull( digests );
85 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
86 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
87 assertEquals( 2, digests.size() );
88 }
89
90 @Test
91 public void testWithOffset()
92 {
93 ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
94 calculator.init( 6 );
95 calculator.update( toBuffer( "World!" ) );
96 Map<String, Object> digests = calculator.get();
97 assertNotNull( digests );
98 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
99 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
100 assertEquals( 2, digests.size() );
101 }
102
103 @Test
104 public void testWithExcessiveOffset()
105 {
106 ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
107 calculator.init( 100 );
108 calculator.update( toBuffer( "World!" ) );
109 Map<String, Object> digests = calculator.get();
110 assertNotNull( digests );
111 assertTrue( digests.get( SHA1 ) instanceof IOException );
112 assertTrue( digests.get( MD5 ) instanceof IOException );
113 assertEquals( 2, digests.size() );
114 }
115
116 @Test
117 public void testUnknownAlgorithm()
118 {
119 ChecksumCalculator calculator = newCalculator( "unknown", SHA1 );
120 calculator.init( 0 );
121 calculator.update( toBuffer( "Hello World!" ) );
122 Map<String, Object> digests = calculator.get();
123 assertNotNull( digests );
124 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
125 assertTrue( digests.get( "unknown" ) instanceof NoSuchAlgorithmException );
126 assertEquals( 2, digests.size() );
127 }
128
129 @Test
130 public void testNoInitCall()
131 {
132 ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
133 calculator.update( toBuffer( "Hello World!" ) );
134 Map<String, Object> digests = calculator.get();
135 assertNotNull( digests );
136 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
137 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
138 assertEquals( 2, digests.size() );
139 }
140
141 @Test
142 public void testRestart()
143 {
144 ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
145 calculator.init( 0 );
146 calculator.update( toBuffer( "Ignored" ) );
147 calculator.init( 0 );
148 calculator.update( toBuffer( "Hello World!" ) );
149 Map<String, Object> digests = calculator.get();
150 assertNotNull( digests );
151 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
152 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
153 assertEquals( 2, digests.size() );
154 }
155
156 @Test
157 public void testRestartAfterError()
158 {
159 ChecksumCalculator calculator = newCalculator( SHA1, MD5 );
160 calculator.init( 100 );
161 calculator.init( 0 );
162 calculator.update( toBuffer( "Hello World!" ) );
163 Map<String, Object> digests = calculator.get();
164 assertNotNull( digests );
165 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) );
166 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) );
167 assertEquals( 2, digests.size() );
168 }
169
170 }