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