26
26
import java .io .ByteArrayOutputStream ;
27
27
import java .io .IOException ;
28
28
import java .nio .ByteBuffer ;
29
+ import java .nio .charset .StandardCharsets ;
29
30
import java .util .List ;
30
31
31
32
import static java .util .Arrays .asList ;
@@ -49,7 +50,7 @@ public class BsonBinaryWriterTest {
49
50
@ BeforeEach
50
51
public void setup () {
51
52
buffer = new BasicOutputBuffer ();
52
- writer = new BsonBinaryWriter (new BsonWriterSettings (100 ), new BsonBinaryWriterSettings (1024 ), buffer );
53
+ writer = new BsonBinaryWriter (new BsonWriterSettings (100 ), new BsonBinaryWriterSettings (12904 ), buffer );
53
54
}
54
55
55
56
@ AfterEach
@@ -61,7 +62,7 @@ public void tearDown() {
61
62
public void shouldThrowWhenMaxDocumentSizeIsExceeded () {
62
63
try {
63
64
writer .writeStartDocument ();
64
- writer .writeBinaryData ("b" , new BsonBinary (new byte [1024 ]));
65
+ writer .writeBinaryData ("b" , new BsonBinary (new byte [12904 ]));
65
66
writer .writeEndDocument ();
66
67
fail ();
67
68
} catch (BsonMaximumSizeExceededException e ) {
@@ -197,16 +198,39 @@ public void testWriteArray() {
197
198
}
198
199
199
200
@ Test
200
- public void testWriteArrayElements () {
201
+ public void testWriteArrayElements () throws IOException {
202
+ ByteArrayOutputStream expectedOutput = new ByteArrayOutputStream ();
203
+ expectedOutput .write (new byte []{
204
+ 88 , 11 , 0 , 0 , //document length
205
+ 4 , // array type
206
+ 97 , 49 , 0 , // "a1" name + null terminator
207
+ 79 , 11 , 0 , 0 }); // array length
208
+
201
209
202
210
writer .writeStartDocument ();
203
211
writer .writeStartArray ("a1" );
204
- writer .writeBoolean (true );
205
- writer .writeBoolean (false );
212
+ int arrayIndex = 0 ;
213
+ while (arrayIndex < 500 ) {
214
+ writer .writeBoolean (true );
215
+
216
+ expectedOutput .write (BsonType .BOOLEAN .getValue ());
217
+ expectedOutput .write (Integer .toString (arrayIndex ++).getBytes (StandardCharsets .UTF_8 ));
218
+ expectedOutput .write (0 ); // null terminator
219
+ expectedOutput .write (1 ); // boolean value
220
+
221
+ writer .writeBoolean (false );
222
+
223
+ expectedOutput .write (BsonType .BOOLEAN .getValue ());
224
+ expectedOutput .write (Integer .toString (arrayIndex ++).getBytes (StandardCharsets .UTF_8 ));
225
+ expectedOutput .write (0 ); // null terminator
226
+ expectedOutput .write (0 ); // boolean value
227
+ }
206
228
writer .writeEndArray ();
229
+ expectedOutput .write (0 ); // end of array
207
230
writer .writeEndDocument ();
208
- byte [] expectedValues = {22 , 0 , 0 , 0 , 4 , 97 , 49 , 0 , 13 , 0 , 0 , 0 , 8 , 48 , 0 , 1 , 8 , 49 , 0 , 0 , 0 , 0 };
209
- assertArrayEquals (expectedValues , buffer .toByteArray ());
231
+ expectedOutput .write (0 ); // end of a document
232
+
233
+ assertArrayEquals (expectedOutput .toByteArray (), buffer .toByteArray ());
210
234
}
211
235
212
236
@ Test
0 commit comments