use of java.net.DatagramSocketImpl in project robovm by robovm.
the class OldDatagramSocketTest method test_sendLjava_net_DatagramPacket.
public void test_sendLjava_net_DatagramPacket() throws Exception {
// Test for method void
// java.net.DatagramSocket.send(java.net.DatagramPacket)
int[] ports = Support_PortManager.getNextPortsForUDP(2);
final int portNumber = ports[0];
class TestDGSend implements Runnable {
Thread pThread;
public TestDGSend(Thread t) {
pThread = t;
}
public void run() {
try {
byte[] rbuf = new byte[1000];
sds = new DatagramSocket(portNumber);
DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length);
sds.setSoTimeout(6000);
sds.receive(sdp);
retval = new String(rbuf, 0, testString.length());
pThread.interrupt();
} catch (java.io.InterruptedIOException e) {
System.out.println("Recv operation timed out");
pThread.interrupt();
ds.close();
} catch (Exception e) {
System.out.println("Failed to establish Dgram server: " + e);
}
}
}
try {
new Thread(new TestDGSend(Thread.currentThread()), "DGServer").start();
ds = new java.net.DatagramSocket(ports[1]);
dp = new DatagramPacket(testString.getBytes(), testString.length(), InetAddress.getLocalHost(), portNumber);
// Wait to allow send to occur
try {
Thread.sleep(500);
ds.send(dp);
Thread.sleep(5000);
} catch (InterruptedException e) {
ds.close();
assertTrue("Incorrect data sent: " + retval, retval.equals(testString));
}
} catch (Exception e) {
fail("Exception during send test : " + e.getMessage());
} finally {
ds.close();
}
/*
SecurityManager sm = new SecurityManager() {
public void checkPermission(Permission perm) {
}
public void checkMulticast(InetAddress maddr) {
throw new SecurityException();
}
public void checkConnect(String host,
int port) {
throw new SecurityException();
}
};
try {
ds = new java.net.DatagramSocket(ports[1]);
dp = new DatagramPacket(testString.getBytes(), testString.length(),
InetAddress.getLocalHost(), portNumber);
SecurityManager oldSm = System.getSecurityManager();
System.setSecurityManager(sm);
try {
ds.send(dp);
fail("SecurityException should be thrown.");
} catch (SecurityException e) {
// expected
} catch (SocketException e) {
fail("SocketException was thrown.");
} finally {
System.setSecurityManager(oldSm);
}
} catch(Exception e) {
fail("Unexpected exception was thrown: " + e.getMessage());
}
*/
DatagramSocket socket = null;
try {
byte[] rbuf = new byte[1000];
DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
SocketAddress address = new InetSocketAddress(portNumber);
DatagramChannel channel = DatagramChannel.open();
channel.configureBlocking(false);
socket = channel.socket();
socket.send(rdp);
fail("IllegalBlockingModeException was not thrown.");
} catch (IllegalBlockingModeException ibme) {
//expected
} catch (IOException ioe) {
fail("IOException was thrown: " + ioe.getMessage());
} finally {
socket.close();
}
//Regression for HARMONY-1118
class testDatagramSocket extends DatagramSocket {
public testDatagramSocket(DatagramSocketImpl impl) {
super(impl);
}
}
class testDatagramSocketImpl extends DatagramSocketImpl {
protected void create() throws SocketException {
}
protected void bind(int arg0, InetAddress arg1) throws SocketException {
}
protected void send(DatagramPacket arg0) throws IOException {
}
protected int peek(InetAddress arg0) throws IOException {
return 0;
}
protected int peekData(DatagramPacket arg0) throws IOException {
return 0;
}
protected void receive(DatagramPacket arg0) throws IOException {
}
protected void setTTL(byte arg0) throws IOException {
}
protected byte getTTL() throws IOException {
return 0;
}
protected void setTimeToLive(int arg0) throws IOException {
}
protected int getTimeToLive() throws IOException {
return 0;
}
protected void join(InetAddress arg0) throws IOException {
}
protected void leave(InetAddress arg0) throws IOException {
}
protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
}
protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
}
protected void close() {
}
public void setOption(int arg0, Object arg1) throws SocketException {
}
public Object getOption(int arg0) throws SocketException {
return null;
}
}
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), 0);
//no exception expected for next line
new testDatagramSocket(new testDatagramSocketImpl()).send(new DatagramPacket(new byte[272], 3, sa));
// Regression test for Harmony-2938
InetAddress i = InetAddress.getByName("127.0.0.1");
DatagramSocket d = new DatagramSocket(0, i);
try {
d.send(new DatagramPacket(new byte[] { 1 }, 1));
fail("should throw NPE.");
} catch (NullPointerException e) {
// expected;
} finally {
d.close();
}
}
use of java.net.DatagramSocketImpl in project robovm by robovm.
the class OldDatagramSocketImplFactoryTest method test_createDatagramSocketImpl.
public void test_createDatagramSocketImpl() throws IllegalArgumentException, IOException {
if (isTestable) {
DatagramSocketImplFactory factory = new TestDatagramSocketImplFactory();
assertFalse(isCreateDatagramSocketImpl);
DatagramSocket.setDatagramSocketImplFactory(factory);
try {
new java.net.DatagramSocket();
assertTrue(isCreateDatagramSocketImpl);
assertTrue(isDatagramSocketImplCalled);
} catch (Exception e) {
fail("Exception during test : " + e.getMessage());
}
try {
DatagramSocket.setDatagramSocketImplFactory(factory);
fail("SocketException was not thrown.");
} catch (SocketException se) {
//expected
}
try {
DatagramSocket.setDatagramSocketImplFactory(null);
fail("SocketException was not thrown.");
} catch (SocketException se) {
//expected
}
} else {
TestDatagramSocketImplFactory dsf = new TestDatagramSocketImplFactory();
DatagramSocketImpl dsi = dsf.createDatagramSocketImpl();
try {
assertNull(dsi.getOption(0));
} catch (SocketException e) {
fail("SocketException was thrown.");
}
}
}
Aggregations