View Javadoc

1   package org.apache.helix;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.concurrent.TimeoutException;
25  
26  import org.apache.log4j.Logger;
27  import org.testng.Assert;
28  
29  public class ScriptTestHelper
30  {
31    private static final Logger LOG = Logger.getLogger(ScriptTestHelper.class);
32  
33    public static final String INTEGRATION_SCRIPT_DIR = "src/main/scripts/integration-test/script";
34    public static final String INTEGRATION_TEST_DIR = "src/main/scripts/integration-test/testcases";
35    public static final String INTEGRATION_LOG_DIR = "src/main/scripts/integration-test/var/log";
36  
37    public static final long EXEC_TIMEOUT = 1200;
38    
39    public static String getPrefix()
40    {
41      StringBuilder prefixBuilder = new StringBuilder("");
42      String prefix = "";
43      String filepath = INTEGRATION_SCRIPT_DIR;
44      File integrationScriptDir = new File(filepath);
45  
46      while (!integrationScriptDir.exists())
47      {
48        prefixBuilder.append("../");
49        prefix = prefixBuilder.toString();
50  
51        integrationScriptDir = new File(prefix + filepath);
52  
53        // Give up
54        if (prefix.length() > 30)
55        {
56          return "";
57        }
58      }
59      return new File(prefix).getAbsolutePath() + "/";
60    }
61    
62    public static ExternalCommand runCommandLineTest(String testName, String... arguments) throws IOException, InterruptedException,
63    TimeoutException
64    {
65      ExternalCommand cmd = ExternalCommand.executeWithTimeout(new File(getPrefix() + INTEGRATION_TEST_DIR),
66                                               testName, EXEC_TIMEOUT, arguments);
67      int exitValue = cmd.exitValue();
68      String output = cmd.getStringOutput("UTF8");
69  
70      if (0 == exitValue)
71      {
72        LOG.info("Test " + testName + " has run. ExitCode=" + exitValue + ". Command output: " + output);
73      }
74      else
75      {
76        LOG.warn("Test " + testName + " is FAILING. ExitCode=" + exitValue + ". Command output: " + output);
77        Assert.fail(output);
78  //      return cmd;
79      }
80      return cmd;
81    }
82  }
83