001package org.apache.maven.wagon.events; 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 junit.framework.TestCase; 023import org.apache.maven.wagon.Wagon; 024import org.apache.maven.wagon.resource.Resource; 025import org.easymock.EasyMock; 026 027import static org.easymock.EasyMock.*; 028 029/** 030 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 031 */ 032public class TransferEventSupportTest 033 extends TestCase 034{ 035 private TransferEventSupport eventSupport; 036 037 private Wagon wagon; 038 039 /** 040 * @see junit.framework.TestCase#setUp() 041 */ 042 protected void setUp() 043 throws Exception 044 { 045 super.setUp(); 046 047 eventSupport = new TransferEventSupport(); 048 049 // TODO: actually test it gets called? 050 wagon = EasyMock.createNiceMock( Wagon.class ); 051 } 052 053 public void testTransferListenerRegistration() 054 { 055 TransferListener mock1 = createMock( TransferListener.class ); 056 eventSupport.addTransferListener( mock1 ); 057 058 assertTrue( eventSupport.hasTransferListener( mock1 ) ); 059 TransferListener mock2 = createMock( TransferListener.class ); 060 061 assertFalse( eventSupport.hasTransferListener( mock2 ) ); 062 063 eventSupport.addTransferListener( mock2 ); 064 065 assertTrue( eventSupport.hasTransferListener( mock1 ) ); 066 assertTrue( eventSupport.hasTransferListener( mock2 ) ); 067 068 eventSupport.removeTransferListener( mock2 ); 069 070 assertTrue( eventSupport.hasTransferListener( mock1 ) ); 071 assertFalse( eventSupport.hasTransferListener( mock2 ) ); 072 073 eventSupport.removeTransferListener( mock1 ); 074 assertFalse( eventSupport.hasTransferListener( mock1 ) ); 075 } 076 077 public void testFireTransferStarted() 078 { 079 TransferListener mock1 = createMock( TransferListener.class ); 080 eventSupport.addTransferListener( mock1 ); 081 082 TransferListener mock2 = createMock( TransferListener.class ); 083 eventSupport.addTransferListener( mock2 ); 084 085 final TransferEvent event = getEvent( wagon ); 086 087 mock1.transferStarted( event ); 088 mock2.transferStarted( event ); 089 090 replay( mock1, mock2 ); 091 092 eventSupport.fireTransferStarted( event ); 093 094 verify( mock1, mock2 ); 095 } 096 097 public void testFireTransferProgress() 098 { 099 TransferListener mock1 = createMock( TransferListener.class ); 100 101 eventSupport.addTransferListener( mock1 ); 102 103 TransferListener mock2 = createMock( TransferListener.class ); 104 105 eventSupport.addTransferListener( mock2 ); 106 107 final TransferEvent event = getEvent( wagon ); 108 final byte[] buffer = "content".getBytes(); 109 110 mock1.transferProgress( event, buffer, 0 ); 111 mock2.transferProgress( event, buffer, 0 ); 112 113 replay( mock1, mock2 ); 114 115 eventSupport.fireTransferProgress( event, buffer , 0 ); 116 117 verify( mock1, mock2 ); 118 } 119 120 public void testFireTransferCompleted() 121 { 122 TransferListener mock1 = createMock( TransferListener.class ); 123 124 eventSupport.addTransferListener( mock1 ); 125 126 TransferListener mock2 = createMock( TransferListener.class ); 127 128 eventSupport.addTransferListener( mock2 ); 129 130 final TransferEvent event = getEvent( wagon ); 131 132 mock1.transferCompleted( event ); 133 mock2.transferCompleted( event ); 134 135 replay( mock1, mock2 ); 136 137 eventSupport.fireTransferCompleted( event ); 138 139 verify( mock1, mock2 ); 140 } 141 142 public void testFireTransferError() 143 { 144 TransferListener mock1 = createMock( TransferListener.class ); 145 146 eventSupport.addTransferListener( mock1 ); 147 148 TransferListener mock2 = createMock( TransferListener.class ); 149 150 eventSupport.addTransferListener( mock2 ); 151 152 final TransferEvent event = getEvent( wagon ); 153 154 mock1.transferError( event ); 155 mock2.transferError( event ); 156 157 replay( mock1, mock2 ); 158 159 eventSupport.fireTransferError( event ); 160 161 verify( mock1, mock2 ); 162 } 163 164 public void testFireDebug() 165 { 166 TransferListener mock1 = createMock( TransferListener.class ); 167 168 eventSupport.addTransferListener( mock1 ); 169 170 TransferListener mock2 = createMock( TransferListener.class ); 171 172 eventSupport.addTransferListener( mock2 ); 173 174 mock1.debug( "mm" ); 175 mock2.debug( "mm" ); 176 177 replay( mock1, mock2 ); 178 179 eventSupport.fireDebug( "mm" ); 180 181 verify( mock1, mock2 ); 182 } 183 184 private TransferEvent getEvent( final Wagon wagon ) 185 { 186 return new TransferEvent( wagon, new Resource(), TransferEvent.TRANSFER_COMPLETED, 187 TransferEvent.REQUEST_GET ); 188 } 189 190}