001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.process;
006:
007: import com.tc.test.TCTestCase;
008:
009: import java.io.File;
010:
011: /**
012: * Unit test for {@link LinkedJavaProcess}.
013: */
014: public class LinkedJavaProcessTest extends TCTestCase {
015: private static final boolean DEBUG = false;
016:
017: public void testRunsRightCommand() throws Exception {
018: LinkedJavaProcess process = new LinkedJavaProcess(
019: LinkedJavaProcessTestMain1.class.getName());
020: process.start();
021:
022: // This is actually stdout...weird Java
023: StreamCollector outCollector = new StreamCollector(process
024: .getInputStream());
025: // This is stderr
026: StreamCollector errCollector = new StreamCollector(process
027: .getErrorStream());
028:
029: outCollector.start();
030: errCollector.start();
031:
032: process.waitFor();
033:
034: outCollector.join(30000);
035: errCollector.join(30000);
036:
037: assertEquals("Ho there!", ignoreStandardWarnings(
038: errCollector.toString()).trim());
039: assertEquals("Hi there!", ignoreStandardWarnings(
040: outCollector.toString()).trim());
041: }
042:
043: private static String ignoreStandardWarnings(String input) {
044: debugPrintln("***** inputString=[" + input + "]");
045:
046: String delimiter = System.getProperty("line.separator", "\n");
047:
048: debugPrintln("***** delimiter=[" + delimiter + "]");
049:
050: String[] output = input.split(delimiter);
051:
052: StringBuffer out = new StringBuffer();
053: for (int i = 0; i < output.length; ++i) {
054: debugPrintln("***** piece=[" + output[i] + "]");
055:
056: if (output[i].startsWith("DATA: ")) {
057: out.append(output[i].substring("DATA: ".length())
058: + delimiter);
059: debugPrintln("***** appending ["
060: + output[i].substring("DATA: ".length())
061: + delimiter + "] to output string");
062: }
063: }
064:
065: debugPrintln("***** outString=[" + out.toString() + "]");
066:
067: return out.toString();
068: }
069:
070: public void testIO() throws Exception {
071: LinkedJavaProcess process = new LinkedJavaProcess(
072: LinkedJavaProcessTestMain2.class.getName());
073: process.start();
074:
075: StreamCollector outCollector = new StreamCollector(process
076: .getInputStream()); // stdout
077: StreamCollector errCollector = new StreamCollector(process
078: .getErrorStream()); // stderr
079:
080: outCollector.start();
081: errCollector.start();
082:
083: process.getOutputStream().write("Test Input!\n".getBytes());
084: process.getOutputStream().flush();
085:
086: process.waitFor();
087:
088: outCollector.join(30000);
089: errCollector.join(30000);
090:
091: assertEquals("out: <Test Input!>", ignoreStandardWarnings(
092: outCollector.toString()).trim());
093: assertEquals("err: <Test Input!>", ignoreStandardWarnings(
094: errCollector.toString()).trim());
095: }
096:
097: public void testExitCode() throws Exception {
098: LinkedJavaProcess process = new LinkedJavaProcess(
099: LinkedJavaProcessTestMain3.class.getName());
100: process.start();
101:
102: process.waitFor();
103: assertEquals(57, process.exitValue());
104: }
105:
106: public void testSetup() throws Exception {
107: LinkedJavaProcess process = new LinkedJavaProcess(
108: LinkedJavaProcessTestMain4.class.getName());
109:
110: File dir = getTempFile("mydir");
111: assertTrue(dir.mkdirs());
112: String pwd = dir.getCanonicalPath();
113: process.setDirectory(dir);
114: process
115: .setEnvironment(new String[] { "LD_LIBRARY_PATH=myenv" });
116: process.setJavaArguments(new String[] { "-Dljpt.foo=myprop" });
117:
118: process.start();
119:
120: StreamCollector outCollector = new StreamCollector(process
121: .getInputStream()); // stdout
122: StreamCollector errCollector = new StreamCollector(process
123: .getErrorStream()); // stderr
124:
125: outCollector.start();
126: errCollector.start();
127:
128: process.waitFor();
129:
130: outCollector.join(30000);
131: errCollector.join(30000);
132:
133: String output = outCollector.toString();
134: String err = errCollector.toString();
135:
136: assertEquals("", ignoreStandardWarnings(err).trim());
137:
138: assertContains("ljpt.foo=myprop", output);
139: assertContains(pwd.toLowerCase(), output.toLowerCase());
140: }
141:
142: public void testKillingParentKillsChildren() throws Exception {
143: File destFile = getTempFile("tkpkc-file");
144: File child1File = new File(destFile.getAbsolutePath()
145: + "-child-1");
146: File child2File = new File(destFile.getAbsolutePath()
147: + "-child-2");
148: LinkedJavaProcess process = new LinkedJavaProcess(
149: LinkedJavaProcessTestMain5.class.getName(),
150: new String[] { destFile.getAbsolutePath(), "true" });
151:
152: process.start();
153:
154: StreamCollector stdout = new StreamCollector(process.STDOUT());
155: stdout.start();
156: StreamCollector stderr = new StreamCollector(process.STDERR());
157: stderr.start();
158:
159: long origSize = destFile.length();
160: Thread.sleep(6000);
161: long newSize = destFile.length();
162:
163: System.err.println("Parent first: new=" + newSize + " old="
164: + origSize);
165: assertTrue(newSize > origSize); // Make sure it's all started + working
166:
167: long child1OrigSize = child1File.length();
168: long child2OrigSize = child2File.length();
169: Thread.sleep(5000);
170: long child1NewSize = child1File.length();
171: long child2NewSize = child2File.length();
172:
173: System.err.println("Child 1 first: new=" + child1NewSize
174: + " old=" + child1OrigSize);
175: System.err.println("Child 2 first: new=" + child2NewSize
176: + " old=" + child2OrigSize);
177: // Make sure the children are all started + working
178: assertTrue(child1NewSize > child1OrigSize);
179: assertTrue(child2NewSize > child2OrigSize);
180:
181: System.out.println(stdout.toString());
182: System.out.println(stderr.toString());
183:
184: process.destroy();
185: // wait for child process heartbeat to time out and kill themselves
186: Thread.sleep(HeartBeatServer.PULSE_INTERVAL * 2);
187:
188: origSize = destFile.length();
189: Thread.sleep(5000);
190: newSize = destFile.length();
191:
192: System.err.println("Parent after kill: new=" + newSize
193: + " old=" + origSize);
194: assertEquals(origSize, newSize); // Make sure the parent is dead
195:
196: child1OrigSize = child1File.length();
197: child2OrigSize = child2File.length();
198: Thread.sleep(5000);
199: child1NewSize = child1File.length();
200: child2NewSize = child2File.length();
201: System.err.println("Child 1 after kill: new=" + child1NewSize
202: + " old=" + child1OrigSize);
203: System.err.println("Child 2 after kill: new=" + child2NewSize
204: + " old=" + child2OrigSize);
205:
206: assertEquals(child1NewSize, child1OrigSize); // Make sure child 1 is dead
207: assertEquals(child2NewSize, child2OrigSize); // Make sure child 1 is dead
208: }
209:
210: private static void debugPrintln(String s) {
211: if (DEBUG) {
212: System.err.println(s);
213: }
214: }
215: }
|