public void testRecipientListAsBeanError() throws Exception {
    counter = 0;

    context.addRoutes(
        new RouteBuilder() {
          @Override
          public void configure() throws Exception {
            context.setTracing(true);

            onException(Exception.class).maximumRedeliveries(2);

            from("direct:start").to("mock:a").bean(MyRecipientBean.class);
          }
        });
    context.start();

    getMockEndpoint("mock:a").expectedMessageCount(1);
    getMockEndpoint("mock:foo").expectedMessageCount(1);
    getMockEndpoint("mock:bar").expectedMessageCount(1);
    getMockEndpoint("mock:baz").expectedMessageCount(0);

    try {
      template.sendBody("direct:start", "Hello World");
      fail("Should throw exception");
    } catch (CamelExecutionException e) {
      // expected
      assertIsInstanceOf(CamelExchangeException.class, e.getCause());
      assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause());
      assertEquals("Damn", e.getCause().getCause().getMessage());
    }

    assertMockEndpointsSatisfied();

    assertEquals(3, counter);
  }
 public void testFail() throws Exception {
   try {
     template.sendBody("direct:fail", "Hello World");
     fail("Should throw an exception");
   } catch (CamelExecutionException e) {
     assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
     AmbiguousMethodCallException ace = (AmbiguousMethodCallException) e.getCause();
     assertEquals(2, ace.getMethods().size());
   }
 }
  @Test
  public void testFailIfNoConsumersAfterConsumersLeave() throws Exception {
    context.addRoutes(
        new RouteBuilder() {
          @Override
          public void configure() throws Exception {
            from("direct-vm:foo").routeId("stopThisRoute").to("mock:foo");
          }
        });

    context.start();

    getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");

    template.sendBody("direct-vm:foo", "Hello World");

    assertMockEndpointsSatisfied();

    context.stopRoute("stopThisRoute");
    TimeUnit.MILLISECONDS.sleep(100);
    try {
      template.sendBody("direct-vm:foo", "Hello World");
      fail("Should throw an exception");
    } catch (CamelExecutionException e) {
      assertIsInstanceOf(DirectVmConsumerNotAvailableException.class, e.getCause());
    }
  }
  public void testNoExistingPropertiesComponentWithoutLocation() throws Exception {
    context.removeComponent("properties");
    context.addRoutes(
        new RouteBuilder() {
          @Override
          public void configure() throws Exception {
            from("direct:start")
                .transform()
                .simple("Hi ${body} do you think ${properties:cheese.quote}?");
          }
        });
    context.start();

    try {
      template.requestBody("direct:start", "Claus", String.class);
      fail("Should have thrown exception");
    } catch (CamelExecutionException e) {
      RuntimeCamelException rce = assertIsInstanceOf(RuntimeCamelException.class, e.getCause());
      IllegalArgumentException iae =
          assertIsInstanceOf(IllegalArgumentException.class, rce.getCause());
      assertEquals(
          "PropertiesComponent with name properties must be defined in CamelContext to support property placeholders in expressions",
          iae.getMessage());
    }
  }
  @Test
  public void testABCClose() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    // we expect ABC in the published message
    // notice: Only 1 message is expected
    mock.expectedBodiesReceived("ABC");

    // send the first message
    template.sendBodyAndHeader("direct:start", "A", "myId", 1);
    // send the 2nd message with the same correlation key
    template.sendBodyAndHeader("direct:start", "B", "myId", 1);
    // the F message has another correlation key
    template.sendBodyAndHeader("direct:start", "F", "myId", 2);
    // now we have 3 messages with the same correlation key
    // and the Aggregator should publish the message
    template.sendBodyAndHeader("direct:start", "C", "myId", 1);

    // sending with correlation id 1 should fail as its closed
    try {
      template.sendBodyAndHeader("direct:start", "A2", "myId", 1);
    } catch (CamelExecutionException e) {
      ClosedCorrelationKeyException cause =
          assertIsInstanceOf(ClosedCorrelationKeyException.class, e.getCause());
      assertEquals("1", cause.getCorrelationKey());
    }

    assertMockEndpointsSatisfied();
  }
  public void testAlways() throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(0);

    try {
      template.sendBody("direct:start", "Hello World");
      fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
      assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
      assertEquals("Forced", e.getCause().getMessage());
    }

    assertMockEndpointsSatisfied();

    // 3 + 1 C and A should be last
    assertEquals("CCCCA", done);
  }
  @Test
  public void testHandleException() throws Exception {
    for (int i = 0; i < DELIVERY_COUNT; i++) {
      MockEndpoint finish = consumerContext.getEndpoint("mock:finish" + i, MockEndpoint.class);
      finish.whenAnyExchangeReceived(
          new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
              throw new RuntimeException("TestException!!!");
            }
          });
      finish.expectedBodiesReceived("1234567890");
    }
    MockEndpoint finish4 = consumerContext.getEndpoint("mock:finish4", MockEndpoint.class);
    finish4.expectedBodiesReceived("1234567890-1");

    MockEndpoint exception = producerContext.getEndpoint("mock:exception", MockEndpoint.class);
    exception.expectedBodiesReceived("1234567890");

    ProducerTemplate producerTemplate = producerContext.createProducerTemplate();
    try {
      producerTemplate.sendBody("direct:start", "1234567890");
      fail("CamelExecutionException expected");
    } catch (CamelExecutionException e) {
      assertEquals("TestException!!!", e.getCause().getMessage());
    }
    producerTemplate.sendBody("direct:start", "1234567890-1");

    MockEndpoint.assertIsSatisfied(consumerContext);
    MockEndpoint.assertIsSatisfied(producerContext);
  }
  public void testRecipientListDoNotStopOnException() throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(0);
    getMockEndpoint("mock:a").expectedMessageCount(1);
    getMockEndpoint("mock:b").expectedMessageCount(1);
    getMockEndpoint("mock:c").expectedMessageCount(1);

    try {
      template.sendBodyAndHeader(
          "direct:start", "Hello World", "foo", "direct:a,direct:b,direct:c");
      fail("Should have thrown exception");
    } catch (CamelExecutionException e) {
      assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
      assertEquals("Damn", e.getCause().getMessage());
    }

    assertMockEndpointsSatisfied();
  }
  @Test
  public void testRollback() throws Exception {
    try {
      template.sendBodyAndHeader("direct:confirm", "bumper", "to", "FATAL");
      fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
      assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
      assertEquals("Simulated fatal error", e.getCause().getMessage());
    }

    // give time for onCompletion to execute
    // as its being executed asynchronously in another thread
    Thread.sleep(1000);

    File file = new File("target/mail/backup/");
    String[] files = file.list();
    assertEquals("There should be no files", 0, files.length);
  }
 @Test
 public void testRequestTimeout() throws Exception {
   try {
     template.requestBody("direct:start", data, String.class);
     fail("Should have thrown exception");
   } catch (CamelExecutionException e) {
     ReadTimeoutException cause = assertIsInstanceOf(ReadTimeoutException.class, e.getCause());
     assertNotNull(cause);
   }
 }
 public void testVmTimeoutWithProcessor() throws Exception {
   try {
     template.requestBody("vm:start2?timeout=4000", "Hello");
     fail("Should have thrown an exception");
   } catch (CamelExecutionException e) {
     ExchangeTimedOutException cause =
         assertIsInstanceOf(ExchangeTimedOutException.class, e.getCause());
     assertEquals(2000, cause.getTimeout());
   }
 }
  @Test
  public void testSignAndVerifyMessage() throws Exception {
    getMockEndpoint("mock:signed").expectedBodiesReceived("Hello World");

    try {
      template.sendBody("direct:sign", "Hello World");
    } catch (CamelExecutionException e) {
      assertMockEndpointsSatisfied();
      assertIsInstanceOf(SignatureException.class, e.getCause());
    }
  }
 public void testProducerEmptyDoneFileName() throws Exception {
   try {
     template.sendBodyAndHeader(
         "file:target/done?doneFileName=", "Hello World", Exchange.FILE_NAME, "hello.txt");
     fail("Should have thrown exception");
   } catch (CamelExecutionException e) {
     IllegalArgumentException cause =
         assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
     assertTrue(
         cause.getMessage(),
         cause.getMessage().startsWith("doneFileName must be specified and not empty"));
   }
 }
 @Test
 public void testMailNoRecipients() throws Exception {
   try {
     template.sendBody("direct:a", "Hello World");
     fail("Should have thrown exception");
   } catch (CamelExecutionException e) {
     MailPreparationException mpe =
         assertIsInstanceOf(MailPreparationException.class, e.getCause());
     IllegalArgumentException iae =
         assertIsInstanceOf(IllegalArgumentException.class, mpe.getCause());
     assertEquals("The mail message does not have any recipients set.", iae.getMessage());
   }
 }
  public void testRedeliveryCounterIsResetWhenHandled() throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(0);
    getMockEndpoint("mock:other").expectedMessageCount(0);

    try {
      template.sendBody("direct:start", "Hello World");
      fail("Should throw an exception");
    } catch (CamelExecutionException e) {
      assertIsInstanceOf(ConnectException.class, e.getCause());
    }

    assertMockEndpointsSatisfied();

    assertEquals("12312345", counter);
  }
 public void testProducerInvalidDoneFileName() throws Exception {
   try {
     template.sendBodyAndHeader(
         "file:target/done?doneFileName=${file:parent}/foo",
         "Hello World",
         Exchange.FILE_NAME,
         "hello.txt");
     fail("Should have thrown exception");
   } catch (CamelExecutionException e) {
     ExpressionIllegalSyntaxException cause =
         assertIsInstanceOf(ExpressionIllegalSyntaxException.class, e.getCause());
     assertTrue(
         cause.getMessage(),
         cause.getMessage().endsWith("Cannot resolve reminder: ${file:parent}/foo"));
   }
 }
