From c14566b7d2047b6747e359bf22ce5a17d972fe82 Mon Sep 17 00:00:00 2001 From: Steffen Pingel Date: Thu, 9 May 2013 05:10:59 +0200 Subject: 407445: [api] provide web locations for testing Change-Id: Ie967ab5336607fac4ba154a7ea35b73c3e217320 Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=407445 --- org.eclipse.mylyn.commons.sdk.util/.project | 2 +- .../eclipse/mylyn/commons/sdk/util/TestUrl.java | 74 ++++++++++++++++++++++ org.eclipse.mylyn.commons.tests/.classpath | 1 + .../mylyn/commons/tests/net/WebUtilTest.java | 20 +++--- 4 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 org.eclipse.mylyn.commons.sdk.util/src/org/eclipse/mylyn/commons/sdk/util/TestUrl.java diff --git a/org.eclipse.mylyn.commons.sdk.util/.project b/org.eclipse.mylyn.commons.sdk.util/.project index d2142fef..627f3824 100644 --- a/org.eclipse.mylyn.commons.sdk.util/.project +++ b/org.eclipse.mylyn.commons.sdk.util/.project @@ -33,7 +33,7 @@ - 1367109048408 + 1368069128607 10 diff --git a/org.eclipse.mylyn.commons.sdk.util/src/org/eclipse/mylyn/commons/sdk/util/TestUrl.java b/org.eclipse.mylyn.commons.sdk.util/src/org/eclipse/mylyn/commons/sdk/util/TestUrl.java new file mode 100644 index 00000000..24424b67 --- /dev/null +++ b/org.eclipse.mylyn.commons.sdk.util/src/org/eclipse/mylyn/commons/sdk/util/TestUrl.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2013 Tasktop Technologies and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tasktop Technologies - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.commons.sdk.util; + +import java.net.MalformedURLException; +import java.net.URL; + +/** + * Provides URLs for testing connections. + * + * @author Steffen Pingel + */ +public class TestUrl { + + public static final TestUrl DEFAULT = new TestUrl(); + + private final String URL_HTTP_404_NOT_FOUND = "http://mylyn.org/notfound"; + + private final String URL_HTTP_CONNECTION_REFUSED = "http://mylyn.org:9999/"; + + private final String URL_HTTP_CONNECTION_TIMEOUT = "http://google.com:9999/"; + + private final String URL_HTTP_OK = "http://mylyn.org/"; + + private final String URL_HTTP_UNKNOWN_HOST = "http://nonexistant.mylyn.org"; + + private final String URL_HTTPS_OK = "https://mylyn.org/"; + + public URL getConnectionRefused() { + return createUrl(URL_HTTP_CONNECTION_REFUSED); + } + + public URL getConnectionTimeout() { + return createUrl(URL_HTTP_CONNECTION_TIMEOUT); + } + + public URL getHttpNotFound() { + return createUrl(URL_HTTP_404_NOT_FOUND); + } + + public URL getHttpOk() { + return createUrl(URL_HTTP_OK); + } + + public URL getHttpsOk() { + return createUrl(URL_HTTPS_OK); + } + + public URL getUnknownHost() { + return createUrl(URL_HTTP_UNKNOWN_HOST); + } + + private URL createUrl(String url) { + try { + return new URL(url); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + + private TestUrl() { + // not intended to be instantiated + } + +} diff --git a/org.eclipse.mylyn.commons.tests/.classpath b/org.eclipse.mylyn.commons.tests/.classpath index 2c447f4e..e2615e50 100644 --- a/org.eclipse.mylyn.commons.tests/.classpath +++ b/org.eclipse.mylyn.commons.tests/.classpath @@ -5,6 +5,7 @@ + diff --git a/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/net/WebUtilTest.java b/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/net/WebUtilTest.java index f5399869..7b09467a 100644 --- a/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/net/WebUtilTest.java +++ b/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/net/WebUtilTest.java @@ -41,6 +41,7 @@ import org.eclipse.mylyn.commons.net.IProxyProvider; import org.eclipse.mylyn.commons.net.WebLocation; import org.eclipse.mylyn.commons.net.WebUtil; import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil; +import org.eclipse.mylyn.commons.sdk.util.TestUrl; import org.eclipse.mylyn.commons.tests.support.TestProxy; import org.eclipse.mylyn.commons.tests.support.TestProxy.Message; import org.eclipse.mylyn.internal.commons.net.AuthenticatedProxy; @@ -139,11 +140,10 @@ public class WebUtilTest extends TestCase { public void testExecute() throws Exception { StubProgressMonitor monitor = new StubProgressMonitor(); HttpClient client = new HttpClient(); - String url = "http://mylyn.org/"; - WebLocation location = new WebLocation(url); + WebLocation location = new WebLocation(TestUrl.DEFAULT.getHttpOk().toString()); HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, monitor); - GetMethod method = new GetMethod(url); + GetMethod method = new GetMethod(location.getUrl()); try { int result = WebUtil.execute(client, hostConfiguration, method, monitor); assertEquals(HttpStatus.SC_OK, result); @@ -155,11 +155,10 @@ public class WebUtilTest extends TestCase { public void testExecuteCancelStalledConnect() throws Exception { final StubProgressMonitor monitor = new StubProgressMonitor(); HttpClient client = new HttpClient(); - String url = "http://google.com:9999/"; - WebLocation location = new WebLocation(url); + WebLocation location = new WebLocation(TestUrl.DEFAULT.getConnectionTimeout().toString()); HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, monitor); - GetMethod method = new GetMethod(url); + GetMethod method = new GetMethod(location.getUrl()); try { Runnable runner = new Runnable() { public void run() { @@ -184,11 +183,10 @@ public class WebUtilTest extends TestCase { public void testExecuteAlreadyCancelled() throws Exception { StubProgressMonitor monitor = new StubProgressMonitor(); HttpClient client = new HttpClient(); - String url = "http://mylyn.org/"; - WebLocation location = new WebLocation(url); + WebLocation location = new WebLocation(TestUrl.DEFAULT.getHttpOk().toString()); HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, monitor); - GetMethod method = new GetMethod(url); + GetMethod method = new GetMethod(location.getUrl()); try { monitor.canceled = true; WebUtil.execute(client, hostConfiguration, method, monitor); @@ -217,13 +215,13 @@ public class WebUtilTest extends TestCase { public void testCreateHostConfigurationProxy() throws Exception { StubProgressMonitor monitor = new StubProgressMonitor(); HttpClient client = new HttpClient(); - WebUtil.createHostConfiguration(client, new WebLocation("http://mylyn.org/", null, null, new IProxyProvider() { + WebUtil.createHostConfiguration(client, new WebLocation(TestUrl.DEFAULT.getHttpOk().toString(), null, null, new IProxyProvider() { public Proxy getProxyForHost(String host, String proxyType) { assertEquals(IProxyData.HTTP_PROXY_TYPE, proxyType); return null; } }), monitor); - WebUtil.createHostConfiguration(client, new WebLocation("https://eclipse.org/", null, null, + WebUtil.createHostConfiguration(client, new WebLocation(TestUrl.DEFAULT.getHttpsOk().toString(), null, null, new IProxyProvider() { public Proxy getProxyForHost(String host, String proxyType) { assertEquals(IProxyData.HTTPS_PROXY_TYPE, proxyType); -- cgit v1.2.3