Example usage for org.apache.commons.io FileUtils readFileToString

List of usage examples for org.apache.commons.io FileUtils readFileToString

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils readFileToString.

Prototype

public static String readFileToString(File file) throws IOException 

Source Link

Document

Reads the contents of a file into a String using the default encoding for the VM.

Usage

From source file:de.eidottermihi.rpicheck.test.LoadAverageTest.java

@Test
public void load_avg() throws IOException, RaspiQueryException {
    String output = FileUtils.readFileToString(
            FileUtils.getFile("src/test/java/de/eidottermihi/rpicheck/test/proc_loadavg.txt"));
    sessionMocker.withCommand(COMMAND, new CommandMocker().withResponse(output).mock());
    double queryLoadAverage = raspiQuery.queryLoadAverage(LoadAveragePeriod.FIVE_MINUTES);
    assertEquals(0.58D, queryLoadAverage, 0.001D);
}

From source file:ch.sbb.releasetrain.config.model.releaseconfig.ReleaseConfigSerializerTest.java

@Test
public void testSerializeReleaseConfig() throws Exception {

    ReleaseConfig release = new ReleaseConfig();
    release.setTyp("devType");
    ActionConfig action = new EmailActionConfig();
    action.getProperties().put("state", "SUCCESS");
    release.getActions().add(action);/* ww  w . ja v a  2 s .c o  m*/

    ActionConfig action2 = new EmailActionConfig();
    action2.getProperties().put("state", "SUCCESS");
    release.getActions().add(action2);

    File file = new File(testFolder.getRoot(), "devType3.yml");
    FileUtils.writeStringToFile(file, configSerializer.convertEntry(release));

    ReleaseConfig release2 = configSerializer.convertEntry(FileUtils.readFileToString(file));
    Assert.assertNotNull(release2);
    Assert.assertEquals(release, release2);

}

From source file:com.qperior.GSAOneBoxProvider.implementations.jive.rest.QPJiveJsonObjectTest.java

private String getJSONString(String fileName) {

    String jsonString = "";

    try {//from ww w.j a v a 2 s  .c  o  m

        URL url = ConfigurationUtils.locate(null, fileName);
        File file = FileUtils.toFile(url);
        jsonString = FileUtils.readFileToString(file);

    } catch (IOException exc) {

        //nothing
    }

    return jsonString;
}

From source file:com.jdom.util.PropertiesUtilTest.java

@Test
public void testWritesOutPropertiesToSpecifiedLocation() throws IOException {
    Properties properties = new Properties();
    properties.setProperty("key", "value");

    File testOutputFile = new File(GamePackListModelTest.setupTestClassDir(getClass()), "output.properties");

    PropertiesUtil.writePropertiesFile(properties, testOutputFile);

    String output = FileUtils.readFileToString(testOutputFile);

    assertTrue("Did not find the property set in the output file!", output.contains("key=value"));
}

From source file:com.predic8.membrane.examples.tests.versioning.RoutingTest.java

@Test
public void test() throws IOException, InterruptedException {
    File base = getExampleDir("versioning/routing");

    String header[] = new String[] { "Content-Type", "text/xml" };
    String request_v11 = FileUtils.readFileToString(new File(base, "request_v11.xml"));
    String request_v20 = FileUtils.readFileToString(new File(base, "request_v20.xml"));

    replaceInFile(new File(base, "proxies.xml"), "8080", "3024");
    replaceInFile(new File(base, "proxies.xml"), "2000", "3025");
    replaceInFile(new File(base, "src/com/predic8/contactservice/Launcher.java"), "8080", "3024");

    Process2 sl = new Process2.Builder().in(base).script("service-proxy").waitForMembrane().start();
    try {//from  ww w . j a v  a 2s.  c  o m
        Process2 antNode1 = new Process2.Builder().in(base).waitAfterStartFor("run:").executable("ant run")
                .start();
        try {
            Thread.sleep(2000); // wait for Endpoints to start

            // directly talk to versioned endpoints
            assertContains("1.1",
                    postAndAssert(200, "http://localhost:3024/ContactService/v11", header, request_v11));
            assertContains("2.0",
                    postAndAssert(200, "http://localhost:3024/ContactService/v20", header, request_v20));

            // talk to wrong endpoint
            postAndAssert(500, "http://localhost:3024/ContactService/v20", header, request_v11);

            // talk to proxy
            assertContains("1.1",
                    postAndAssert(200, "http://localhost:3025/ContactService", header, request_v11));
            assertContains("2.0",
                    postAndAssert(200, "http://localhost:3025/ContactService", header, request_v20));

        } finally {
            antNode1.killScript();
        }

    } finally {
        sl.killScript();
    }

}

From source file:com.playonlinux.core.scripts.AnyScriptFactoryImplementation.java

@Override
public Script createInstanceFromFile(File scriptFile) throws ScriptFailureException {
    try {// ww w  . j a  v  a2s .c o  m
        return createInstance(FileUtils.readFileToString(scriptFile));
    } catch (IOException e) {
        throw new ScriptFailureException(e);
    }
}

From source file:edu.lternet.pasta.common.EmlUtility.java

