KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > TestNGContext


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16
17 */

18
19 package com.martiansoftware.nailgun;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.net.NetworkInterface JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import junit.framework.TestCase;
29
30 /**
31  *
32  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
33  */

34 public class TestNGContext extends TestCase {
35     
36     public void testNGContextSettersAndGetters() {
37         NGContext context = new NGContext();
38         
39         String JavaDoc[] args = { "a", "b", "c" };
40         context.setArgs(args);
41         context.setCommand("testCommand");
42         Properties JavaDoc remoteEnv = new Properties JavaDoc();
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 JavaDoc exitStream = new ByteArrayOutputStream JavaDoc();
63         context.setExitStream(new PrintStream JavaDoc(exitStream));
64
65         context.exit(1);
66         assertEquals('1', exitStream.toByteArray()[0]);
67     }
68
69     public void testNGContextLoopbackAssertion() throws Exception JavaDoc {
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 JavaDoc {
78
79         NGContext context = new NGContext();
80         InetAddress JavaDoc 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 JavaDoc t) {}
87         try {
88             context.assertLoopbackClient();
89             fail(obviousExternal + " passed as local client.");
90         } catch (Throwable JavaDoc t) {}
91     }
92     
93     public void testNGContextNonLoopbackAssertions() throws Exception JavaDoc {
94         NGContext context = new NGContext();
95         int nonLoopbackAddressCount = 0;
96         for (Enumeration JavaDoc e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
97             NetworkInterface JavaDoc iface = (NetworkInterface JavaDoc) e.nextElement();
98             
99             for (Enumeration JavaDoc a = iface.getInetAddresses(); a.hasMoreElements();) {
100                 InetAddress JavaDoc addr = (InetAddress JavaDoc) 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 JavaDoc 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