1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 * @author Boris V. Kuznetsov 20 */ 21 22 package javax.net; 23 24 import java.io.IOException; 25 import java.net.InetAddress; 26 import java.net.Socket; 27 import java.net.SocketException; 28 import java.net.UnknownHostException; 29 30 import junit.framework.TestCase; 31 32 33 /** 34 * Tests for <code>SocketFactory</code> class methods. 35 */ 36 public class SocketFactoryTest extends TestCase { 37 38 /* 39 * Class under test for java.net.Socket createSocket() 40 */ testCreateSocket()41 public final void testCreateSocket() { 42 SocketFactory sf = new MySocketFactory(); 43 try { 44 sf.createSocket(); 45 fail("No expected SocketException"); 46 } catch (SocketException e) { 47 } catch (IOException e) { 48 fail(e.toString()); 49 } 50 } 51 52 /* 53 * Class under test for javax.net.SocketFactory getDefault() 54 */ testGetDefault()55 public final void testGetDefault() { 56 SocketFactory sf = SocketFactory.getDefault(); 57 Socket s; 58 if (!(sf instanceof DefaultSocketFactory)) { 59 fail("Incorrect class instance"); 60 } 61 try { 62 s = sf.createSocket("localhost", 8082); 63 s.close(); 64 } catch (IOException e) { 65 } 66 try { 67 s = sf.createSocket("localhost", 8081, InetAddress.getLocalHost(), 8082); 68 s.close(); 69 } catch (IOException e) { 70 } 71 try { 72 s = sf.createSocket(InetAddress.getLocalHost(), 8081); 73 s.close(); 74 } catch (IOException e) { 75 } 76 try { 77 s = sf.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082); 78 s.close(); 79 } catch (IOException e) { 80 } 81 } 82 } 83 84 class MySocketFactory extends SocketFactory { 85 @Override createSocket(String host, int port)86 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 87 throw new IOException(); 88 } 89 90 @Override createSocket(String host, int port, InetAddress localHost, int localPort)91 public Socket createSocket(String host, int port, InetAddress localHost, int localPort) 92 throws IOException, UnknownHostException { 93 throw new IOException(); 94 } 95 96 @Override createSocket(InetAddress host, int port)97 public Socket createSocket(InetAddress host, int port) throws IOException { 98 throw new IOException(); 99 } 100 101 @Override createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)102 public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) 103 throws IOException { 104 throw new IOException(); 105 } 106 107 } 108