Esempio n. 17
0
  public void testSynchronizeFailure() throws Exception {
    getMockEndpoint("mock:sync").expectedMessageCount(1);
    getMockEndpoint("mock:sync").expectedPropertyReceived(Exchange.ON_COMPLETION, true);

    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(0);

    try {
      template.sendBody("direct:start", "Kabom");
      fail("Should throw exception");
    } catch (CamelExecutionException e) {
      assertEquals("Kabom", e.getCause().getMessage());
    }

    assertMockEndpointsSatisfied();
  }
Esempio n. 18
0
  @Test
  public void testStreamCacheToFileShouldBeDeletedInCaseOfException() throws Exception {
    try {
      template.requestBody("direct:start", null, String.class);
      fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
      HttpOperationFailedException hofe =
          assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
      String s = context.getTypeConverter().convertTo(String.class, hofe.getResponseBody());
      assertEquals("Response body", body, s);
    }

    // the temporary files should have been deleted
    File file = new File("target/cachedir");
    String[] files = file.list();
    assertEquals("There should be no files", 0, files.length);
  }
  public void testThrowExceptionAtA() throws Exception {
    counter = 0;

    getMockEndpoint("mock:a").expectedMessageCount(1);
    getMockEndpoint("mock:b").expectedMessageCount(0);

    try {
      template.sendBody("direct:test2", "Hello World");
      fail("Should have thrown exception");
    } catch (CamelExecutionException e) {
      assertEquals("Forced", e.getCause().getCause().getMessage());
    }

    assertMockEndpointsSatisfied();

    assertEquals(1 + 3, counter); // first call + 3 redeliveries
  }
  @Test
  /** Operation: DELETE Body contains: null */
  public void testDeleteWithNull() throws Exception {
    resetMocks();

    boolean threwException = false;

    // Exercise the route with a DELETE operation
    try {
      template.sendBodyAndHeader("direct:sampleInput", null, "Operation", "DELETE");
    } catch (CamelExecutionException cee) {
      Assert.isInstanceOf(FrameworkProducerException.class, cee.getCause());
      threwException = true;
    }

    Assert.isTrue(threwException);
  }
  @Test
  public void testAsyncEndpointRollback() {
    end.whenAnyExchangeReceived(
        new Processor() {
          public void process(Exchange exchange) throws Exception {
            throw new IllegalArgumentException("Damn");
          }
        });

    try {
      producer.sendBody(3);
      fail("Should have thrown exception");
    } catch (CamelExecutionException e) {
      assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause());
      assertEquals(0, table.values().size());
    }
  }
 @Test
 public void pipelineAborts() {
   end.expectedMessageCount(1);
   end.whenAnyExchangeReceived(
       new Processor() {
         public void process(Exchange exchange) throws Exception {
           throw new MyException();
         }
       });
   after.expectedMessageCount(1);
   try {
     producer.sendBody(3);
     assertFalse(true);
   } catch (CamelExecutionException e) {
     assertEquals(MyException.class, e.getCause().getCause().getClass());
     assertEquals(0, table.values().size());
   }
 }
  @Test
  /** Operation: DELETE Body contains: List<String> with no members */
  public void testDeleteWithEmptyListOfIds() throws Exception {
    resetMocks();

    final List<String> metacardIdList = new ArrayList<String>();
    boolean threwException = false;

    // Exercise the route with a DELETE operation
    try {
      template.sendBodyAndHeader("direct:sampleInput", metacardIdList, "Operation", "DELETE");
    } catch (CamelExecutionException cee) {
      Assert.isInstanceOf(FrameworkProducerException.class, cee.getCause());
      threwException = true;
    }

    Assert.isTrue(threwException);
  }
  @Test
  /** Operation: UPDATE Body contains: String */
  public void testUpdateWithInvalidType() throws Exception {
    resetMocks();

    boolean threwException = false;

    // Exercise the route with a UPDATE operation
    try {
      template.sendBodyAndHeader(
          "direct:sampleInput", new String("WRONG TYPE"), "Operation", "UPDATE");
    } catch (CamelExecutionException cee) {
      Assert.isInstanceOf(FrameworkProducerException.class, cee.getCause());
      threwException = true;
    }

    Assert.isTrue(threwException);
  }
  public void testInOut() throws Exception {
    context.addRoutes(
        new RouteBuilder() {
          @Override
          public void configure() throws Exception {
            from("direct-vm:start").to("direct-vm:foo");
          }
        });

    context.start();

    try {
      template.requestBody("direct-vm:start", "Hello World");
      fail("Should throw an exception");
    } catch (CamelExecutionException e) {
      assertIsInstanceOf(DirectVmConsumerNotAvailableException.class, e.getCause());
    }
  }
  @Test
  /** Operation: CREATE Body contains: List<String> */
  public void testCreateWithInvalidListType() throws Exception {
    resetMocks();

    final List<String> metacards = new ArrayList<String>();
    metacards.add(new String("WRONG TYPE"));

    boolean threwException = false;

    // Exercise the route with a CREATE operation
    try {
      template.sendBodyAndHeader("direct:sampleInput", metacards, "Operation", "CREATE");
    } catch (CamelExecutionException cee) {
      Assert.isInstanceOf(FrameworkProducerException.class, cee.getCause());
      threwException = true;
    }

    Assert.isTrue(threwException);
  }
  @Test
  /** Operation: UPDATE Body contains: List<Metacard> with null member */
  public void testUpdateWithNullInListOfMetacards() throws Exception {
    resetMocks();

    final List<Metacard> metacards = new ArrayList<Metacard>();
    metacards.add(null);

    boolean threwException = false;

    // Exercise the route with a UPDATE operation
    try {
      template.sendBodyAndHeader("direct:sampleInput", metacards, "Operation", "UPDATE");
    } catch (CamelExecutionException cee) {
      Assert.isInstanceOf(FrameworkProducerException.class, cee.getCause());
      threwException = true;
    }

    Assert.isTrue(threwException);
  }
  public void testMulticastParalllelStopOnExceptionStop() throws Exception {
    // we run in parallel so we may get 0 or 1 messages
    getMockEndpoint("mock:foo").expectedMinimumMessageCount(0);
    getMockEndpoint("mock:bar").expectedMinimumMessageCount(0);
    getMockEndpoint("mock:baz").expectedMinimumMessageCount(0);
    // we should not complete and thus 0
    getMockEndpoint("mock:result").expectedMessageCount(0);

    try {
      template.sendBody("direct:start", "Kaboom");
      fail("Should thrown an exception");
    } catch (CamelExecutionException e) {
      ExecutionException ee = assertIsInstanceOf(ExecutionException.class, e.getCause());
      CamelExchangeException cause =
          assertIsInstanceOf(CamelExchangeException.class, ee.getCause());
      assertTrue(cause.getMessage().startsWith("Parallel processing failed for number "));
      assertTrue(cause.getMessage().contains("Exchange[Message: Kaboom]"));
      assertEquals("Forced", cause.getCause().getMessage());
    }

    assertMockEndpointsSatisfied();
  }
  @Test
  /** Operation: DELETE Body contains: Integer */
  public void testDeleteWithInvalidType() throws Exception {
    resetMocks();

    class InvalidObject {
      public String toString() {
        return null;
      }
    }

    boolean threwException = false;

    // Exercise the route with a DELETE operation
    try {
      template.sendBodyAndHeader("direct:sampleInput", new InvalidObject(), "Operation", "DELETE");
    } catch (CamelExecutionException cee) {
      Assert.isInstanceOf(FrameworkProducerException.class, cee.getCause());
      threwException = true;
    }

    Assert.isTrue(threwException);
  }
  @Test
  public void testSplitStopOnException() throws Exception {
    MockEndpoint split = getMockEndpoint("mock:split");
    // we expect 1 messages to be split since the 2nd message should fail
    split.expectedBodiesReceived("Camel rocks");

    // and no combined aggregated message since we stop on exception
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(0);

    // now send a message with an unknown letter (F) which forces an exception to occur
    try {
      template.sendBody("direct:start", "A,F,C");
      fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
      CamelExchangeException cause = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
      IllegalArgumentException iae =
          assertIsInstanceOf(IllegalArgumentException.class, cause.getCause());
      assertEquals("Key not a known word F", iae.getMessage());
    }

    assertMockEndpointsSatisfied();
  }