-
-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathTestIOChannel.java
More file actions
36 lines (30 loc) · 1.16 KB
/
TestIOChannel.java
File metadata and controls
36 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.jruby.util;
import org.jruby.Ruby;
import org.jruby.api.Access;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.nio.ByteBuffer;
public class TestIOChannel {
Ruby runtime;
@Before
public void setup() {
runtime = Ruby.newInstance();
runtime.evalScriptlet("class MyO; def write(str); @str = str; str.length; end; end");
}
@Test
public void testBufferReuse() throws Exception {
ThreadContext context = runtime.getCurrentContext();
IRubyObject myo = Access.getClass(context, "MyO").newInstance(context, Block.NULL_BLOCK);
IOChannel.IOWritableByteChannel ioc = new IOChannel.IOWritableByteChannel(myo);
ByteBuffer buf = ByteBuffer.wrap(new byte[] {(byte) 'f', (byte) 'o', (byte) 'o'});
ioc.write(buf);
IRubyObject written = myo.getInstanceVariables().getInstanceVariable("@str");
Assert.assertEquals("foo", written.toString());
buf.put(0, (byte)'b');
Assert.assertEquals("foo", written.toString());
}
}