001package org.apache.maven.wagon; 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 org.apache.maven.wagon.observers.ChecksumObserver; 023import org.apache.maven.wagon.resource.Resource; 024import org.codehaus.plexus.util.FileUtils; 025import org.codehaus.plexus.util.IOUtil; 026 027import java.io.File; 028import java.io.FileInputStream; 029import java.io.FileOutputStream; 030import java.io.InputStream; 031import java.io.OutputStream; 032import java.text.SimpleDateFormat; 033 034/** 035 * @author <a href="mailto:brett@apache.org">Brett Porter</a> 036 */ 037public abstract class StreamingWagonTestCase 038 extends WagonTestCase 039{ 040 public void testStreamingWagon() 041 throws Exception 042 { 043 if ( supportsGetIfNewer() ) 044 { 045 setupRepositories(); 046 047 setupWagonTestingFixtures(); 048 049 streamRoundTripTesting(); 050 051 tearDownWagonTestingFixtures(); 052 } 053 } 054 055 public void testFailedGetToStream() 056 throws Exception 057 { 058 setupRepositories(); 059 060 setupWagonTestingFixtures(); 061 062 message( "Getting test artifact from test repository " + testRepository ); 063 064 StreamingWagon wagon = (StreamingWagon) getWagon(); 065 066 wagon.addTransferListener( checksumObserver ); 067 068 wagon.connect( testRepository, getAuthInfo() ); 069 070 destFile = FileTestUtils.createUniqueFile( getName(), getName() ); 071 072 destFile.deleteOnExit(); 073 074 OutputStream stream = null; 075 076 try 077 { 078 stream = new FileOutputStream( destFile ); 079 wagon.getToStream( "fubar.txt", stream ); 080 fail( "File was found when it shouldn't have been" ); 081 } 082 catch ( ResourceDoesNotExistException e ) 083 { 084 // expected 085 assertTrue( true ); 086 } 087 finally 088 { 089 wagon.removeTransferListener( checksumObserver ); 090 091 wagon.disconnect(); 092 093 IOUtil.close( stream ); 094 095 tearDownWagonTestingFixtures(); 096 } 097 } 098 099 public void testWagonGetIfNewerToStreamIsNewer() 100 throws Exception 101 { 102 if ( supportsGetIfNewer() ) 103 { 104 setupRepositories(); 105 setupWagonTestingFixtures(); 106 int expectedSize = putFile(); 107 getIfNewerToStream( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ) + 30000, false, 108 expectedSize ); 109 } 110 } 111 112 public void testWagonGetIfNewerToStreamIsOlder() 113 throws Exception 114 { 115 if ( supportsGetIfNewer() ) 116 { 117 setupRepositories(); 118 setupWagonTestingFixtures(); 119 int expectedSize = putFile(); 120 getIfNewerToStream( new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2006-01-01" ).getTime(), true, 121 expectedSize ); 122 } 123 } 124 125 public void testWagonGetIfNewerToStreamIsSame() 126 throws Exception 127 { 128 if ( supportsGetIfNewer() ) 129 { 130 setupRepositories(); 131 setupWagonTestingFixtures(); 132 int expectedSize = putFile(); 133 getIfNewerToStream( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ), false, 134 expectedSize ); 135 } 136 } 137 138 private void getIfNewerToStream( long timestamp, boolean expectedResult, int expectedSize ) 139 throws Exception 140 { 141 StreamingWagon wagon = (StreamingWagon) getWagon(); 142 143 ProgressAnswer progressAnswer = setupGetIfNewerTest( wagon, expectedResult, expectedSize ); 144 145 connectWagon( wagon ); 146 147 OutputStream stream = new LazyFileOutputStream( destFile ); 148 149 try 150 { 151 boolean result = wagon.getIfNewerToStream( this.resource, stream, timestamp ); 152 assertEquals( expectedResult, result ); 153 } 154 finally 155 { 156 IOUtil.close( stream ); 157 } 158 159 disconnectWagon( wagon ); 160 161 assertGetIfNewerTest( progressAnswer, expectedResult, expectedSize ); 162 163 tearDownWagonTestingFixtures(); 164 } 165 166 public void testFailedGetIfNewerToStream() 167 throws Exception 168 { 169 if ( supportsGetIfNewer() ) 170 { 171 setupRepositories(); 172 setupWagonTestingFixtures(); 173 message( "Getting test artifact from test repository " + testRepository ); 174 StreamingWagon wagon = (StreamingWagon) getWagon(); 175 wagon.addTransferListener( checksumObserver ); 176 wagon.connect( testRepository, getAuthInfo() ); 177 destFile = FileTestUtils.createUniqueFile( getName(), getName() ); 178 destFile.deleteOnExit(); 179 OutputStream stream = null; 180 try 181 { 182 stream = new FileOutputStream( destFile ); 183 wagon.getIfNewerToStream( "fubar.txt", stream, 0 ); 184 fail( "File was found when it shouldn't have been" ); 185 } 186 catch ( ResourceDoesNotExistException e ) 187 { 188 // expected 189 assertTrue( true ); 190 } 191 finally 192 { 193 wagon.removeTransferListener( checksumObserver ); 194 195 wagon.disconnect(); 196 197 IOUtil.close( stream ); 198 199 tearDownWagonTestingFixtures(); 200 } 201 } 202 } 203 204 protected void streamRoundTripTesting() 205 throws Exception 206 { 207 message( "Stream round trip testing ..." ); 208 209 int expectedSize = putStream(); 210 211 assertNotNull( "check checksum is not null", checksumObserver.getActualChecksum() ); 212 213 assertEquals( "compare checksums", "6b144b7285ffd6b0bc8300da162120b9", checksumObserver.getActualChecksum() ); 214 215 checksumObserver = new ChecksumObserver(); 216 217 getStream( expectedSize ); 218 219 assertNotNull( "check checksum is not null", checksumObserver.getActualChecksum() ); 220 221 assertEquals( "compare checksums", "6b144b7285ffd6b0bc8300da162120b9", checksumObserver.getActualChecksum() ); 222 223 // Now compare the conents of the artifact that was placed in 224 // the repository with the contents of the artifact that was 225 // retrieved from the repository. 226 227 String sourceContent = FileUtils.fileRead( sourceFile ); 228 229 String destContent = FileUtils.fileRead( destFile ); 230 231 assertEquals( sourceContent, destContent ); 232 } 233 234 private int putStream() 235 throws Exception 236 { 237 String content = "test-resource.txt\n"; 238 sourceFile = new File( FileTestUtils.getTestOutputDir(), "test-resource" ); 239 sourceFile.getParentFile().mkdirs(); 240 FileUtils.fileWrite( sourceFile.getAbsolutePath(), content ); 241 242 StreamingWagon wagon = (StreamingWagon) getWagon(); 243 244 ProgressAnswer progressAnswer = replayMockForPut( resource, content, wagon ); 245 246 message( "Putting test artifact: " + resource + " into test repository " + testRepository ); 247 248 connectWagon( wagon ); 249 250 InputStream stream = null; 251 252 try 253 { 254 stream = new FileInputStream( sourceFile ); 255 wagon.putFromStream( stream, resource, sourceFile.length(), sourceFile.lastModified() ); 256 } 257 catch ( Exception e ) 258 { 259 logger.error( "error while putting resources to the FTP Server", e ); 260 } 261 finally 262 { 263 IOUtil.close( stream ); 264 } 265 266 disconnectWagon( wagon ); 267 268 verifyMock( progressAnswer, content.length() ); 269 return content.length(); 270 } 271 272 private void getStream( int expectedSize ) 273 throws Exception 274 { 275 destFile = FileTestUtils.createUniqueFile( getName(), getName() ); 276 destFile.deleteOnExit(); 277 278 StreamingWagon wagon = (StreamingWagon) getWagon(); 279 280 ProgressAnswer progressAnswer = replaceMockForGet( wagon, expectedSize ); 281 282 message( "Getting test artifact from test repository " + testRepository ); 283 284 connectWagon( wagon ); 285 286 OutputStream stream = null; 287 288 try 289 { 290 stream = new FileOutputStream( destFile ); 291 wagon.getToStream( this.resource, stream ); 292 } 293 catch ( Exception e ) 294 { 295 logger.error( "error while reading resources from the FTP Server", e ); 296 } 297 finally 298 { 299 IOUtil.close( stream ); 300 } 301 302 disconnectWagon( wagon ); 303 304 verifyMock( progressAnswer, expectedSize ); 305 } 306}