Exemplo n.º 1
0
  /**
   * Creates a new Insert.
   *
   * @param header The header proceeding the insert message. This is used to locate the end of the
   *     insert.
   * @param in The stream to read the insert message from.
   * @throws IOException On a failure reading the insert message.
   */
  public Insert(final Header header, final BsonInputStream in) throws IOException {

    final long position = in.getBytesRead();
    final long end = (position + header.getLength()) - Header.SIZE;

    final int flags = in.readInt();
    init(in.readCString());

    // Read the documents to the end of the message.
    myDocuments = new ArrayList<Document>();
    while (in.getBytesRead() < end) {
      myDocuments.add(in.readDocument());
    }

    myContinueOnError = (flags & CONTINUE_ON_ERROR_BIT) == CONTINUE_ON_ERROR_BIT;
    myDocumentsSize = -1;
  }
  /**
   * Test method for {@link Query#Query(Header,BsonInputStream)}.
   *
   * @throws IOException On a test failure.
   */
  @SuppressWarnings("boxing")
  @Test
  public void testQueryHeaderBsonInputStream() throws IOException {
    final Random random = new Random(System.currentTimeMillis());

    final List<Message> objs1 = new ArrayList<Message>();

    final Document doc1 = BuilderFactory.start().addInteger("1", 0).build();
    final Document doc2 = BuilderFactory.start().addInteger("1", 1).build();
    final Document doc3 = BuilderFactory.start().addInteger("1", 2).build();
    for (final String databaseName : Arrays.asList("n1", "n2", "n3")) {
      for (final String collectionName : Arrays.asList("c1", "c2", "c3")) {
        for (final Document query : Arrays.asList(doc1, doc2, doc3)) {
          for (final Document returnFields : Arrays.asList(doc1, doc2, doc3, null)) {
            for (final int batchSize : Arrays.asList(0)) {
              for (final int limit : Arrays.asList(0)) {
                for (final int numberToSkip : Arrays.asList(0, 1, 2, 0xFFF)) {
                  final boolean tailable = random.nextBoolean();
                  final ReadPreference readPreference =
                      random.nextBoolean() ? ReadPreference.PRIMARY : ReadPreference.SECONDARY;
                  final boolean noCursorTimeout = random.nextBoolean();
                  final boolean awaitData = random.nextBoolean();
                  final boolean exhaust = random.nextBoolean();
                  final boolean partial = random.nextBoolean();

                  objs1.add(
                      new Query(
                          databaseName,
                          collectionName,
                          query,
                          returnFields,
                          batchSize,
                          limit,
                          numberToSkip,
                          tailable,
                          readPreference,
                          noCursorTimeout,
                          awaitData,
                          exhaust,
                          partial));
                }
              }
            }
          }
        }
      }
    }

    for (final Message message : objs1) {

      final ByteArrayOutputStream out = new ByteArrayOutputStream();
      final BsonOutputStream bOut = new BsonOutputStream(out);

      message.write(1234, bOut);

      final byte[] bytes = out.toByteArray();
      assertThat(message.size(), is(bytes.length));

      final ByteArrayInputStream in = new ByteArrayInputStream(bytes);
      final BsonInputStream bIn = new BsonInputStream(in);

      final Header header = new Header(bIn);

      assertEquals(Operation.QUERY, header.getOperation());
      assertEquals(1234, header.getRequestId());
      assertEquals(0, header.getResponseId());
      assertEquals(out.size(), header.getLength());

      final Query read = new Query(header, bIn);
      if (message.getReadPreference() == ReadPreference.PRIMARY) {
        assertEquals(message, read);
      }
    }
  }