@Test public void testListToArrayInvalidCoercion() { try { @SuppressWarnings("serial") Integer[] val = coerce(ImmutableList.of("myWrongVal"), new TypeToken<Integer[]>() { }); Asserts.shouldHaveFailedPreviously("val="+val); } catch (ClassCoercionException e) { Asserts.expectedFailureContains(e, "Cannot coerce", "myWrongVal", "to java.lang.Integer"); } }
@Test public void testFailsIfNoNoargBuildMethod() { try { coerce(ImmutableMap.of("arg1", "val1"), MyClazzWithNoNoargBuildMethod.class); Asserts.shouldHaveFailedPreviously(); } catch (ClassCoercionException e) { Asserts.expectedFailureContains(e, "Builder for", "MyClazzWithNoNoargBuildMethod", "has no build() method"); } }
@Test public void testFailsIfExtraArgs() { try { coerce(ImmutableMap.of("arg1", "val1", "arg2", "val2", "arg3", "val3"), MyClazz.class); Asserts.shouldHaveFailedPreviously(); } catch (ClassCoercionException e) { Asserts.expectedFailureContains(e, "Builder for", "MyClazz", "failed to call method for arg3"); } }
@Test public void testFailsIfNoBuildMethod() { try { coerce(ImmutableMap.of("arg1", "val1"), MyClazzWithNoBuildMethod.class); Asserts.shouldHaveFailedPreviously(); } catch (ClassCoercionException e) { Asserts.expectedFailureContains(e, "Builder for", "MyClazzWithNoBuildMethod", "has no build() method"); } }
@Test public void testFailsIfNoNoargBuilderMethod() { try { coerce(ImmutableMap.of("arg1", "val1"), MyClazzWithNoNoargBuilderMethod.class); Asserts.shouldHaveFailedPreviously(); } catch (ClassCoercionException e) { Asserts.expectedFailureContains(e, "MyClazzWithNoNoargBuilderMethod", "no adapter known"); } }
@Test public void testDslNonDeferredInvalidMethod() throws Exception { try { createAndStartApplication( "services:", "- type: " + BasicApplication.class.getName(), " brooklyn.config:", " dest: $brooklyn:self().invalidMethod()"); Asserts.shouldHaveFailedPreviously("Non-existing non-deferred method should fail deployment"); } catch (CompoundRuntimeException e) { Asserts.expectedFailureContains(e, "No such function 'invalidMethod'"); } }
@Test public void testNoEntityTypeSpecifiedInTopLevelService() throws Exception { try { createAndStartApplication( "services:", "- foo: " + TestEntityImpl.class.getName()); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureContains(e, "must declare a type"); } }
@Test public void testArrayEntryInvalidCoercion() { try { @SuppressWarnings("serial") Integer[] val = coerce(new String[] {"myWrongVal"}, new TypeToken<Integer[]>() { }); Asserts.shouldHaveFailedPreviously("val="+val); } catch (ClassCoercionException e) { Asserts.expectedFailureContains(e, "Cannot coerce", "myWrongVal", "to java.lang.Integer"); } }
@Test public void testMaybeAbsentToOptional() { Maybe<String> m = Maybe.absent("nope"); Optional<String> o = m.toOptional(); Assert.assertFalse(o.isPresent()); try { o.get(); throw Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureContains(e, "absent"); } }
private void assertPortRulesException(Collection<String> portRules, Class<? extends Exception> type, String message) { try { Networking.portRulesToRanges(portRules); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { if (!type.isInstance(e)) throw e; Asserts.expectedFailureContains(e, message); } }
private void startAndAssertException(TestApplication app, Collection<? extends Location> locs) { try { app.start(locs); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureContains(e, "Error invoking start"); } }
@Test public void testDeferredDslChainingOnNullConfig() throws Exception { final Entity app = createAndStartApplication( "services:", "- type: " + BasicApplication.class.getName(), " brooklyn.config:", " dest: $brooklyn:config(\"targetEntity\").getId()"); try { assertEquals(getConfigEventually(app, DEST), app.getId()); Asserts.shouldHaveFailedPreviously("Expected to fail because targetEntity config is null"); } catch (Exception e) { Asserts.expectedFailureContains(e, "config(\"targetEntity\") evaluates to null"); } }
@Test public void testGetEntityFailsWithMessageIfLooksLikeApiErrorWithMessage() { try { BrooklynApi.getEntity( newBadRequestResponse("{ error: 400, message: \"Foo\" }"), TaskSummary.class); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureContains(e, "Foo"); } }
@Test public void testCoerceStringToPredicate() { assertEquals(coerce("alwaysFalse", Predicate.class), Predicates.alwaysFalse()); assertEquals(coerce("alwaysTrue", Predicate.class), Predicates.alwaysTrue()); assertEquals(coerce("isNull", Predicate.class), Predicates.isNull()); assertEquals(coerce("notNull", Predicate.class), Predicates.notNull()); try { coerce("wrongInput", Predicate.class); Asserts.shouldHaveFailedPreviously(); } catch (RuntimeException e) { Asserts.expectedFailureContains(e, "Cannot convert string 'wrongInput' to predicate"); } }
@Test public void testCreateEntityUsingPrivateConstructorFails() { try { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class).impl(TestEntityPrivateConstructorImpl.class)); Asserts.shouldHaveFailedPreviously("entity="+entity); } catch (Exception e) { Asserts.expectedFailureContains(e, "must have a no-argument constructor"); } } private static class TestEntityPrivateConstructorImpl extends TestEntityImpl {
private void assertAddEnricherThrowsIllegalStateException(EnricherSpec<?> spec, String expectedPhrase) { try { app.enrichers().add(spec); Asserts.shouldHaveFailedPreviously(); } catch (IllegalStateException e) { Asserts.expectedFailureContains(e, expectedPhrase); } } }
private void assertAddEnricherThrowsIllegalArgumentException(EnricherSpec<?> spec, String expectedPhrase) { try { app.enrichers().add(spec); Asserts.shouldHaveFailedPreviously(); } catch (IllegalArgumentException e) { Asserts.expectedFailureContains(e, expectedPhrase); } } }
private void assertLoadFails(ClassLoaderUtils clu, String bundledClassName) { BundledName className = new BundledName(bundledClassName); try { clu.loadClass(bundledClassName); Asserts.shouldHaveFailedPreviously("Using loader " + clu); } catch (ClassNotFoundException e) { Asserts.expectedFailureContains(e, bundledClassName, "not found on the application class path, nor in the bundle white list"); } BundledName resource = className.toResource(); String bundledResource = resource.toString(); assertNull(clu.getResource(bundledResource), resource + " is supposed to fail resource loading, but it was successful"); assertEquals(clu.getResources(bundledResource), ImmutableList.of(), resource + " is supposed to fail resource loading, but it was successful"); }
@Test public void testStartWithVanillaFailingChild() throws Exception { app.addChild(EntitySpec.create(FailingEntity.class) .configure(FailingEntity.FAIL_ON_START, true)); try { app.start(ImmutableList.of()); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureContains(e, "Simulating entity start failure for test"); } assertStateEventually(app, Lifecycle.RUNNING, Lifecycle.ON_FIRE, false); }
@Test public void testWaitFor() throws Exception { entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Duration timeout = Duration.ONE_MILLISECOND; Entities.waitFor(entity, applicationIdEqualTo(app.getApplicationId()), timeout); try { Entities.waitFor(entity, applicationIdEqualTo(app.getApplicationId() + "-wrong"), timeout); Asserts.shouldHaveFailedPreviously("Entities.waitFor() should have timed out"); } catch (Exception e) { Asserts.expectedFailureContains(e, "Timeout waiting for "); } }