@Test(timeout=300000)
public void testXAttrMultiAddRemoveErrors() throws Exception {

  // Test that the same XAttr can not be multiset twice
  List<XAttr> existingXAttrs = Lists.newArrayList();
  List<XAttr> toAdd = Lists.newArrayList();
  toAdd.add(generatedXAttrs.get(0));
  toAdd.add(generatedXAttrs.get(1));
  toAdd.add(generatedXAttrs.get(2));
  toAdd.add(generatedXAttrs.get(0));
  try {
    fsdir.setINodeXAttrs(existingXAttrs, toAdd, EnumSet.of(XAttrSetFlag
        .CREATE));
    fail("Specified the same xattr to be set twice");
  } catch (IOException e) {
    GenericTestUtils.assertExceptionContains("Cannot specify the same " +
        "XAttr to be set", e);
  }
  // Test that CREATE and REPLACE flags are obeyed
  toAdd.remove(generatedXAttrs.get(0));
  existingXAttrs.add(generatedXAttrs.get(0));
  try {
    fsdir.setINodeXAttrs(existingXAttrs, toAdd, EnumSet.of(XAttrSetFlag
        .CREATE));
    fail("Set XAttr that is already set without REPLACE flag");
  } catch (IOException e) {
    GenericTestUtils.assertExceptionContains("already exists", e);
  }
  try {
    fsdir.setINodeXAttrs(existingXAttrs, toAdd, EnumSet.of(XAttrSetFlag
        .REPLACE));
    fail("Set XAttr that does not exist without the CREATE flag");
  } catch (IOException e) {
    GenericTestUtils.assertExceptionContains("does not exist", e);
  }
  // Sanity test for CREATE
  toAdd.remove(generatedXAttrs.get(0));
  List<XAttr> newXAttrs = fsdir.setINodeXAttrs(existingXAttrs, toAdd,
      EnumSet.of(XAttrSetFlag.CREATE));
  assertEquals("Unexpected toAdd size", 2, toAdd.size());
  for (XAttr x : toAdd) {
    assertTrue("Did not find added XAttr " + x, newXAttrs.contains(x));
  }
  existingXAttrs = newXAttrs;
  // Sanity test for REPLACE
  toAdd = Lists.newArrayList();
  for (int i=0; i<3; i++) {
    XAttr xAttr = (new XAttr.Builder())
        .setNameSpace(XAttr.NameSpace.SYSTEM)
        .setName("a" + i)
        .setValue(new byte[] { (byte) (i*2) })
        .build();
    toAdd.add(xAttr);
  }
  newXAttrs = fsdir.setINodeXAttrs(existingXAttrs, toAdd,
      EnumSet.of(XAttrSetFlag.REPLACE));
  assertEquals("Unexpected number of new XAttrs", 3, newXAttrs.size());
  for (int i=0; i<3; i++) {
    assertArrayEquals("Unexpected XAttr value",
        new byte[] {(byte)(i*2)}, newXAttrs.get(i).getValue());
  }
  existingXAttrs = newXAttrs;
  // Sanity test for CREATE+REPLACE
  toAdd = Lists.newArrayList();
  for (int i=0; i<4; i++) {
    toAdd.add(generatedXAttrs.get(i));
  }
  newXAttrs = fsdir.setINodeXAttrs(existingXAttrs, toAdd,
      EnumSet.of(XAttrSetFlag.CREATE, XAttrSetFlag.REPLACE));
  verifyXAttrsPresent(newXAttrs, 4);
}