use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class BytesInputOutputBufferTest method getContentViaInputStream.
@Test(description = "Get content via InputStream")
public void getContentViaInputStream() throws IOException, URISyntaxException {
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
Channel channel = new MockByteChannel(byteChannel, 0);
final InputStream inputStream = channel.getInputStream();
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream))) {
final String result = buffer.lines().collect(Collectors.joining("\n"));
Assert.assertEquals(result, "123456", "Did not get expected string output.");
}
inputStream.close();
}
use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class BytesInputOutputBufferTest method requestForExcessBytes.
@Test(description = "Request for bytes more exceeding the available limit")
public void requestForExcessBytes() throws IOException, URISyntaxException {
int requestedLimit = 10;
int expectedLimit = 6;
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
Channel channel = new MockByteChannel(byteChannel);
int numberOfBytes = read(requestedLimit, channel).getNumberOfBytesRead();
channel.close();
Assert.assertEquals(numberOfBytes, expectedLimit);
}
use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class BytesInputOutputBufferTest method writeBytesToFile.
@Test(description = "Write bytes to file")
public void writeBytesToFile() throws IOException {
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "write.txt");
Channel channel = new MockByteChannel(byteChannel);
byte[] bytes = "hello".getBytes();
int numberOfBytesWritten = writeFull(bytes, bytes.length, channel);
Assert.assertEquals(numberOfBytesWritten, bytes.length);
}
use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class AsyncReadWriteTest method writeCharacters.
@Test(description = "Test writing characters through async io framework")
public void writeCharacters() throws IOException, ExecutionException, InterruptedException {
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "write.txt");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
String text = "HelloNJ";
int numberOfBytes = text.getBytes().length;
WriteCharactersEvent event = new WriteCharactersEvent(characterChannel, text, 0);
Future<EventResult> future = eventManager.publish(event);
EventResult eventResult = future.get();
int numberOfCharactersWritten = (int) eventResult.getResponse();
Assert.assertEquals(numberOfCharactersWritten, numberOfBytes);
characterChannel.close();
}
use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class AsyncReadWriteTest method readCharacters.
@Test(description = "Tests reading characters through the async io framework")
public void readCharacters() throws Exception {
int numberOfCharactersToRead = 2;
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/longChars.txt");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
ReadCharactersEvent event = new ReadCharactersEvent(characterChannel, numberOfCharactersToRead);
Future<EventResult> future = eventManager.publish(event);
EventResult eventResult = future.get();
String content = (String) eventResult.getResponse();
Assert.assertEquals("NJa", content);
numberOfCharactersToRead = 3;
event = new ReadCharactersEvent(characterChannel, numberOfCharactersToRead);
future = eventManager.publish(event);
eventResult = future.get();
content = (String) eventResult.getResponse();
Assert.assertEquals("bcNJ", content);
numberOfCharactersToRead = 4;
event = new ReadCharactersEvent(characterChannel, numberOfCharactersToRead);
future = eventManager.publish(event);
eventResult = future.get();
content = (String) eventResult.getResponse();
Assert.assertEquals("ff", content);
characterChannel.close();
}
Aggregations