001package org.apache.maven.wagon.providers.http;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.wagon.FileTestUtils;
023import org.apache.maven.wagon.TransferFailedException;
024import org.apache.maven.wagon.Wagon;
025import org.apache.maven.wagon.repository.Repository;
026import org.apache.maven.wagon.shared.http.HttpConfiguration;
027import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
028import org.eclipse.jetty.servlet.ServletHolder;
029
030import java.io.File;
031import java.util.Random;
032
033/**
034 * User: jdumay Date: 24/01/2008 Time: 17:17:34
035 */
036public class HttpWagonTimeoutTest
037    extends HttpWagonHttpServerTestCase
038{
039    protected void setUp()
040        throws Exception
041    {
042        super.setUp();
043        ServletHolder servlets = new ServletHolder( new WaitForeverServlet() );
044        context.addServlet( servlets, "/*" );
045        startServer();
046    }
047
048    public void testGetTimeout()
049        throws Exception
050    {
051        Exception thrown = null;
052
053        try
054        {
055            Wagon wagon = getWagon();
056            wagon.setReadTimeout( 1000 );
057
058            Repository testRepository = new Repository();
059            testRepository.setUrl( "http://localhost:" + getPort() );
060
061            wagon.connect( testRepository );
062
063            File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
064            destFile.deleteOnExit();
065
066            wagon.get( "/timeoutfile", destFile );
067
068            wagon.disconnect();
069        }
070        catch ( Exception e )
071        {
072            thrown = e;
073        }
074        finally
075        {
076            stopServer();
077        }
078
079        assertNotNull( thrown );
080        assertEquals( TransferFailedException.class, thrown.getClass() );
081    }
082
083    public void testResourceExits()
084        throws Exception
085    {
086        Exception thrown = null;
087
088        try
089        {
090            Wagon wagon = getWagon();
091            wagon.setReadTimeout( 1000 );
092
093            Repository testRepository = new Repository();
094            testRepository.setUrl( "http://localhost:" + getPort() );
095
096            wagon.connect( testRepository );
097
098            wagon.resourceExists( "/timeoutfile" );
099
100            wagon.disconnect();
101        }
102        catch ( Exception e )
103        {
104            thrown = e;
105        }
106        finally
107        {
108            stopServer();
109        }
110
111        assertNotNull( thrown );
112        assertEquals( TransferFailedException.class, thrown.getClass() );
113    }
114
115    public void testPutTimeout()
116        throws Exception
117    {
118        Exception thrown = null;
119
120        try
121        {
122            Wagon wagon = getWagon();
123            wagon.setReadTimeout( 1000 );
124
125            Repository testRepository = new Repository();
126            testRepository.setUrl( "http://localhost:" + getPort() );
127
128            wagon.connect( testRepository );
129
130            File destFile = File.createTempFile( "Hello", null );
131            destFile.deleteOnExit();
132
133            wagon.put( destFile, "/timeoutfile" );
134
135            wagon.disconnect();
136        }
137        catch ( Exception e )
138        {
139            thrown = e;
140        }
141        finally
142        {
143            stopServer();
144        }
145
146        assertNotNull( thrown );
147        assertEquals( TransferFailedException.class, thrown.getClass() );
148    }
149
150    public void testConnectionTimeout()
151        throws Exception
152    {
153        Exception thrown = null;
154
155        try
156        {
157            HttpWagon wagon = (HttpWagon) getWagon();
158            wagon.setHttpConfiguration(
159                new HttpConfiguration().setAll( new HttpMethodConfiguration().setConnectionTimeout( 500 ) ) );
160
161            Repository testRepository = new Repository();
162            Random random = new Random( );
163            testRepository.setUrl( "http://localhost:" + random.nextInt( 2048 ));
164
165            wagon.connect( testRepository );
166
167            long start = System.currentTimeMillis();
168            wagon.resourceExists( "/foobar" );
169            long end = System.currentTimeMillis();
170
171            wagon.disconnect();
172
173            // validate we have a default time out 60000
174            assertTrue( (end - start) >= 500 && (end - start) < 1000 );
175
176        }
177        catch ( Exception e )
178        {
179            thrown = e;
180        }
181        finally
182        {
183            stopServer();
184        }
185
186        assertNotNull( thrown );
187        assertEquals( TransferFailedException.class, thrown.getClass() );
188    }
189
190}