private void expectContainerWithArrayValueAttributeButWrongComponentType() { exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("Container type")); exception.expectMessage(containsString(ContainerWithArrayValueAttributeButWrongComponentType.class.getName())); exception.expectMessage(containsString("must declare a 'value' attribute for an array of type")); exception.expectMessage(containsString(InvalidRepeatable.class.getName())); }
private void expectNonRepeatableAnnotation() { exception.expect(IllegalArgumentException.class); exception.expectMessage(startsWith("Annotation type must be a repeatable annotation")); exception.expectMessage(containsString("failed to resolve container type for")); exception.expectMessage(containsString(NonRepeatable.class.getName())); }
@Test public void withExplicitScheduledTaskRegistrar() throws InterruptedException { Assume.group(TestGroup.PERFORMANCE); ctx = new AnnotationConfigApplicationContext(ExplicitScheduledTaskRegistrarConfig.class); assertEquals(1, ctx.getBean(ScheduledTaskHolder.class).getScheduledTasks().size()); Thread.sleep(100); assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10)); assertThat(ctx.getBean(ExplicitScheduledTaskRegistrarConfig.class).threadName, startsWith("explicitScheduler1")); }
@Test public void synthesizeAnnotationWithAttributeAliasForMetaAnnotationThatIsNotMetaPresent() throws Exception { AliasedComposedContextConfigNotMetaPresent annotation = AliasedComposedContextConfigNotMetaPresentClass.class.getAnnotation(AliasedComposedContextConfigNotMetaPresent.class); exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("@AliasFor declaration on attribute 'xmlConfigFile' in annotation")); exception.expectMessage(containsString(AliasedComposedContextConfigNotMetaPresent.class.getName())); exception.expectMessage(containsString("declares an alias for attribute 'location' in meta-annotation")); exception.expectMessage(containsString(ContextConfig.class.getName())); exception.expectMessage(containsString("not meta-present")); synthesizeAnnotation(annotation); }
private void assertToStringForWebMappingWithPathAndValue(WebMapping webMapping) { String string = webMapping.toString(); assertThat(string, startsWith("@" + WebMapping.class.getName() + "(")); assertThat(string, containsString("value=[/test]")); assertThat(string, containsString("path=[/test]")); assertThat(string, containsString("name=bar")); assertThat(string, containsString("method=")); assertThat(string, containsString("[GET, POST]")); assertThat(string, endsWith(")")); }
@Test public void synthesizeAnnotationWithAttributeAliasForNonexistentAttribute() throws Exception { AliasForNonexistentAttribute annotation = AliasForNonexistentAttributeClass.class.getAnnotation(AliasForNonexistentAttribute.class); exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("Attribute 'foo' in")); exception.expectMessage(containsString(AliasForNonexistentAttribute.class.getName())); exception.expectMessage(containsString("is declared as an @AliasFor nonexistent attribute 'bar'")); synthesizeAnnotation(annotation); }
@Test public void synthesizeAnnotationWithAttributeAliasWithoutMirroredAliasFor() throws Exception { AliasForWithoutMirroredAliasFor annotation = AliasForWithoutMirroredAliasForClass.class.getAnnotation(AliasForWithoutMirroredAliasFor.class); exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("Attribute 'bar' in")); exception.expectMessage(containsString(AliasForWithoutMirroredAliasFor.class.getName())); exception.expectMessage(containsString("@AliasFor [foo]")); synthesizeAnnotation(annotation); }
@Test public void synthesizeAnnotationWhereAliasForIsMissingAttributeDeclaration() throws Exception { AliasForWithMissingAttributeDeclaration annotation = AliasForWithMissingAttributeDeclarationClass.class.getAnnotation(AliasForWithMissingAttributeDeclaration.class); exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("@AliasFor declaration on attribute 'foo' in annotation")); exception.expectMessage(containsString(AliasForWithMissingAttributeDeclaration.class.getName())); exception.expectMessage(containsString("points to itself")); synthesizeAnnotation(annotation); }
@Test public void synthesizeAnnotationWithAttributeAliasForAttributeWithDifferentDefaultValue() throws Exception { AliasForAttributeWithDifferentDefaultValue annotation = AliasForAttributeWithDifferentDefaultValueClass.class.getAnnotation(AliasForAttributeWithDifferentDefaultValue.class); exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("Misconfigured aliases")); exception.expectMessage(containsString(AliasForAttributeWithDifferentDefaultValue.class.getName())); exception.expectMessage(containsString("attribute 'foo' in annotation")); exception.expectMessage(containsString("attribute 'bar' in annotation")); exception.expectMessage(containsString("same default value")); synthesizeAnnotation(annotation); }
@Test public void synthesizeAnnotationFromMapWithAttributeOfIncorrectType() throws Exception { Map<String, Object> map = Collections.singletonMap(VALUE, 42L); exception.expect(IllegalArgumentException.class); exception.expectMessage(startsWith("Attributes map")); exception.expectMessage(containsString("returned a value of type [java.lang.Long]")); exception.expectMessage(containsString("for attribute 'value'")); exception.expectMessage(containsString("but a value of type [java.lang.String] is required")); exception.expectMessage(containsString("as defined by annotation type [" + Component.class.getName() + "]")); synthesizeAnnotation(map, Component.class, null); }
@Test public void synthesizeAnnotationWithAttributeAliasForAttributeOfDifferentType() throws Exception { AliasForAttributeOfDifferentType annotation = AliasForAttributeOfDifferentTypeClass.class.getAnnotation(AliasForAttributeOfDifferentType.class); exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("Misconfigured aliases")); exception.expectMessage(containsString(AliasForAttributeOfDifferentType.class.getName())); exception.expectMessage(containsString("attribute 'foo'")); exception.expectMessage(containsString("attribute 'bar'")); exception.expectMessage(containsString("same return type")); synthesizeAnnotation(annotation); }
@Test public void synthesizeAnnotationWithAttributeAliasWithMirroredAliasForWrongAttribute() throws Exception { AliasForWithMirroredAliasForWrongAttribute annotation = AliasForWithMirroredAliasForWrongAttributeClass.class.getAnnotation(AliasForWithMirroredAliasForWrongAttribute.class); exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("Attribute 'bar' in")); exception.expectMessage(containsString(AliasForWithMirroredAliasForWrongAttribute.class.getName())); exception.expectMessage(either(containsString("must be declared as an @AliasFor [foo], not [quux]")). or(containsString("is declared as an @AliasFor nonexistent attribute 'quux'"))); synthesizeAnnotation(annotation); }
@Test public void usesForwardedHostAsHostIfHeaderIsSet() throws Exception { this.request.addHeader("X-Forwarded-Host", "somethingDifferent"); adaptRequestFromForwardedHeaders(); UriComponents uriComponents = fromController(PersonControllerImpl.class).build(); assertThat(uriComponents.toUriString(), startsWith("http://somethingDifferent")); }
@Test public void usesFirstHostOfXForwardedHost() throws Exception { this.request.addHeader("X-Forwarded-Host", "barfoo:8888, localhost:8088"); adaptRequestFromForwardedHeaders(); UriComponents uriComponents = fromController(PersonControllerImpl.class).build(); assertThat(uriComponents.toUriString(), startsWith("http://barfoo:8888")); }
@Test public void setAttributesFromMessageWithMissingSessionId() { this.thrown.expect(IllegalStateException.class); this.thrown.expectMessage(startsWith("No session id in")); SimpAttributesContextHolder.setAttributesFromMessage(new GenericMessage<Object>("")); }
@Test public void testAttributeHamcrestMatchers() throws Exception { mockMvc.perform(get("/")) .andExpect(model().attribute("integer", equalTo(3))) .andExpect(model().attribute("string", allOf(startsWith("a string"), endsWith("value")))) .andExpect(model().attribute("person", hasProperty("name", equalTo("a name")))); }
@Test public void fromMethodCallPlain() { UriComponents uriComponents = fromMethodCall(on(ControllerWithMethods.class).myMethod(null)).build(); assertThat(uriComponents.toUriString(), startsWith("http://localhost")); assertThat(uriComponents.toUriString(), endsWith("/something/else")); }
@Test public void hamcrestMatcher() throws Exception { this.mockMvc.perform(get("/music/people")) .andExpect(jsonPath("$.composers[0].name", startsWith("Johann"))) .andExpect(jsonPath("$.performers[0].name", endsWith("Ashkenazy"))) .andExpect(jsonPath("$.performers[1].name", containsString("di Me"))) .andExpect(jsonPath("$.composers[1].name", isIn(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))); }
@Test public void contentStringStartsWith() throws Exception { this.mockServer.expect(content().string(startsWith("foo"))).andRespond(withSuccess()); executeAndVerify("foo123"); }
@Test public void setAttributesFromMessageWithMissingSessionAttributes() { this.thrown.expect(IllegalStateException.class); this.thrown.expectMessage(startsWith("No session attributes in")); SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(); headerAccessor.setSessionId("session1"); Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders()); SimpAttributesContextHolder.setAttributesFromMessage(message); }