Example usage for org.apache.commons.io IOUtils toInputStream

List of usage examples for org.apache.commons.io IOUtils toInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils toInputStream.

Prototype

public static InputStream toInputStream(String input) 

Source Link

Document

Convert the specified string to an input stream, encoded as bytes using the default character encoding of the platform.

Usage

From source file:com.elsevier.spark_xml_utils.xslt.S3URIResolver.java

/**
 * Return the requested stylesheet. If the stylesheet hasn't been cached,
 * then save the stylesheet to the cache. The assumption (although not
 * required) is that the imported/included stylesheets will be stored in an
 * S3 bucket and accessible using an S3 url.
 * //  w  ww .  ja va 2  s  .  co m
 * @param href url for the stylesheet
 * @param base not used (assuming absolute urls)
 */
public Source resolve(String href, String base) throws TransformerException {

    try {

        // Check local cache
        if (stylesheetMap.containsKey(href)) {

            return new StreamSource(IOUtils.toInputStream(stylesheetMap.get(href)));

        } else {

            // Read the data from the URL and populate the cache
            URL theUrl = new URL(href);
            stylesheetMap.put(href, IOUtils.toString(theUrl.openStream()));

            // Return a StreamSource
            return new StreamSource(IOUtils.toInputStream(stylesheetMap.get(href)));

        }

    } catch (IOException e) {

        log.error("Problems resolving a stylesheet. URI:" + href + " " + e.getMessage(), e);
        throw new TransformerException(e.getMessage());

    }

}

From source file:com.elsevier.xml.S3URIResolver.java

/**
 * Return the requested stylesheet. If the stylesheet hasn't been cached,
 * then save the stylesheet to the cache. The assumption (although not
 * required) is that the imported/included stylesheets will be stored in an
 * S3 bucket and accessible using an S3 url.
 * /*from  w ww  .  j  av  a 2 s .  c  om*/
 * @param href url for the stylesheet
 * @param base not used (assuming absolute urls)
 */
public Source resolve(String href, String base) throws TransformerException {

    try {
        // Check local cache
        if (stylesheetMap.containsKey(href)) {
            return new StreamSource(IOUtils.toInputStream(stylesheetMap.get(href)));
        } else {

            // Read the data from the URL and populate the cache
            URL theUrl = new URL(href);
            synchronized (this) {
                stylesheetMap.put(href, IOUtils.toString(theUrl.openStream()));
            }
            // Return a StreamSource
            return new StreamSource(IOUtils.toInputStream(stylesheetMap.get(href)));
        }

    } catch (IOException e) {

        log.error("Problems resolving a stylesheet. URI:" + href + " " + e.getMessage(), e);
        throw new TransformerException(e.getMessage());

    }
}

From source file:com.allogy.couch.importers.command.BufferedCouchImporterTest.java

private ImportCommand createImportCommand(long size) {
    String id = UUID.randomUUID().toString();
    InputStream dataInputStream = IOUtils.toInputStream(UUID.randomUUID().toString());

    ImportCommand importCommand = mock(ImportCommand.class);
    stub(importCommand.getTargetCouchDbConnector()).toReturn(targetCouchDbConnector);
    stub(importCommand.getId()).toReturn(id);
    stub(importCommand.getDataStream()).toReturn(dataInputStream);
    stub(importCommand.getSize()).toReturn(size);

    return importCommand;
}

From source file:mitm.common.util.NameValueLineIteratorTest.java

@Test
public void testNameValueLineIterator() throws IOException {
    String data = "test=123\n test =   456  \n  test=\ntest2";

    NameValueLineIterator.Entry entry;//from  ww w.j  a v a2 s.c  o m

    Iterator<NameValueLineIterator.Entry> it = new NameValueLineIterator(IOUtils.toInputStream(data));

    assertTrue(it.hasNext());
    entry = it.next();
    assertEquals("test", entry.getName());
    assertEquals("123", entry.getValue());

    assertTrue(it.hasNext());
    entry = it.next();
    assertEquals("test", entry.getName());
    assertEquals("456", entry.getValue());

    assertTrue(it.hasNext());
    entry = it.next();
    assertEquals("test", entry.getName());
    assertEquals("", entry.getValue());

    assertFalse(it.hasNext());
}

From source file:com.ngdata.hbaseindexer.conf.XmlIndexerConfWriterTest.java

