package net.spy.memcached;

import java.net.InetSocketAddress;
import java.util.List;

import junit.framework.TestCase;

/**
 * Test the AddrUtils stuff.
 */
public class AddrUtilTest extends TestCase {

	public void testSingle() throws Exception {
		List<InetSocketAddress> addrs=
			AddrUtil.getAddresses("www.google.com:80");
		assertEquals(1, addrs.size());
		assertEquals("www.google.com", addrs.get(0).getHostName());
		assertEquals(80, addrs.get(0).getPort());
	}

	public void testTwo() throws Exception {
		List<InetSocketAddress> addrs=
			AddrUtil.getAddresses("www.google.com:80 www.yahoo.com:81");
		assertEquals(2, addrs.size());
		assertEquals("www.google.com", addrs.get(0).getHostName());
		assertEquals(80, addrs.get(0).getPort());
		assertEquals("www.yahoo.com", addrs.get(1).getHostName());
		assertEquals(81, addrs.get(1).getPort());
	}

	public void testThree() throws Exception {
		List<InetSocketAddress> addrs=
			AddrUtil.getAddresses(" ,  www.google.com:80 ,, ,, www.yahoo.com:81 , ,,");
		assertEquals(2, addrs.size());
		assertEquals("www.google.com", addrs.get(0).getHostName());
		assertEquals(80, addrs.get(0).getPort());
		assertEquals("www.yahoo.com", addrs.get(1).getHostName());
		assertEquals(81, addrs.get(1).getPort());
	}

	public void testBrokenHost() throws Exception {
		String s="www.google.com:80 www.yahoo.com:81:more";
		try {
			List<InetSocketAddress> addrs=AddrUtil.getAddresses(s);
			fail("Expected failure, got " + addrs);
		} catch(NumberFormatException e) {
			e.printStackTrace();
			assertEquals("For input string: \"more\"", e.getMessage());
		}
	}

	public void testBrokenHost2() throws Exception {
		String s="www.google.com:80 www.yahoo.com";
		try {
			List<InetSocketAddress> addrs=AddrUtil.getAddresses(s);
			fail("Expected failure, got " + addrs);
		} catch(IllegalArgumentException e) {
			assertEquals("Invalid server ``www.yahoo.com'' in list:  "
					+ s, e.getMessage());
		}
	}

	public void testBrokenList() throws Exception {
		String s="";
		try {
			List<InetSocketAddress> addrs=AddrUtil.getAddresses(s);
			fail("Expected failure, got " + addrs);
		} catch(IllegalArgumentException e) {
			assertEquals("No hosts in list:  ``''", e.getMessage());
		}
	}

	public void testBrokenList2() throws Exception {
		String s="   ";
		try {
			List<InetSocketAddress> addrs=AddrUtil.getAddresses(s);
			fail("Expected failure, got " + addrs);
		} catch(IllegalArgumentException e) {
			assertEquals("No hosts in list:  ``   ''", e.getMessage());
		}
	}

	public void testNullList() throws Exception {
		String s=null;
		try {
			List<InetSocketAddress> addrs=AddrUtil.getAddresses(s);
			fail("Expected failure, got " + addrs);
		} catch(NullPointerException e) {
			assertEquals("Null host list", e.getMessage());
		}
	}

	public void testIPv6Host() throws Exception {
		List<InetSocketAddress> addrs=
			AddrUtil.getAddresses("::1:80");
		assertEquals(1, addrs.size());
		assertEquals("localhost", addrs.get(0).getHostName());
		assertEquals(80, addrs.get(0).getPort());
	}
}

@@ -73,19 +73,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0Aimpo
@@ -2985,36 +2985,249 @@
 ));%0A
-%09%09assertEquals(%22localhost%22, 
+%0A%09        Set%3CString%3E validLocalhostNames=new HashSet%3CString%3E();%0A%09%09validLocalhostNames.add(%22localhost%22);%0A%09%09validLocalhostNames.add(%22ip6-localhost%22);%0A%09%09validLocalhostNames.add(%22localhost6.localdomain6%22);%0A%09%09assert(validLocalhostNames.contains(
 addr
@@ -3241,32 +3241,33 @@
 ).getHostName())
+)
 ;%0A%09%09assertEquals

Fix AddrUtilTest#testIPv6Host() hostname assertion.