Search in sources :

Example 16 with MockByteChannel

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();
}
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 17 with MockByteChannel

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);
}
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 18 with MockByteChannel

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);
}
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 19 with MockByteChannel

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

Example 20 with MockByteChannel

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