/* 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.grinder.util;

import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.junit.Test;

public class NetworkUtilTest {
	@Test
	public void testLocalHostAddress() {
		try {
			System.out.println("Local addr:" + InetAddress.getLocalHost().getHostAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		String localHostAddress = NetworkUtil.getLocalHostAddress();
		assertThat(localHostAddress, notNullValue());
		assertThat(localHostAddress, not("127.0.0.1"));
		localHostAddress = NetworkUtil.getLocalHostAddress();
		assertThat(localHostAddress, notNullValue());
		assertThat(localHostAddress, not("127.0.0.1"));
	}

	@Test
	public void testLocalHostAddressByConnecting() {

		String localHostAddress = NetworkUtil.getLocalHostAddress("www.baidu.com", 80);
		assertThat(localHostAddress, notNullValue());
		assertThat(localHostAddress, not("127.0.0.1"));
		localHostAddress = NetworkUtil.getLocalHostAddress("www.invalidaddresssss", 80);
		assertThat(localHostAddress, notNullValue());
		assertThat(localHostAddress, not("127.0.0.1"));
	}
   
	@Test
	public void testLocalHostName() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		try {
			System.out.println("Local host:" + InetAddress.getLocalHost().getHostName());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		String localHostAddress = NetworkUtil.getLocalHostName();
		System.out.println("NetworkUtil.getLocalHostName:" + localHostAddress);
		assertThat(localHostAddress, notNullValue());
		
		Class<?> networkClas = Class.forName("net.grinder.util.NetworkUtil");
		Method NonLoopbackMethod= networkClas.getDeclaredMethod("getFirstNonLoopbackAddress", new Class[] {boolean.class, boolean.class});
		NonLoopbackMethod.setAccessible(true);
		NonLoopbackMethod.invoke(networkClas, true,false);
		
	}
	   
		@Test
		public void testLocalHostNameByConnecting() {
			String localHostAddress = NetworkUtil.getLocalHostName("www.baidu.com", 80);
			System.out.println("NetworkUtil.getLocalHostName:" + localHostAddress);
			assertThat(localHostAddress, notNullValue());			
		}
}

@@ -587,16 +587,56 @@
 .util;%0A%0A
+import static org.hamcrest.Matchers.is;%0A
 import s
@@ -1913,19 +1913,16 @@
 %22));%0A%09%7D%0A
-   
 %0A%09@Test%0A
@@ -1953,17 +1953,22 @@
 stName()
- 
+%0A%09%09%09%09%09
 throws C
@@ -1989,17 +1989,22 @@
 ception,
- 
+%0A%09%09%09%09%09
 Security
@@ -2013,17 +2013,22 @@
 ception,
- 
+%0A%09%09%09%09%09
 NoSuchMe
@@ -2033,33 +2033,38 @@
 MethodException,
- 
+%0A%09%09%09%09%09
 IllegalArgumentE
@@ -2072,17 +2072,22 @@
 ception,
- 
+%0A%09%09%09%09%09
 IllegalA
@@ -2101,17 +2101,22 @@
 ception,
- 
+%0A%09%09%09%09%09
 Invocati
@@ -2471,18 +2471,16 @@
 lue());%0A
-%09%09
 %0A%09%09Class
@@ -2570,16 +2570,17 @@
 ckMethod
+ 
 = networ
@@ -2646,16 +2646,21 @@
 lass%5B%5D %7B
+%0A%09%09%09%09
 boolean.
@@ -2679,16 +2679,17 @@
 an.class
+ 
 %7D);%0A%09%09No
@@ -2774,16 +2774,17 @@
 rue,
+ 
 false);%0A
 %09%09%0A%09
@@ -2783,28 +2783,20 @@
 e);%0A
-%09%09
 %0A%09%7D%0A
-%09   %0A%09
+%0A
 %09@Test%0A
-%09
 %09pub
@@ -2840,17 +2840,16 @@
 g() %7B%0A%09%09
-%09
 String l
@@ -2917,17 +2917,16 @@
 %22, 80);%0A
-%09
 %09%09System
@@ -2993,17 +2993,16 @@
 ess);%0A%09%09
-%09
 assertTh
@@ -3042,13 +3042,238 @@
 ());
-%09%09%09%0A%09
+%0A%09%7D%0A%0A%09@Test%0A%09public void testIPValidation() %7B%0A%09%09assertThat(NetworkUtil.isValidIP(%2210.10.10.10%22), is(true));%0A%09%09assertThat(NetworkUtil.isValidIP(%2210.10.10.1011%22), is(false));%0A%09%09assertThat(NetworkUtil.isValidIP(%22hello%22), is(false));%0A
 %09%7D%0A%7D

Add test