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 org.apache.maven.wagon.FileTestUtils;
23  import org.apache.maven.wagon.ResourceDoesNotExistException;
24  import org.apache.maven.wagon.TransferFailedException;
25  import org.apache.maven.wagon.Wagon;
26  import org.apache.maven.wagon.authorization.AuthorizationException;
27  import org.apache.maven.wagon.repository.Repository;
28  import org.eclipse.jetty.servlet.ServletHolder;
29  
30  import java.io.File;
31  
32  /**
33   * User: jdumay Date: 24/01/2008 Time: 17:17:34
34   */
35  public class HttpWagonErrorTest
36      extends HttpWagonHttpServerTestCase
37  {
38      private int serverPort;
39  
40      protected void setUp()
41          throws Exception
42      {
43          super.setUp();
44          ServletHolder servlets = new ServletHolder( new ErrorWithMessageServlet() );
45          context.addServlet( servlets, "/*" );
46          startServer();
47          serverPort = getPort();
48      }
49  
50      public void testGet401()
51          throws Exception
52      {
53          Exception thrown = null;
54  
55          try
56          {
57              Wagon wagon = getWagon();
58  
59              Repository testRepository = new Repository();
60              testRepository.setUrl( "http://localhost:" + serverPort );
61  
62              wagon.connect( testRepository );
63  
64              File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
65              destFile.deleteOnExit();
66  
67              wagon.get( "401", destFile );
68  
69              wagon.disconnect();
70          }
71          catch ( Exception e )
72          {
73              thrown = e;
74          }
75          finally
76          {
77              stopServer();
78          }
79  
80          assertNotNull( thrown );
81          assertEquals( AuthorizationException.class, thrown.getClass() );
82          assertEquals( "authentication failed for http://localhost:" + serverPort + "/401, status: 401 "
83              + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
84      }
85  
86      public void testGet403()
87          throws Exception
88      {
89          Exception thrown = null;
90  
91          try
92          {
93              Wagon wagon = getWagon();
94  
95              Repository testRepository = new Repository();
96              testRepository.setUrl( "http://localhost:" + serverPort );
97  
98              wagon.connect( testRepository );
99  
100             File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
101             destFile.deleteOnExit();
102 
103             wagon.get( "403", destFile );
104 
105             wagon.disconnect();
106         }
107         catch ( Exception e )
108         {
109             thrown = e;
110         }
111         finally
112         {
113             stopServer();
114         }
115 
116         assertNotNull( thrown );
117         assertEquals( AuthorizationException.class, thrown.getClass() );
118         assertEquals( "authorization failed for http://localhost:" + serverPort + "/403, status: 403 "
119             + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
120     }
121 
122     public void testGet404()
123             throws Exception
124     {
125         Exception thrown = null;
126 
127         try
128         {
129             Wagon wagon = getWagon();
130 
131             Repository testRepository = new Repository();
132             testRepository.setUrl( "http://localhost:" + serverPort );
133 
134             wagon.connect( testRepository );
135 
136             File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
137             destFile.deleteOnExit();
138 
139             wagon.get( "404", destFile );
140 
141             wagon.disconnect();
142         }
143         catch ( Exception e )
144         {
145             thrown = e;
146         }
147         finally
148         {
149             stopServer();
150         }
151 
152         assertNotNull( thrown );
153         assertEquals( ResourceDoesNotExistException.class, thrown.getClass() );
154         assertEquals( "resource missing at http://localhost:" + serverPort + "/404, status: 404 "
155             + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
156     }
157 
158     public void testGet407()
159         throws Exception
160     {
161         Exception thrown = null;
162 
163         try
164         {
165             Wagon wagon = getWagon();
166 
167             Repository testRepository = new Repository();
168             testRepository.setUrl( "http://localhost:" + getPort() );
169 
170             wagon.connect( testRepository );
171 
172             File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
173             destFile.deleteOnExit();
174 
175             wagon.get( "407", destFile );
176 
177             wagon.disconnect();
178         }
179         catch ( Exception e )
180         {
181             thrown = e;
182         }
183         finally
184         {
185             stopServer();
186         }
187 
188         assertNotNull( thrown );
189         assertEquals( AuthorizationException.class, thrown.getClass() );
190         assertEquals( "proxy authentication failed for http://localhost:" + serverPort + "/407, status: 407 "
191             + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
192     }
193 
194     public void testGet500()
195         throws Exception
196     {
197         Exception thrown = null;
198 
199         try
200         {
201             Wagon wagon = getWagon();
202 
203             Repository testRepository = new Repository();
204             testRepository.setUrl( "http://localhost:" + serverPort );
205 
206             wagon.connect( testRepository );
207 
208             File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
209             destFile.deleteOnExit();
210 
211             wagon.get( "500", destFile );
212 
213             wagon.disconnect();
214         }
215         catch ( Exception e )
216         {
217             thrown = e;
218         }
219         finally
220         {
221             stopServer();
222         }
223 
224         assertNotNull( thrown );
225         assertEquals( TransferFailedException.class, thrown.getClass() );
226         assertEquals( "transfer failed for http://localhost:" + serverPort + "/500, status: 500 "
227             + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
228     }
229 }