You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by gh...@apache.org on 2006/04/11 00:10:40 UTC
svn commit: r393091 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/net/ test/java/tests/api/java/net/
Author: gharley
Date: Mon Apr 10 15:10:39 2006
New Revision: 393091
URL: http://svn.apache.org/viewcvs?rev=393091&view=rev
Log:
HARMONY 223 : Six new J2SE 5 classes in java.net package haven't been implemented yet in Harmony
Added:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/CookieHandler.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/HttpRetryException.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Proxy.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelector.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelectorImpl.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/CookieHandlerTest.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java (with props)
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxyTest.java (with props)
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java?rev=393091&r1=393090&r2=393091&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java Mon Apr 10 15:10:39 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
package java.net;
-
/**
* This class is able to obtain authentication info for an connection, usually
* from user. First the application has to set the default authenticator which
@@ -34,10 +33,10 @@
private static Authenticator thisAuthenticator;
private static final NetPermission requestPasswordAuthenticationPermission = new NetPermission(
- "requestPasswordAuthentication");
+ "requestPasswordAuthentication"); //$NON-NLS-1$
private static final NetPermission setDefaultAuthenticatorPermission = new NetPermission(
- "setDefaultAuthenticator");
+ "setDefaultAuthenticator"); //$NON-NLS-1$
// the requester connection info
private String host;
@@ -141,10 +140,12 @@
InetAddress rAddr, int rPort, String rProtocol, String rPrompt,
String rScheme) {
SecurityManager sm = System.getSecurityManager();
- if (sm != null)
+ if (sm != null) {
sm.checkPermission(requestPasswordAuthenticationPermission);
- if (thisAuthenticator == null)
+ }
+ if (thisAuthenticator == null) {
return null;
+ }
// set the requester info so it knows what it is requesting
// authentication for
thisAuthenticator.addr = rAddr;
@@ -169,8 +170,9 @@
*/
public static void setDefault(Authenticator a) {
SecurityManager sm = System.getSecurityManager();
- if (sm != null)
+ if (sm != null) {
sm.checkPermission(setDefaultAuthenticatorPermission);
+ }
thisAuthenticator = a;
}
@@ -204,10 +206,12 @@
String rHost, InetAddress rAddr, int rPort, String rProtocol,
String rPrompt, String rScheme) {
SecurityManager sm = System.getSecurityManager();
- if (sm != null)
+ if (sm != null) {
sm.checkPermission(requestPasswordAuthenticationPermission);
- if (thisAuthenticator == null)
+ }
+ if (thisAuthenticator == null) {
return null;
+ }
// set the requester info so it knows what it is requesting
// authentication for
thisAuthenticator.host = rHost;
@@ -228,5 +232,60 @@
*/
protected final String getRequestingHost() {
return host;
+ }
+
+ /**
+ * an enum class of requestor type
+ *
+ */
+ // FIXME: This class needs java 5 Enum feature support.
+ // The code will be modified when Enum feature is ready.
+ public static final class RequestorType {
+
+ private String name;
+
+ /**
+ * type of proxy server
+ */
+ public static final Authenticator.RequestorType PROXY = new RequestorType(
+ "PROXY"); //$NON-NLS-1$
+
+ /**
+ * type of origin server
+ */
+ public static final Authenticator.RequestorType SERVER = new RequestorType(
+ "SERVER"); //$NON-NLS-1$
+
+ private static final RequestorType[] rt = { PROXY, SERVER };
+
+ private RequestorType(String name) {
+ this.name = name;
+ }
+
+ /**
+ *
+ * @return an array of requestor types
+ */
+ public static final RequestorType[] values() {
+ return rt;
+ }
+
+ /**
+ *
+ * @param name
+ * requestor types name
+ * @return a requestor type according to name if exist
+ */
+ public static RequestorType valueOf(String name) {
+ if (null == name) {
+ throw new NullPointerException();
+ }
+ for (int i = 0; i < rt.length; i++) {
+ if (rt[i].name.equals(name)) {
+ return rt[i];
+ }
+ }
+ throw new IllegalArgumentException(name);
+ }
}
}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/CookieHandler.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/CookieHandler.java?rev=393091&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/CookieHandler.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/CookieHandler.java Mon Apr 10 15:10:39 2006
@@ -0,0 +1,87 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.net;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * This class is ready for managing a stateful cookie with HTTP protocol
+ *
+ */
+public abstract class CookieHandler {
+
+ private static CookieHandler systemWideCookieHandler;
+
+ private final static NetPermission getCookieHandlerPermission = new NetPermission(
+ "getCookieHandler"); //$NON-NLS-1$
+
+ private final static NetPermission setCookieHandlerPermission = new NetPermission(
+ "setCookieHandler"); //$NON-NLS-1$
+
+ /**
+ * returns a system-wide cookie handler, or null if not set
+ *
+ * @return a cookie handler
+ */
+ public static CookieHandler getDefault() {
+ SecurityManager sm = System.getSecurityManager();
+ if (null != sm) {
+ sm.checkPermission(getCookieHandlerPermission);
+ }
+ return systemWideCookieHandler;
+ }
+
+ /**
+ * sets a system-wide cookie handler
+ *
+ * @param cHandler
+ * the cookie handler to set
+ */
+ public static void setDefault(CookieHandler cHandler) {
+ SecurityManager sm = System.getSecurityManager();
+ if (null != sm) {
+ sm.checkPermission(setCookieHandlerPermission);
+ }
+ systemWideCookieHandler = cHandler;
+ }
+
+ /**
+ * searchs and gets all cookies in the cache by the specified uri in the
+ * request header.
+ *
+ * @param uri
+ * the specified uri to search for
+ * @param requestHeaders
+ * a list of request headers
+ * @return a map that record all such cookies, the map is unchangeable
+ * @throws IOException
+ * if some error of I/O operation occurs
+ */
+ public abstract Map get(URI uri, Map requestHeaders) throws IOException;
+
+ /**
+ * sets cookies according to uri and responseHeaders
+ *
+ * @param uri
+ * the specified uri
+ * @param responseHeaders
+ * a list of request headers
+ * @throws IOException
+ * if some error of I/O operation occurs
+ */
+ public abstract void put(URI uri, Map responseHeaders) throws IOException;
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/CookieHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/HttpRetryException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/HttpRetryException.java?rev=393091&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/HttpRetryException.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/HttpRetryException.java Mon Apr 10 15:10:39 2006
@@ -0,0 +1,83 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.net;
+
+import java.io.IOException;
+
+/**
+ * The exception to be thrown when a request cannot be retried.
+ *
+ */
+public class HttpRetryException extends IOException {
+
+ private static final long serialVersionUID = -9186022286469111381L;
+
+ private int responseCode;
+
+ private String location = null;
+
+ /**
+ * new a HttpRetryException by given detail message and responseCode
+ *
+ * @param detail
+ * detail for this exception
+ * @param code
+ * http response code to return
+ */
+ public HttpRetryException(String detail, int code) {
+ super(detail);
+ responseCode = code;
+ }
+
+ /**
+ * new a HttpRetryException by given detail message, responseCode and the
+ * Location response header
+ *
+ * @param detail
+ * detail for this exception
+ * @param code
+ * http response code to return
+ * @param location
+ * the error resulted from redirection, the Location header can
+ * be recorded
+ */
+ public HttpRetryException(String detail, int code, String location) {
+ super(detail);
+ responseCode = code;
+ this.location = location;
+ }
+
+ /**
+ * @return the Location header recorded
+ */
+ public String getLocation() {
+ return location;
+ }
+
+ /**
+ * @return the detail reason for this exception
+ */
+ public String getReason() {
+ return getMessage();
+ }
+
+ /**
+ * @return a http response code
+ */
+ public int responseCode() {
+ return responseCode;
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/HttpRetryException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Proxy.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Proxy.java?rev=393091&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Proxy.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Proxy.java Mon Apr 10 15:10:39 2006
@@ -0,0 +1,226 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package java.net;
+
+/**
+ * <p>
+ * This class is about proxy setting. A proxy contains <code>type</code>,
+ * proxy host address information. There are three types of <code>Proxy</code>:
+ * <li>Direct type proxy</li>
+ * <li>HTTP type proxy</li>
+ * <li>SOCKS type proxy</li>
+ * </p>
+ * <p>
+ * A <code>Proxy</code> instance is immutable.
+ * </p>
+ */
+public class Proxy {
+
+ /**
+ * Represents <code>Proxy.Type.DIRECT</code> type proxy setting. It tells
+ * protocol handlers not to use any proxy.
+ */
+ public static final Proxy NO_PROXY = new Proxy();
+
+ private Proxy.Type type;
+
+ private SocketAddress address;
+
+ /**
+ * New a <code>Proxy</code> instance. SocketAddress must NOT be null when
+ * <code>type</code> is either <code>Proxy.Type.HTTP</code> or
+ * <code>Proxy.Type.SOCKS</code>. For <code>Proxy.Type.DIRECT</code>
+ * type proxy, use <code>Proxy.NO_PROXY</code> directly instead of
+ * constructing it.
+ *
+ * @param type
+ * proxy type
+ * @param sa
+ * proxy address
+ * @throws IllegalArgumentException
+ * when <code>type</code> is <code>Proxy.Type.DIRECT</code>
+ * or SocketAddress is null.
+ */
+ public Proxy(Proxy.Type type, SocketAddress sa) {
+ /*
+ * Don't use DIRECT type to construct a proxy instance directly.
+ * SocketAddress must NOT be null.
+ */
+ if (type == Type.DIRECT || null == sa) {
+ throw new IllegalArgumentException(
+ "Illegal Proxy.Type or SocketAddress argument.");
+ }
+ this.type = type;
+ address = sa;
+ }
+
+ /*
+ * Constructs a Proxy instance, which is Proxy.DIRECT type with null
+ * SocketAddress. This constructor is used for NO_PROXY.
+ */
+ private Proxy() {
+ type = Type.DIRECT;
+ address = null;
+ }
+
+ /**
+ * Gets the proxy type.
+ *
+ * @return the proxy type.
+ */
+ public Proxy.Type type() {
+ return type;
+ }
+
+ /**
+ * Gets the proxy address.
+ *
+ * @return the proxy address for <code>HTTP</code> and <code>SOCKS</code>
+ * type proxy. Returns null for <code>DIRECT</code> type proxy.
+ */
+ public SocketAddress address() {
+ return address;
+ }
+
+ /**
+ * <p>
+ * Representing string of the proxy. The string consists of
+ * <code>type.toString()</code> and <code>address.toString()</code> if
+ * <code>type</code> and <code>address</code> are not null.
+ * </p>
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ * @return representing string of the proxy.
+ */
+ public String toString() {
+ String proxyString = String.valueOf(type);
+ if (null != address) {
+ proxyString += "/" + address.toString();
+ }
+ return proxyString;
+ }
+
+ /**
+ * <p>
+ * Compare <code>obj</code> with current proxy. Returns false if the
+ * <code>obj</code> is not a <code>Proxy</code> object. Returns true if
+ * and only if the <code>obj</code> has the same <code>address</code>
+ * and <code>type</code> value as current proxy.
+ * </p>
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ * @return true if <code>obj</code> represents the same proxy. Otherwise,
+ * returns false.
+ */
+ public final boolean equals(Object obj) {
+ if(this == obj){
+ return true;
+ }
+ if (!(obj instanceof Proxy)) {
+ return false;
+ }
+ Proxy another = (Proxy) obj;
+ // address is null when and only when it's NO_PROXY.
+ return (type == another.type) && address.equals(another.address);
+ }
+
+ /**
+ * gets the hash code of <code>Proxy</code>.
+ *
+ * @see java.lang.Object#hashCode()
+ * @return the hash code of <code>Proxy</code>.
+ */
+ public final int hashCode() {
+ int ret = 0;
+ ret += type.hashCode();
+ if (null != address) {
+ ret += address.hashCode();
+ }
+ return ret;
+ }
+
+ /**
+ * The proxy type, includes <code>DIRECT</code>, <code>HTTP</code> and
+ * <code>SOCKS</code>.
+ */
+ // FIXME: This class needs java 5 Enum feature support.
+ // The code will be modified when Enum feature is ready.
+ public static final class Type {
+ /**
+ * Direct connection. Connect without any proxy.
+ */
+ public static final Type DIRECT = new Type("DIRECT");
+
+ /**
+ * HTTP type proxy. It's often used by protocol handlers such as HTTP,
+ * HTTPS and FTP.
+ */
+ public static final Type HTTP = new Type("HTTP");
+
+ /**
+ * SOCKS type proxy.
+ */
+ public static final Type SOCKS = new Type("SOCKS");
+
+ private String name;
+
+ /*
+ * Build-in proxy list.
+ */
+ private static final Type buildInProxyList[] = { DIRECT, HTTP, SOCKS };
+
+ Type(String name) {
+ this.name = name;
+ }
+
+ /**
+ * Retrieve <code>Proxy</code> by <code>name</code>.
+ *
+ * @throws NullPointerException
+ * if <code>name</code> is null.
+ * @throws IllegalArgumentException
+ * if <code>name</code> is not a valid proxy type name.
+ */
+ public static Proxy.Type valueOf(String name) {
+ if (null == name) {
+ throw new NullPointerException();
+ }
+ for (int i = 0; i < buildInProxyList.length; ++i) {
+ if (buildInProxyList[i].name.equals(name)) {
+ return buildInProxyList[i];
+ }
+ }
+ throw new IllegalArgumentException(name);
+ }
+
+ /**
+ * Retrieve an array contains all available proxy types. The sequence of
+ * proxy types is the same as their declaration.
+ *
+ * @return an array contains all available proxy types.
+ */
+ public static final Proxy.Type[] values() {
+ return buildInProxyList;
+ }
+
+ /**
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ return name;
+ }
+
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Proxy.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelector.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelector.java?rev=393091&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelector.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelector.java Mon Apr 10 15:10:39 2006
@@ -0,0 +1,130 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package java.net;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * <p>
+ * Selects applicable proxies when connecting to network resouce represented by
+ * a <code>URI</code>. An implementation of <code>ProxySelector</code>
+ * should be a concrete subclass of <code>ProxySelector</code>. Method
+ * <code>select</code> returns a list of proxies according to the
+ * <code>uri</code>. If a connection can't be established, the caller should
+ * notify proxy selector by invoking <code>connectFailed</code> method.
+ * </p>
+ * <p>
+ * A proxy selector can be registered/unregistered by calling
+ * <code>setDefault</code> method and retrieved by calling
+ * <code>getDefault</code> method.
+ * </p>
+ *
+ */
+public abstract class ProxySelector {
+
+ private static ProxySelector defaultSelector = new ProxySelectorImpl();
+
+ /*
+ * "getProxySelector" permission. getDefault method requires this
+ * permission.
+ */
+ private final static NetPermission getProxySelectorPermission = new NetPermission(
+ "getProxySelector");
+
+ /*
+ * "setProxySelector" permission. setDefault method requires this
+ * permission.
+ */
+ private final static NetPermission setProxySelectorPermission = new NetPermission(
+ "setProxySelector");
+
+ /**
+ * Constructor method.
+ */
+ public ProxySelector() {
+
+ }
+
+ /**
+ * Gets system default <code>ProxySelector</code>.
+ *
+ * @return system default <code>ProxySelector</code>.
+ * @throws SecurtiyException
+ * If a security manager is installed and it doesn't have
+ * <code>NetPermission("getProxySelector")</code>.
+ */
+ public static ProxySelector getDefault() {
+ SecurityManager sm = System.getSecurityManager();
+ if (null != sm) {
+ sm.checkPermission(getProxySelectorPermission);
+ }
+ return defaultSelector;
+ }
+
+ /**
+ * Sets system default <code>ProxySelector</code>. Unsets system default
+ * <code>ProxySelector</code> if <code>selector</code> is null.
+ *
+ * @throws SecurtiyException
+ * If a security manager is installed and it doesn't have
+ * <code>NetPermission("setProxySelector")</code>.
+ */
+ public static void setDefault(ProxySelector selector) {
+ SecurityManager sm = System.getSecurityManager();
+ if (null != sm) {
+ sm.checkPermission(setProxySelectorPermission);
+ }
+ defaultSelector = selector;
+ }
+
+ /**
+ * Gets applicable proxies based on the accessing protocol of
+ * <code>uri</code>. The format of URI is defined as below:
+ * <li>http URI stands for http connection.</li>
+ * <li>https URI stands for https connection.</li>
+ * <li>ftp URI stands for ftp connection.</li>
+ * <li>socket:://ip:port URI stands for tcp client sockets connection.</li>
+ *
+ * @param uri
+ * the destination <code>URI</code> object.
+ * @return a list contains all applicable proxies. If no proxy is available,
+ * returns a list only contains one element
+ * <code>Proxy.NO_PROXY</code>.
+ * @throws IllegalArgumentException
+ * If any argument is null.
+ */
+ public abstract List select(URI uri);
+
+ /**
+ * If the connection can not be established to the proxy server, this method
+ * will be called. An implementation may adjust proxy the sequence of
+ * proxies returned by <code>select(String, String)</code>.
+ *
+ * @param uri
+ * the <code>URI</code> that the connection fails to connect
+ * to.
+ * @param sa
+ * <code>SocketAddress</code> of the proxy.
+ * @param ioe
+ * The <code>IOException</code> which is thrown during
+ * connection establishment.
+ * @throws IllegalArgumentException
+ * If any argument is null.
+ */
+ public abstract void connectFailed(URI uri, SocketAddress sa,
+ IOException ioe);
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelectorImpl.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelectorImpl.java?rev=393091&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelectorImpl.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelectorImpl.java Mon Apr 10 15:10:39 2006
@@ -0,0 +1,305 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package java.net;
+
+import java.io.IOException;
+import java.security.AccessController;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.harmony.luni.util.PriviAction;
+
+/*
+ * Default implementation for ProxySelector
+ */
+class ProxySelectorImpl extends ProxySelector {
+
+ private static final int HTTP_PROXY_PORT = 80;
+
+ private static final int HTTPS_PROXY_PORT = 443;
+
+ private static final int FTP_PROXY_PORT = 80;
+
+ private static final int SOCKS_PROXY_PORT = 1080;
+
+ /*
+ * Constructor method.
+ */
+ public ProxySelectorImpl() {
+ super();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.net.ProxySelector#connectFailed(java.net.URI,
+ * java.net.SocketAddress, java.io.IOException)
+ */
+ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+ if (null == uri || null == sa || null == ioe) {
+ throw new IllegalArgumentException("null argument is not allowed");
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.net.ProxySelector#select(java.net.URI)
+ */
+ public List select(URI uri) {
+ // argument check
+ if (null == uri) {
+ throw new IllegalArgumentException("null argument is not allowed");
+ }
+ // check scheme
+ String scheme = uri.getScheme();
+ if (null == scheme) {
+ throw new IllegalArgumentException();
+ }
+ // check host
+ String host = uri.getHost();
+ if (null == host) {
+ throw new IllegalArgumentException();
+ }
+
+ Proxy proxy = Proxy.NO_PROXY;
+
+ if ("http".equals(scheme)) { //$NON-NLS-1$
+ proxy = selectHttpProxy(host);
+ } else if ("https".equals(scheme)) { //$NON-NLS-1$
+ proxy = selectHttpsProxy();
+ } else if ("ftp".equals(scheme)) { //$NON-NLS-1$
+ proxy = selectFtpProxy(host);
+ } else if ("socket".equals(scheme)) { //$NON-NLS-1$
+ proxy = selectSocksProxy();
+ }
+ List proxyList = new ArrayList(1);
+ proxyList.add(proxy);
+ return proxyList;
+ }
+
+ /*
+ * Gets proxy for http request. 1. gets from "http.proxyHost", then gets
+ * port from "http.proxyPort", or from "proxyPort" if "http.proxyPort" is
+ * unavailable. 2. gets from "proxyHost" if 1 is unavaible,then get port
+ * from "proxyPort", or from "http.proxyPort" if "proxyPort" is unavailable.
+ * 3. gets from "socksProxyHost" if 2 is unavailable.
+ */
+
+ private Proxy selectHttpProxy(String uriHost) {
+ String host;
+ String port = null;
+ Proxy.Type type = Proxy.Type.DIRECT;
+
+ String nonProxyHosts = getSystemProperty("http.nonProxyHosts"); //$NON-NLS-1$
+ // if host is in non proxy host list, returns Proxy.NO_PROXY
+ if (isNonProxyHost(uriHost, nonProxyHosts)) {
+ return Proxy.NO_PROXY;
+ }
+
+ host = getSystemProperty("http.proxyHost"); //$NON-NLS-1$
+ if (null != host) {
+ // case 1: http.proxyHost is set, use exact http proxy
+ type = Proxy.Type.HTTP;
+ port = getSystemPropertyOrAlternative("http.proxyPort", //$NON-NLS-1$
+ "proxyPort", String.valueOf(HTTP_PROXY_PORT)); //$NON-NLS-1$
+ } else if((host = getSystemProperty("proxyHost", null)) != null) {
+ // case 2: proxyHost is set, use exact http proxy
+ type = Proxy.Type.HTTP;
+ port = getSystemPropertyOrAlternative("proxyPort", //$NON-NLS-1$
+ "http.proxyPort", String.valueOf(HTTP_PROXY_PORT)); //$NON-NLS-1$
+
+ }else if((host = getSystemProperty("socksProxyHost"))!=null){
+ // case 3: use socks proxy instead
+ type = Proxy.Type.SOCKS;
+ port = getSystemProperty(
+ "socksProxyPort", String.valueOf(SOCKS_PROXY_PORT)); //$NON-NLS-1$
+ }
+ int defaultPort = (type == Proxy.Type.SOCKS) ? SOCKS_PROXY_PORT
+ : HTTP_PROXY_PORT;
+ return createProxy(type, host, port, defaultPort);
+ }
+
+ /*
+ * Gets proxy for https request.
+ */
+ private Proxy selectHttpsProxy() {
+ String host ;
+ String port = null;
+ Proxy.Type type = Proxy.Type.DIRECT;
+
+ host = getSystemProperty("https.proxyHost");
+ if (null != host) {
+ // case 1: use exact https proxy
+ type = Proxy.Type.HTTP;
+ port = getSystemProperty(
+ "https.proxyPort", String.valueOf(HTTPS_PROXY_PORT)); //$NON-NLS-1$
+ } else {
+ host = getSystemProperty("socksProxyHost"); //$NON-NLS-1$
+ if (null != host) {
+ // case 2: use socks proxy instead
+ type = Proxy.Type.SOCKS;
+ port = getSystemProperty(
+ "socksProxyPort", String.valueOf(SOCKS_PROXY_PORT)); //$NON-NLS-1$
+ }
+ }
+ int defaultPort = (type == Proxy.Type.SOCKS) ? SOCKS_PROXY_PORT
+ : HTTPS_PROXY_PORT;
+ return createProxy(type, host, port, defaultPort);
+ }
+
+ /*
+ * Gets proxy for ftp request.
+ */
+
+ private Proxy selectFtpProxy(String uriHost) {
+ String host;
+ String port = null;
+ Proxy.Type type = Proxy.Type.DIRECT;
+ String nonProxyHosts = getSystemProperty("ftp.nonProxyHosts"); //$NON-NLS-1$
+ // if host is in non proxy host list, returns Proxy.NO_PROXY
+ if (isNonProxyHost(uriHost, nonProxyHosts)) {
+ return Proxy.NO_PROXY;
+ }
+
+ host = getSystemProperty("ftp.proxyHost");
+ if (null != host) {
+ // case 1: use exact ftp proxy
+ type = Proxy.Type.HTTP;
+ port = getSystemProperty(
+ "ftp.proxyPort", String.valueOf(FTP_PROXY_PORT)); //$NON-NLS-1$
+ } else {
+ host = getSystemProperty("socksProxyHost"); //$NON-NLS-1$
+ if (null != host) {
+ // case 2: use socks proxy instead
+ type = Proxy.Type.SOCKS;
+ port = getSystemProperty(
+ "socksProxyPort", String.valueOf(SOCKS_PROXY_PORT)); //$NON-NLS-1$
+ }
+ }
+ int defaultPort = (type == Proxy.Type.SOCKS) ? SOCKS_PROXY_PORT
+ : FTP_PROXY_PORT;
+ return createProxy(type, host, port, defaultPort);
+ }
+
+ /*
+ * Gets proxy for socks request.
+ */
+ private Proxy selectSocksProxy() {
+ String host;
+ String port = null;
+ Proxy.Type type = Proxy.Type.DIRECT;
+
+ host = getSystemProperty("socksProxyHost"); //$NON-NLS-1$
+ if (null != host) {
+ type = Proxy.Type.SOCKS;
+ port = getSystemProperty(
+ "socksProxyPort", String.valueOf(SOCKS_PROXY_PORT)); //$NON-NLS-1$
+ }
+ return createProxy(type, host, port, SOCKS_PROXY_PORT);
+ }
+
+ /*
+ * checks whether the host needs proxy. return true if it doesn't need a
+ * proxy.
+ */
+ private boolean isNonProxyHost(String host, String nonProxyHosts) {
+ // nonProxyHosts is not set
+ if (null == nonProxyHosts) {
+ return false;
+ }
+ // Construct regex expression of nonProxyHosts
+ int length = nonProxyHosts.length();
+ char ch;
+ StringBuilder buf = new StringBuilder(length);
+ for (int i = 0; i < nonProxyHosts.length(); i++) {
+ ch = nonProxyHosts.charAt(i);
+ switch (ch) {
+ case '.':
+ buf.append("\\.");
+ break;
+ case '*':
+ buf.append(".*");
+ break;
+ default:
+ buf.append(ch);
+ }
+ }
+ String nonProxyHostsReg = buf.toString();
+ // check whether the host is the nonProxyHosts.
+ return host.matches(nonProxyHostsReg);
+ }
+
+ /*
+ * Create Proxy by "type","host" and "port".
+ */
+ private Proxy createProxy(Proxy.Type type, String host, String port,
+ int defaultPort) {
+ Proxy proxy;
+ if (type == Proxy.Type.DIRECT) {
+ proxy = Proxy.NO_PROXY;
+ } else {
+ int iPort;
+ try {
+ iPort = Integer.valueOf(port).intValue();
+ } catch (NumberFormatException e) {
+ iPort = defaultPort;
+ }
+ proxy = new Proxy(type, InetSocketAddress.createUnresolved(host,
+ iPort));
+ }
+ return proxy;
+ }
+
+ /*
+ * gets system property, privileged operation. If the value of the property
+ * is null or empty String, it returns defaultValue.
+ */
+ private String getSystemProperty(final String property) {
+ return getSystemProperty(property, null);
+ }
+
+ /*
+ * gets system property, privileged operation. If the value of the property
+ * is null or empty String, it returns defaultValue.
+ */
+ private String getSystemProperty(final String property,
+ final String defaultVaule) {
+ String value = (String) AccessController.doPrivileged(new PriviAction(
+ property));
+ if (null == value || "".equals(value)) {
+ value = defaultVaule;
+ }
+ return value;
+ }
+
+ /*
+ * gets system property, privileged operation. If the value of "key"
+ * property is null, then retrieve value from "alternative" property.
+ * Finllay, if the value is null or empty String, it returns defaultValue.
+ */
+ private String getSystemPropertyOrAlternative(final String key,
+ final String alternativeKey, final String defaultValue) {
+ String value = getSystemProperty(key);
+ if (value == null) {
+ value = getSystemProperty(alternativeKey);
+ if (null == value) {
+ value = defaultValue;
+ }
+ }
+ return value;
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ProxySelectorImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java?rev=393091&r1=393090&r2=393091&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java Mon Apr 10 15:10:39 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +30,9 @@
public static Test suite() {
TestSuite suite = new TestSuite("Tests for java.net");
// $JUnit-BEGIN$
+ suite.addTestSuite(AuthenticatorTest.class);
suite.addTestSuite(BindExceptionTest.class);
+ suite.addTestSuite(CookieHandlerTest.class);
suite.addTestSuite(ConnectExceptionTest.class);
suite.addTestSuite(DatagramPacketTest.class);
suite.addTestSuite(DatagramSocketTest.class);
@@ -47,6 +49,8 @@
suite.addTestSuite(NoRouteToHostExceptionTest.class);
suite.addTestSuite(PasswordAuthenticationTest.class);
suite.addTestSuite(ProtocolExceptionTest.class);
+ suite.addTestSuite(ProxyTest.class);
+ suite.addTestSuite(ProxySelectorTest.class);
suite.addTestSuite(ResponseCacheTest.class);
suite.addTestSuite(ServerSocketTest.class);
suite.addTestSuite(SocketTest.class);
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java?rev=393091&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java Mon Apr 10 15:10:39 2006
@@ -0,0 +1,57 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.net;
+
+import java.net.Authenticator;
+import java.net.Authenticator.RequestorType;
+
+import junit.framework.TestCase;
+
+public class AuthenticatorTest extends TestCase {
+
+ /**
+ * @tests java.net.Authenticator.RequestorType#valueOf(String)
+ */
+ public void test_RequestorType_valueOfLjava_lang_String() throws Exception {
+ assertEquals(RequestorType.PROXY, Authenticator.RequestorType
+ .valueOf("PROXY"));
+ assertEquals(RequestorType.SERVER, Authenticator.RequestorType
+ .valueOf("SERVER"));
+ try {
+ RequestorType rt = Authenticator.RequestorType.valueOf("BADNAME");
+ fail("Must throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // correct
+ }
+ // Some old RIs,which throw IllegalArgumentException, will fail
+ // this test case. Latest RIs throw NullPointerException.
+ try {
+ RequestorType rt = Authenticator.RequestorType.valueOf(null);
+ fail("Must throw NullPointerException");
+ } catch (NullPointerException e) {
+ // correct
+ }
+ }
+
+ /**
+ * @tests java.net.Authenticator.RequestorType#values()
+ */
+ public void test_RequestorType_values() throws Exception {
+ RequestorType[] rt = RequestorType.values();
+ assertEquals(RequestorType.PROXY, rt[0]);
+ assertEquals(RequestorType.SERVER, rt[1]);
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/CookieHandlerTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/CookieHandlerTest.java?rev=393091&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/CookieHandlerTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/CookieHandlerTest.java Mon Apr 10 15:10:39 2006
@@ -0,0 +1,127 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.net;
+
+import java.io.IOException;
+import java.net.CookieHandler;
+import java.net.NetPermission;
+import java.net.URI;
+import java.security.Permission;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+public class CookieHandlerTest extends TestCase {
+
+ /**
+ * @tests java.net.CookieHandler#getDefault()
+ */
+ public void test_GetDefault() {
+ assertNull(CookieHandler.getDefault());
+ }
+
+ /**
+ * @tests java.net.CookieHandler#setDefault(CookieHandler)
+ */
+ public void test_SetDefault_java_net_cookieHandler() {
+ MockCookieHandler rc1 = new MockCookieHandler();
+ MockCookieHandler rc2 = new MockCookieHandler();
+ CookieHandler.setDefault(rc1);
+ assertSame(CookieHandler.getDefault(), rc1);
+ CookieHandler.setDefault(rc2);
+ assertSame(CookieHandler.getDefault(), rc2);
+ CookieHandler.setDefault(null);
+ assertNull(CookieHandler.getDefault());
+ }
+
+ /**
+ * @tests java.net.CookieHandler#getDefault()
+ */
+ public void testGetDefault_Security() {
+ SecurityManager old = System.getSecurityManager();
+ try {
+ System.setSecurityManager(new MockSM());
+ } catch (SecurityException e) {
+ System.err.println("Unable to reset securityManager,test ignored");
+ return;
+ }
+ try {
+ CookieHandler.getDefault();
+ fail("should throw SecurityException");
+ } catch (SecurityException e) {
+ // correct
+ } finally {
+ System.setSecurityManager(old);
+ }
+ }
+
+ /**
+ * @tests java.net.CookieHandler#setDefault(CookieHandler)
+ */
+ public void testSetDefault_Security() {
+ CookieHandler rc = new MockCookieHandler();
+ SecurityManager old = System.getSecurityManager();
+ try {
+ System.setSecurityManager(new MockSM());
+ } catch (SecurityException e) {
+ System.err.println("Unable to reset securityManager,test ignored");
+ return;
+ }
+
+ try {
+ CookieHandler.setDefault(rc);
+ fail("should throw SecurityException");
+ } catch (SecurityException e) {
+ // correct
+ } finally {
+ System.setSecurityManager(old);
+ }
+ }
+
+ class MockCookieHandler extends CookieHandler {
+
+ public Map get(URI uri, Map requestHeaders) throws IOException {
+ return null;
+ }
+
+ public void put(URI uri, Map responseHeaders) throws IOException {
+ // empty
+ }
+
+ }
+
+ class MockSM extends SecurityManager {
+ public void checkPermission(Permission permission) {
+ if (permission instanceof NetPermission) {
+ if ("setCookieHandler".equals(permission.getName())) {
+ throw new SecurityException();
+ }
+ }
+
+ if (permission instanceof NetPermission) {
+ if ("getCookieHandler".equals(permission.getName())) {
+ throw new SecurityException();
+ }
+ }
+
+ if (permission instanceof RuntimePermission) {
+ if ("setSecurityManager".equals(permission.getName())) {
+ return;
+ }
+ }
+ }
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/CookieHandlerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java?rev=393091&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java Mon Apr 10 15:10:39 2006
@@ -0,0 +1,569 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.net;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.NetPermission;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.security.Permission;
+import java.util.List;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+public class ProxySelectorTest extends TestCase {
+
+ private static final String HTTP_PROXY_HOST = "127.0.0.1";
+
+ private static final int HTTP_PROXY_PORT = 80;
+
+ private static final String HTTPS_PROXY_HOST = "127.0.0.2";
+
+ private static final int HTTPS_PROXY_PORT = 443;
+
+ private static final String FTP_PROXY_HOST = "127.0.0.3";
+
+ private static final int FTP_PROXY_PORT = 80;
+
+ private static final String SOCKS_PROXY_HOST = "127.0.0.4";
+
+ private static final int SOCKS_PROXY_PORT = 1080;
+
+ private static URI httpUri;
+
+ private static URI ftpUri;
+
+ private static URI httpsUri;
+
+ private static URI tcpUri;
+
+ private List proxyList;
+
+ private ProxySelector selector = ProxySelector.getDefault();
+
+ static {
+ try {
+ httpUri = new URI("http://test.com");
+ ftpUri = new URI("ftp://test.com");
+ httpsUri = new URI("https://test.com");
+ tcpUri = new URI("socket://host.com");
+ } catch (URISyntaxException e) {
+
+ }
+ }
+
+ /*
+ * Original system properties must be restored after running each test case.
+ */
+ private Properties orignalSystemProperties;
+
+ /**
+ * @tests java.net.ProxySelector#getDefault()
+ */
+ public void test_getDefault() {
+ ProxySelector selector1 = ProxySelector.getDefault();
+ assertNotNull(selector1);
+
+ ProxySelector selector2 = ProxySelector.getDefault();
+ assertSame(selector1, selector2);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#getDefault()
+ */
+ public void test_getDefault_Security() {
+ SecurityManager orignalSecurityManager = System.getSecurityManager();
+ try {
+ System.setSecurityManager(new MockSecurityManager());
+ } catch (SecurityException e) {
+ System.err.println("No setSecurityManager permission.");
+ System.err.println("test_getDefault_Security is not tested");
+ return;
+ }
+ try {
+ ProxySelector.getDefault();
+ fail("should throw SecurityException");
+ } catch (SecurityException e) {
+ // expected
+ } finally {
+ System.setSecurityManager(orignalSecurityManager);
+ }
+ }
+
+ /**
+ * @tests java.net.ProxySelector#setDefault(ProxySelector)}
+ */
+ public void test_setDefaultLjava_net_ProxySelector() {
+ ProxySelector originalSelector = ProxySelector.getDefault();
+ try {
+ ProxySelector newSelector = new MockProxySelector();
+ ProxySelector.setDefault(newSelector);
+ assertSame(newSelector, ProxySelector.getDefault());
+ // use null to unset
+ ProxySelector.setDefault(null);
+ assertSame(null, ProxySelector.getDefault());
+ } finally {
+ ProxySelector.setDefault(originalSelector);
+ }
+ }
+
+ /**
+ * @tests java.net.ProxySelector#setDefault(ProxySelector)}
+ */
+ public void test_setDefaultLjava_net_ProxySelector_Security() {
+ ProxySelector originalSelector = ProxySelector.getDefault();
+ SecurityManager orignalSecurityManager = System.getSecurityManager();
+ try {
+ System.setSecurityManager(new MockSecurityManager());
+ } catch (SecurityException e) {
+ System.err.println("No setSecurityManager permission.");
+ System.err
+ .println("test_setDefaultLjava_net_ProxySelector_Security is not tested");
+ return;
+ }
+ try {
+ ProxySelector.setDefault(new MockProxySelector());
+ fail("should throw SecurityException");
+ } catch (SecurityException e) {
+ // expected
+ } finally {
+ System.setSecurityManager(orignalSecurityManager);
+ ProxySelector.setDefault(originalSelector);
+ }
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectExact()
+ throws URISyntaxException {
+ // no proxy, return a proxyList only contains NO_PROXY
+ proxyList = selector.select(httpUri);
+ assertProxyEquals(proxyList,Proxy.NO_PROXY);
+
+ // set http proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+ System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
+ // set https proxy
+ System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
+ System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
+ // set ftp proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
+ // set socks proxy
+ System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
+ System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
+
+ proxyList = selector.select(httpUri);
+ assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTP_PROXY_HOST,HTTP_PROXY_PORT);
+
+ proxyList = selector.select(httpsUri);
+ assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTPS_PROXY_HOST,HTTPS_PROXY_PORT);
+
+ proxyList = selector.select(ftpUri);
+ assertProxyEquals(proxyList,Proxy.Type.HTTP,FTP_PROXY_HOST,FTP_PROXY_PORT);
+
+ proxyList = selector.select(tcpUri);
+ assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
+
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectExact_DefaultPort()
+ throws URISyntaxException {
+ // set http proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+
+ // set https proxy
+ System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
+ // set ftp proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ // set socks proxy
+ System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
+
+ proxyList = selector.select(httpUri);
+ assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTP_PROXY_HOST,HTTP_PROXY_PORT);
+
+ proxyList = selector.select(httpsUri);
+ assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTPS_PROXY_HOST,HTTPS_PROXY_PORT);
+
+ proxyList = selector.select(ftpUri);
+ assertProxyEquals(proxyList,Proxy.Type.HTTP,FTP_PROXY_HOST,FTP_PROXY_PORT);
+
+ proxyList = selector.select(tcpUri);
+ assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
+
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectExact_InvalidPort()
+ throws URISyntaxException {
+ final String INVALID_PORT = "abc";
+
+ // set http proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+ System.setProperty("http.proxyPort", INVALID_PORT);
+ // set https proxy
+ System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
+ System.setProperty("https.proxyPort", INVALID_PORT);
+ // set ftp proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ System.setProperty("ftp.proxyPort", INVALID_PORT);
+ // set socks proxy
+ System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
+ System.setProperty("socksproxyPort", INVALID_PORT);
+
+ proxyList = selector.select(httpUri);
+ assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTP_PROXY_HOST,HTTP_PROXY_PORT);
+
+ proxyList = selector.select(httpsUri);
+ assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTPS_PROXY_HOST,HTTPS_PROXY_PORT);
+
+ proxyList = selector.select(ftpUri);
+ assertProxyEquals(proxyList,Proxy.Type.HTTP,FTP_PROXY_HOST,FTP_PROXY_PORT);
+
+ proxyList = selector.select(tcpUri);
+ assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ // RI may fail this test case.
+ // Uncomment this test case when regex.jar is ready.
+ /*
+ public void test_selectLjava_net_URI_Select_NonProxyHosts()
+ throws URISyntaxException {
+ // RI's bug. Some RIs may fail this test case.
+ URI[] httpUris = { new URI("http://test.com"),
+ new URI("http://10.10.1.2"), new URI("http://a"),
+ new URI("http://def.abc.com") };
+ URI[] ftpUris = { new URI("ftp://test.com"),
+ new URI("ftp://10.10.1.2"), new URI("ftp://a"),
+ new URI("ftp://def.abc.com") };
+
+ // set http proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+ System.setProperty("http.nonProxyHosts", "a|b|tes*|10.10.*|*.abc.com");
+ // set ftp proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ System.setProperty("ftp.nonProxyHosts", "a|b|tes*|10.10.*|*.abc.com");
+
+ for (int i = 0; i < httpUris.length; i++) {
+ proxyList = selector.select(httpUris[i]);
+ assertProxyEquals(proxyList,Proxy.NO_PROXY);
+ }
+
+ for (int i = 0; i < ftpUris.length; i++) {
+ proxyList = selector.select(ftpUris[i]);
+ assertProxyEquals(proxyList,Proxy.NO_PROXY);
+ }
+ }*/
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectLikeHTTP()
+ throws URISyntaxException {
+ System.setProperty("http.proxyHost", "");
+ // set https proxy
+ System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
+ System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
+ // set ftp proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
+ // set socks proxy
+ System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
+ System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
+
+ proxyList = selector.select(httpUri);
+ assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectNoHTTP()
+ throws URISyntaxException {
+ // set https proxy
+ System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
+ System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
+ // set ftp proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
+
+ proxyList = selector.select(httpUri);
+ assertProxyEquals(proxyList,Proxy.NO_PROXY);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectLikeHTTPS()
+ throws URISyntaxException {
+ // set http proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+ System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
+ // set https proxy host empty
+ System.setProperty("http.proxyHost", "");
+ // set ftp proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
+ // set socks proxy
+ System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
+ System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
+
+ proxyList = selector.select(httpsUri);
+ assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectNoHTTPS()
+ throws URISyntaxException {
+ // set https proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+ System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
+ // set ftp proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
+
+ proxyList = selector.select(httpsUri);
+ assertProxyEquals(proxyList,Proxy.NO_PROXY);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectLikeFTP()
+ throws URISyntaxException {
+ // set http proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+ System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
+ // set ftp host empty
+ System.setProperty("ftp.proxyHost", "");
+ // set https proxy
+ System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
+ System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
+ // set socks proxy
+ System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
+ System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
+
+ proxyList = selector.select(ftpUri);
+ assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectNoFTP()
+ throws URISyntaxException {
+ // set http proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+ System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
+ // set https proxy
+ System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
+ System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
+
+ proxyList = selector.select(ftpUri);
+ assertProxyEquals(proxyList,Proxy.NO_PROXY);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_SelectNoSOCKS()
+ throws URISyntaxException {
+ // set http proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+ System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
+ // set https proxy
+ System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
+ System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
+ // set socks proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
+
+ proxyList = selector.select(tcpUri);
+ assertProxyEquals(proxyList,Proxy.NO_PROXY);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_connectionFailedLjava_net_URILjava_net_SocketAddressLjava_io_IOException()
+ throws URISyntaxException {
+ // set http proxy
+ System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
+ System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
+ // set https proxy
+ System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
+ System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
+ // set ftp proxy
+ System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
+ System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
+ // set socks proxy
+ System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
+ System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
+
+ List proxyList1 = selector.select(httpUri);
+ assertNotNull(proxyList1);
+ assertEquals(1, proxyList1.size());
+ Proxy proxy1 = (Proxy) proxyList1.get(0);
+ selector
+ .connectFailed(httpUri, proxy1.address(), new SocketException());
+
+ List proxyList2 = selector.select(httpUri);
+ assertNotNull(proxyList2);
+ assertEquals(1, proxyList2.size());
+ Proxy proxy2 = (Proxy) proxyList2.get(0);
+ // Default implemention doesn't change the proxy list
+ assertEquals(proxy1, proxy2);
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_connectionFailedLjava_net_URILjava_net_SocketAddressLjava_io_IOException_IllegalArguement()
+ throws URISyntaxException {
+ SocketAddress sa = InetSocketAddress.createUnresolved("127.0.0.1", 0);
+ try {
+ selector.connectFailed(null, sa, new SocketException());
+ fail("should throw IllegalArgumentException if any argument is null.");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ selector.connectFailed(httpUri, null, new SocketException());
+ fail("should throw IllegalArgumentException if any argument is null.");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ selector.connectFailed(httpUri, sa, null);
+ fail("should throw IllegalArgumentException if any argument is null.");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ }
+
+ /**
+ * @tests java.net.ProxySelector#select(URI)
+ */
+ public void test_selectLjava_net_URI_IllegalArgument()
+ throws URISyntaxException {
+ URI[] illegalUris = { new URI("abc"), new URI("http"), null };
+ for (int i = 0; i < illegalUris.length; i++) {
+ try {
+ selector.select(illegalUris[i]);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+ }
+
+ /*
+ * asserts whether selectedProxyList contains one and only one element,
+ * and the element equals proxy.
+ */
+ private void assertProxyEquals(List selectedProxyList, Proxy proxy) {
+ assertNotNull(selectedProxyList);
+ assertEquals(1, selectedProxyList.size());
+ assertEquals((Proxy) selectedProxyList.get(0), proxy);
+ }
+
+ /*
+ * asserts whether selectedProxyList contains one and only one element,
+ * and the element equals proxy which is represented by arguments "type",
+ * "host","port".
+ */
+ private void assertProxyEquals(List selectedProxyList, Proxy.Type type,
+ String host, int port) {
+ SocketAddress sa = InetSocketAddress.createUnresolved(host, port);
+ Proxy proxy = new Proxy(type, sa);
+ assertProxyEquals(selectedProxyList, proxy);
+ }
+
+ /*
+ * Mock selector for setDefault test
+ */
+ static class MockProxySelector extends ProxySelector {
+
+ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+
+ }
+
+ public List select(URI uri) {
+ return null;
+ }
+ }
+
+ /*
+ * MockSecurityMaanger. It denies NetPermission("getProxySelector") and
+ * NetPermission("setProxySelector").
+ */
+ class MockSecurityManager extends SecurityManager {
+ public void checkPermission(Permission permission) {
+ if (permission instanceof NetPermission) {
+ if ("getProxySelector".equals(permission.getName())) {
+ throw new SecurityException();
+ }
+ }
+
+ if (permission instanceof NetPermission) {
+ if ("setProxySelector".equals(permission.getName())) {
+ throw new SecurityException();
+ }
+ }
+
+ if (permission instanceof RuntimePermission) {
+ if ("setSecurityManager".equals(permission.getName())) {
+ return;
+ }
+ }
+ }
+ }
+
+ /*
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ // save original system properties
+ orignalSystemProperties = (Properties) System.getProperties().clone();
+ }
+
+ /*
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ // restore orignal system properties
+ System.setProperties(orignalSystemProperties);
+ super.tearDown();
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxyTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxyTest.java?rev=393091&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxyTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxyTest.java Mon Apr 10 15:10:39 2006
@@ -0,0 +1,230 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.net;
+
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+import java.net.SocketAddress;
+
+import junit.framework.TestCase;
+
+public class ProxyTest extends TestCase {
+
+ private SocketAddress address = new InetSocketAddress("127.0.0.1", 1234);
+
+ /**
+ * @tests java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress)
+ */
+ public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal() {
+ // test HTTP type proxy
+ Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
+ assertEquals(Proxy.Type.HTTP, proxy.type());
+ assertEquals(address, proxy.address());
+
+ // test SOCKS type proxy
+ proxy = new Proxy(Proxy.Type.SOCKS, address);
+ assertEquals(Proxy.Type.SOCKS, proxy.type());
+ assertEquals(address, proxy.address());
+
+ // test DIRECT type proxy
+ proxy = Proxy.NO_PROXY;
+ assertEquals(Proxy.Type.DIRECT, proxy.type());
+ assertNull(proxy.address());
+ }
+
+ /**
+ * @tests java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress)
+ */
+ public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_IllegalAddress() {
+ Proxy proxy = null;
+ // test HTTP type proxy
+ try {
+ proxy = new Proxy(Proxy.Type.HTTP, null);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ // test SOCKS type proxy
+ try {
+ proxy = new Proxy(Proxy.Type.SOCKS, null);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ // test DIRECT type proxy
+ try {
+ proxy = new Proxy(Proxy.Type.DIRECT, null);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ // test DIRECT type proxy, any address is illegal
+ try {
+ proxy = new Proxy(Proxy.Type.DIRECT, address);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ }
+
+ /**
+ * @tests java.net.Proxy#hashCode()
+ * @see also see test_equalsLjava_lang_Object_Equals
+ */
+ public void test_hashCode() {
+ // This method has been tested in test_equalsLjava_lang_Object_Equals.
+ }
+
+ /**
+ * @tests java.net.Proxy#type()
+ */
+ public void test_type() {
+ // This method has been tested in test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal.
+ }
+
+ /**
+ * @tests java.net.Proxy#address() This method has been tested in
+ * Constructor test case.
+ */
+ public void test_address() {
+ // This method has been tested in test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal.
+ }
+
+ /**
+ * @tests java.net.Proxy#toString()
+ */
+ public void test_toString() {
+ Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
+ // include type String
+ assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
+ // include address String
+ assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1);
+
+ proxy = new Proxy(Proxy.Type.SOCKS, address);
+ // include type String
+ assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
+ // include address String
+ assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1);
+
+ proxy = Proxy.NO_PROXY;
+ // include type String
+ assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
+
+ proxy = new Proxy(null, address);
+ // ensure no NPE is thrown
+ proxy.toString();
+
+ }
+
+ /**
+ * @tests java.net.Proxy#equals(Object)
+ */
+ public void test_equalsLjava_lang_Object_Equals() {
+ SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234);
+ SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1234);
+ // HTTP type
+ Proxy proxy1 = new Proxy(Proxy.Type.HTTP, address1);
+ Proxy proxy2 = new Proxy(Proxy.Type.HTTP, address2);
+ assertTrue(proxy1.equals(proxy2));
+ // assert hashCode
+ assertTrue(proxy1.hashCode() == proxy2.hashCode());
+
+ // SOCKS type
+ Proxy proxy3 = new Proxy(Proxy.Type.SOCKS, address1);
+ Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, address2);
+ assertTrue(proxy3.equals(proxy4));
+ // assert hashCode
+ assertTrue(proxy3.hashCode() == proxy4.hashCode());
+
+ // null type
+ Proxy proxy5 = new Proxy(null, address1);
+ Proxy proxy6 = new Proxy(null, address2);
+ assertTrue(proxy5.equals(proxy6));
+ }
+
+ /**
+ * @tests java.net.Proxy#equals(Object)
+ */
+ public void test_equalsLjava_lang_Object_NotEquals() {
+ SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234);
+ SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1235);
+ Proxy proxy[] = { new Proxy(Proxy.Type.HTTP, address1),
+ new Proxy(Proxy.Type.HTTP, address2),
+ new Proxy(Proxy.Type.SOCKS, address1),
+ new Proxy(Proxy.Type.SOCKS, address2), Proxy.NO_PROXY,
+ new Proxy(null, address1), new Proxy(null, address2) };
+ // All of them are not equals
+ for (int i = 0; i < proxy.length; i++) {
+ for (int j = i + 1; j < proxy.length; j++) {
+ assertFalse(proxy[i].equals(proxy[j]));
+ }
+ }
+ // Not equals to an Object type instance. Ensure no exception is thrown.
+ assertFalse(proxy[0].equals(new Object()));
+ }
+
+ /**
+ * @tests java.net.Proxy.Type#valueOf(String)
+ */
+ public void test_Type_valueOfLjava_lang_String_Normal() {
+ assertEquals(Proxy.Type.DIRECT, Proxy.Type.valueOf("DIRECT"));
+ assertEquals(Proxy.Type.HTTP, Proxy.Type.valueOf("HTTP"));
+ assertEquals(Proxy.Type.SOCKS, Proxy.Type.valueOf("SOCKS"));
+ }
+
+ /**
+ * @tests java.net.Proxy.Type#valueOf(String)
+ */
+ public void test_Type_valueOfLjava_lang_String_IllegalName() {
+ String[] illegalName = { "Direct", "direct", "http", "socks",
+ "illegalName", "" };
+ for (int i = 0; i < illegalName.length; i++) {
+ try {
+ Proxy.Type.valueOf(illegalName[i]);
+ fail("should throw IllegalArgumentException, illegalName:"
+ + illegalName);
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+ }
+
+ /**
+ * @tests java.net.Proxy.Type#valueOf(String)
+ */
+ public void test_Type_valueOfLjava_lang_String_NullPointerException() {
+ // Some old RIs,which throw IllegalArgumentException, will fail
+ // this test case. Latest RIs throw NullPointerException.
+ try {
+ Proxy.Type.valueOf(null);
+ fail("should throw NullPointerException.");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.net.Proxy.Type#values()
+ */
+ public void test_Type_values() {
+ Proxy.Type types[] = Proxy.Type.values();
+ assertEquals(3, types.length);
+ assertEquals(Proxy.Type.DIRECT, types[0]);
+ assertEquals(Proxy.Type.HTTP, types[1]);
+ assertEquals(Proxy.Type.SOCKS, types[2]);
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProxyTest.java
------------------------------------------------------------------------------
svn:eol-style = native