CommonUtil { public static Process execCommand(String cmd) { if (cmd == null || cmd.isEmpty()) { return null; } Process p = null; try { p = new ProcessBuilder(Arrays.asList(cmd.split(" "))).start(); p.waitFor(); } catch (Exception e) { p = null; logger.error("Failed to execute command " + cmd + " : " + e.getMessage()); } return p; }  static void setOwnerOnlyReadWrite(String filename); static String getConfDir(); static File getConfigurationFile(final String filename, final String typeName); static String readJsonFile(URL fileURL); static String readJsonFile(final String fileName); static String dataFromFile(String filePath); static void gracefulRackTopologyOutput(
         Map<String, String> racksTopology, String filename, String delimeter); static void prettyOutputStrings(List<Object> objs, String fileName, String delimeter); static List<String> inputsConvert(String inputs); static Set<String> inputsConvertSet(String inputs); static String inputsConvert(Set<String> words); static String inputsConvert(Map<K, V> wordsMap); static boolean isBlank(final String str); static String notNull(final String str, final String desStr); static boolean validateVcResourceName(final String input); static boolean validateResourceName(final String input); static boolean validateDistroName(final String input); static boolean validateClusterName(final String input); static boolean validateNodeGroupName(final String input); static boolean validataPathInfo(final String input); static boolean validateVcDataStoreNames(List<String> names); static boolean matchDatastorePattern(Set<String> patterns, Set<String> datastores); static String escapePattern(String pattern); static String getDatastoreJavaPattern(String pattern); static String getUUID(); static long makeVmMemoryDivisibleBy4(long memory); static boolean validateClusterPassword(String password); static boolean passwordContainInvalidCharacter(String password); static String randomString(int count); static String getClusterName(String vmName); static long getVmIndex(String vmName); static Process execCommand(String cmd); static String decode(final String paramName); static String encode(final String paramName); static String getCurrentTimestamp(); static String toHexString(byte[] bytes); static KeyStore loadAppMgrKeyStore(String keystorePath); static boolean validateUrl(String url, List<String> errorMsgs); static String mergeErrorMsgList(List<String> errorMsgs); static String formatWarningMsg(String warningMsg); static boolean checkServerConnection(final String host, final int port, int waitTime); static Object waitForThreadResult(Future<?> result, int waitTime); static boolean validateDnsType(NetworkDnsType dnsType); static void writeFile(File file, String content); static void writeFile(File file, String content, boolean append); static boolean getBooleanFromString(String booleanString, boolean defaultValue); static String getCustomizedSudoCmd(); static StorageRead.DiskScsiControllerType getSystemAndSwapControllerType(); static StorageRead.DiskScsiControllerType getDataDiskControllerType(); static char[] allChars; }
@Test public void testExecCommand() throws ParseException { String tmpDir = System.getProperty("java.io.tmpdir"); if ( tmpDir.endsWith(File.separator) ) { tmpDir = tmpDir.substring(0, tmpDir.length()-1); } String filePathName = tmpDir + File.separator + "abc"; File f = new File(filePathName); Assert.assertFalse(f.exists()); String cmd = "touch " + filePathName; Process p = CommonUtil.execCommand(cmd); Assert.assertNotNull(p); Assert.assertTrue(f.exists()); f.delete(); }