Example usage for org.apache.commons.io.input ReaderInputStream ReaderInputStream

List of usage examples for org.apache.commons.io.input ReaderInputStream ReaderInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.input ReaderInputStream ReaderInputStream.

Prototype

public ReaderInputStream(Reader reader) 

Source Link

Document

Construct a new ReaderInputStream that uses the default character encoding with a default input buffer size of 1024 characters.

Usage

From source file:org.eclipse.sw360.licenseinfo.parsers.CLIParserTest.java

@Test
public void testIsApplicableToFailsOnMalformedXML() throws Exception {
    AttachmentContent content = new AttachmentContent().setId("A1").setFilename("a.xml")
            .setContentType("application/xml");
    when(connector.getAttachmentStream(eq(content), anyObject(), anyObject()))
            .thenReturn(new ReaderInputStream(new StringReader("this is not an xml file")));
    assertFalse(parser.isApplicableTo(attachment, new User(), new Project()));
}

From source file:org.eclipse.sw360.licenseinfo.parsers.CLIParserTest.java

@Test
public void testGetCLI() throws Exception {
    Attachment cliAttachment = new Attachment("A1", "a.xml");
    when(connector.getAttachmentStream(anyObject(), anyObject(), anyObject()))
            .thenReturn(new ReaderInputStream(new StringReader(CLI_TESTFILE)));
    LicenseInfoParsingResult res = parser.getLicenseInfos(cliAttachment, new User(), new Project()).stream()
            .findFirst()//from  ww  w .ja  v a  2s  . co  m
            .orElseThrow(() -> new RuntimeException("Parser returned empty LisenceInfoParsingResult list"));
    assertLicenseInfoParsingResult(res);
    assertThat(res.getStatus(), is(LicenseInfoRequestStatus.SUCCESS));
    assertThat(res.getLicenseInfo(), notNullValue());
    assertThat(res.getLicenseInfo().getFilenames(), contains("a.xml"));
    assertThat(res.getLicenseInfo().getLicenseNamesWithTexts().size(), is(1));
    assertThat(
            res.getLicenseInfo().getLicenseNamesWithTexts().stream().map(LicenseNameWithText::getLicenseText)
                    .collect(Collectors.toSet()),
            containsInAnyOrder("jQuery projects are released under the terms of the MIT license."));
    assertThat(res.getLicenseInfo().getCopyrights().size(), is(2));
    assertThat(res.getLicenseInfo().getCopyrights(),
            containsInAnyOrder("Copyrights", "(c) jQuery Foundation, Inc. | jquery.org"));

}

From source file:org.eclipse.sw360.licenseinfo.parsers.CLIParserTest.java

@Test
public void testGetCLIFailsOnMalformedXML() throws Exception {
    Attachment cliAttachment = new Attachment("A1", "a.xml");
    when(connector.getAttachmentStream(anyObject(), anyObject(), anyObject())).thenReturn(
            new ReaderInputStream(new StringReader(CLI_TESTFILE.replaceAll("</Content>", "</Broken>"))));
    LicenseInfoParsingResult res = parser.getLicenseInfos(cliAttachment, new User(), new Project()).stream()
            .findFirst()/*from w w w  .java2 s.  c  om*/
            .orElseThrow(() -> new RuntimeException("Parser returned empty LisenceInfoParsingResult list"));
    assertLicenseInfoParsingResult(res, LicenseInfoRequestStatus.FAILURE);
    assertThat(res.getStatus(), is(LicenseInfoRequestStatus.FAILURE));
    assertThat(res.getLicenseInfo(), notNullValue());
    assertThat(res.getLicenseInfo().getFilenames(), contains("a.xml"));

}

From source file:org.eclipse.sw360.licenseinfo.parsers.CombinedCLIParserTest.java

@Test
public void testIsApplicableToFailsOnIncorrectRootElement() throws Exception {
    AttachmentContent content = new AttachmentContent().setId("A1").setFilename("a.xml")
            .setContentType("application/xml");
    when(connector.getAttachmentStream(content, new User(), new Project()))
            .thenReturn(new ReaderInputStream(new StringReader("<wrong-root/>")));
    assertFalse(parser.isApplicableTo(attachment, new User(), new Project()));
}

From source file:org.eclipse.sw360.licenseinfo.parsers.CombinedCLIParserTest.java

@Test
public void testIsApplicableToFailsOnMalformedXML() throws Exception {
    AttachmentContent content = new AttachmentContent().setId("A1").setFilename("a.xml")
            .setContentType("application/xml");
    when(connector.getAttachmentStream(content, new User(), new Project()))
            .thenReturn(new ReaderInputStream(new StringReader("this is not an xml file")));
    assertFalse(parser.isApplicableTo(attachment, new User(), new Project()));
}

From source file:org.eclipse.sw360.licenseinfo.parsers.CombinedCLIParserTest.java

