Example usage for org.springframework.util FileCopyUtils copyToString

List of usage examples for org.springframework.util FileCopyUtils copyToString

Introduction

In this page you can find the example usage for org.springframework.util FileCopyUtils copyToString.

Prototype

public static String copyToString(@Nullable Reader in) throws IOException 

Source Link

Document

Copy the contents of the given Reader into a String.

Usage

From source file:com.googlecode.flyway.core.util.ResourceUtils.java

/**
 * Loads this resource in a string using this encoding.
 *
 * @param resource The resource to load.
 * @param encoding The encoding of the resource.
 * @return The resource contents as a string.
 *///from  ww w .  j a v a  2  s .c o m
public static String loadResourceAsString(Resource resource, String encoding) {
    try {
        Reader reader = new InputStreamReader(resource.getInputStream(), Charset.forName(encoding));
        return FileCopyUtils.copyToString(reader);
    } catch (IOException e) {
        throw new IllegalStateException(
                "Unable to load resource: " + resource.getDescription() + " (encoding: " + encoding + ")", e);
    }
}

From source file:org.springsource.sinspctr.BannerUtils.java

private static String readBanner() {
    try {//from   www  .ja v  a 2  s  .  c om
        return FileCopyUtils
                .copyToString(new InputStreamReader(
                        BannerUtils.class.getClassLoader().getResourceAsStream("banner.txt")))
                .replaceAll("(\\r|\\n)+", LINE_SEPARATOR);
    } catch (IOException e) {
        throw new IllegalStateException("Could not read banner.txt");
    }
}

From source file:io.fabric8.maven.sample.enricher.app.HelloController.java

@RequestMapping("/")
public String index() throws IOException {
    return FileCopyUtils.copyToString(new FileReader("/data/mySecretFileData")) + "<br>\n"
            + System.getenv("SECRET_PROPERTY");
}

From source file:io.fabric8.camel.tooling.util.StandAloneRoutesXmlMarshalToTextTest.java

@Test
public void testMarshalToText() throws Exception {
    String text = FileCopyUtils
            .copyToString(new FileReader(new File(getBaseDir(), "src/test/resources/routes.xml")));

    RouteDefinition route = new RouteDefinition();
    route.from("seda:new.in").to("seda:new.out");

    String actual = tool.marshalToText(text, Arrays.asList(route));
    System.out.println("Got " + actual);

    assertTrue("Missing seda:new.in for: " + actual, actual.contains("seda:new.in"));
}

From source file:com.googlecode.flyway.core.dbsupport.oracle.OracleSqlScriptSmallTest.java

@Test
public void parseSqlStatements() throws Exception {
    String source = FileCopyUtils.copyToString(new InputStreamReader(
            new ClassPathResource("migration/dbsupport/oracle/sql/placeholders/V1.sql").getInputStream(),
            Charset.forName("UTF-8")));

    OracleSqlScript sqlScript = new OracleSqlScript(source, PlaceholderReplacer.NO_PLACEHOLDERS);
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(3, sqlStatements.size());
    assertEquals(18, sqlStatements.get(0).getLineNumber());
    assertEquals(27, sqlStatements.get(1).getLineNumber());
    assertEquals(32, sqlStatements.get(2).getLineNumber());
    assertEquals("COMMIT", sqlStatements.get(2).getSql());
}

From source file:io.fabric8.camel.tooling.util.NamespacePreserveTest.java

@Test
public void testUpdateAnXmlAndKeepNamespaces() throws Exception {
    File file = new File(getBaseDir(), "src/test/resources/namespaceRoute.xml");
    XmlModel xm = assertRoutes(file, 1, null);

    // now lets modify the xml
    List<RouteDefinition> definitionList = xm.getRouteDefinitionList();
    RouteDefinition route = new RouteDefinition().from("file:foo").to("file:bar");
    definitionList.add(route);/*from  w w  w .j  av  a 2 s .  c  o  m*/

    System.out.println("Routes now: " + xm.getRouteDefinitionList());

    String text = FileCopyUtils.copyToString(new FileReader(file));

    RouteXml helper = new RouteXml();
    String newText = helper.marshalToText(text, definitionList);

    System.out.println("newText: " + newText);

    assertTrue("Generated XML has missing XML namespace declaration " + "http://acme.com/foo",
            newText.contains("http://acme.com/foo"));
    assertTrue("Generated XML has missing XML namespace declaration " + "urn:barNamespace",
            newText.contains("urn:barNamespace"));
}

