public void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect()
throws IOException {
String initialString = "text";
InputStream targetStream = IOUtils.toInputStream(initialString);
}
public void testCharSequenceToInputStream() throws Exception {
CharSequence csq = new StringBuilder("Abc123Xyz!");
InputStream inStream = IOUtils.toInputStream(csq);
byte[] bytes = IOUtils.toByteArray(inStream);
assertEqualContent(csq.toString().getBytes(), bytes);
inStream = IOUtils.toInputStream(csq, null);
bytes = IOUtils.toByteArray(inStream);
assertEqualContent(csq.toString().getBytes(), bytes);
inStream = IOUtils.toInputStream(csq, "UTF-8");
bytes = IOUtils.toByteArray(inStream);
assertEqualContent(csq.toString().getBytes("UTF-8"), bytes);
}
private static InputStream convertStringToInputStreamCommonIO(String name) {
InputStream result = IOUtils.toInputStream(name, StandardCharsets.UTF_8);
return result;
}
String source = "This is the source of my input stream";
InputStream in = org.apache.commons.io.IOUtils.toInputStream(source, "UTF-8");
public class StringToIOApache {
public static void main(String[] args) throws IOException {
String inputString = "This is a String to demo as how to convert it to input stream using Apache Commons IO";
InputStream inputStream = IOUtils.toInputStream(inputString, StandardCharsets.UTF_8);
}
}
public void mapper_boundWithMultipleNamedAndOneUnnamedInputStreams_shouldReadCorrespondingStreams() {
String streamName = "foo";
String secondStreamName = "bar";
stream = new CarmlStream(streamName);
InputStream inputStream = IOUtils.toInputStream(input);
InputStream secondInputStream = IOUtils.toInputStream(secondInput);
String unnamedInput = "unnamed test input";
InputStream unnamedInputStream = IOUtils.toInputStream(unnamedInput);
mapper.bindInputStream(streamName, inputStream);
mapper.bindInputStream(secondStreamName, secondInputStream);
mapper.bindInputStream(unnamedInputStream);
assertThat(mapper.getSourceManager().getSource(streamName), is(input));
assertThat(mapper.getSourceManager().getSource(secondStreamName), is(secondInput));
assertThat(mapper.getSourceManager().getSource(RmlMapper.DEFAULT_STREAM_NAME), is(unnamedInput));
}
public void run_on_valid_response() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
RequestContext context = RequestContext.getCurrentContext();
context.setRequest(request);
MockHttpServletResponse response = new MockHttpServletResponse();
context.setResponseGZipped(false);
context.setResponse(response);
InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
context.setResponseDataStream(in);
filter.run();
assertEquals("UTF-8", response.getCharacterEncoding());
assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
public void testGetVersion_WithMockedManifest_ReturnsValidVersion() throws IOException {
// Arrange.
PowerMockito.mockStatic(VersionUtil.class);
PowerMockito.when(VersionUtil.getVersion()).thenCallRealMethod();
PowerMockito.when(VersionUtil.class.getPackage()).thenReturn(this.aPackage);
PowerMockito.when(VersionUtil.class.getClassLoader()).thenReturn(this.classLoader);
PowerMockito.when(this.classLoader.findResource(Matchers.anyString())).thenReturn(this.mockedUrl);
InputStream stubInputStream = IOUtils.toInputStream(this.MANIFEST_CONTENT, Charset.defaultCharset());
PowerMockito.when(this.mockedUrl.openStream()).thenReturn(stubInputStream);
PowerMockito.when(this.aPackage.getImplementationVersion()).thenReturn("1.0.0");
// Act.
Version version = VersionUtil.getVersion();
// Assert.
Assert.assertTrue("11" == version.getBuild());
Assert.assertEquals("1476885882", version.getBuildTime());
Assert.assertTrue(1 == version.getMajor());
Assert.assertTrue(2 == version.getMinor());
}
public void sync() throws IOException {
InputStream csv = IOUtils.toInputStream(sgc2csv(null).toString(), ENCODING_P2S);
AppliConfig appliConfig = AppliConfig.findAppliConfigByKey("P2S_EXPORT_CSV_FILE_NAME");
String filename = appliConfig.getValue();
Date date = new Date();
p2sVfsAccessService.putFile(null, date.getTime() + "_" + filename, csv, false);
}
public void readSuccessful() throws IOException {
String path = "file";
File file = new File(sourceDir, path);
InputStream inputStream = IOUtils.toInputStream(fileContent, "UTF-8");
FileUtils.copyInputStreamToFile(inputStream, file);
String result = access.read(path);
assertNotNull(result);
assertEquals(fileContent, result);
}