@Test
public void testGetCLI() throws Exception {
    Attachment cliAttachment = new Attachment("A1", "a.xml");
    when(connector.getAttachmentStream(anyObject(), anyObject(), anyObject()))
            .thenReturn(new ReaderInputStream(new StringReader(cliTestfile)));
    List<LicenseInfoParsingResult> results = parser.getLicenseInfos(cliAttachment, new User(), new Project());
    assertThat(results.size(), is(1));//from  w ww .  ja v a 2 s .  co m
    LicenseInfoParsingResult res = results.get(0);
    assertLicenseInfoParsingResult(res);
    assertThat(res.getLicenseInfo().getFilenames(), containsInAnyOrder("a.xml"));
    assertThat(res.getLicenseInfo().getLicenseNamesWithTexts().size(), is(3));
    assertThat(
            res.getLicenseInfo().getLicenseNamesWithTexts().stream().map(LicenseNameWithText::getLicenseText)
                    .collect(Collectors.toSet()),
            containsInAnyOrder("License1Text", "License2Text", "License3&'Text"));
    LicenseNameWithText l2 = res.getLicenseInfo().getLicenseNamesWithTexts().stream()
            .filter(l -> l.getLicenseName().equals("License2")).findFirst().orElseThrow(AssertionError::new);
    assertThat(l2.getAcknowledgements(), is("License2Acknowledgements"));
    assertThat(res.getLicenseInfo().getCopyrights().size(), is(5));
    assertThat(res.getLicenseInfo().getCopyrights(),
            containsInAnyOrder("Copyright1", "Copyright2", "Copyright3", "Copyright4", "Copyright5"));
    assertThat(res.getVendor(), is("VendorA"));
    assertThat(res.getName(), is("r1"));
    assertThat(res.getVersion(), is("1.0"));
}

From source file:org.fit.cssbox.swingbox.SwingBoxEditorKit.java

@Override
public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException {

    if (doc instanceof org.fit.cssbox.swingbox.SwingBoxDocument) {
        InputStream is = new ReaderInputStream(in);
        readImpl(is, (org.fit.cssbox.swingbox.SwingBoxDocument) doc, pos);
    } else {/*  w  w w . jav a 2  s . c  om*/
        super.read(in, doc, pos);
    }
}

From source file:org.gbif.pubindex.service.impl.ArticleIndexerImplTest.java

@Test
@Ignore/*from   w w  w  .  j  a v a 2s.  c  o m*/
public void testTextExtraction() {
    ArticleIndexerImpl ai = new ArticleIndexerImpl(new PubindexConfig(), null, null, null, null);
    Article a = new Article();
    a.setId(1);

    // needs to be html/xml/rdf
    assertNull(ai.extractText(a, new ReaderInputStream(new StringReader(" Hallo  my dear"))));

    assertEquals(" Hallo  my dear", ai.extractText(a,
            new ReaderInputStream(new StringReader("<html><body> Hallo  my dear</body></html>"))));

    StringBuilder text = new StringBuilder();
    text.append("<p>");
    text.append('\u0000');
    text.append("Carla");
    text.append("</p>");
    assertEquals("Carla", ai.extractText(a, new ReaderInputStream(new StringReader(text.toString()))));

    text.append("<p>");
    text.append('\u003d');
    text.append('\u0000');
    text.append("</p>");
    assertEquals("Carla=", ai.extractText(a, new ReaderInputStream(new StringReader(text.toString()))));
}

From source file:org.grails.plugin.platform.events.push.GrailsWebsocketProtocol.java

@Override
public List<AtmosphereRequest> onTextStream(WebSocket webSocket, Reader r) {
    //Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string.
    AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
    if (resource == null) {
        logger.trace("The WebSocket has been closed before the message was processed.");
        return null;
    }//  w w w  .  j  av  a 2  s .c  om

    AtmosphereRequest request = resource.getRequest();
    List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
    list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType,
            destroyable).inputStream(new ReaderInputStream(r)).build());

    return list;
}

From source file:org.icesquirrel.jsr223.IcesquirrelScriptEngine.java

@Override
public Object eval(Reader reader, ScriptContext context) throws ScriptException {

    SquirrelInterpretedScript sis = new SquirrelInterpretedScript();

    /*//from  w  w  w. j a v a  2 s  .c o  m
     * If the global bindings is already a SquirrelTable, use that directly.
     * A squirrel table is kind of compatible with a bindings object, except
     * the key may be an Object instead of a string.
     */
    Bindings bindings = context.getBindings(ScriptContext.GLOBAL_SCOPE);
    if (bindings instanceof BindingsAdapter) {
        sis.setRootTable(((BindingsAdapter) bindings).getTable());
    } else {
        // Otherwise copy global bindings into our root table
        for (Entry<String, Object> s : bindings.entrySet()) {
            bind(sis, s);
        }
    }

    // Now the engine scoped bindings. If these are already a squirrel
    // table, add them as
    // a delegate, otherwise copy
    Bindings engBindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    SquirrelTable addedDelegate = null;
    if (engBindings instanceof BindingsAdapter) {
        BindingsAdapter ba = (BindingsAdapter) engBindings;
        if (ba.getTable() != sis.getRootTable()) {
            SquirrelTable pt = sis.setRootTable(ba.getTable());
            if (pt != null && !ba.getTable().getDelegates().contains(pt)) {
                ba.getTable().addDelegate(pt);
                addedDelegate = pt;
            }
        }
    } else {
        for (Entry<String, Object> s : engBindings.entrySet()) {
            bind(sis, s);
        }
    }

    SquirrelRuntime runtime = SquirrelRuntime.getDefaultRuntime();
    SquirrelExecutionContext ctx = null;
    if (!SquirrelExecutionContext.is()) {
        ctx = new SquirrelExecutionContext();
        ctx.start(sis.getRootTable(), runtime, new SquirrelInterpretedProcessor());
    }

    try {
        try {
            // TODO encoding
            sis.execute(new ReaderInputStream(reader));
        } catch (SquirrelException se) {
            // TODO line number etc
            throw new ScriptException(se);
        } catch (Exception e) {
            // TODO line number etc
            throw new ScriptException(e);
        }
        Object result = sis.getResult();
        if (result instanceof SquirrelObjectWrapper) {
            return ((SquirrelObjectWrapper) result).getObject();
        }
        return result;
    } finally {
        if (addedDelegate != null) {
            sis.getRootTable().removeDelegate(addedDelegate);
        }
        if (ctx != null) {
            ctx.stop();
        }
    }
}