CommonUtil { public static String readJsonFile(URL fileURL) { StringBuilder jsonBuff = new StringBuilder(); if (fileURL != null) { String fileName = fileURL.getFile(); InputStream in = null; try { in = new BufferedInputStream(fileURL.openStream()); Reader rd = new InputStreamReader(in, "UTF-8"); int c = 0; while ((c = rd.read()) != -1) { jsonBuff.append((char) c); } } catch (IOException e) { logger.error(e.getMessage() + "\n Can not find " + fileName + " or IO read error."); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { logger.error(e.getMessage() + "\n Can not close " + fileName + "."); } } } return jsonBuff.toString(); }  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 testReadJsonFileSucc() throws ParseException { String fileName = "/whitelist.json"; try { URL url = this.getClass().getResource(fileName); FileInputStream fis = new FileInputStream(url.getPath()); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); StringBuilder sb = new StringBuilder(); String line = ""; while ( (line = br.readLine()) != null ) { sb.append(line); } br.close(); String contentRead = CommonUtil.readJsonFile(url); String testString = sb.toString(); contentRead = contentRead.replaceAll("\n", ""); Assert.assertEquals(contentRead, testString); } catch (IOException e) { e.printStackTrace(); } }
@Test public void testReadJsonFileNotExisted() throws ParseException { String contentRead = CommonUtil.readJsonFile("Not_Existed"); Assert.assertEquals(contentRead, ""); }