KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > exec > RuntimeExecTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util.exec;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.alfresco.util.exec.RuntimeExec.ExecutionResult;
24
25 import junit.framework.TestCase;
26
27 /**
28  * @see org.alfresco.util.exec.RuntimeExec
29  *
30  * @author Derek Hulley
31  */

32 public class RuntimeExecTest extends TestCase
33 {
34     /**
35      * execute a statement and catch the output
36      */

37     public void testStreams() throws Exception JavaDoc
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 JavaDoc out = ret.getStdOut();
45         String JavaDoc 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 JavaDoc
52     {
53         RuntimeExec exec = new RuntimeExec();
54
55         // set the command
56
Map JavaDoc<String JavaDoc, String JavaDoc> commandMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(3, 1.0f);
57         commandMap.put(".*", "TEST");
58         exec.setCommandMap(commandMap);
59         
60         String JavaDoc commandStr = exec.getCommand();
61         assertEquals("Expected default match to work", "TEST", commandStr);
62     }
63     
64     public void testWithProperties() throws Exception JavaDoc
65     {
66         RuntimeExec exec = new RuntimeExec();
67
68         // set the command
69
Map JavaDoc<String JavaDoc, String JavaDoc> commandMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(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         // set the default properties
76
Map JavaDoc<String JavaDoc, String JavaDoc> defaultProperties = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(1, 1.0f);
77         defaultProperties.put("path", ".");
78         exec.setDefaultProperties(defaultProperties);
79         
80         // check that the command lines generated are correct
81
String JavaDoc defaultCommand = exec.getCommand();
82         String JavaDoc dynamicCommand = exec.getCommand(Collections.singletonMap("path", "./"));
83         // check
84
String JavaDoc os = System.getProperty("os.name");
85         String JavaDoc defaultCommandCheck = null;
86         String JavaDoc 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