From source file:io.fabric8.camel.tooling.util.CamelPrefixOnRootElementTest.java

@Test
public void testUpdateAnXmlWhichUsesCamelPrefixOnRootElement() throws Exception {
    String name = "src/test/resources/camelPrefixOnRoot.xml";
    File file = new File(getBaseDir(), name);
    XmlModel x = assertRoutes(file, 1, null);

    // now lets modify the xml
    List<RouteDefinition> definitionList = x.getRouteDefinitionList();
    RouteDefinition route = new RouteDefinition().from("file:foo").to("file:bar");
    definitionList.add(route);/*from w  w w.ja va2  s.  c o  m*/

    System.out.println("Routes now: " + x.getRouteDefinitionList());

    String text = FileCopyUtils.copyToString(new FileReader(file));

    RouteXml helper = new RouteXml();
    String newText = helper.marshalToText(text, definitionList);

    System.out.println("newText: " + newText);

    assertTrue(newText.contains("Configures the Camel Context"));

    assertValid(x);
}

From source file:io.fabric8.camel.tooling.util.CamelPrefixFromSpringArchetypeTest.java

@Test
public void testUpdateAnXmlWhichUsesCamelPrefixOnRootElement() throws Exception {
    String name = "src/test/resources/springArchetypeWithRootCamelPrefix.xml";
    File file = new File(getBaseDir(), name);
    XmlModel x = assertRoutes(file, 1, null);

    // now lets modify the xml
    List<RouteDefinition> definitionList = x.getRouteDefinitionList();
    RouteDefinition route = new RouteDefinition().from("file:foo").to("file:bar");
    definitionList.add(route);/*from ww w . ja  va2s. c  o m*/

    System.out.println("Routes now: " + x.getRouteDefinitionList());

    String text = FileCopyUtils.copyToString(new FileReader(file));

    RouteXml helper = new RouteXml();
    String newText = helper.marshalToText(text, definitionList);

    System.out.println("newText: " + newText);

    assertTrue(newText.contains("Configures the Camel Context"));

    assertValid(x);
}

From source file:io.bosh.client.AbstractDirectorTest.java

protected String payload(String filename) {
    ClassPathResource resource = new ClassPathResource(filename);
    try {/*from   ww  w  .ja  va2  s .  co  m*/
        return FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
    } catch (IOException e) {
        throw new RuntimeException("TEST SETUP ERROR: unable to find test resources file with name " + filename,
                e);
    }

}

From source file:org.cloudfoundry.maven.common.UiUtilsTest.java

@Test
public void testRenderTextTable() {
    final List<String> services = new ArrayList<String>();

    services.add("mysql");
    services.add("MyMongoInstance");

    final List<String> uris = new ArrayList<String>();

    uris.add("cf-rocks.api.run.pivotal.io");
    uris.add("spring-rocks.api.run.pivotal.io");

    String expectedTableAsString = null;

    try {/*from  www.  j av  a2  s  .  com*/
        expectedTableAsString = FileCopyUtils.copyToString(new InputStreamReader(
                UiUtilsTest.class.getResourceAsStream("testRenderTextTable-expected-output.txt")));
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail();
    }

    Assert.assertNotNull(expectedTableAsString);

    final CloudApplication app1 = new CloudApplication("first", "command", "buildpack", 512, 1, uris, services,
            AppState.STARTED);
    final CloudApplication app2 = new CloudApplication("second", "command", "buildpack", 1024, 2, uris,
            services, AppState.STOPPED);

    final List<CloudApplication> applications = Arrays.asList(app1, app2);

    final String renderedTableAsString = UiUtils.renderCloudApplicationsDataAsTable(applications);

    LOGGER.info("\n" + renderedTableAsString);

    Assert.assertEquals(expectedTableAsString, renderedTableAsString);
}