View Javadoc
1   package org.apache.maven.wagon.providers.http;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import junit.framework.Assert;
23  import org.apache.maven.wagon.Wagon;
24  import org.apache.maven.wagon.observers.Debug;
25  import org.apache.maven.wagon.repository.Repository;
26  import org.codehaus.plexus.PlexusTestCase;
27  import org.codehaus.plexus.util.IOUtil;
28  import org.eclipse.jetty.server.Server;
29  import org.eclipse.jetty.servlet.ServletContextHandler;
30  import org.eclipse.jetty.servlet.ServletHolder;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServlet;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import java.io.File;
39  import java.io.FileInputStream;
40  import java.io.IOException;
41  import java.io.RandomAccessFile;
42  
43  /**
44   * @author Olivier Lamy
45   */
46  public class HugeFileDownloadTest
47      extends PlexusTestCase
48  {
49  
50      private static final Logger LOGGER = LoggerFactory.getLogger( HugeFileDownloadTest.class );
51  
52      private static long HUGE_FILE_SIZE =
53          Integer.valueOf( Integer.MAX_VALUE ).longValue() + Integer.valueOf( Integer.MAX_VALUE ).longValue();
54  
55      private Server server;
56  
57      public void testDownloadHugeFileWithContentLength()
58          throws Exception
59      {
60          final File hugeFile = new File( getBasedir(), "target/hugefile.txt" );
61          if ( !hugeFile.exists() || hugeFile.length() < HUGE_FILE_SIZE )
62          {
63              makeHugeFile( hugeFile );
64          }
65  
66          server = new Server( 0 );
67  
68          ServletContextHandler root = new ServletContextHandler( ServletContextHandler.SESSIONS );
69          root.setResourceBase( new File( getBasedir(), "target" ).getAbsolutePath() );
70          ServletHolder servletHolder = new ServletHolder( new HttpServlet()
71          {
72              @Override
73              protected void doGet( HttpServletRequest req, HttpServletResponse resp )
74                  throws ServletException, IOException
75              {
76                  FileInputStream fis = new FileInputStream( hugeFile );
77  
78                  resp.addHeader( "Content-Length", String.valueOf( hugeFile.length() ) );
79                  IOUtil.copy( fis, resp.getOutputStream() );
80                  fis.close();
81              }
82          } );
83          root.addServlet( servletHolder, "/*" );
84          server.setHandler( root );
85  
86          server.start();
87  
88          File dest = null;
89          try
90          {
91              Wagon wagon = getWagon();
92              wagon.connect( new Repository( "id", "http://localhost:" + server.getConnectors()[0].getLocalPort() ) );
93  
94              dest = File.createTempFile( "huge", "txt" );
95  
96              LOGGER.info( "Fetching 'hugefile.txt' with content length" );
97              wagon.get( "hugefile.txt", dest );
98  
99              Assert.assertTrue( dest.length() >= HUGE_FILE_SIZE );
100             LOGGER.info( "The file was successfully fetched" );
101 
102             wagon.disconnect();
103         }
104         finally
105         {
106             server.stop();
107             dest.delete();
108             hugeFile.delete();
109         }
110 
111     }
112 
113     public void testDownloadHugeFileWithChunked()
114         throws Exception
115     {
116         final File hugeFile = new File( getBasedir(), "target/hugefile.txt" );
117         if ( !hugeFile.exists() || hugeFile.length() < HUGE_FILE_SIZE )
118         {
119             makeHugeFile( hugeFile );
120         }
121 
122         server = new Server( 0 );
123 
124         ServletContextHandler root = new ServletContextHandler( ServletContextHandler.SESSIONS );
125         root.setResourceBase( new File( getBasedir(), "target" ).getAbsolutePath() );
126         ServletHolder servletHolder = new ServletHolder( new HttpServlet()
127         {
128             @Override
129             protected void doGet( HttpServletRequest req, HttpServletResponse resp )
130                 throws ServletException, IOException
131             {
132                 FileInputStream fis = new FileInputStream( hugeFile );
133 
134                 IOUtil.copy( fis, resp.getOutputStream() );
135                 fis.close();
136             }
137         } );
138         root.addServlet( servletHolder, "/*" );
139         server.setHandler( root );
140 
141         server.start();
142 
143         File dest = null;
144         try
145         {
146             Wagon wagon = getWagon();
147             wagon.connect( new Repository( "id", "http://localhost:" + server.getConnectors()[0].getLocalPort() ) );
148 
149             dest = File.createTempFile( "huge", "txt" );
150 
151             LOGGER.info( "Fetching 'hugefile.txt' in chunks" );
152             wagon.get( "hugefile.txt", dest );
153 
154             Assert.assertTrue( dest.length() >= HUGE_FILE_SIZE );
155             LOGGER.info( "The file was successfully fetched" );
156 
157             wagon.disconnect();
158         }
159         finally
160         {
161             server.stop();
162             dest.delete();
163             hugeFile.delete();
164         }
165 
166     }
167 
168     protected Wagon getWagon()
169         throws Exception
170     {
171         Wagon wagon = (Wagon) lookup( Wagon.ROLE, "http" );
172 
173         Debug debug = new Debug();
174 
175         wagon.addSessionListener( debug );
176 
177         return wagon;
178     }
179 
180     private void makeHugeFile( File hugeFile )
181         throws Exception
182     {
183         LOGGER.info( "Creating test file" );
184         RandomAccessFile ra = new RandomAccessFile( hugeFile.getPath(), "rw" );
185         ra.setLength( HUGE_FILE_SIZE + 1L );
186         ra.seek( HUGE_FILE_SIZE );
187         ra.write( 1 );
188         ra.close();
189         LOGGER.info( "Test file created" );
190     }
191 
192 }