@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));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.CHECKSUMTYPE));
  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));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.CHECKSUMTYPE));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR));
  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));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.CHECKSUMTYPE));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR));
  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));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.CHECKSUMTYPE));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR));
  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));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.CHECKSUMTYPE));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR));
  options = OptionsParser.parse(new String[] {
      "-pbrgupcaxt",
      "-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));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.CHECKSUMTYPE));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.ACL));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.XATTR));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.TIMES));
  options = OptionsParser.parse(new String[] {
      "-pc",
      "-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));
  Assert.assertTrue(options.shouldPreserve(FileAttribute.CHECKSUMTYPE));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL));
  Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR));
  options = OptionsParser.parse(new String[] {
      "-p",
      "-f",
      "hdfs://localhost:8020/source/first",
      "hdfs://localhost:8020/target/"});
  Assert.assertEquals(DistCpOptionSwitch.PRESERVE_STATUS_DEFAULT.length() - 2,
      options.getPreserveAttributes().size());
  try {
    OptionsParser.parse(new String[] {
        "-pabcd",
        "-f",
        "hdfs://localhost:8020/source/first",
        "hdfs://localhost:8020/target"});
    Assert.fail("Invalid preserve attribute");
  }
  catch (NoSuchElementException ignore) {}
  Builder builder = new DistCpOptions.Builder(
      new Path("hdfs://localhost:8020/source/first"),
      new Path("hdfs://localhost:8020/target/"));
  Assert.assertFalse(
      builder.build().shouldPreserve(FileAttribute.PERMISSION));
  builder.preserve(FileAttribute.PERMISSION);
  Assert.assertTrue(builder.build().shouldPreserve(FileAttribute.PERMISSION));
  builder.preserve(FileAttribute.PERMISSION);
  Assert.assertTrue(builder.build().shouldPreserve(FileAttribute.PERMISSION));
}