use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class BytesInputOutputBufferTest method multiReadFromFixedBuffer.
@Test(description = "Read bytes from fix buffer into multiple reads")
public void multiReadFromFixedBuffer() throws IOException, URISyntaxException {
int initialReadLimit = 3;
int secondLapReadLimit = 3;
int thirdLapReadLimit = 3;
// During the 3rd lap all the bytes were read
int thirdLapReadLimitExpected = 0;
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
Channel channel = new MockByteChannel(byteChannel);
int numberOfBytesRead = read(initialReadLimit, channel).getNumberOfBytesRead();
// This should hold the number of bytes read
Assert.assertEquals(numberOfBytesRead, initialReadLimit);
numberOfBytesRead = read(secondLapReadLimit, channel).getNumberOfBytesRead();
// This should hold the number of bytes get
Assert.assertEquals(numberOfBytesRead, secondLapReadLimit);
numberOfBytesRead = read(thirdLapReadLimit, channel).getNumberOfBytesRead();
channel.close();
// This should hold the number of bytes get
Assert.assertEquals(numberOfBytesRead, thirdLapReadLimitExpected);
}
use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class BytesInputOutputBufferTest method checkChannelCloseStatus.
@Test(description = "Validate getting InputStream from closed channel", expectedExceptions = BallerinaIOException.class, expectedExceptionsMessageRegExp = "Channel is already closed.")
public void checkChannelCloseStatus() 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();
channel.close();
channel.getInputStream();
}
use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class BytesInputOutputBufferTest method fileStreamCopyTest.
@Test(description = "Copy I/O byte file as a stream")
public void fileStreamCopyTest() throws IOException, URISyntaxException {
final int readLimit = 10000;
int totalNumberOfBytesRead = 0;
int totalNumberOfBytesWritten = 0;
final int numberOfBytesInFile = 45613;
int numberOfBytesRead = 38938;
int numberOfBytesWritten;
// Number of characters in this file would be 6
ByteChannel readByteChannel = TestUtil.openForReading("datafiles/io/images/ballerina.png");
ByteChannel writeByteChannel = TestUtil.openForWriting(currentDirectoryPath + "ballerinaCopy.png");
Channel readChannel = new MockByteChannel(readByteChannel);
Channel writeChannel = new MockByteChannel(writeByteChannel);
while (numberOfBytesRead > 0) {
ReadByteResult readByteResult = read(readLimit, readChannel);
byte[] readBytes = readByteResult.getContent();
numberOfBytesRead = readByteResult.getNumberOfBytesRead();
numberOfBytesWritten = writeFull(readBytes, numberOfBytesRead, writeChannel);
totalNumberOfBytesRead = totalNumberOfBytesRead + numberOfBytesRead;
totalNumberOfBytesWritten = totalNumberOfBytesWritten + numberOfBytesWritten;
}
Assert.assertEquals(totalNumberOfBytesRead, numberOfBytesInFile);
Assert.assertEquals(totalNumberOfBytesRead, totalNumberOfBytesWritten);
}
use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class BytesInputOutputBufferTest method multiReadFile.
@Test(description = "Reads files into multiple iterations")
public void multiReadFile() throws IOException, URISyntaxException {
int initialReadLimit = 3;
int secondLapReadLimit = 3;
int thirdLapReadLimit = 3;
// During the 3rd lap all the bytes were get
int thirdLapReadLimitExpected = 0;
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
Channel channel = new MockByteChannel(byteChannel);
byte[] readBytes = read(initialReadLimit, channel).getContent();
// This should hold the number of bytes get
Assert.assertEquals(readBytes.length, initialReadLimit);
readBytes = read(secondLapReadLimit, channel).getContent();
// This should hold the number of bytes get
Assert.assertEquals(readBytes.length, secondLapReadLimit);
ReadByteResult result = read(thirdLapReadLimit, channel);
int numberOfReadBytes = result.getNumberOfBytesRead();
channel.close();
// This should hold the number of bytes get
Assert.assertEquals(numberOfReadBytes, thirdLapReadLimitExpected);
}
use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.
the class CharacterInputOutputBufferTest method readCorruptedCharacters.
@Test(description = "Read corrupted characters from text")
public void readCorruptedCharacters() throws IOException, URISyntaxException {
int numberOfCharactersToRead;
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/corruptedText");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
numberOfCharactersToRead = 2;
String readCharacters = characterChannel.read(numberOfCharactersToRead);
Assert.assertEquals("�T", readCharacters);
numberOfCharactersToRead = 11;
readCharacters = characterChannel.read(numberOfCharactersToRead);
Assert.assertEquals("est Curr ß", readCharacters);
numberOfCharactersToRead = 4;
readCharacters = characterChannel.read(numberOfCharactersToRead);
Assert.assertEquals(" �", readCharacters);
readCharacters = characterChannel.read(numberOfCharactersToRead);
Assert.assertEquals(readCharacters, "");
characterChannel.close();
}
Aggregations