1 package org.objectweb.celtix.common.commands; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.File ; 5 import java.io.PrintStream ; 6 import java.net.URL ; 7 import java.util.StringTokenizer ; 8 9 import junit.framework.TestCase; 10 11 import org.objectweb.celtix.common.i18n.Message; 12 13 public class ForkedCommandTest extends TestCase { 14 15 private static final String [] ENV_COMMAND; 16 17 static { 18 if (System.getProperty("os.name").startsWith("Windows")) { 19 ENV_COMMAND = new String [] {"cmd", "/c", "set"}; 20 } else { 21 ENV_COMMAND = new String [] {"env"}; 22 } 23 } 24 25 public void testBasics() { 26 ForkedCommand fc1 = new ForkedCommand(); 27 String cmdline1 = fc1.toString(); 28 assertNull(cmdline1); 29 try { 30 fc1.execute(); 31 } catch (ForkedCommandException ex) { 32 assertEquals("NO_ARGUMENTS_EXC", ex.getCode()); 33 } 34 String [] args = new String [] {"a", "b", "c d e"}; 35 ForkedCommand fc2 = new ForkedCommand(args); 36 String cmdline2 = fc2.toString(); 37 assertTrue(cmdline2.startsWith("a")); 38 assertTrue(cmdline2.endsWith("\"")); 39 fc1.setArgs(args); 40 cmdline1 = fc1.toString(); 41 assertEquals(cmdline1, cmdline2); 42 43 new ForkedCommandException(new NullPointerException ()); 44 Message msg = org.easymock.classextension.EasyMock.createMock(Message.class); 45 new ForkedCommandException(msg, new NullPointerException ()); 46 } 47 48 public void testExecuteInDefaultEnvironment() { 49 ByteArrayOutputStream bosOut = new ByteArrayOutputStream (); 50 ByteArrayOutputStream bosErr = new ByteArrayOutputStream (); 51 52 executeEnvCommand(null, bosOut, bosErr); 53 54 String output = bosOut.toString(); 55 assertTrue(output.indexOf("AVAR") < 0 || output.indexOf("BVAR") < 0); 56 } 57 58 public void testExecuteInNonDefaultEnvironment() { 59 ByteArrayOutputStream bosOut = new ByteArrayOutputStream (); 60 ByteArrayOutputStream bosErr = new ByteArrayOutputStream (); 61 String [] env = new String [3]; 62 env[0] = "BVAR=strange"; 63 if (System.getProperty("os.name").startsWith("Windows")) { 64 env[1] = "AVAR=something %BVAR%"; 65 env[2] = "AVAR=something very %BVAR%"; 66 } else { 67 env[1] = "AVAR=something $BVAR"; 68 env[2] = "AVAR=something very $BVAR"; 69 } 70 71 72 executeEnvCommand(env, bosOut, bosErr); 73 74 76 boolean found = false; 77 String output = bosOut.toString(); 78 StringTokenizer st = new StringTokenizer (output, System.getProperty("line.separator")); 79 while (st.hasMoreTokens()) { 80 String line = st.nextToken(); 81 if (line.length() > 0) { 82 if (System.getProperty("os.name").startsWith("Windows")) { 83 if ("AVAR=something very %BVAR%".equals(line)) { 84 found = true; 85 break; 86 } 87 } else { 88 if ("AVAR=something very $BVAR".equals(line) 89 || "AVAR=something $BVAR".equals(line)) { 90 found = true; 91 break; 92 } 93 } 94 } 95 } 96 assertTrue(found); 97 98 } 99 100 public void testTimeout() throws Exception { 101 URL url = TestCommand.class.getResource("TestCommand.class"); 102 File file = new File (url.getFile()); 103 file = file.getParentFile(); 104 file = new File (file, "../../../../.."); 105 String [] cmd = new String [] { 106 JavaHelper.getJavaCommand(), 107 "-classpath", 108 file.getCanonicalPath(), 109 "org.objectweb.celtix.common.commands.TestCommand", 110 "-duration", 111 "60000", 112 }; 113 ForkedCommand fc = new ForkedCommand(cmd); 114 try { 115 fc.execute(1); 116 fail("Expected ForkedCommandException not thrown."); 117 } catch (ForkedCommandException ex) { 118 assertEquals("TIMEOUT_EXC", ex.getCode()); 119 } 120 } 121 122 private void executeEnvCommand(String [] env, ByteArrayOutputStream bosOut, ByteArrayOutputStream bosErr) { 123 124 ForkedCommand fc = new ForkedCommand(ENV_COMMAND); 125 if (null != env) { 126 fc.setEnvironment(env); 127 } 128 fc.joinErrOut(true); 129 130 PrintStream pso = new PrintStream (bosOut); 131 PrintStream pse = new PrintStream (bosErr); 132 fc.setOutputStream(pso); 133 fc.setErrorStream(pse); 134 135 int result = fc.execute(); 136 assertEquals(0, result); 137 138 } 139 140 141 142 143 144 } 145 | Popular Tags |