@Test public void testGetFirstThrowableMatching() throws Exception { NullPointerException npe = new NullPointerException("simulated"); IllegalStateException ise = new IllegalStateException("simulated", npe); assertEquals(Exceptions.getFirstThrowableMatching(ise, Predicates.instanceOf(IllegalStateException.class)), ise); assertEquals(Exceptions.getFirstThrowableMatching(ise, Predicates.instanceOf(NullPointerException.class)), npe); assertNull(Exceptions.getFirstThrowableMatching(ise, Predicates.alwaysFalse())); }
@Test public void testCustomizerPropagatesException() throws Exception { SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocationThrowsException.class) .configure("address", "4.3.2.1")); FixedListMachineProvisioningLocation<?> provisioningLoc = mgmt.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class) .configure("machines", MutableSet.of(machine))); MachineEntity entity = app.createAndManageChild(EntitySpec.create(MachineEntity.class) .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true) .configure(MachineEntity.PROVISIONING_PROPERTIES.subKey(JcloudsLocation.MACHINE_LOCATION_CUSTOMIZERS.getName()), ImmutableSet.of(customizer))); try { entity.start(ImmutableList.of(provisioningLoc)); fail(); } catch (RuntimeException e) { if (Exceptions.getFirstThrowableMatching(e, Predicates.compose(StringPredicates.containsLiteral("simulated failure"), Functions.toStringFunction())) == null) { throw e; }; assertFalse(Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class).isPresent()); } }
protected void runBuildCreateRolesExpectingFailure(Map<String, Map<String, ?>> roles, Class<? extends Exception> expectedException, String phraseToContain) throws Exception { stubbedMachine = app.getManagementContext().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) .configure("sshToolClass", RecordingSshTool.class.getName()) .configure("address", "1.2.3.4")); PostgreSqlNode pgsql = app.createAndManageChild(EntitySpec.create(PostgreSqlNode.class) .impl(PostgreSqlNodeForTestingImpl.class) .configure(PostgreSqlNode.ROLES, roles) .location(stubbedMachine)); PostgreSqlNodeForTestingImpl impl = (PostgreSqlNodeForTestingImpl) Entities.deproxy(pgsql); PostgreSqlSshDriver driver = (PostgreSqlSshDriver) impl.doInitDriver(stubbedMachine); try { String query = driver.buildCreateRolesQuery(); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Throwable e2 = Exceptions.getFirstThrowableMatching(e, Predicates.instanceOf(expectedException)); if (e2 == null) throw e; Asserts.expectedFailureContains(e2, phraseToContain); } } }