Program: 160309
Project: 160309
Buggy source:
public void testViewIndex() throws CBLiteException {
  Map<String,Object> dict1=new HashMap<String,Object>();
  dict1.put("String_Node_Str","String_Node_Str");
  Map<String,Object> dict2=new HashMap<String,Object>();
  dict2.put("String_Node_Str","String_Node_Str");
  Map<String,Object> dict3=new HashMap<String,Object>();
  dict3.put("String_Node_Str","String_Node_Str");
  Map<String,Object> dictX=new HashMap<String,Object>();
  dictX.put("String_Node_Str","String_Node_Str");
  CBLRevisionInternal rev1=putDoc(database,dict1);
  CBLRevisionInternal rev2=putDoc(database,dict2);
  CBLRevisionInternal rev3=putDoc(database,dict3);
  putDoc(database,dictX);
  CBLView view=createView(database);
  Assert.assertEquals(1,view.getViewId());
  Assert.assertTrue(view.isStale());
  CBLStatus updated=view.updateIndex();
  Assert.assertEquals(CBLStatus.OK,updated.getCode());
  List<Map<String,Object>> dumpResult=view.dump();
  Log.v(TAG,"String_Node_Str" + dumpResult);
  Assert.assertEquals(3,dumpResult.size());
  Assert.assertEquals("String_Node_Str",dumpResult.get(0).get("String_Node_Str"));
  Assert.assertEquals(1,dumpResult.get(0).get("String_Node_Str"));
  Assert.assertEquals("String_Node_Str",dumpResult.get(2).get("String_Node_Str"));
  Assert.assertEquals(2,dumpResult.get(2).get("String_Node_Str"));
  Assert.assertEquals("String_Node_Str",dumpResult.get(1).get("String_Node_Str"));
  Assert.assertEquals(3,dumpResult.get(1).get("String_Node_Str"));
  Assert.assertFalse(view.isStale());
  updated=view.updateIndex();
  Assert.assertEquals(CBLStatus.NOT_MODIFIED,updated.getCode());
  CBLRevisionInternal threeUpdated=new CBLRevisionInternal(rev3.getDocId(),rev3.getRevId(),false,database);
  Map<String,Object> newdict3=new HashMap<String,Object>();
  newdict3.put("String_Node_Str","String_Node_Str");
  threeUpdated.setProperties(newdict3);
  CBLStatus status=new CBLStatus();
  rev3=database.putRevision(threeUpdated,rev3.getRevId(),false,status);
  Assert.assertTrue(status.isSuccessful());
  Map<String,Object> dict4=new HashMap<String,Object>();
  dict4.put("String_Node_Str","String_Node_Str");
  CBLRevisionInternal rev4=putDoc(database,dict4);
  CBLRevisionInternal twoDeleted=new CBLRevisionInternal(rev2.getDocId(),rev2.getRevId(),true,database);
  database.putRevision(twoDeleted,rev2.getRevId(),false,status);
  Assert.assertTrue(status.isSuccessful());
  Assert.assertTrue(view.isStale());
  updated=view.updateIndex();
  Assert.assertEquals(CBLStatus.OK,updated.getCode());
  dumpResult=view.dump();
  Log.v(TAG,"String_Node_Str" + dumpResult);
  Assert.assertEquals(3,dumpResult.size());
  Assert.assertEquals("String_Node_Str",dumpResult.get(2).get("String_Node_Str"));
  Assert.assertEquals(1,dumpResult.get(2).get("String_Node_Str"));
  Assert.assertEquals("String_Node_Str",dumpResult.get(0).get("String_Node_Str"));
  Assert.assertEquals(5,dumpResult.get(0).get("String_Node_Str"));
  Assert.assertEquals("String_Node_Str",dumpResult.get(1).get("String_Node_Str"));
  Assert.assertEquals(6,dumpResult.get(1).get("String_Node_Str"));
  List<CBLQueryRow> rows=view.queryWithOptions(null);
  Assert.assertEquals(3,rows.size());
  Assert.assertEquals("String_Node_Str",rows.get(2).getKey());
  Assert.assertEquals(rev1.getDocId(),rows.get(2).getDocumentId());
  Assert.assertEquals("String_Node_Str",rows.get(0).getKey());
  Assert.assertEquals(rev3.getDocId(),rows.get(0).getDocumentId());
  Assert.assertEquals("String_Node_Str",rows.get(1).getKey());
  Assert.assertEquals(rev4.getDocId(),rows.get(1).getDocumentId());
  view.removeIndex();
}

