1/* 2 * Copyright 2008 the original author or authors. 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 */ 16package org.mockftpserver.core.util; 17 18import org.apache.log4j.Logger; 19import org.mockftpserver.core.CommandSyntaxException; 20import org.mockftpserver.test.AbstractTest; 21 22import java.net.InetAddress; 23import java.net.UnknownHostException; 24 25/** 26 * Tests for the PortParser class 27 * 28 * @author Chris Mair 29 * @version $Revision$ - $Date$ 30 */ 31public final class PortParserTest extends AbstractTest { 32 33 private static final Logger LOG = Logger.getLogger(PortParserTest.class); 34 private static final String[] PARAMETERS = new String[]{"192", "22", "250", "44", "1", "206"}; 35 private static final String[] PARAMETERS_INSUFFICIENT = new String[]{"7", "29", "99", "11", "77"}; 36 private static final int PORT = (1 << 8) + 206; 37 private static final InetAddress HOST = inetAddress("192.22.250.44"); 38 39 /** 40 * Test the parseHost() method 41 * 42 * @throws java.net.UnknownHostException 43 */ 44 public void testParseHost() throws UnknownHostException { 45 InetAddress host = PortParser.parseHost(PARAMETERS); 46 assertEquals("InetAddress", HOST, host); 47 } 48 49 /** 50 * Test the parsePortNumber() method 51 */ 52 public void testParsePortNumber() { 53 int portNumber = PortParser.parsePortNumber(PARAMETERS); 54 assertEquals("portNumber", PORT, portNumber); 55 } 56 57 /** 58 * Test the parseHost() method, passing in null 59 * 60 * @throws java.net.UnknownHostException 61 */ 62 public void testParseHost_Null() throws UnknownHostException { 63 try { 64 PortParser.parseHost(null); 65 fail("Expected CommandSyntaxException"); 66 } 67 catch (CommandSyntaxException expected) { 68 LOG.info("Expected: " + expected); 69 } 70 } 71 72 /** 73 * Test the parseHost() method, passing in a String[] with not enough parameters 74 * 75 * @throws java.net.UnknownHostException 76 */ 77 public void testParseHost_InsufficientParameters() throws UnknownHostException { 78 try { 79 PortParser.parseHost(PARAMETERS_INSUFFICIENT); 80 fail("Expected CommandSyntaxException"); 81 } 82 catch (CommandSyntaxException expected) { 83 LOG.info("Expected: " + expected); 84 } 85 } 86 87 /** 88 * Test the parsePortNumber() method, passing in null 89 * 90 * @throws java.net.UnknownHostException 91 */ 92 public void testParsePortNumber_Null() throws UnknownHostException { 93 try { 94 PortParser.parsePortNumber(null); 95 fail("Expected CommandSyntaxException"); 96 } 97 catch (CommandSyntaxException expected) { 98 LOG.info("Expected: " + expected); 99 } 100 } 101 102 /** 103 * Test the parsePortNumber() method, passing in a String[] with not enough parameters 104 * 105 * @throws java.net.UnknownHostException 106 */ 107 public void testParsePortNumber_InsufficientParameters() throws UnknownHostException { 108 try { 109 PortParser.parsePortNumber(PARAMETERS_INSUFFICIENT); 110 fail("Expected CommandSyntaxException"); 111 } 112 catch (CommandSyntaxException expected) { 113 LOG.info("Expected: " + expected); 114 } 115 } 116 117 /** 118 * Test convertHostAndPortToStringOfBytes() method 119 */ 120 public void testConvertHostAndPortToStringOfBytes() throws UnknownHostException { 121 int port = (23 << 8) + 77; 122 InetAddress host = InetAddress.getByName("196.168.44.55"); 123 String result = PortParser.convertHostAndPortToCommaDelimitedBytes(host, port); 124 LOG.info("result=" + result); 125 assertEquals("result", "196,168,44,55,23,77", result); 126 } 127 128}