Search in sources :

Example 21 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class CharacterInputOutputBufferTest method readLongCharacters.

@Test(description = "Reads characters which are represented with long bytes")
public void readLongCharacters() throws IOException, URISyntaxException {
    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());
    String readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("NJa", readCharacters);
    numberOfCharactersToRead = 3;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("bcNJ", readCharacters);
    numberOfCharactersToRead = 4;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("ff", 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)

Example 22 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class CharacterInputOutputBufferTest method readFussyCharacters.

@Test(description = "Read characters which does not fit to the fixed buffer limit")
public void readFussyCharacters() throws IOException, URISyntaxException {
    int numberOfCharactersToRead = 2;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/utf8file.txt");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    String readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters, "aa");
    numberOfCharactersToRead = 4;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters, "abbNJ");
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters.length(), 0);
    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)

Example 23 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class CharacterInputOutputBufferTest method writeCharacters.

@Test(description = "Write characters to file")
public void writeCharacters() throws IOException {
    // 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;
    int numberOfBytesWritten = characterChannel.write(text, 0);
    Assert.assertEquals(numberOfBytesWritten, numberOfBytes);
    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)

Example 24 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class CharacterInputOutputBufferTest method readCharacters.

@Test(description = "Read characters into multiple iterations")
public void readCharacters() throws IOException, URISyntaxException {
    int numberOfCharactersToRead = 2;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    String readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters.length(), numberOfCharactersToRead);
    numberOfCharactersToRead = 1;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters.length(), numberOfCharactersToRead);
    numberOfCharactersToRead = 4;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters.length(), 3);
    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)

Example 25 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class AsyncReadWriteTest method readTextRecords.

@Test(description = "Test which will read text records from a given channel using async io framework")
public void readTextRecords() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    int expectedFieldCount = 3;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample.csv");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
    DelimitedRecordReadEvent event = new DelimitedRecordReadEvent(recordChannel);
    Future<EventResult> future = eventManager.publish(event);
    EventResult eventResult = future.get();
    String[] readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    event = new DelimitedRecordReadEvent(recordChannel);
    future = eventManager.publish(event);
    eventResult = future.get();
    readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    event = new DelimitedRecordReadEvent(recordChannel);
    future = eventManager.publish(event);
    eventResult = future.get();
    readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    event = new DelimitedRecordReadEvent(recordChannel);
    future = eventManager.publish(event);
    eventResult = future.get();
    readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, 0);
    recordChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) 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) DelimitedRecordReadEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) 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