summaryrefslogtreecommitdiffstats
path: root/src/test/java/com/btr/proxy/selector/misc/ProxyListFallbackSelectorTest.java
blob: 12c172c78befd242d1cafa2917b94bf0307b5428 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.btr.proxy.selector.misc;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.btr.proxy.TestUtil;

/*****************************************************************************
 *  Unit Tests for the ProxyListFallbackSelector
 *  
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2011
 ****************************************************************************/

public class ProxyListFallbackSelectorTest {

	private ProxyListFallbackSelector selector;

	/*************************************************************************
	 * Setup before tests.
	 ************************************************************************/
	@Before
	public void setup() {
		this.selector = new ProxyListFallbackSelector(
		new ProxySelector() {
			@Override
			public List<Proxy> select(URI uri) {
				return Arrays.asList(TestUtil.HTTP_TEST_PROXY, TestUtil.HTTPS_TEST_PROXY, Proxy.NO_PROXY);
			}
			@Override
			public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
				// Not used on the delegate
			}
		});
	}
	
	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testList() {
		List<Proxy> result = this.selector.select(TestUtil.HTTP_TEST_URI);
		assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0));
		assertEquals(TestUtil.HTTPS_TEST_PROXY, result.get(1));
		assertEquals(Proxy.NO_PROXY, result.get(2));
	}
	
	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testFailedProxy() {
		this.selector.connectFailed(
				TestUtil.HTTP_TEST_URI, 
				TestUtil.HTTP_TEST_PROXY.address(), 
				new IOException("TEST"));
		
		List<Proxy> result = this.selector.select(TestUtil.HTTP_TEST_URI);
		
		assertEquals(TestUtil.HTTPS_TEST_PROXY, result.get(0));
		assertEquals(Proxy.NO_PROXY, result.get(1));
	}
	
	/*************************************************************************
	 * Test method
	 * @throws InterruptedException if the test wait period was interrupted
	 ************************************************************************/
	@Test
	public void testFailedProxyRetry() throws InterruptedException {
		this.selector.setRetryAfterMs(100);
		this.selector.connectFailed(
				TestUtil.HTTP_TEST_URI, 
				TestUtil.HTTP_TEST_PROXY.address(), 
				new IOException("TEST"));
		
		List<Proxy> result = this.selector.select(TestUtil.HTTP_TEST_URI);
		assertEquals(2, result.size());
		
		Thread.sleep(200);
		result = this.selector.select(TestUtil.HTTP_TEST_URI);
		assertEquals(3, result.size());
	}

}