Search in sources :

Example 11 with MockByteChannel

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

the class CharacterInputOutputBufferTest method readCorruptedCharactersIntoMultipleChannelReads.

@Test(description = "Read from file which has all corrupted chars")
public void readCorruptedCharactersIntoMultipleChannelReads() throws IOException, URISyntaxException {
    int numberOfCharactersToRead;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/corruptedText2");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    numberOfCharactersToRead = 3;
    String readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("���", readCharacters);
    numberOfCharactersToRead = 3;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("��a", 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)

Example 12 with MockByteChannel

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

the class RecordInputOutputTest method writeRecords.

@Test(description = "Writes records to channel")
public void writeRecords() throws IOException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "records.csv");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
    String[] recordOne = { "Foo", "Bar", "911" };
    BStringArray recordOneArr = new BStringArray(recordOne);
    recordChannel.write(recordOneArr);
    String[] recordTwo = { "Jim", "Com", "119" };
    BStringArray recordTwoArr = new BStringArray(recordTwo);
    recordChannel.write(recordTwoArr);
    recordChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) 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) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) BStringArray(org.ballerinalang.model.values.BStringArray) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 13 with MockByteChannel

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

the class RecordInputOutputTest method readNonIndentedRecords.

@Test(description = "Read records which are not indented properly")
public void readNonIndentedRecords() throws IOException, URISyntaxException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample2.csv");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
    String[] readRecord = recordChannel.read();
    Assert.assertEquals(readRecord.length, 9);
    Assert.assertTrue(recordChannel.hasNext(), "Expecting more records but received as EOL.");
    // This will be a blank record
    readRecord = recordChannel.read();
    Assert.assertEquals(readRecord.length, 1);
    Assert.assertTrue(recordChannel.hasNext(), "Expecting more records but received as EOL.");
    readRecord = recordChannel.read();
    Assert.assertEquals(readRecord.length, 9);
    Assert.assertFalse(recordChannel.hasNext(), "Last record received, but indicate as more records available.");
    recordChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) 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) 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)

Example 14 with MockByteChannel

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

the class AsyncReadWriteTest method readBytes.

@Test(description = "Read into fixed byte[] using async io framework")
public void readBytes() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    byte[] content = new byte[2];
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
    Channel channel = new MockByteChannel(byteChannel);
    byte[] expected = { 49, 50 };
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    expected = new byte[] { 51, 52 };
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    expected = new byte[] { 53, 54 };
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    int expectedNumberOfBytes = 0;
    content = new byte[2];
    expected = new byte[] { 0, 0 };
    int numberOfBytesRead = IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(numberOfBytesRead, expectedNumberOfBytes);
    Assert.assertEquals(expected, content);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) 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) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 15 with MockByteChannel

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

the class AsyncReadWriteTest method readContentValidationTest.

@Test(description = "Reads bytes and validate the byte content")
public void readContentValidationTest() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    byte[] content = new byte[3];
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
    Channel channel = new MockByteChannel(byteChannel);
    byte[] expected = "123".getBytes();
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    expected = "456".getBytes();
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    content = new byte[3];
    expected = new byte[3];
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) 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) 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