@Test public void testExpectedFailures() { Asserts.expectedFailureOfType(new IllegalStateException(), NoSuchElementException.class, IllegalStateException.class); Asserts.expectedFailureOfType(new IllegalStateException(), NoSuchElementException.class, Object.class); try { Asserts.expectedFailureOfType(new IllegalStateException(), NoSuchElementException.class, Error.class); Asserts.shouldHaveFailedPreviously(); } catch (Throwable e) { Asserts.expectedFailure(e); } }
private void assertImmediatelyFails(Object orig, Class<? extends Exception> expected) { try { Maybe<Object> result = getImmediately(orig); Asserts.shouldHaveFailedPreviously("result="+result); } catch (Exception e) { Asserts.expectedFailureOfType(e, expected); } assertFalse(Thread.currentThread().isInterrupted()); }
@Test public void testMaybeToOptionalFailsOnNull() { Maybe<String> m = Maybe.ofAllowingNull(null); try { m.toOptional(); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureOfType(e, NullPointerException.class); } }
@Test public void testContextAwarePredicateInformedOfEntity() { try { app.createAndManageChild(EntitySpec.create(EntityWithContextAwareConstraint.class) .displayName("Mr. Big") .configure("must-be-display-name", "Mr. Bag")); fail("Expected exception when managing entity with incorrect config"); } catch (Exception e) { Asserts.expectedFailureOfType(e, ConstraintViolationException.class); } }
@Test public void startRequiresThatMemberSpecArgumentIsGiven() throws Exception { DynamicCluster c = app.createAndManageChild(EntitySpec.create(DynamicCluster.class)); try { c.start(ImmutableList.of(loc)); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureOfType(e, IllegalStateException.class); } }
@Test public void testEntityNotFound() throws Exception { BrooklynDslDeferredSupplier<?> dsl = BrooklynDslCommon.entity("myIdDoesNotExist"); Maybe<?> actualValue = execDslImmediately(dsl, Entity.class, app, true); Assert.assertTrue(actualValue.isAbsent()); try { actualValue.get(); Asserts.shouldHaveFailedPreviously("actual="+actualValue); } catch (Exception e) { Asserts.expectedFailureOfType(e, NoSuchElementException.class); } }
@Test public void startThenStopThenStartWithNewLocationFails() throws Exception { DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(TestEntity.class))); try { cluster.start(ImmutableList.of(loc)); cluster.stop(); cluster.start(ImmutableList.of(loc2)); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureOfType(e, IllegalStateException.class); } }
@Test public void testQuickFutureResolved() { // Result of task is -1, outside of the range specified by the config key. try { EntityRequiringConfigKeyInRange child = app.createAndManageChild(EntitySpec.create(EntityRequiringConfigKeyInRange.class) .configure(EntityRequiringConfigKeyInRange.RANGE, sleepingTask(Duration.ZERO, -1))); // may or may not fail above, depending on speed, but should fail if assert after forcing resolution Object value = child.getConfig(EntityRequiringConfigKeyInRange.RANGE); // NB the call above does not currently/necessarily apply validation log.debug(JavaClassNames.niceClassAndMethod()+" got "+value+" for "+EntityRequiringConfigKeyInRange.RANGE+", now explicitly validating"); ConfigConstraints.assertValid(child); fail("Expected exception when managing entity with incorrect config; instead passed assertion and got: "+value); } catch (Exception e) { Asserts.expectedFailureOfType(e, ConstraintViolationException.class); } }
Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureOfType(e, Resizable.InsufficientCapacityException.class); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureOfType(e, Resizable.InsufficientCapacityException.class);
@Test public void testThrowsIfTargetEffectorDoesntExist() { TestEntity a = app.createAndManageChild(EntitySpec.create(TestEntity.class)); ProxyEffector proxyEffector = new ProxyEffector(ImmutableMap.of( AddEffector.EFFECTOR_NAME, "proxy-effector", ProxyEffector.TARGET_ENTITY, a, ProxyEffector.TARGET_EFFECTOR_NAME, "kajnfksjdnfkjsdnf")); EntityInternal b = Entities.deproxy(app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(proxyEffector))); try { b.invoke(b.getEffector("proxy-effector"), ImmutableMap.of("arg", "value")) .getUnchecked(Duration.ONE_MINUTE); Asserts.shouldHaveFailedPreviously("expected exception when invoking effector that does not exist"); } catch (Exception e) { Asserts.expectedFailureOfType(e, NoSuchElementException.class); } }
@Test public void testResizeWhereChildThrowsNoMachineAvailableExceptionIsPropagatedAsInsufficientCapacityException() throws Exception { final DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(FailingEntity.class) .configure(FailingEntity.FAIL_ON_START, true) .configure(FailingEntity.EXCEPTION_CLAZZ, NoMachinesAvailableException.class)) .configure(DynamicCluster.INITIAL_SIZE, 0)); cluster.start(ImmutableList.of(loc)); try { cluster.resize(1); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureOfType(e, Resizable.InsufficientCapacityException.class); } }
@Test public void testResizeDirectly() throws Exception { assertSize(0); cluster.resize(2); assertSize(2); // Won't get a location to successfully resize (byon location only has 2 machines); // so still left with 2 members (failed node not quarantined, because exception well understood) try { cluster.resize(3); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureOfType(e, Resizable.InsufficientCapacityException.class); } assertSize(2, 0, 1); // Resize down; will delete one of our nodes cluster.resize(1); assertSize(1, 0, 2); // Resize back up to 2 should be allowed cluster.resize(2); assertSize(2, 0, 2); }
public void testGetImmediatelyDoesntFallBackToDeferredCallOnNotAvailable() throws Exception { final MyImmediateAndDeferredSupplier supplier = new MyImmediateAndDeferredSupplier(new ImmediateSupplier.ImmediateValueNotAvailableException()); Maybe<CallInfo> callInfo = Tasks.resolving(supplier).as(CallInfo.class).context(app).immediately(true).getMaybe(); Asserts.assertNotPresent(callInfo); try { callInfo.get(); Asserts.shouldHaveFailedPreviously("resolution should have failed now the ImmediateSupplier is not expected to fallback to other evaluation forms; instead got "+callInfo); } catch (Exception e) { Asserts.expectedFailureOfType(e, ImmediateValueNotAvailableException.class); } }