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