1 17 package org.alfresco.util.exec; 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.alfresco.util.exec.RuntimeExec.ExecutionResult; 24 25 import junit.framework.TestCase; 26 27 32 public class RuntimeExecTest extends TestCase 33 { 34 37 public void testStreams() throws Exception 38 { 39 RuntimeExec exec = new RuntimeExec(); 40 exec.setCommand("find abc"); 41 ExecutionResult ret = exec.execute(); 42 assertTrue("Expected error code", ret.getExitValue() != 0); 43 44 String out = ret.getStdOut(); 45 String err = ret.getStdErr(); 46 47 assertTrue("Didn't expect any non-error output", out.length() == 0); 48 assertTrue("No error output found", err.length() > 0); 49 } 50 51 public void testWildcard() throws Exception 52 { 53 RuntimeExec exec = new RuntimeExec(); 54 55 Map <String , String > commandMap = new HashMap <String , String >(3, 1.0f); 57 commandMap.put(".*", "TEST"); 58 exec.setCommandMap(commandMap); 59 60 String commandStr = exec.getCommand(); 61 assertEquals("Expected default match to work", "TEST", commandStr); 62 } 63 64 public void testWithProperties() throws Exception 65 { 66 RuntimeExec exec = new RuntimeExec(); 67 68 Map <String , String > commandMap = new HashMap <String , String >(3, 1.0f); 70 commandMap.put("Windows.*", "dir \"${path}\""); 71 commandMap.put("Linux", "ls '${path}'"); 72 commandMap.put("*", "wibble ${path}"); 73 exec.setCommandMap(commandMap); 74 75 Map <String , String > defaultProperties = new HashMap <String , String >(1, 1.0f); 77 defaultProperties.put("path", "."); 78 exec.setDefaultProperties(defaultProperties); 79 80 String defaultCommand = exec.getCommand(); 82 String dynamicCommand = exec.getCommand(Collections.singletonMap("path", "./")); 83 String os = System.getProperty("os.name"); 85 String defaultCommandCheck = null; 86 String dynamicCommandCheck = null; 87 if (os.matches("Windows.*")) 88 { 89 defaultCommandCheck = "dir \".\""; 90 dynamicCommandCheck = "dir \"./\""; 91 } 92 else if (os.equals("Linux")) 93 { 94 defaultCommandCheck = "ls '.'"; 95 dynamicCommandCheck = "ls './'"; 96 } 97 else 98 { 99 defaultCommandCheck = "wibble ."; 100 dynamicCommandCheck = "wibble ./"; 101 } 102 assertEquals("Default command for OS " + os + " is incorrect", defaultCommandCheck, defaultCommand); 103 assertEquals("Dynamic command for OS " + os + " is incorrect", dynamicCommandCheck, dynamicCommand); 104 } 105 } 106 | Popular Tags |