public static EmlPackageId emlPackageIdFromEML(File emlFile) throws Exception {
    EmlPackageId emlPackageId = null;/* www . ja  v  a 2 s.  c  o  m*/

    if (emlFile != null) {
        String emlString = FileUtils.readFileToString(emlFile);
        try {
            DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = documentBuilder.parse(IOUtils.toInputStream(emlString));
            emlPackageId = getEmlPackageId(document);
        }
        /*
         * If a parsing exception is thrown, attempt to parse the packageId
         * using regular expressions. This could be fooled by comments text
         * in the EML document but is still better than nothing at all.
         */
        catch (SAXException e) {
            StringTokenizer stringTokenizer = new StringTokenizer(emlString, "\n");
            String DOUBLE_QUOTE_PATTERN = "packageId=\"([^\"]*)\"";
            String SINGLE_QUOTE_PATTERN = "packageId='([^']*)'";
            Pattern doubleQuotePattern = Pattern.compile(DOUBLE_QUOTE_PATTERN);
            Pattern singleQuotePattern = Pattern.compile(SINGLE_QUOTE_PATTERN);
            while (stringTokenizer.hasMoreElements()) {
                String token = stringTokenizer.nextToken();
                if (token.contains("packageId")) {

                    Matcher doubleQuoteMatcher = doubleQuotePattern.matcher(token);
                    if (doubleQuoteMatcher.find()) {
                        String packageId = doubleQuoteMatcher.group(1);
                        System.out.println(packageId);
                        EmlPackageIdFormat formatter = new EmlPackageIdFormat(Delimiter.DOT);
                        emlPackageId = formatter.parse(packageId);
                        break;
                    }

                    Matcher singleQuoteMatcher = singleQuotePattern.matcher(token);
                    if (singleQuoteMatcher.find()) {
                        String packageId = singleQuoteMatcher.group(1);
                        EmlPackageIdFormat formatter = new EmlPackageIdFormat(Delimiter.DOT);
                        emlPackageId = formatter.parse(packageId);
                        break;
                    }

                }
            }
        }
    }

    return emlPackageId;
}

From source file:io.github.microcks.util.JsonSchemaValidatorTest.java

@Test
public void testValidateJsonSuccess() {
    boolean valid = false;
    String schemaText = null;// w  w w. jav a 2  s. c  o m
    String jsonText = "{\"name\": \"307\", \"model\": \"Peugeot 307\", \"year\": 2003}";

    try {
        // Load schema from file.
        schemaText = FileUtils
                .readFileToString(new File("target/test-classes/io/github/microcks/util/car-schema.json"));
        // Validate Json according schema.
        valid = JsonSchemaValidator.isJsonValid(schemaText, jsonText);
    } catch (Exception e) {
        fail("Exception should not be thrown");
    }

    // Assert Json object is valid.
    assertTrue(valid);
}

From source file:com.act.biointerpretation.metadata.Genus.java

public static Map<String, Genus> parseGenuses() throws Exception {
    // Import genus classifications for 'cloned'
    Map<String, Genus> nameToGenus = new HashMap<>();

    // TODO: Move this to resources directory?
    File termfile = new File("data/ProteinMetadata/2016_12_07-cloned_genus_wikipedia_classification.txt");
    String data = FileUtils.readFileToString(termfile);
    String[] regions = data.split(">");

    //Parse each organism's Genus info
    for (String region : regions) {
        if (region.trim().isEmpty()) {
            continue;
        }//w w w.java 2 s .c  o  m
        String[] lines = region.split("\\r|\\r?\\n");

        //Parse first line
        String firstline = lines[0];
        String organism = firstline.substring(1);

        //Initialize data to parse
        String Domain = null;
        String Kingdom = null;
        String Phylum = null;
        String CClass = null;
        String Order = null;
        String Family = null;
        String Cgenus = null;

        //Parse remaining line
        for (int i = 1; i < lines.length; i++) {
            String line = lines[i];
            if (line.isEmpty()) {
                continue;
            }
            String[] tabs = line.split("[:\t]");
            if (tabs[0].equals("Domain")) {
                Domain = tabs[2];
            } else if (tabs[0].equals("Kingdom")) {
                Kingdom = tabs[2];
            } else if (tabs[0].equals("Phylum")) {
                Phylum = tabs[2];
            } else if (tabs[0].equals("Class")) {
                CClass = tabs[2];
            } else if (tabs[0].equals("Order")) {
                Order = tabs[2];
            } else if (tabs[0].equals("Family")) {
                Family = tabs[2];
            } else if (tabs[0].equals("Genus")) {
                Cgenus = tabs[2];
            }
        }

        if (Domain == null) {
            if (Kingdom.equals("Animalia")) {
                Domain = "Eukaryota";
            } else if (Kingdom.equals("Bacteria")) {
                Domain = "Bacteria";
            } else if (Kingdom.equals("Fungi")) {
                Domain = "Eukaryota";
            } else if (Kingdom.equals("Plantae")) {
                Domain = "Eukaryota";
            }
        }

        //Construct and store the Genus
        Genus genus = new Genus(Domain, Kingdom, Phylum, CClass, Order, Family, Cgenus);
        nameToGenus.put(organism, genus);
    }

    return nameToGenus;
}

From source file:net.duckling.falcon.api.mstatic.VersionStartupListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServletContext context = sce.getServletContext();
    String versionFile = context.getInitParameter("version-file");
    String fullpath = context.getRealPath(versionFile);
    try {/*from ww  w .  j  a  v  a 2  s  .co  m*/
        String version = FileUtils.readFileToString(new File(fullpath));
        if (version != null) {
            version = version.trim();
        } else {
            version = "N/A";
        }
        VersionAttributeHelper.setVersion(context, version);
    } catch (IOException e) {
        log.error("Reading version file failed.", e);
    }
}