private void testStartStop2(final String filename) throws Exception {
    FileDeploymentManager fdm = new FileDeploymentManager(Long.MAX_VALUE);

    FileDeploymentManagerTest.log.debug("Filename is " + filename);

    File file = new File("target/test-classes/");

    file.mkdirs();

    file = new File("target/test-classes/" + filename);

    FileDeploymentManagerTest.log.debug(file.getAbsoluteFile());

    file.createNewFile();

    FakeDeployer deployer = new FakeDeployer(filename);

    fdm.start();

    try {
      fdm.registerDeployer(deployer);
      URI expected = file.toURI();
      URI deployedUrl = deployer.deployedUri;
      Assert.assertTrue(expected.toString().equalsIgnoreCase(deployedUrl.toString()));
      deployer.deployedUri = null;
      fdm.start();
      Assert.assertNull(deployer.deployedUri);
      fdm.stop();
    } finally {
      file.delete();
      fdm.stop();
    }
  }
  public void testUndeployAndDeployAgain() throws Exception {
    FileDeploymentManager fdm = new FileDeploymentManager(Long.MAX_VALUE);

    fdm.start();

    String filename = "fdm_test_file.xml1";

    File file = new File("target/test-classes/");

    file.mkdirs();

    file = new File("target/test-classes/" + filename);

    file.createNewFile();

    FakeDeployer deployer = new FakeDeployer(filename);
    try {
      URI uri = file.toURI();
      deployer.deploy(uri);

      fdm.registerDeployer(deployer);

      Assert.assertEquals(1, fdm.getDeployers().size());
      Assert.assertTrue(fdm.getDeployers().contains(deployer));
      Assert.assertEquals(1, fdm.getDeployed().size());
      Assert.assertEquals(file.toURI(), deployer.deployedUri);
      deployer.deployedUri = null;
      file.delete();

      // This should cause undeployment

      deployer.undeploy(uri);
      Assert.assertEquals(file.toURI(), deployer.unDeployedUri);

      fdm.run();

      Assert.assertEquals(1, fdm.getDeployers().size());
      Assert.assertTrue(fdm.getDeployers().contains(deployer));
      Assert.assertEquals(0, fdm.getDeployed().size());

      // Recreate file and it should be redeployed

      file.createNewFile();

      deployer.deploy(uri);

      fdm.run();

      Assert.assertEquals(1, fdm.getDeployers().size());
      Assert.assertTrue(fdm.getDeployers().contains(deployer));
      Assert.assertEquals(1, fdm.getDeployed().size());

      Assert.assertEquals(file.toURI(), deployer.deployedUri);
    } finally {
      file.delete();
      fdm.stop();
    }
  }
  public void testRedeploy() throws Exception {
    FileDeploymentManager fdm = new FileDeploymentManager(Long.MAX_VALUE);

    fdm.start();

    String filename = "fdm_test_file.xml1";

    File file = new File("target/test-classes/");

    file.mkdirs();

    file = new File("target/test-classes/" + filename);

    file.createNewFile();
    long oldLastModified = file.lastModified();

    FakeDeployer deployer = new FakeDeployer(filename);
    try {
      URI url = file.toURI();
      deployer.deploy(url);

      fdm.registerDeployer(deployer);
      Assert.assertEquals(file.toURI(), deployer.deployedUri);
      // Touch the file
      file.setLastModified(oldLastModified + 1000);

      deployer.redeploy(url);

      fdm.run();

      Assert.assertEquals(1, fdm.getDeployers().size());
      Assert.assertTrue(fdm.getDeployers().contains(deployer));
      Assert.assertEquals(1, fdm.getDeployed().size());
      URI expected = file.toURI();
      URI deployedUrl = deployer.deployedUri;
      Assert.assertTrue(expected.toString().equalsIgnoreCase(deployedUrl.toString()));
      Pair<URI, Deployer> pair = new Pair<URI, Deployer>(url, deployer);
      Assert.assertEquals(oldLastModified + 1000, fdm.getDeployed().get(pair).lastModified);
      deployer.reDeployedUri = null;
      // Scanning again should not redeploy

      fdm.run();

      Assert.assertEquals(oldLastModified + 1000, fdm.getDeployed().get(pair).lastModified);
      Assert.assertNull(deployer.reDeployedUri);
    } finally {
      file.delete();
      fdm.stop();
    }
  }
  public void testRegisterUnregister() throws Exception {
    FileDeploymentManager fdm = new FileDeploymentManager(Long.MAX_VALUE);

    fdm.start();

    String filename1 = "fdm_test_file.xml1";
    String filename2 = "fdm_test_file.xml2";
    String filename3 = "fdm_test_file.xml3";

    File file1 = new File("target/test-classes/");
    File file2 = new File("target/test-classes/");
    File file3 = new File("target/test-classes/");

    file1.mkdirs();
    file2.mkdirs();
    file3.mkdirs();

    file1 = new File("target/test-classes/" + filename1);
    file2 = new File("target/test-classes/" + filename2);
    file3 = new File("target/test-classes/" + filename3);

    file1.createNewFile();
    file2.createNewFile();
    file3.createNewFile();

    FakeDeployer deployer1 = new FakeDeployer(filename1);
    FakeDeployer deployer2 = new FakeDeployer(filename2);
    FakeDeployer deployer3 = new FakeDeployer(filename3);
    FakeDeployer deployer4 =
        new FakeDeployer(filename3); // Can have multiple deployers on the same file
    try {
      URI url1 = file1.toURI();
      deployer1.deploy(url1);

      URI url2 = file2.toURI();
      deployer2.deploy(url2);

      URI url3 = file3.toURI();
      deployer3.deploy(url3);

      deployer4.deploy(url3);

      fdm.registerDeployer(deployer1);
      fdm.registerDeployer(deployer2);
      fdm.registerDeployer(deployer3);
      fdm.registerDeployer(deployer4);

      Assert.assertEquals(4, fdm.getDeployers().size());
      Assert.assertTrue(fdm.getDeployers().contains(deployer1));
      Assert.assertTrue(fdm.getDeployers().contains(deployer2));
      Assert.assertTrue(fdm.getDeployers().contains(deployer3));
      Assert.assertTrue(fdm.getDeployers().contains(deployer4));
      Assert.assertEquals(4, fdm.getDeployed().size());

      Assert.assertEquals(file1.toURI(), deployer1.deployedUri);
      Assert.assertEquals(file2.toURI(), deployer2.deployedUri);
      Assert.assertEquals(file3.toURI(), deployer3.deployedUri);
      Assert.assertEquals(file3.toURI(), deployer4.deployedUri);
      // Registering same again should do nothing

      fdm.registerDeployer(deployer1);

      Assert.assertEquals(4, fdm.getDeployers().size());
      Assert.assertTrue(fdm.getDeployers().contains(deployer1));
      Assert.assertTrue(fdm.getDeployers().contains(deployer2));
      Assert.assertTrue(fdm.getDeployers().contains(deployer3));
      Assert.assertTrue(fdm.getDeployers().contains(deployer4));
      Assert.assertEquals(4, fdm.getDeployed().size());

      fdm.unregisterDeployer(deployer1);

      Assert.assertEquals(3, fdm.getDeployers().size());
      Assert.assertTrue(fdm.getDeployers().contains(deployer2));
      Assert.assertTrue(fdm.getDeployers().contains(deployer3));
      Assert.assertTrue(fdm.getDeployers().contains(deployer4));
      Assert.assertEquals(3, fdm.getDeployed().size());

      fdm.unregisterDeployer(deployer2);
      fdm.unregisterDeployer(deployer3);

      Assert.assertEquals(1, fdm.getDeployers().size());
      Assert.assertTrue(fdm.getDeployers().contains(deployer4));
      Assert.assertEquals(1, fdm.getDeployed().size());

      fdm.unregisterDeployer(deployer4);

      Assert.assertEquals(0, fdm.getDeployers().size());
      Assert.assertEquals(0, fdm.getDeployed().size());

      // Now unregister again - should do nothing

      fdm.unregisterDeployer(deployer1);

      Assert.assertEquals(0, fdm.getDeployers().size());
      Assert.assertEquals(0, fdm.getDeployed().size());
    } finally {
      file1.delete();
      file2.delete();
      file3.delete();
      fdm.stop();
    }
  }