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.mortbay.jetty.Server;
28  import org.mortbay.jetty.servlet.Context;
29  import org.mortbay.jetty.servlet.DefaultServlet;
30  import org.mortbay.jetty.servlet.ServletHolder;
31  
32  import javax.servlet.ServletException;
33  import javax.servlet.http.HttpServlet;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.IOException;
39  import java.io.RandomAccessFile;
40  
41  /**
42   * @author Olivier Lamy
43   */
44  public class HugeFileDownloadTest
45      extends PlexusTestCase
46  {
47  
48      private static long HUGE_FILE_SIZE =
49          Integer.valueOf( Integer.MAX_VALUE ).longValue() + Integer.valueOf( Integer.MAX_VALUE ).longValue();
50  
51      private Server server;
52  
53      public void testDownloadHugeFileWithContentLength()
54          throws Exception
55      {
56          File hugeFile = new File( getBasedir(), "target/hugefile.txt" );
57          if ( !hugeFile.exists() || hugeFile.length() < HUGE_FILE_SIZE )
58          {
59              makeHugeFile( hugeFile );
60          }
61  
62          server = new Server( 0 );
63  
64          Context root = new Context( server, "/", Context.SESSIONS );
65          root.setResourceBase( new File( getBasedir(), "target" ).getAbsolutePath() );
66          ServletHolder servletHolder = new ServletHolder( new DefaultServlet() );
67          root.addServlet( servletHolder, "/*" );
68  
69          server.start();
70  
71          File dest = null;
72          try
73          {
74              Wagon wagon = getWagon();
75              wagon.connect( new Repository( "id", "http://localhost:" + server.getConnectors()[0].getLocalPort() ) );
76  
77              dest = File.createTempFile( "huge", "txt" );
78  
79              wagon.get( "hugefile.txt", dest );
80  
81              Assert.assertTrue( dest.length() >= HUGE_FILE_SIZE );
82  
83              wagon.disconnect();
84          }
85          finally
86          {
87              server.start();
88              dest.delete();
89              hugeFile.delete();
90          }
91  
92  
93      }
94  
95      public void testDownloadHugeFileWithChunked()
96          throws Exception
97      {
98          final File hugeFile = new File( getBasedir(), "target/hugefile.txt" );
99          if ( !hugeFile.exists() || hugeFile.length() < HUGE_FILE_SIZE )
100         {
101             makeHugeFile( hugeFile );
102         }
103 
104         server = new Server( 0 );
105 
106         Context root = new Context( server, "/", Context.SESSIONS );
107         root.setResourceBase( new File( getBasedir(), "target" ).getAbsolutePath() );
108         ServletHolder servletHolder = new ServletHolder( new HttpServlet()
109         {
110             @Override
111             protected void doGet( HttpServletRequest req, HttpServletResponse resp )
112                 throws ServletException, IOException
113             {
114                 FileInputStream fis = new FileInputStream( hugeFile );
115 
116                 byte[] buffer = new byte[8192];
117                 int len = 0;
118                 while ( ( len = fis.read( buffer ) ) != -1 )
119                 {
120                     resp.getOutputStream().write( buffer, 0, len );
121                 }
122                 fis.close();
123             }
124         } );
125         root.addServlet( servletHolder, "/*" );
126 
127         server.start();
128 
129         File dest = null;
130         try
131         {
132             Wagon wagon = getWagon();
133             wagon.connect( new Repository( "id", "http://localhost:" + server.getConnectors()[0].getLocalPort() ) );
134 
135             dest = File.createTempFile( "huge", "txt" );
136 
137             wagon.get( "hugefile.txt", dest );
138 
139             Assert.assertTrue( dest.length() >= HUGE_FILE_SIZE );
140 
141             wagon.disconnect();
142         }
143         finally
144         {
145             server.start();
146             dest.delete();
147             hugeFile.delete();
148         }
149 
150 
151     }
152 
153 
154     protected Wagon getWagon()
155         throws Exception
156     {
157         Wagon wagon = (Wagon) lookup( Wagon.ROLE, "http" );
158 
159         Debug debug = new Debug();
160 
161         wagon.addSessionListener( debug );
162 
163         return wagon;
164     }
165 
166     private void makeHugeFile( File hugeFile )
167         throws Exception
168     {
169         RandomAccessFile ra = new RandomAccessFile( hugeFile.getPath(), "rw" );
170         ra.setLength( HUGE_FILE_SIZE + 1 );
171         ra.seek( HUGE_FILE_SIZE );
172         ra.write( 1 );
173 
174     }
175 
176 }