@Test
  public void testIdempotent() throws Exception {
    String uri = "file:target/test-classes/idempotent?idempotent=true&delay=10";
    context
        .getRouteDefinitions()
        .get(1)
        .adviceWith(
            context,
            new AdviceWithRouteBuilder() {
              @Override
              public void configure() throws Exception {
                replaceFromWith(uri);
                weaveByType(ToDefinition.class).selectFirst().replace().to("mock:endpoint");
              }
            });
    MockEndpoint end = context.getEndpoint("mock:endpoint", MockEndpoint.class);
    end.expectedMessageCount(1);

    producer.sendBodyAndHeader(
        "file://target/test-classes/idempotent", Exchange.FILE_NAME, "FCOO1.nc");

    end.assertIsSatisfied();

    String fileName =
        (String) end.getReceivedExchanges().get(0).getIn().getHeader(Exchange.FILE_NAME_ONLY);
    assertEquals(fileName, "FCOO1.nc");

    // reset the mock
    end.reset();
    end.expectedMessageCount(0);

    // move file back
    File file = new File("target/test-classes/idempotent/.camel/FCOO1.nc");
    File renamed = new File("target/test-classes/idempotent/FCOO1.nc");
    file.renameTo(renamed);

    producer.sendBodyAndHeader(
        "file://target/test-classes/idempotent", Exchange.FILE_NAME, "FCOO1.nc");

    // let some time pass to let the consumer try to consume even though it cannot
    Thread.sleep(100);
    end.assertIsSatisfied();

    FileEndpoint fe = context.getEndpoint(uri, FileEndpoint.class);
    assertNotNull(fe);

    // Make sure that there are no incoming messages
    MemoryIdempotentRepository repo = (MemoryIdempotentRepository) fe.getInProgressRepository();
    assertEquals("Should be no in-progress files", 0, repo.getCacheSize());
  }
  @Test
  public void testFilePollerB() throws Exception {
    String expected = "xxx";
    final MockEndpoint mockEndpoint = getMockEndpoint("mock:" + routeATo);
    cc.getRouteDefinitions()
        .get(0)
        .adviceWith(
            cc,
            new RouteBuilder() {
              @Override
              public void configure() throws Exception {
                // intercept sending to mock:foo and do something else
                interceptSendToEndpoint(routeATo).to(mockEndpoint);
              }
            });

    mockEndpoint.expectedBodiesReceived(expected);

    Endpoint ep = cc.getEndpoint(routeAFrom);
    Assert.assertNotNull(ep);
    ProducerTemplate pt = cc.createProducerTemplate();
    pt.start();
    pt.sendBody(ep, expected);

    assertMockEndpointsSatisfied();
  }
  public boolean createEndpoint(String uri) throws Exception {
    if (context.hasEndpoint(uri) != null) {
      // endpoint already exists
      return false;
    }

    Endpoint endpoint = context.getEndpoint(uri);
    if (endpoint != null) {
      // ensure endpoint is registered, as the management strategy could have been configured to not
      // always
      // register new endpoints in JMX, so we need to check if its registered, and if not register
      // it manually
      ObjectName on =
          context
              .getManagementStrategy()
              .getManagementNamingStrategy()
              .getObjectNameForEndpoint(endpoint);
      if (on != null && !context.getManagementStrategy().getManagementAgent().isRegistered(on)) {
        // register endpoint as mbean
        Object me =
            context
                .getManagementStrategy()
                .getManagementObjectStrategy()
                .getManagedObjectForEndpoint(context, endpoint);
        context.getManagementStrategy().getManagementAgent().register(me, on);
      }
      return true;
    } else {
      return false;
    }
  }
  @Test
  public void testNotTooOldFilter() throws Exception {
    fakeFtpServer.start();
    int port = fakeFtpServer.getServerControlPort();
    context
        .getRouteDefinitions()
        .get(0)
        .adviceWith(
            context,
            new AdviceWithRouteBuilder() {
              @Override
              public void configure() throws Exception {
                replaceFromWith(
                    "ftp://[email protected]:"
                        + port
                        + "?password=password&filter=#notTooOld&passiveMode=true");
                weaveByType(ToDefinition.class).selectFirst().replace().to("mock:end");
              }
            });
    MockEndpoint end = context.getEndpoint("mock:end", MockEndpoint.class);
    end.expectedMessageCount(1);

    end.assertIsSatisfied();

    // Make sure that the received file has the correct file name
    String fileName =
        (String) end.getReceivedExchanges().get(0).getIn().getHeader(Exchange.FILE_NAME_ONLY);
    assertEquals(fileName, "DMI1.nc");

    fakeFtpServer.stop();
  }
  public boolean canSendToEndpoint(String endpointUri) {
    try {
      Endpoint endpoint = context.getEndpoint(endpointUri);
      if (endpoint != null) {
        Producer producer = endpoint.createProducer();
        return producer != null;
      }
    } catch (Exception e) {
      // ignore
    }

    return false;
  }