1   // 
2   // Copyright (c) 2003-2005, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   //   
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //   
8   // * Redistributions of source code must retain the above copyright notice,  
9   // this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  // this list of conditions and the following disclaimer in the documentation  
12  // and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  // nor the names of its contributors may be used to endorse or promote products  
15  // derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.external;
30  
31  import java.io.IOException;
32  
33  import org.objectledge.utils.LedgeTestCase;
34  
35  /***
36   * Test for the ProcessExecutor class.
37   *
38   * @author <a href="rafal@caltha.pl">RafaƂ Krzewski</a>
39   * @version $Id: ProcessExecutorTest.java,v 1.2 2006/11/06 16:51:12 zwierzem Exp $
40   */
41  public class ProcessExecutorTest
42      extends LedgeTestCase
43  {
44      private ScriptDirectory scriptDir;
45      
46      private ProcessExecutor executor;
47      
48      public void setUp()
49      {
50          boolean unix = false;
51          String osName = System.getProperty("os.name");
52          if(osName.equals("Linux") || osName.equals("Mac OS X"))
53          {
54              unix = true;
55          }
56          
57          scriptDir = new ScriptDirectory(getFileSystem(), 
58              ScriptDirectory.DEFAULT_PROVIDER,
59              unix ? "external" : null,
60              ScriptDirectory.DEFAULT_PATH_PATTERN,
61              ScriptDirectory.DEFAULT_COMMAND);
62  
63          executor = new ProcessExecutor(getLogger(), ProcessExecutor.DEFAULT_SHELL);
64          
65          initLog4J("ERROR");
66      }
67      
68      public void testPlainExec() throws IOException
69      {
70          ExecutionResult res = executor.exec(scriptDir.getPath("test.sh"));
71          assertEquals(0, res.getExitValue());
72      }    
73      
74      public void testExitValue() throws IOException
75      {
76          ExecutionResult res = executor.exec(scriptDir.getPath("exitValue.sh"));
77          assertEquals(4, res.getExitValue());        
78      }
79      
80      public void testInput() throws IOException
81      {
82          ExecutionResult res;
83          res = executor.exec("bad input", false, false, scriptDir.getPath("input.sh"));
84          assertEquals(1, res.getExitValue());
85          res = executor.exec("good input", false, false, scriptDir.getPath("input.sh"));
86          assertEquals(0, res.getExitValue());
87      }
88      
89      public void testOuptut() throws IOException
90      {
91          ExecutionResult res;
92          res = executor.exec(true, false, scriptDir.getPath("test.sh"));
93          assertEquals("test.sh\n", res.getOutput());
94      }
95  
96      public void testError() throws IOException
97      {
98          ExecutionResult res;
99          res = executor.exec(false, true, scriptDir.getPath("error.sh"));
100         assertEquals(0, res.getExitValue());
101         assertEquals("error.sh\n", res.getError());
102     }
103     
104     public void testConifg()
105         throws Exception
106     {
107         checkSchema("config/org.objectledge.external.ProcessExecutor.xml",
108             "org/objectledge/external/ProcessExecutor.rng");
109         executor = new ProcessExecutor(getLogger(), getConfig(getFileSystem(),
110             "config/org.objectledge.external.ProcessExecutor.xml"));
111     }
112 }