001package org.eclipse.aether.connector.basic; 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 static org.junit.Assert.*; 023 024import java.io.File; 025import java.io.IOException; 026import java.io.UnsupportedEncodingException; 027import java.net.URI; 028import java.nio.ByteBuffer; 029import java.security.NoSuchAlgorithmException; 030import java.util.ArrayList; 031import java.util.List; 032import java.util.Map; 033 034import org.eclipse.aether.internal.test.util.TestFileUtils; 035import org.eclipse.aether.spi.connector.layout.RepositoryLayout; 036import org.junit.Before; 037import org.junit.Test; 038 039public class ChecksumCalculatorTest 040{ 041 042 private static final String SHA1 = "SHA-1"; 043 044 private static final String MD5 = "MD5"; 045 046 private File file; 047 048 private ChecksumCalculator newCalculator( String... algos ) 049 { 050 List<RepositoryLayout.Checksum> checksums = new ArrayList<RepositoryLayout.Checksum>(); 051 for ( String algo : algos ) 052 { 053 checksums.add( new RepositoryLayout.Checksum( algo, URI.create( "irrelevant" ) ) ); 054 } 055 return ChecksumCalculator.newInstance( file, checksums ); 056 } 057 058 private ByteBuffer toBuffer( String data ) 059 { 060 try 061 { 062 return ByteBuffer.wrap( data.getBytes( "UTF-8" ) ); 063 } 064 catch ( UnsupportedEncodingException e ) 065 { 066 throw new IllegalStateException( e ); 067 } 068 } 069 070 @Before 071 public void init() 072 throws Exception 073 { 074 file = TestFileUtils.createTempFile( "Hello World!" ); 075 } 076 077 @Test 078 public void testNoOffset() 079 { 080 ChecksumCalculator calculator = newCalculator( SHA1, MD5 ); 081 calculator.init( 0 ); 082 calculator.update( toBuffer( "Hello World!" ) ); 083 Map<String, Object> digests = calculator.get(); 084 assertNotNull( digests ); 085 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) ); 086 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) ); 087 assertEquals( 2, digests.size() ); 088 } 089 090 @Test 091 public void testWithOffset() 092 { 093 ChecksumCalculator calculator = newCalculator( SHA1, MD5 ); 094 calculator.init( 6 ); 095 calculator.update( toBuffer( "World!" ) ); 096 Map<String, Object> digests = calculator.get(); 097 assertNotNull( digests ); 098 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) ); 099 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}