diff options
Diffstat (limited to 'src/test/java/com/btr/proxy/selector/misc')
-rw-r--r-- | src/test/java/com/btr/proxy/selector/misc/ProtocolDispatchTest.java | 87 | ||||
-rw-r--r-- | src/test/java/com/btr/proxy/selector/misc/ProxyListFallbackSelectorTest.java | 94 |
2 files changed, 181 insertions, 0 deletions
diff --git a/src/test/java/com/btr/proxy/selector/misc/ProtocolDispatchTest.java b/src/test/java/com/btr/proxy/selector/misc/ProtocolDispatchTest.java new file mode 100644 index 0000000..da27327 --- /dev/null +++ b/src/test/java/com/btr/proxy/selector/misc/ProtocolDispatchTest.java @@ -0,0 +1,87 @@ +package com.btr.proxy.selector.misc; + +import static org.junit.Assert.*; + +import java.net.Proxy; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.btr.proxy.TestUtil; +import com.btr.proxy.selector.fixed.FixedProxySelector; + +/***************************************************************************** + * Unit Tests for the ProtocolDispatchSelector + * + * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009 + ****************************************************************************/ + +public class ProtocolDispatchTest { + + private static ProtocolDispatchSelector ps; + + @BeforeClass + public static void setup() { + ps = new ProtocolDispatchSelector(); + ps.setSelector("http", new FixedProxySelector(TestUtil.HTTP_TEST_PROXY)); + ps.setSelector("https", new FixedProxySelector(TestUtil.HTTPS_TEST_PROXY)); + ps.setSelector("ftp", new FixedProxySelector(TestUtil.FTP_TEST_PROXY)); + } + + /************************************************************************* + * Test method + ************************************************************************/ + @Test + public void testDispatchHttp() { + List<Proxy> result = ps.select(TestUtil.HTTP_TEST_URI); + assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0)); + } + + /************************************************************************* + * Test method + ************************************************************************/ + @Test + public void testDispatchHttps() { + List<Proxy> result = ps.select(TestUtil.HTTPS_TEST_URI); + assertEquals(TestUtil.HTTPS_TEST_PROXY, result.get(0)); + } + + /************************************************************************* + * Test method + ************************************************************************/ + @Test + public void testDispatchFtp() { + List<Proxy> result = ps.select(TestUtil.FTP_TEST_URI); + assertEquals(TestUtil.FTP_TEST_PROXY, result.get(0)); + } + + /************************************************************************* + * Test method + ************************************************************************/ + @Test + public void testRemove() { + ProtocolDispatchSelector px = new ProtocolDispatchSelector(); + FixedProxySelector selector = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY); + px.setSelector("http", selector); + assertEquals(selector, px.getSelector("http")); + px.removeSelector("http"); + assertNull(px.getSelector("http")); + } + + /************************************************************************* + * Test method + ************************************************************************/ + @Test + public void testFallback() { + ProtocolDispatchSelector px = new ProtocolDispatchSelector(); + FixedProxySelector selector = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY); + px.setFallbackSelector(selector); + + List<Proxy> proxies = px.select(TestUtil.HTTP_TEST_URI); + + assertEquals(TestUtil.HTTP_TEST_PROXY, proxies.get(0)); + } + +} + diff --git a/src/test/java/com/btr/proxy/selector/misc/ProxyListFallbackSelectorTest.java b/src/test/java/com/btr/proxy/selector/misc/ProxyListFallbackSelectorTest.java new file mode 100644 index 0000000..12c172c --- /dev/null +++ b/src/test/java/com/btr/proxy/selector/misc/ProxyListFallbackSelectorTest.java @@ -0,0 +1,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()); + } + +} + |