Fixed version (ground truth):
public void testViewIndex() throws CBLiteException {
  Map<String,Object> dict1=new HashMap<String,Object>();
  dict1.put("String_Node_Str","String_Node_Str");
  Map<String,Object> dict2=new HashMap<String,Object>();
  dict2.put("String_Node_Str","String_Node_Str");
  Map<String,Object> dict3=new HashMap<String,Object>();
  dict3.put("String_Node_Str","String_Node_Str");
  Map<String,Object> dictX=new HashMap<String,Object>();
  dictX.put("String_Node_Str","String_Node_Str");
  CBLRevisionInternal rev1=putDoc(database,dict1);
  CBLRevisionInternal rev2=putDoc(database,dict2);
  CBLRevisionInternal rev3=putDoc(database,dict3);
  putDoc(database,dictX);
  CBLView view=createView(database);
  Assert.assertEquals(1,view.getViewId());
  Assert.assertTrue(view.isStale());
  CBLStatus updated=view.updateIndex();
  Assert.assertEquals(CBLStatus.OK,updated.getCode());
  List<Map<String,Object>> dumpResult=view.dump();
  Log.v(TAG,"String_Node_Str" + dumpResult);
  Assert.assertEquals(3,dumpResult.size());
  Assert.assertEquals("String_Node_Str",dumpResult.get(0).get("String_Node_Str"));
  Assert.assertEquals(1,dumpResult.get(0).get("String_Node_Str"));
  Assert.assertEquals("String_Node_Str",dumpResult.get(2).get("String_Node_Str"));
  Assert.assertEquals(2,dumpResult.get(2).get("String_Node_Str"));
  Assert.assertEquals("String_Node_Str",dumpResult.get(1).get("String_Node_Str"));
  Assert.assertEquals(3,dumpResult.get(1).get("String_Node_Str"));
  Assert.assertFalse(view.isStale());
  boolean gotNotModfiedException=false;
  try {
    updated=view.updateIndex();
  }
 catch (  CBLiteException e) {
    if (e.getCBLStatus().getCode() == CBLStatus.NOT_MODIFIED) {
      gotNotModfiedException=true;
    }
  }
  Assert.assertTrue(gotNotModfiedException);
  CBLRevisionInternal threeUpdated=new CBLRevisionInternal(rev3.getDocId(),rev3.getRevId(),false,database);
  Map<String,Object> newdict3=new HashMap<String,Object>();
  newdict3.put("String_Node_Str","String_Node_Str");
  threeUpdated.setProperties(newdict3);
  CBLStatus status=new CBLStatus();
  rev3=database.putRevision(threeUpdated,rev3.getRevId(),false,status);
  Assert.assertTrue(status.isSuccessful());
  Map<String,Object> dict4=new HashMap<String,Object>();
  dict4.put("String_Node_Str","String_Node_Str");
  CBLRevisionInternal rev4=putDoc(database,dict4);
  CBLRevisionInternal twoDeleted=new CBLRevisionInternal(rev2.getDocId(),rev2.getRevId(),true,database);
  database.putRevision(twoDeleted,rev2.getRevId(),false,status);
  Assert.assertTrue(status.isSuccessful());
  Assert.assertTrue(view.isStale());
  updated=view.updateIndex();
  Assert.assertEquals(CBLStatus.OK,updated.getCode());
  dumpResult=view.dump();
  Log.v(TAG,"String_Node_Str" + dumpResult);
  Assert.assertEquals(3,dumpResult.size());
  Assert.assertEquals("String_Node_Str",dumpResult.get(2).get("String_Node_Str"));
  Assert.assertEquals(1,dumpResult.get(2).get("String_Node_Str"));
  Assert.assertEquals("String_Node_Str",dumpResult.get(0).get("String_Node_Str"));
  Assert.assertEquals(5,dumpResult.get(0).get("String_Node_Str"));
  Assert.assertEquals("String_Node_Str",dumpResult.get(1).get("String_Node_Str"));
  Assert.assertEquals(6,dumpResult.get(1).get("String_Node_Str"));
  List<CBLQueryRow> rows=view.queryWithOptions(null);
  Assert.assertEquals(3,rows.size());
  Assert.assertEquals("String_Node_Str",rows.get(2).getKey());
  Assert.assertEquals(rev1.getDocId(),rows.get(2).getDocumentId());
  Assert.assertEquals("String_Node_Str",rows.get(0).getKey());
  Assert.assertEquals(rev3.getDocId(),rows.get(0).getDocumentId());
  Assert.assertEquals("String_Node_Str",rows.get(1).getKey());
  Assert.assertEquals(rev4.getDocId(),rows.get(1).getDocumentId());
  view.removeIndex();
}