@Test
public void testPreserve() {

  DistCpOptions options = OptionsParser.parse(new String[] {
      "-f",
      "hdfs://localhost:8020/source/first",
      "hdfs://localhost:8020/target/"});
  Assert.assertFalse(options.shouldPreserve(FileAttribute.BLOCKSIZE));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.REPLICATION));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.PERMISSION));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.USER));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.GROUP));
  options = OptionsParser.parse(new String[] {
      "-p",
      "-f",
      "hdfs://localhost:8020/source/first",
      "hdfs://localhost:8020/target/"});
  Assert.assertTrue(options.shouldPreserve(FileAttribute.BLOCKSIZE));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.REPLICATION));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.USER));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.GROUP));
  options = OptionsParser.parse(new String[] {
      "-p",
      "hdfs://localhost:8020/source/first",
      "hdfs://localhost:8020/target/"});
  Assert.assertTrue(options.shouldPreserve(FileAttribute.BLOCKSIZE));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.REPLICATION));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.USER));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.GROUP));
  options = OptionsParser.parse(new String[] {
      "-pbr",
      "-f",
      "hdfs://localhost:8020/source/first",
      "hdfs://localhost:8020/target/"});
  Assert.assertTrue(options.shouldPreserve(FileAttribute.BLOCKSIZE));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.REPLICATION));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.PERMISSION));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.USER));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.GROUP));
  options = OptionsParser.parse(new String[] {
      "-pbrgup",
      "-f",
      "hdfs://localhost:8020/source/first",
      "hdfs://localhost:8020/target/"});
  Assert.assertTrue(options.shouldPreserve(FileAttribute.BLOCKSIZE));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.REPLICATION));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.USER));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.GROUP));
  options = OptionsParser.parse(new String[] {
      "-p",
      "-f",
      "hdfs://localhost:8020/source/first",
      "hdfs://localhost:8020/target/"});
  int i = 0;
  Iterator<FileAttribute> attribIterator = options.preserveAttributes();
  while (attribIterator.hasNext()) {
    attribIterator.next();
    i++;
  }
  Assert.assertEquals(i, 5);
  try {
    OptionsParser.parse(new String[] {
        "-pabc",
        "-f",
        "hdfs://localhost:8020/source/first",
        "hdfs://localhost:8020/target"});
    Assert.fail("Invalid preserve attribute");
  }
  catch (IllegalArgumentException ignore) {}
  catch (NoSuchElementException ignore) {}
  options = OptionsParser.parse(new String[] {
      "-f",
      "hdfs://localhost:8020/source/first",
      "hdfs://localhost:8020/target/"});
  Assert.assertFalse(options.shouldPreserve(FileAttribute.PERMISSION));
  options.preserve(FileAttribute.PERMISSION);
  Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION));
  options.preserve(FileAttribute.PERMISSION);
  Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION));
}