1 18 19 package com.martiansoftware.nailgun; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.PrintStream ; 23 import java.net.InetAddress ; 24 import java.net.NetworkInterface ; 25 import java.util.Enumeration ; 26 import java.util.Properties ; 27 28 import junit.framework.TestCase; 29 30 34 public class TestNGContext extends TestCase { 35 36 public void testNGContextSettersAndGetters() { 37 NGContext context = new NGContext(); 38 39 String [] args = { "a", "b", "c" }; 40 context.setArgs(args); 41 context.setCommand("testCommand"); 42 Properties remoteEnv = new Properties (); 43 remoteEnv.setProperty("one", "1"); 44 remoteEnv.setProperty("two", "2"); 45 context.setEnv(remoteEnv); 46 context.setPort(123); 47 context.setWorkingDirectory("/test"); 48 49 assertEquals("b", context.getArgs()[1]); 50 assertEquals("testCommand", context.getCommand()); 51 assertEquals("2", context.getEnv().getProperty("two")); 52 assertEquals(123, context.getPort()); 53 assertEquals("/test", context.getWorkingDirectory()); 54 55 NGServer server = new NGServer(); 56 context.setNGServer(server); 57 assertEquals(server, context.getNGServer()); 58 } 59 60 public void testNGContextExit() { 61 NGContext context = new NGContext(); 62 ByteArrayOutputStream exitStream = new ByteArrayOutputStream (); 63 context.setExitStream(new PrintStream (exitStream)); 64 65 context.exit(1); 66 assertEquals('1', exitStream.toByteArray()[0]); 67 } 68 69 public void testNGContextLoopbackAssertion() throws Exception { 70 NGContext context = new NGContext(); 71 context.setInetAddress(InetAddress.getLocalHost()); 72 assertEquals(InetAddress.getLocalHost(), context.getInetAddress()); 73 context.assertLocalClient(); 74 context.assertLoopbackClient(); 75 } 76 77 public void testNGContextExternalInetAddress() throws Exception { 78 79 NGContext context = new NGContext(); 80 InetAddress obviousExternal = InetAddress.getByName("www.google.com"); 81 context.setInetAddress(obviousExternal); 82 assertEquals(obviousExternal, context.getInetAddress()); 83 try { 84 context.assertLocalClient(); 85 fail(obviousExternal + " passed as local client."); 86 } catch (Throwable t) {} 87 try { 88 context.assertLoopbackClient(); 89 fail(obviousExternal + " passed as local client."); 90 } catch (Throwable t) {} 91 } 92 93 public void testNGContextNonLoopbackAssertions() throws Exception { 94 NGContext context = new NGContext(); 95 int nonLoopbackAddressCount = 0; 96 for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) { 97 NetworkInterface iface = (NetworkInterface ) e.nextElement(); 98 99 for (Enumeration a = iface.getInetAddresses(); a.hasMoreElements();) { 100 InetAddress addr = (InetAddress ) a.nextElement(); 101 102 if (!addr.equals(InetAddress.getLocalHost())) { 103 ++nonLoopbackAddressCount; 104 105 context.setInetAddress(addr); 106 context.assertLocalClient(); 107 try { 108 context.assertLoopbackClient(); 109 fail(addr + " passed as loopback client."); 110 } catch (Throwable t) {} 111 } 112 } 113 } 114 if (nonLoopbackAddressCount == 0) { 115 fail("No non-loopback addresses tested. Sorry, but this test requires the test machine to have an IP address."); 116 } 117 } 118 119 } 120 | Popular Tags |