OldSocketImplFactoryTest.java revision 1f8243e3d2b5a3f8e0398c304d1dea0395cbc368
1package libcore.java.net; 2 3import java.io.IOException; 4import java.io.InputStream; 5import java.io.OutputStream; 6import java.lang.reflect.Field; 7import java.net.InetAddress; 8import java.net.Socket; 9import java.net.SocketAddress; 10import java.net.SocketException; 11import java.net.SocketImpl; 12import java.net.SocketImplFactory; 13import junit.framework.TestCase; 14 15 16public class OldSocketImplFactoryTest extends TestCase { 17 18 SocketImplFactory oldFactory = null; 19 Field factoryField = null; 20 21 boolean isTestable = false; 22 23 boolean iSocketImplCalled = false; 24 boolean isCreateSocketImpl = false; 25 26 public void test_createSocketImpl() throws IOException { 27 MockSocketImplFactory factory = new MockSocketImplFactory(); 28 if(isTestable) { 29 assertFalse(isCreateSocketImpl); 30 Socket.setSocketImplFactory(factory); 31 32 try { 33 new Socket(); 34 assertTrue(isCreateSocketImpl); 35 assertTrue(iSocketImplCalled); 36 } catch (Exception e) { 37 fail("Exception during test : " + e.getMessage()); 38 } 39 40 try { 41 Socket.setSocketImplFactory(factory); 42 fail("SocketException was not thrown."); 43 } catch(SocketException se) { 44 //expected 45 } 46 47 try { 48 Socket.setSocketImplFactory(null); 49 fail("SocketException was not thrown."); 50 } catch(SocketException se) { 51 //expected 52 } 53 54 } else { 55 SocketImpl si = factory.createSocketImpl(); 56 try { 57 assertNull(si.getOption(0)); 58 } catch (SocketException e) { 59 fail("SocketException was thrown."); 60 } 61 } 62 } 63 64 public void setUp() { 65 Field [] fields = Socket.class.getDeclaredFields(); 66 int counter = 0; 67 for (Field field : fields) { 68 if (SocketImplFactory.class.equals(field.getType())) { 69 counter++; 70 factoryField = field; 71 } 72 } 73 74 if(counter == 1) { 75 76 isTestable = true; 77 78 factoryField.setAccessible(true); 79 try { 80 oldFactory = (SocketImplFactory) factoryField.get(null); 81 } catch (IllegalArgumentException e) { 82 fail("IllegalArgumentException was thrown during setUp: " 83 + e.getMessage()); 84 } catch (IllegalAccessException e) { 85 fail("IllegalAccessException was thrown during setUp: " 86 + e.getMessage()); 87 } 88 } 89 } 90 91 public void tearDown() { 92 if(isTestable) { 93 try { 94 factoryField.set(null, oldFactory); 95 } catch (IllegalArgumentException e) { 96 fail("IllegalArgumentException was thrown during tearDown: " 97 + e.getMessage()); 98 } catch (IllegalAccessException e) { 99 fail("IllegalAccessException was thrown during tearDown: " 100 + e.getMessage()); 101 } 102 } 103 } 104 105 class MockSocketImplFactory implements SocketImplFactory { 106 107 public SocketImpl createSocketImpl() { 108 isCreateSocketImpl = true; 109 return new MockSocketImpl(); 110 } 111 } 112 113 class MockSocketImpl extends SocketImpl { 114 115 public MockSocketImpl() { 116 super(); 117 iSocketImplCalled = true; 118 } 119 @Override 120 protected void accept(SocketImpl arg0) throws IOException { 121 } 122 123 @Override 124 protected int available() throws IOException { 125 return 0; 126 } 127 128 @Override 129 protected void bind(InetAddress arg0, int arg1) throws IOException { 130 } 131 132 @Override 133 protected void close() throws IOException { 134 } 135 136 @Override 137 protected void connect(String arg0, int arg1) throws IOException { 138 } 139 140 @Override 141 protected void connect(InetAddress arg0, int arg1) throws IOException { 142 } 143 144 @Override 145 protected void connect(SocketAddress arg0, int arg1) throws IOException { 146 } 147 148 @Override 149 protected void create(boolean arg0) throws IOException { 150 } 151 152 @Override 153 protected InputStream getInputStream() throws IOException { 154 return null; 155 } 156 157 @Override 158 protected OutputStream getOutputStream() throws IOException { 159 return null; 160 } 161 162 @Override 163 protected void listen(int arg0) throws IOException { 164 } 165 166 @Override 167 protected void sendUrgentData(int arg0) throws IOException { 168 } 169 170 public Object getOption(int arg0) throws SocketException { 171 return null; 172 } 173 174 public void setOption(int arg0, Object arg1) throws SocketException { 175 } 176 } 177} 178