1 package org.apache.maven.archiva.policies;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.codehaus.plexus.PlexusTestCase;
23 import org.codehaus.plexus.util.FileUtils;
25 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.util.Properties;
33 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
36 public class ChecksumPolicyTest
37 extends PlexusTestCase
39 private static final String GOOD = "good";
41 private static final String BAD = "bad";
43 public void testFailOnFileOnly()
46 assertFailSetting( false, null, null );
49 public void testFailOnFileWithBadMd5AndBadSha1()
52 assertFailSetting( false, BAD, BAD );
55 public void testFailOnFileWithBadMd5AndGoodSha1()
58 assertFailSetting( false, BAD, GOOD );
61 public void testFailOnFileWithBadMd5Only()
64 assertFailSetting( false, BAD, null );
67 public void testFailOnFileWithBadSha1Only()
70 assertFailSetting( false, null, BAD );
73 public void testFailOnFileWithGoodMd5AndBadSha1()
76 assertFailSetting( false, GOOD, BAD );
79 public void testFailOnFileWithGoodMd5AndGoodSha1()
82 assertFailSetting( true, GOOD, GOOD );
85 public void testFailOnFileWithGoodMd5Only()
88 assertFailSetting( true, GOOD, null );
91 public void testFailOnFileWithGoodSha1Only()
94 assertFailSetting( true, null, GOOD );
97 public void testFixOnFileOnly()
100 assertFixSetting( true, null, null );
103 public void testFixOnFileWithBadMd5AndBadSha1()
106 assertFixSetting( true, BAD, BAD );
109 public void testFixOnFileWithBadMd5AndGoodSha1()
112 assertFixSetting( true, BAD, GOOD );
115 public void testFixOnFileWithBadMd5Only()
118 assertFixSetting( true, BAD, null );
121 public void testFixOnFileWithBadSha1Only()
124 assertFixSetting( true, null, BAD );
127 public void testFixOnFileWithGoodMd5AndBadSha1()
130 assertFixSetting( true, GOOD, BAD );
133 public void testFixOnFileWithGoodMd5AndGoodSha1()
136 assertFixSetting( true, GOOD, GOOD );
139 public void testFixOnFileWithGoodMd5Only()
142 assertFixSetting( true, GOOD, null );
145 public void testFixOnFileWithGoodSha1Only()
148 assertFixSetting( true, null, GOOD );
151 public void testIgnored()
154 PostDownloadPolicy policy = lookupPolicy();
155 File localFile = createTestableFiles( null, null );
156 Properties request = createRequest();
158 assertTrue( policy.applyPolicy( ChecksumPolicy.IGNORED, request, localFile ) );
161 private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
164 PostDownloadPolicy policy = lookupPolicy();
165 File localFile = createTestableFiles( md5State, sha1State );
166 Properties request = createRequest();
168 boolean actualResult = policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
169 String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
171 if ( actualResult == false )
173 assertFalse( msg + " local file should not exist:", localFile.exists() );
174 File md5File = new File( localFile.getAbsolutePath() + ".sha1" );
175 File sha1File = new File( localFile.getAbsolutePath() + ".md5" );
176 assertFalse( msg + " local md5 file should not exist:", md5File.exists() );
177 assertFalse( msg + " local sha1 file should not exist:", sha1File.exists() );
180 assertEquals( createMessage( ChecksumPolicy.FAIL, md5State, sha1State ), expectedResult, actualResult );
183 private void assertFixSetting( boolean expectedResult, String md5State, String sha1State )
186 PostDownloadPolicy policy = lookupPolicy();
187 File localFile = createTestableFiles( md5State, sha1State );
188 Properties request = createRequest();
190 boolean actualResult = policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
191 assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
193 // End result should be legitimate SHA1 and MD5 files.
194 File md5File = new File( localFile.getAbsolutePath() + ".md5" );
195 File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
197 assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", md5File.exists() && md5File.isFile() );
198 assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", sha1File.exists() && sha1File.isFile() );
200 String actualMd5Contents = readChecksumFile( md5File );
201 String actualSha1Contents = readChecksumFile( sha1File );
203 String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548 artifact.jar";
204 String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f artifact.jar";
206 assertEquals( "ChecksumPolicy.apply(FIX) md5 contents:", expectedMd5Contents, actualMd5Contents );
207 assertEquals( "ChecksumPolicy.apply(FIX) sha1 contents:", expectedSha1Contents, actualSha1Contents );
211 * Read the first line from the checksum file, and return it (trimmed).
213 private String readChecksumFile( File checksumFile )
216 FileReader freader = null;
217 BufferedReader buf = null;
221 freader = new FileReader( checksumFile );
222 buf = new BufferedReader( freader );
223 return buf.readLine();
232 if ( freader != null )
239 private String createMessage( String settingType, String md5State, String sha1State )
241 StringBuffer msg = new StringBuffer();
242 msg.append( "Expected result of ChecksumPolicy.apply(" );
243 msg.append( settingType.toUpperCase() );
244 msg.append( ") when working with " );
245 if ( md5State == null )
251 msg.append( "a " ).append( md5State.toUpperCase() );
254 msg.append( " MD5 and " );
256 if ( sha1State == null )
262 msg.append( "a " ).append( sha1State.toUpperCase() );
264 msg.append( " SHA1:" );
266 return msg.toString();
269 private Properties createRequest()
271 Properties request = new Properties();
273 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
278 private File createTestableFiles( String md5State, String sha1State )
281 File sourceDir = new File( "src/test/resources/checksums/" );
282 File destDir = new File( "target/checksum-tests/" + getName() + "/" );
284 FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );
286 if ( md5State != null )
288 File md5File = new File( sourceDir, "artifact.jar.md5-" + md5State );
289 assertTrue( "Testable file exists: " + md5File.getName() + ":", md5File.exists() && md5File.isFile() );
290 File destFile = new File( destDir, "artifact.jar.md5" );
291 FileUtils.copyFile( md5File, destFile );
294 if ( sha1State != null )
296 File sha1File = new File( sourceDir, "artifact.jar.sha1-" + sha1State );
297 assertTrue( "Testable file exists: " + sha1File.getName() + ":", sha1File.exists() && sha1File.isFile() );
298 File destFile = new File( destDir, "artifact.jar.sha1" );
299 FileUtils.copyFile( sha1File, destFile );
302 File localFile = new File( destDir, "artifact.jar" );
306 private PostDownloadPolicy lookupPolicy()
309 PostDownloadPolicy policy = (PostDownloadPolicy) lookup( PostDownloadPolicy.class.getName(), "checksum" );
310 assertNotNull( policy );