|
| 1 | +package com.vertispan.grpc.fetch; |
| 2 | + |
| 3 | +import com.google.gwt.junit.client.GWTTestCase; |
| 4 | +import elemental2.core.Uint8Array; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +/** |
| 10 | + * Browser-emulation behavior tests for {@link ByteBufferOutputStream}, including Uint8Array export semantics. |
| 11 | + * Also includes delegate stubs for JVM-only ByteBufferOutputStream unit checks. |
| 12 | + */ |
| 13 | +public class ByteBufferOutputStreamGwtTest extends GWTTestCase { |
| 14 | + @Override |
| 15 | + public String getModuleName() { |
| 16 | + return "com.vertispan.grpc.fetch.Fetch"; |
| 17 | + } |
| 18 | + |
| 19 | + public void testByteBufferOutputStreamExportsWrittenBytes() throws IOException { |
| 20 | + ByteBufferOutputStream stream = new ByteBufferOutputStream(4); |
| 21 | + stream.write(new byte[]{1, 2}, 0, 2); |
| 22 | + stream.write(new byte[]{3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, 0, 10); |
| 23 | + |
| 24 | + List<Uint8Array> buffers = stream.getBuffers(); |
| 25 | + assertEquals(2, buffers.size()); |
| 26 | + |
| 27 | + Uint8Array first = buffers.get(0); |
| 28 | + assertEquals(4, first.length); |
| 29 | + assertEquals(1, first.getAt(0).intValue()); |
| 30 | + assertEquals(2, first.getAt(1).intValue()); |
| 31 | + assertEquals(3, first.getAt(2).intValue()); |
| 32 | + assertEquals(4, first.getAt(3).intValue()); |
| 33 | + |
| 34 | + Uint8Array second = buffers.get(1); |
| 35 | + assertEquals(8, second.length); |
| 36 | + assertEquals(5, second.getAt(0).intValue()); |
| 37 | + assertEquals(6, second.getAt(1).intValue()); |
| 38 | + assertEquals(7, second.getAt(2).intValue()); |
| 39 | + assertEquals(8, second.getAt(3).intValue()); |
| 40 | + assertEquals(9, second.getAt(4).intValue()); |
| 41 | + assertEquals(10, second.getAt(5).intValue()); |
| 42 | + assertEquals(11, second.getAt(6).intValue()); |
| 43 | + assertEquals(12, second.getAt(7).intValue()); |
| 44 | + } |
| 45 | + |
| 46 | + public void testByteBufferOutputStreamTruncatesFinalExportedBufferLength() throws IOException { |
| 47 | + ByteBufferOutputStream stream = new ByteBufferOutputStream(8); |
| 48 | + stream.write(new byte[]{1, 2, 3, 4, 5, 6}, 0, 6); |
| 49 | + stream.write(new byte[]{7, 8, 9, 10, 11}, 0, 5); |
| 50 | + |
| 51 | + List<Uint8Array> buffers = stream.getBuffers(); |
| 52 | + assertEquals(2, buffers.size()); |
| 53 | + |
| 54 | + Uint8Array first = buffers.get(0); |
| 55 | + assertEquals(8, first.length); |
| 56 | + assertEquals(1, first.getAt(0).intValue()); |
| 57 | + assertEquals(8, first.getAt(7).intValue()); |
| 58 | + |
| 59 | + Uint8Array second = buffers.get(1); |
| 60 | + assertEquals(3, second.length); |
| 61 | + assertEquals(9, second.getAt(0).intValue()); |
| 62 | + assertEquals(10, second.getAt(1).intValue()); |
| 63 | + assertEquals(11, second.getAt(2).intValue()); |
| 64 | + } |
| 65 | + |
| 66 | + public void testByteBufferOutputStreamExportsEmptyWhenNothingWritten() { |
| 67 | + ByteBufferOutputStream stream = new ByteBufferOutputStream(8); |
| 68 | + assertTrue(stream.getBuffers().isEmpty()); |
| 69 | + } |
| 70 | + |
| 71 | + public void testByteBufferOutputStreamPreservesUnsignedByteValues() throws IOException { |
| 72 | + ByteBufferOutputStream stream = new ByteBufferOutputStream(8); |
| 73 | + stream.write(new byte[]{-1, -128, 127}, 0, 3); |
| 74 | + |
| 75 | + List<Uint8Array> buffers = stream.getBuffers(); |
| 76 | + assertEquals(1, buffers.size()); |
| 77 | + assertEquals(255, buffers.get(0).getAt(0).intValue()); |
| 78 | + assertEquals(128, buffers.get(0).getAt(1).intValue()); |
| 79 | + assertEquals(127, buffers.get(0).getAt(2).intValue()); |
| 80 | + } |
| 81 | + |
| 82 | + public void testByteBufferOutputStreamFillsExactlyWithWriteInt() throws IOException { |
| 83 | + ByteBufferOutputStream stream = new ByteBufferOutputStream(4); |
| 84 | + stream.write(1); |
| 85 | + stream.write(2); |
| 86 | + stream.write(3); |
| 87 | + stream.write(4); |
| 88 | + |
| 89 | + List<Uint8Array> buffers = stream.getBuffers(); |
| 90 | + assertEquals(1, buffers.size()); |
| 91 | + assertEquals(4, buffers.get(0).length); |
| 92 | + assertEquals(1, buffers.get(0).getAt(0).intValue()); |
| 93 | + assertEquals(2, buffers.get(0).getAt(1).intValue()); |
| 94 | + assertEquals(3, buffers.get(0).getAt(2).intValue()); |
| 95 | + assertEquals(4, buffers.get(0).getAt(3).intValue()); |
| 96 | + } |
| 97 | + |
| 98 | + public void testByteBufferOutputStreamFillsExactlyWithWriteArray() throws IOException { |
| 99 | + ByteBufferOutputStream stream = new ByteBufferOutputStream(4); |
| 100 | + stream.write(new byte[]{1, 2, 3, 4}, 0, 4); |
| 101 | + |
| 102 | + List<Uint8Array> buffers = stream.getBuffers(); |
| 103 | + assertEquals(1, buffers.size()); |
| 104 | + assertEquals(4, buffers.get(0).length); |
| 105 | + assertEquals(1, buffers.get(0).getAt(0).intValue()); |
| 106 | + assertEquals(2, buffers.get(0).getAt(1).intValue()); |
| 107 | + assertEquals(3, buffers.get(0).getAt(2).intValue()); |
| 108 | + assertEquals(4, buffers.get(0).getAt(3).intValue()); |
| 109 | + } |
| 110 | + |
| 111 | + public void testByteBufferOutputStreamRolloverAfterExactFill() throws IOException { |
| 112 | + ByteBufferOutputStream stream = new ByteBufferOutputStream(4); |
| 113 | + stream.write(new byte[]{1, 2, 3, 4}, 0, 4); |
| 114 | + stream.write(5); |
| 115 | + |
| 116 | + List<Uint8Array> buffers = stream.getBuffers(); |
| 117 | + assertEquals(2, buffers.size()); |
| 118 | + assertEquals(4, buffers.get(0).length); |
| 119 | + assertEquals(1, buffers.get(1).length); |
| 120 | + assertEquals(5, buffers.get(1).getAt(0).intValue()); |
| 121 | + } |
| 122 | + |
| 123 | + public void testByteBufferOutputStreamLargeFirstWriteThenAppend() throws IOException { |
| 124 | + ByteBufferOutputStream stream = new ByteBufferOutputStream(4); |
| 125 | + stream.write(new byte[]{1, 2, 3, 4, 5, 6}, 0, 6); |
| 126 | + stream.write(new byte[]{7, 8}, 0, 2); |
| 127 | + |
| 128 | + List<Uint8Array> buffers = stream.getBuffers(); |
| 129 | + assertEquals(2, buffers.size()); |
| 130 | + assertEquals(6, buffers.get(0).length); |
| 131 | + assertEquals(2, buffers.get(1).length); |
| 132 | + assertEquals(1, buffers.get(0).getAt(0).intValue()); |
| 133 | + assertEquals(6, buffers.get(0).getAt(5).intValue()); |
| 134 | + assertEquals(7, buffers.get(1).getAt(0).intValue()); |
| 135 | + assertEquals(8, buffers.get(1).getAt(1).intValue()); |
| 136 | + } |
| 137 | + |
| 138 | + public void testDelegatesJvmSingleByteAndArrayCase() throws Exception { |
| 139 | + new ByteBufferOutputStreamTest().writesSingleByteAndArrayIntoCurrentBuffer(); |
| 140 | + } |
| 141 | + |
| 142 | + public void testDelegatesJvmOversizedRemainingCase() throws Exception { |
| 143 | + new ByteBufferOutputStreamTest().allocatesLargerBufferForRemainingBytes(); |
| 144 | + } |
| 145 | + |
| 146 | + public void testDelegatesJvmOversizedInitialWriteCase() throws Exception { |
| 147 | + new ByteBufferOutputStreamTest().replacesEmptyBufferForOversizedWrite(); |
| 148 | + } |
| 149 | + |
| 150 | + public void testDelegatesJvmBoundsValidationCase() { |
| 151 | + new ByteBufferOutputStreamTest().rejectsInvalidWriteBounds(); |
| 152 | + } |
| 153 | +} |
0 commit comments