001package org.apache.maven.wagon.observers; 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 java.io.File; 023import java.io.IOException; 024import java.security.NoSuchAlgorithmException; 025 026import junit.framework.TestCase; 027 028import org.apache.maven.wagon.ConnectionException; 029import org.apache.maven.wagon.ResourceDoesNotExistException; 030import org.apache.maven.wagon.TransferFailedException; 031import org.apache.maven.wagon.Wagon; 032import org.apache.maven.wagon.WagonMock; 033import org.apache.maven.wagon.authorization.AuthorizationException; 034import org.apache.maven.wagon.events.TransferEvent; 035import org.apache.maven.wagon.events.TransferListener; 036import org.apache.maven.wagon.repository.Repository; 037import org.apache.maven.wagon.resource.Resource; 038 039public class ChecksumObserverTest 040 extends TestCase 041{ 042 private Wagon wagon; 043 044 public void setUp() 045 throws Exception 046 { 047 super.setUp(); 048 049 wagon = new WagonMock( true ); 050 051 Repository repository = new Repository(); 052 wagon.connect( repository ); 053 } 054 055 public void tearDown() 056 throws Exception 057 { 058 wagon.disconnect(); 059 060 super.tearDown(); 061 } 062 063 public void testSubsequentTransfersAfterTransferError() 064 throws NoSuchAlgorithmException, ResourceDoesNotExistException, AuthorizationException, IOException 065 { 066 TransferListener listener = new ChecksumObserver(); 067 068 wagon.addTransferListener( listener ); 069 070 File testFile = File.createTempFile( "wagon", "tmp" ); 071 testFile.deleteOnExit(); 072 073 try 074 { 075 wagon.get( "resource", testFile ); 076 fail(); 077 } 078 catch ( TransferFailedException e ) 079 { 080 assertTrue( true ); 081 } 082 083 try 084 { 085 wagon.get( "resource", testFile ); 086 fail(); 087 } 088 catch ( TransferFailedException e ) 089 { 090 assertTrue( true ); 091 } 092 093 testFile.delete(); 094 } 095 096 public void testChecksum() 097 throws NoSuchAlgorithmException 098 { 099 ChecksumObserver listener = new ChecksumObserver( "SHA-1" ); 100 101 Resource resource = new Resource( "resource" ); 102 103 TransferEvent transferEvent = 104 new TransferEvent( wagon, resource, TransferEvent.TRANSFER_INITIATED, TransferEvent.REQUEST_GET ); 105 106 listener.transferInitiated( transferEvent ); 107 108 transferEvent = new TransferEvent( wagon, resource, TransferEvent.TRANSFER_STARTED, TransferEvent.REQUEST_GET ); 109 110 listener.transferStarted( transferEvent ); 111 112 transferEvent = new TransferEvent( wagon, resource, TransferEvent.TRANSFER_PROGRESS, TransferEvent.REQUEST_GET ); 113 114 listener.transferProgress( transferEvent, "checksum\n".getBytes(), 9 ); 115 116 transferEvent = 117 new TransferEvent( wagon, resource, TransferEvent.TRANSFER_COMPLETED, TransferEvent.REQUEST_GET ); 118 119 listener.transferCompleted( transferEvent ); 120 121 assertEquals( "2e5daf0201ddeb068a62d5e08da18657ab2c6be9", listener.getActualChecksum() ); 122 } 123}