View Javadoc

1   package org.apache.maven.wagon.providers.http;
2   
3   import junit.framework.Assert;
4   import org.apache.maven.wagon.Wagon;
5   import org.apache.maven.wagon.observers.Debug;
6   import org.apache.maven.wagon.repository.Repository;
7   import org.codehaus.plexus.PlexusTestCase;
8   import org.mortbay.jetty.Server;
9   import org.mortbay.jetty.servlet.Context;
10  import org.mortbay.jetty.servlet.DefaultServlet;
11  import org.mortbay.jetty.servlet.ServletHolder;
12  
13  import javax.servlet.ServletException;
14  import javax.servlet.http.HttpServlet;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.io.IOException;
20  import java.io.RandomAccessFile;
21  
22  /**
23   * @author Olivier Lamy
24   */
25  public class HugeFileDownloadTest
26      extends PlexusTestCase
27  {
28  
29      private static long HUGE_FILE_SIZE =
30          Integer.valueOf( Integer.MAX_VALUE ).longValue() + Integer.valueOf( Integer.MAX_VALUE ).longValue();
31  
32      private Server server;
33  
34      public void testDownloadHugeFileWithContentLength()
35          throws Exception
36      {
37          File hugeFile = new File( getBasedir(), "/target/hugefile.txt" );
38          if ( !hugeFile.exists() || hugeFile.length() < HUGE_FILE_SIZE )
39          {
40              makeHugeFile( hugeFile );
41          }
42  
43          server = new Server( 0 );
44  
45          Context root = new Context( server, "/", Context.SESSIONS );
46          root.setResourceBase( new File( getBasedir(), "/target" ).getAbsolutePath() );
47          ServletHolder servletHolder = new ServletHolder( new DefaultServlet() );
48          root.addServlet( servletHolder, "/*" );
49  
50          server.start();
51  
52          try
53          {
54              Wagon wagon = getWagon();
55              wagon.connect( new Repository( "id", "http://localhost:" + server.getConnectors()[0].getLocalPort() ) );
56  
57              File dest = File.createTempFile( "huge", "txt" );
58  
59              wagon.get( "hugefile.txt", dest );
60  
61              Assert.assertTrue( dest.length() >= HUGE_FILE_SIZE );
62  
63              wagon.disconnect();
64          }
65          finally
66          {
67              server.start();
68          }
69  
70  
71      }
72  
73      public void testDownloadHugeFileWithChunked()
74          throws Exception
75      {
76          final File hugeFile = new File( getBasedir(), "/target/hugefile.txt" );
77          if ( !hugeFile.exists() || hugeFile.length() < HUGE_FILE_SIZE )
78          {
79              makeHugeFile( hugeFile );
80          }
81  
82          server = new Server( 0 );
83  
84          Context root = new Context( server, "/", Context.SESSIONS );
85          root.setResourceBase( new File( getBasedir(), "/target" ).getAbsolutePath() );
86          ServletHolder servletHolder = new ServletHolder( new HttpServlet()
87          {
88              @Override
89              protected void doGet( HttpServletRequest req, HttpServletResponse resp )
90                  throws ServletException, IOException
91              {
92                  FileInputStream fis = new FileInputStream( hugeFile );
93  
94                  byte[] buffer = new byte[8192];
95                  int len = 0;
96                  while ( ( len = fis.read( buffer ) ) != -1 )
97                  {
98                      resp.getOutputStream().write( buffer, 0, len );
99                  }
100                 fis.close();
101             }
102         } );
103         root.addServlet( servletHolder, "/*" );
104 
105         server.start();
106 
107         try
108         {
109             Wagon wagon = getWagon();
110             wagon.connect( new Repository( "id", "http://localhost:" + server.getConnectors()[0].getLocalPort() ) );
111 
112             File dest = File.createTempFile( "huge", "txt" );
113 
114             wagon.get( "hugefile.txt", dest );
115 
116             Assert.assertTrue( dest.length() >= HUGE_FILE_SIZE );
117 
118             wagon.disconnect();
119         }
120         finally
121         {
122             server.start();
123         }
124 
125 
126     }
127 
128 
129     protected Wagon getWagon()
130         throws Exception
131     {
132         Wagon wagon = (Wagon) lookup( Wagon.ROLE, "http" );
133 
134         Debug debug = new Debug();
135 
136         wagon.addSessionListener( debug );
137 
138         return wagon;
139     }
140 
141     private void makeHugeFile( File hugeFile )
142         throws Exception
143     {
144         RandomAccessFile ra = new RandomAccessFile( hugeFile.getPath(), "rw" );
145         ra.setLength( HUGE_FILE_SIZE + 1 );
146         ra.seek( HUGE_FILE_SIZE );
147         ra.write( 1 );
148 
149     }
150 
151 }