@Test
public void testWrite() throws Exception {
    Map<String, String> params = Maps.newHashMap();
    params.put("thename", "thevalue");
    IndexerConf conf = new IndexerConfBuilder().table("the-table").mappingType(IndexerConf.MappingType.COLUMN)
            .rowReadMode(IndexerConf.RowReadMode.DYNAMIC).uniqueyKeyField("keyfield").rowField("rf")
            .columnFamilyField("cf-field").tableNameField("tn-field").globalParams(params)
            .mapperClass(DefaultResultToSolrMapper.class)
            .uniqueKeyFormatterClass(StringUniqueKeyFormatter.class)
            .addFieldDefinition("fieldname", "fieldvalue", FieldDefinition.ValueSource.VALUE, "fieldtype",
                    params)/*from   w w w. j a  v a  2s.  c  o m*/
            .addDocumentExtractDefinition("theprefix", "valueexpr", FieldDefinition.ValueSource.VALUE,
                    "deftype", params)
            .build();

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    XmlIndexerConfWriter.writeConf(conf, os);

    String xmlString = os.toString();

    IndexerConf conf2 = null;
    try {
        IndexerComponentFactory factory = IndexerComponentFactoryUtil.getComponentFactory(
                DefaultIndexerComponentFactory.class.getName(), IOUtils.toInputStream(xmlString),
                Maps.<String, String>newHashMap());
        conf2 = factory.createIndexerConf();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Xml is not valid");
    }

    Assert.assertEquals(conf.getTable(), conf2.getTable());
    Assert.assertEquals(conf.getMappingType(), conf2.getMappingType());
    Assert.assertEquals(conf.getRowReadMode(), conf2.getRowReadMode());
    Assert.assertEquals(conf.getUniqueKeyField(), conf2.getUniqueKeyField());
    Assert.assertEquals(conf.getRowField(), conf2.getRowField());
    Assert.assertEquals(conf.getColumnFamilyField(), conf2.getColumnFamilyField());
    Assert.assertEquals(conf.getTableNameField(), conf2.getTableNameField());
    Assert.assertEquals(conf.getGlobalParams(), conf2.getGlobalParams());
    Assert.assertEquals(conf.getMapperClass(), conf2.getMapperClass());
    Assert.assertEquals(conf.getUniqueKeyFormatterClass(), conf2.getUniqueKeyFormatterClass());
    Assert.assertEquals(conf.getFieldDefinitions().size(), conf2.getFieldDefinitions().size());
    Assert.assertEquals(conf.getDocumentExtractDefinitions().size(),
            conf2.getDocumentExtractDefinitions().size());
}

From source file:gobblin.source.extractor.filebased.FileBasedExtractorTest.java

public void testReadRecordWithEmptyFiles() throws DataRecordException, IOException, FileBasedHelperException {
    String file1 = "file1.txt";
    String file2 = "file2.txt";
    String file3 = "file3.txt";

    WorkUnitState state = new WorkUnitState();
    state.setProp(ConfigurationKeys.SOURCE_FILEBASED_FILES_TO_PULL, Joiner.on(",").join(file1, file2, file3));

    FileBasedHelper fsHelper = Mockito.mock(FileBasedHelper.class);
    Mockito.when(fsHelper.getFileStream(file1)).thenReturn(IOUtils.toInputStream(""));
    Mockito.when(fsHelper.getFileStream(file2)).thenReturn(IOUtils.toInputStream(""));
    Mockito.when(fsHelper.getFileStream(file3)).thenReturn(IOUtils.toInputStream(""));

    FileBasedExtractor<String, String> extractor = new DummyFileBasedExtractor<String, String>(state, fsHelper);

    Assert.assertEquals(getNumRecords(extractor), 0);
}

From source file:com.collective.celos.ci.testing.fixtures.convert.JsonExpandConverterTest.java

@Test
public void testJsonExpandConverter() throws Exception {
    JsonExpandConverter converter = new JsonExpandConverter(Sets.newHashSet("change"));
    String jsonExample = "{\"id\":\"134f50faa804d30\",\"change\":\"{\\\"daystamp\\\":\\\"20140901\\\",\\\"context\\\":\\\"none\\\"}\",\"origin\":\"dc\"}\n"
            + "{\"id\":\"134f50faa804d31\",\"change\":\"{\\\"daystamp\\\":\\\"20140902\\\",\\\"context\\\":\\\"none\\\"}\",\"origin\":\"dc\"}";

    InputStream inputStream = IOUtils.toInputStream(jsonExample);
    FixFile expanded = converter.convert(null, new FixFile(inputStream));
    String expandedStr = IOUtils.toString(expanded.getContent());

    String expectedStr = "{\"id\":\"134f50faa804d30\",\"change\":{\"daystamp\":\"20140901\",\"context\":\"none\"},\"origin\":\"dc\"}\n"
            + "{\"id\":\"134f50faa804d31\",\"change\":{\"daystamp\":\"20140902\",\"context\":\"none\"},\"origin\":\"dc\"}";

    Map<JsonElement, Integer> expected = Utils.fillMapWithJsonFromIS(IOUtils.toInputStream(expectedStr));
    Map<JsonElement, Integer> result = Utils.fillMapWithJsonFromIS(IOUtils.toInputStream(expandedStr));
    Assert.assertEquals(expected, result);

}

From source file:io.dfox.junit.http.example.ExampleTest.java

@Fixture
public void createNote() throws IOException {
    JsonNode notesFixture = getTestData("notes.json");
    JsonNode noteFixture = notesFixture.path("fixture");
    String name = noteFixture.path("name").asText();
    String contents = noteFixture.path("contents").asText();

    repository.saveNote(name, IOUtils.toInputStream(contents));
}

From source file:edu.northwestern.bioinformatics.studycalendar.xml.StudyCalendarXmlSerializerTest.java

public void testReadDocumentThrowsXmlExceptionForPoorlyFormedXml() throws Exception {
    try {/*from  w ww .  ja v  a 2s.  co m*/
        serializer.readDocument(IOUtils.toInputStream("<map foo/>"));
        fail("Exception not thrown");
    } catch (StudyCalendarXmlParsingException scxe) {
        System.out.println(scxe);
        assertContains(scxe.getMessage(), "foo");
    }
}

From source file:com.norconex.importer.handler.transformer.impl.ReplaceTransformerTest.java

@Test
public void testWriteRead() throws IOException {
    ReplaceTransformer t = new ReplaceTransformer();
    Reader reader = new InputStreamReader(IOUtils.toInputStream(xml));
    t.loadFromXML(reader);/*from   www  .  j a  va  2  s . com*/
    reader.close();
    System.out.println("Writing/Reading this: " + t);
    ConfigurationUtil.assertWriteRead(t);
}