Search in sources :

Example 6 with MockByteChannel

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);
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 7 with MockByteChannel

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();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) BufferedReader(java.io.BufferedReader) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 8 with MockByteChannel

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);
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 9 with MockByteChannel

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);
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 10 with MockByteChannel

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();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Aggregations

ByteChannel (java.nio.channels.ByteChannel)29 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)29 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)29 Test (org.testng.annotations.Test)29 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)16 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)7 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 InputStream (java.io.InputStream)3 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ByteBuffer (java.nio.ByteBuffer)1 BString (org.ballerinalang.model.values.BString)1 ReadCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent)1 WriteCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent)1 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)1