@SuppressWarnings("unchecked") private void assertImmutable(Map<?,?> val) { try { ((Map<Object, Object>)val).put("myNewKey", "myNewVal"); Asserts.shouldHaveFailedPreviously("Map of type " + val.getClass().getName() + " was mutable"); } catch (UnsupportedOperationException e) { // expected - success } }
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 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 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"); } }
private Exception assertStartMethodFails(FailingEntity entity) { try { entity.start(locs); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailure(e); return e; } return null; }
@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 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 testFailsFastOnInvalidCoercion() { try { newLocation(MutableMap.of("int1", "thisisnotanint")); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { if (Exceptions.getFirstThrowableOfType(e, IllegalArgumentException.class) == null) { throw e; } } }
@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 testAddOddStringsAtRootIsError() { try { Jsonya.newInstance().add("a", 1, "b"); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureContainsIgnoreCase(e, "odd"); } }
@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 {
@Test public void testJsonyaDeepSimpleFailure() { try { Jsonya.of(europeMap()).atExisting("euroope"); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureContainsIgnoreCase(e, "euroope", "no", "found"); } }
private void assertAddEnricherThrowsIllegalStateException(EnricherSpec<?> spec, String expectedPhrase) { try { app.enrichers().add(spec); Asserts.shouldHaveFailedPreviously(); } catch (IllegalStateException 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 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 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 "); } }