001: /*
002:
003: Copyright 2004, Martian Software, Inc.
004:
005: Licensed under the Apache License, Version 2.0 (the "License");
006: you may not use this file except in compliance with the License.
007: You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016:
017: */
018:
019: package com.martiansoftware.nailgun;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.PrintStream;
023: import java.net.InetAddress;
024: import java.net.NetworkInterface;
025: import java.util.Enumeration;
026: import java.util.Properties;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: *
032: * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
033: */
034: public class TestNGContext extends TestCase {
035:
036: public void testNGContextSettersAndGetters() {
037: NGContext context = new NGContext();
038:
039: String[] args = { "a", "b", "c" };
040: context.setArgs(args);
041: context.setCommand("testCommand");
042: Properties remoteEnv = new Properties();
043: remoteEnv.setProperty("one", "1");
044: remoteEnv.setProperty("two", "2");
045: context.setEnv(remoteEnv);
046: context.setPort(123);
047: context.setWorkingDirectory("/test");
048:
049: assertEquals("b", context.getArgs()[1]);
050: assertEquals("testCommand", context.getCommand());
051: assertEquals("2", context.getEnv().getProperty("two"));
052: assertEquals(123, context.getPort());
053: assertEquals("/test", context.getWorkingDirectory());
054:
055: NGServer server = new NGServer();
056: context.setNGServer(server);
057: assertEquals(server, context.getNGServer());
058: }
059:
060: public void testNGContextExit() {
061: NGContext context = new NGContext();
062: ByteArrayOutputStream exitStream = new ByteArrayOutputStream();
063: context.setExitStream(new PrintStream(exitStream));
064:
065: context.exit(1);
066: assertEquals('1', exitStream.toByteArray()[0]);
067: }
068:
069: public void testNGContextLoopbackAssertion() throws Exception {
070: NGContext context = new NGContext();
071: context.setInetAddress(InetAddress.getLocalHost());
072: assertEquals(InetAddress.getLocalHost(), context
073: .getInetAddress());
074: context.assertLocalClient();
075: context.assertLoopbackClient();
076: }
077:
078: public void testNGContextExternalInetAddress() throws Exception {
079:
080: NGContext context = new NGContext();
081: InetAddress obviousExternal = InetAddress
082: .getByName("www.google.com");
083: context.setInetAddress(obviousExternal);
084: assertEquals(obviousExternal, context.getInetAddress());
085: try {
086: context.assertLocalClient();
087: fail(obviousExternal + " passed as local client.");
088: } catch (Throwable t) {
089: }
090: try {
091: context.assertLoopbackClient();
092: fail(obviousExternal + " passed as local client.");
093: } catch (Throwable t) {
094: }
095: }
096:
097: public void testNGContextNonLoopbackAssertions() throws Exception {
098: NGContext context = new NGContext();
099: int nonLoopbackAddressCount = 0;
100: for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e
101: .hasMoreElements();) {
102: NetworkInterface iface = (NetworkInterface) e.nextElement();
103:
104: for (Enumeration a = iface.getInetAddresses(); a
105: .hasMoreElements();) {
106: InetAddress addr = (InetAddress) a.nextElement();
107:
108: if (!addr.equals(InetAddress.getLocalHost())) {
109: ++nonLoopbackAddressCount;
110:
111: context.setInetAddress(addr);
112: context.assertLocalClient();
113: try {
114: context.assertLoopbackClient();
115: fail(addr + " passed as loopback client.");
116: } catch (Throwable t) {
117: }
118: }
119: }
120: }
121: if (nonLoopbackAddressCount == 0) {
122: fail("No non-loopback addresses tested. Sorry, but this test requires the test machine to have an IP address.");
123: }
124: }
125:
126: }
|