Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.java.net;
     18 
     19 import java.io.IOException;
     20 import java.lang.reflect.Field;
     21 import java.net.DatagramPacket;
     22 import java.net.DatagramSocket;
     23 import java.net.DatagramSocketImpl;
     24 import java.net.DatagramSocketImplFactory;
     25 import java.net.InetAddress;
     26 import java.net.NetworkInterface;
     27 import java.net.SocketAddress;
     28 import java.net.SocketException;
     29 import junit.framework.TestCase;
     30 
     31 public class OldDatagramSocketImplFactoryTest extends TestCase {
     32 
     33     DatagramSocketImplFactory oldFactory = null;
     34     Field factoryField = null;
     35 
     36     boolean isTestable = false;
     37 
     38     boolean isDatagramSocketImplCalled = false;
     39     boolean isCreateDatagramSocketImpl = false;
     40 
     41     public void test_createDatagramSocketImpl() throws IllegalArgumentException, IOException {
     42         if (isTestable) {
     43 
     44             DatagramSocketImplFactory factory = new TestDatagramSocketImplFactory();
     45             assertFalse(isCreateDatagramSocketImpl);
     46             DatagramSocket.setDatagramSocketImplFactory(factory);
     47 
     48             try {
     49                 new java.net.DatagramSocket();
     50                 assertTrue(isCreateDatagramSocketImpl);
     51                 assertTrue(isDatagramSocketImplCalled);
     52             } catch (Exception e) {
     53                 fail("Exception during test : " + e.getMessage());
     54 
     55             }
     56 
     57             try {
     58                 DatagramSocket.setDatagramSocketImplFactory(factory);
     59                 fail("SocketException was not thrown.");
     60             } catch(SocketException se) {
     61                 //expected
     62             }
     63 
     64             try {
     65                 DatagramSocket.setDatagramSocketImplFactory(null);
     66                 fail("SocketException was not thrown.");
     67             } catch(SocketException se) {
     68                 //expected
     69             }
     70 
     71         } else {
     72 
     73             TestDatagramSocketImplFactory dsf = new TestDatagramSocketImplFactory();
     74             DatagramSocketImpl dsi = dsf.createDatagramSocketImpl();
     75             try {
     76                 assertNull(dsi.getOption(0));
     77             } catch (SocketException e) {
     78                 fail("SocketException was thrown.");
     79             }
     80         }
     81     }
     82 
     83     public void setUp() {
     84         Field [] fields = DatagramSocket.class.getDeclaredFields();
     85         int counter = 0;
     86         for (Field field : fields) {
     87             if (DatagramSocketImplFactory.class.equals(field.getType())) {
     88                 counter++;
     89                 factoryField = field;
     90             }
     91         }
     92 
     93         if(counter == 1) {
     94 
     95             isTestable = true;
     96 
     97             factoryField.setAccessible(true);
     98             try {
     99                 oldFactory = (DatagramSocketImplFactory) factoryField.get(null);
    100             } catch (IllegalArgumentException e) {
    101                 fail("IllegalArgumentException was thrown during setUp: "
    102                         + e.getMessage());
    103             } catch (IllegalAccessException e) {
    104                 fail("IllegalAccessException was thrown during setUp: "
    105                         + e.getMessage());
    106             }
    107         }
    108     }
    109 
    110     public void tearDown() {
    111         if(isTestable) {
    112             try {
    113                 factoryField.set(null, oldFactory);
    114             } catch (IllegalArgumentException e) {
    115                 fail("IllegalArgumentException was thrown during tearDown: "
    116                         + e.getMessage());
    117             } catch (IllegalAccessException e) {
    118                 fail("IllegalAccessException was thrown during tearDown: "
    119                         + e.getMessage());
    120             }
    121         }
    122     }
    123 
    124     class TestDatagramSocketImplFactory implements DatagramSocketImplFactory {
    125         public DatagramSocketImpl createDatagramSocketImpl() {
    126             isCreateDatagramSocketImpl = true;
    127             return new TestDatagramSocketImpl();
    128         }
    129     }
    130 
    131     class TestDatagramSocketImpl extends DatagramSocketImpl {
    132 
    133         @Override
    134         protected void bind(int arg0, InetAddress arg1) throws SocketException {
    135         }
    136 
    137         @Override
    138         protected void close() {
    139         }
    140 
    141         @Override
    142         protected void create() throws SocketException {
    143             isDatagramSocketImplCalled = true;
    144         }
    145 
    146         @Override
    147         protected byte getTTL() throws IOException {
    148             return 0;
    149         }
    150 
    151         @Override
    152         protected int getTimeToLive() throws IOException {
    153             return 0;
    154         }
    155 
    156         @Override
    157         protected void join(InetAddress arg0) throws IOException {
    158         }
    159 
    160         @Override
    161         protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
    162         }
    163 
    164         @Override
    165         protected void leave(InetAddress arg0) throws IOException {
    166         }
    167 
    168         @Override
    169         protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
    170         }
    171 
    172         @Override
    173         public int peek(InetAddress arg0) throws IOException {
    174             return 10;
    175         }
    176 
    177         @Override
    178         protected int peekData(DatagramPacket arg0) throws IOException {
    179             return 0;
    180         }
    181 
    182         @Override
    183         protected void receive(DatagramPacket arg0) throws IOException {
    184         }
    185 
    186         @Override
    187         protected void send(DatagramPacket arg0) throws IOException {
    188         }
    189 
    190         @Override
    191         protected void setTTL(byte arg0) throws IOException {
    192         }
    193 
    194         @Override
    195         protected void setTimeToLive(int arg0) throws IOException {
    196         }
    197 
    198         public Object getOption(int arg0) throws SocketException {
    199             return null;
    200         }
    201 
    202         public void setOption(int arg0, Object arg1) throws SocketException {
    203         }
    204     }
    205 }
    206