summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/tests/api/javax/net/SocketFactoryTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'luni/src/test/java/tests/api/javax/net/SocketFactoryTest.java')
-rw-r--r--luni/src/test/java/tests/api/javax/net/SocketFactoryTest.java230
1 files changed, 68 insertions, 162 deletions
diff --git a/luni/src/test/java/tests/api/javax/net/SocketFactoryTest.java b/luni/src/test/java/tests/api/javax/net/SocketFactoryTest.java
index 2250602..e939a9b 100644
--- a/luni/src/test/java/tests/api/javax/net/SocketFactoryTest.java
+++ b/luni/src/test/java/tests/api/javax/net/SocketFactoryTest.java
@@ -33,200 +33,135 @@ import javax.net.SocketFactory;
import junit.framework.TestCase;
-import tests.support.Support_PortManager;
-
-
-/**
- * Tests for <code>SocketFactory</code> class methods.
- */
public class SocketFactoryTest extends TestCase {
- /**
- * javax.net.SocketFactory#SocketFactory()
- */
- public void test_Constructor() {
- try {
- MySocketFactory sf = new MySocketFactory();
- } catch (Exception e) {
- fail("Unexpected exception " + e.toString());
- }
+ public void test_Constructor() throws Exception {
+ new MySocketFactory();
}
- /**
- * javax.net.SocketFactory#createSocket()
- */
- public final void test_createSocket_01() {
+ public final void test_createSocket() throws Exception {
SocketFactory sf = SocketFactory.getDefault();
- try {
- Socket s = sf.createSocket();
- assertNotNull(s);
- assertEquals(-1, s.getLocalPort());
- assertEquals(0, s.getPort());
- } catch (Exception e) {
- fail("Unexpected exception: " + e);
- }
+ Socket s = sf.createSocket();
+ assertNotNull(s);
+ assertEquals(-1, s.getLocalPort());
+ assertEquals(0, s.getPort());
MySocketFactory msf = new MySocketFactory();
try {
msf.createSocket();
fail("No expected SocketException");
- } catch (SocketException e) {
- } catch (IOException e) {
- fail(e.toString());
+ } catch (SocketException expected) {
}
}
- /**
- * javax.net.SocketFactory#createSocket(String host, int port)
- */
- public final void test_createSocket_02() {
+ public final void test_createSocket_StringI() throws Exception {
SocketFactory sf = SocketFactory.getDefault();
- int portNumber = Support_PortManager.getNextPort();
- int sport = startServer("Cons String,I");
+ int sport = new ServerSocket(0).getLocalPort();
int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
- try {
- Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport);
- assertNotNull(s);
- assertTrue("Failed to create socket", s.getPort() == sport);
- } catch (Exception e) {
- fail("Unexpected exception: " + e);
- }
+ Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport);
+ assertNotNull(s);
+ assertTrue("Failed to create socket", s.getPort() == sport);
try {
- Socket s = sf.createSocket("bla-bla", sport);
+ sf.createSocket("bla-bla", sport);
fail("UnknownHostException wasn't thrown");
- } catch (UnknownHostException uhe) {
- //expected
- } catch (Exception e) {
- fail(e + " was thrown instead of UnknownHostException");
+ } catch (UnknownHostException expected) {
}
for (int i = 0; i < invalidPorts.length; i++) {
try {
- Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i]);
+ sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i]);
fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
- } catch (IllegalArgumentException iae) {
- //expected
- } catch (Exception e) {
- fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
+ } catch (IllegalArgumentException expected) {
}
}
try {
- Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), portNumber);
+ sf.createSocket(InetAddress.getLocalHost().getHostName(), s.getLocalPort());
fail("IOException wasn't thrown");
- } catch (IOException ioe) {
- //expected
+ } catch (IOException expected) {
}
SocketFactory f = SocketFactory.getDefault();
try {
- Socket s = f.createSocket("localhost", 8082);
+ f.createSocket(InetAddress.getLocalHost().getHostName(), 8082);
fail("IOException wasn't thrown ...");
- } catch (IOException e) {
+ } catch (IOException expected) {
}
}
- /**
- * javax.net.SocketFactory#createSocket(InetAddress host, int port)
- */
- public final void test_createSocket_03() {
+ public final void test_createSocket_InetAddressI() throws Exception {
SocketFactory sf = SocketFactory.getDefault();
- int portNumber = Support_PortManager.getNextPort();
- int sport = startServer("Cons InetAddress,I");
+ int sport = new ServerSocket(0).getLocalPort();
int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
- try {
- Socket s = sf.createSocket(InetAddress.getLocalHost(), sport);
- assertNotNull(s);
- assertTrue("Failed to create socket", s.getPort() == sport);
- } catch (Exception e) {
- fail("Unexpected exception: " + e);
- }
+ Socket s = sf.createSocket(InetAddress.getLocalHost(), sport);
+ assertNotNull(s);
+ assertTrue("Failed to create socket", s.getPort() == sport);
for (int i = 0; i < invalidPorts.length; i++) {
try {
- Socket s = sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i]);
+ sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i]);
fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
- } catch (IllegalArgumentException iae) {
- //expected
- } catch (Exception e) {
- fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
+ } catch (IllegalArgumentException expected) {
}
}
try {
- Socket s = sf.createSocket(InetAddress.getLocalHost(), portNumber);
+ sf.createSocket(InetAddress.getLocalHost(), s.getLocalPort());
fail("IOException wasn't thrown");
- } catch (IOException ioe) {
- //expected
+ } catch (IOException expected) {
}
SocketFactory f = SocketFactory.getDefault();
try {
- Socket s = f.createSocket(InetAddress.getLocalHost(), 8081);
+ f.createSocket(InetAddress.getLocalHost(), 8081);
fail("IOException wasn't thrown ...");
- } catch (IOException e) {
+ } catch (IOException expected) {
}
}
- /**
- * javax.net.SocketFactory#createSocket(InetAddress address, int port,
- * InetAddress localAddress, int localPort)
- */
- public final void test_createSocket_04() {
+ public final void test_createSocket_InetAddressIInetAddressI() throws Exception {
SocketFactory sf = SocketFactory.getDefault();
- int portNumber = Support_PortManager.getNextPort();
- int sport = startServer("Cons InetAddress,I,InetAddress,I");
+ int sport = new ServerSocket(0).getLocalPort();
int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
- try {
- Socket s = sf.createSocket(InetAddress.getLocalHost(), sport,
- InetAddress.getLocalHost(), portNumber);
- assertNotNull(s);
- assertTrue("1: Failed to create socket", s.getPort() == sport);
- assertTrue("2: Failed to create socket", s.getLocalPort() == portNumber);
- } catch (Exception e) {
- fail("Unexpected exception: " + e);
- }
+ Socket s = sf.createSocket(InetAddress.getLocalHost(), sport,
+ InetAddress.getLocalHost(), 0);
+ assertNotNull(s);
+ assertTrue("1: Failed to create socket", s.getPort() == sport);
+ int portNumber = s.getLocalPort();
for (int i = 0; i < invalidPorts.length; i++) {
try {
- Socket s = sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i],
- InetAddress.getLocalHost(), portNumber);
+ sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i],
+ InetAddress.getLocalHost(), portNumber);
fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
- } catch (IllegalArgumentException iae) {
- //expected
- } catch (Exception e) {
- fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
+ } catch (IllegalArgumentException expected) {
}
try {
- Socket s = sf.createSocket(InetAddress.getLocalHost(), sport,
- InetAddress.getLocalHost(), invalidPorts[i]);
+ sf.createSocket(InetAddress.getLocalHost(), sport,
+ InetAddress.getLocalHost(), invalidPorts[i]);
fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
- } catch (IllegalArgumentException iae) {
- //expected
- } catch (Exception e) {
- fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
+ } catch (IllegalArgumentException expected) {
}
}
try {
- Socket s = sf.createSocket(InetAddress.getLocalHost(), sport,
- InetAddress.getLocalHost(), portNumber);
+ sf.createSocket(InetAddress.getLocalHost(), sport,
+ InetAddress.getLocalHost(), portNumber);
fail("IOException wasn't thrown");
- } catch (IOException ioe) {
- //expected
+ } catch (IOException expected) {
}
SocketFactory f = SocketFactory.getDefault();
try {
- Socket s = f.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082);
+ f.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082);
fail("IOException wasn't thrown ...");
- } catch (IOException e) {
+ } catch (IOException expected) {
}
}
@@ -234,59 +169,41 @@ public class SocketFactoryTest extends TestCase {
* javax.net.SocketFactory#createSocket(String host, int port,
* InetAddress localHost, int localPort)
*/
- public final void test_createSocket_05() {
+ public final void test_createSocket_05() throws Exception {
SocketFactory sf = SocketFactory.getDefault();
- int portNumber = Support_PortManager.getNextPort();
- int sport = startServer("Cons String,I,InetAddress,I");
+ int sport = new ServerSocket(0).getLocalPort();
int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
- try {
- Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
- InetAddress.getLocalHost(), portNumber);
- assertNotNull(s);
- assertTrue("1: Failed to create socket", s.getPort() == sport);
- assertTrue("2: Failed to create socket", s.getLocalPort() == portNumber);
- } catch (Exception e) {
- fail("Unexpected exception: " + e);
- }
+ Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
+ InetAddress.getLocalHost(), 0);
+ assertNotNull(s);
+ assertTrue("1: Failed to create socket", s.getPort() == sport);
- portNumber = Support_PortManager.getNextPort();
try {
- Socket s = sf.createSocket("bla-bla", sport, InetAddress.getLocalHost(), portNumber);
+ sf.createSocket("bla-bla", sport, InetAddress.getLocalHost(), 0);
fail("UnknownHostException wasn't thrown");
- } catch (UnknownHostException uhe) {
- //expected
- } catch (Exception e) {
- fail(e + " was thrown instead of UnknownHostException");
+ } catch (UnknownHostException expected) {
}
for (int i = 0; i < invalidPorts.length; i++) {
- portNumber = Support_PortManager.getNextPort();
try {
- Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i],
- InetAddress.getLocalHost(), portNumber);
+ sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i],
+ InetAddress.getLocalHost(), 0);
fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
- } catch (IllegalArgumentException iae) {
- //expected
- } catch (Exception e) {
- fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
+ } catch (IllegalArgumentException expected) {
}
try {
- Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
- InetAddress.getLocalHost(), invalidPorts[i]);
+ sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
+ InetAddress.getLocalHost(), invalidPorts[i]);
fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
- } catch (IllegalArgumentException iae) {
- //expected
- } catch (Exception e) {
- fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
+ } catch (IllegalArgumentException expected) {
}
}
- SocketFactory f = SocketFactory.getDefault();
try {
- Socket s = f.createSocket("localhost", 8081, InetAddress.getLocalHost(), 8082);
+ sf.createSocket(InetAddress.getLocalHost().getHostName(), 8081, InetAddress.getLocalHost(), 8082);
fail("IOException wasn't thrown ...");
- } catch (IOException e) {
+ } catch (IOException expected) {
}
}
@@ -297,12 +214,12 @@ public class SocketFactoryTest extends TestCase {
SocketFactory sf = SocketFactory.getDefault();
Socket s;
try {
- s = sf.createSocket("localhost", 8082);
+ s = sf.createSocket(InetAddress.getLocalHost().getHostName(), 8082);
s.close();
} catch (IOException e) {
}
try {
- s = sf.createSocket("localhost", 8081, InetAddress.getLocalHost(), 8082);
+ s = sf.createSocket(InetAddress.getLocalHost().getHostName(), 8081, InetAddress.getLocalHost(), 8082);
s.close();
} catch (IOException e) {
}
@@ -317,17 +234,6 @@ public class SocketFactoryTest extends TestCase {
} catch (IOException e) {
}
}
-
- protected int startServer(String name) {
- int portNumber = Support_PortManager.getNextPort();
- ServerSocket ss = null;
- try {
- ss = new ServerSocket(portNumber);
- } catch (IOException e) {
- fail(name + ": " + e);
- }
- return ss.getLocalPort();
- }
}
class MySocketFactory extends SocketFactory {