@Test public void synthesizeAnnotationWithAttributeAliasesWithDifferentValues() throws Exception { ContextConfig contextConfig = synthesizeAnnotation(ContextConfigMismatch.class.getAnnotation(ContextConfig.class)); exception.expect(AnnotationConfigurationException.class); getValue(contextConfig); }
private void assertMissingTextAttribute(Map<String, Object> attributes) { exception.expect(IllegalArgumentException.class); exception.expectMessage(startsWith("Attributes map")); exception.expectMessage(containsString("returned null for required attribute 'text'")); exception.expectMessage(containsString("defined by annotation type [" + AnnotationWithoutDefaults.class.getName() + "]")); synthesizeAnnotation(attributes, AnnotationWithoutDefaults.class, null); }
@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); }
@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 synthesizeAnnotationWhereAliasForHasDuplicateAttributeDeclaration() throws Exception { AliasForWithDuplicateAttributeDeclaration annotation = AliasForWithDuplicateAttributeDeclarationClass.class.getAnnotation(AliasForWithDuplicateAttributeDeclaration.class); exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("In @AliasFor declared on attribute 'foo' in annotation")); exception.expectMessage(containsString(AliasForWithDuplicateAttributeDeclaration.class.getName())); exception.expectMessage(containsString("attribute 'attribute' and its alias 'value' are present with values of [baz] and [bar]")); synthesizeAnnotation(annotation); }
@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 synthesizeAnnotationWithAttributeAliasForWithMissingDefaultValues() throws Exception { AliasForWithMissingDefaultValues annotation = AliasForWithMissingDefaultValuesClass.class.getAnnotation(AliasForWithMissingDefaultValues.class); exception.expect(AnnotationConfigurationException.class); exception.expectMessage(startsWith("Misconfigured aliases")); exception.expectMessage(containsString(AliasForWithMissingDefaultValues.class.getName())); exception.expectMessage(containsString("attribute 'foo' in annotation")); exception.expectMessage(containsString("attribute 'bar' in annotation")); exception.expectMessage(containsString("default values")); synthesizeAnnotation(annotation); }
@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 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 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 synthesizeAnnotationWithImplicitAliasesForAliasPair() throws Exception { Class<?> clazz = ImplicitAliasesForAliasPairContextConfigClass.class; ImplicitAliasesForAliasPairContextConfig config = clazz.getAnnotation(ImplicitAliasesForAliasPairContextConfig.class); assertNotNull(config); ImplicitAliasesForAliasPairContextConfig synthesizedConfig = synthesizeAnnotation(config); assertThat(synthesizedConfig, instanceOf(SynthesizedAnnotation.class)); assertEquals("xmlFile: ", "test.xml", synthesizedConfig.xmlFile()); assertEquals("groovyScript: ", "test.xml", synthesizedConfig.groovyScript()); }
@Test public void synthesizeAnnotationWithTransitiveImplicitAliasesForAliasPair() throws Exception { Class<?> clazz = TransitiveImplicitAliasesForAliasPairContextConfigClass.class; TransitiveImplicitAliasesForAliasPairContextConfig config = clazz.getAnnotation(TransitiveImplicitAliasesForAliasPairContextConfig.class); assertNotNull(config); TransitiveImplicitAliasesForAliasPairContextConfig synthesizedConfig = synthesizeAnnotation(config); assertThat(synthesizedConfig, instanceOf(SynthesizedAnnotation.class)); assertEquals("xml: ", "test.xml", synthesizedConfig.xml()); assertEquals("groovy: ", "test.xml", synthesizedConfig.groovy()); }
@Test public void synthesizeAnnotationWithTransitiveImplicitAliases() throws Exception { Class<?> clazz = TransitiveImplicitAliasesContextConfigClass.class; TransitiveImplicitAliasesContextConfig config = clazz.getAnnotation(TransitiveImplicitAliasesContextConfig.class); assertNotNull(config); TransitiveImplicitAliasesContextConfig synthesizedConfig = synthesizeAnnotation(config); assertThat(synthesizedConfig, instanceOf(SynthesizedAnnotation.class)); assertEquals("xml: ", "test.xml", synthesizedConfig.xml()); assertEquals("groovy: ", "test.xml", synthesizedConfig.groovy()); }
@Test public void synthesizeAnnotationFromMapWithAttributeAliasesThatOverrideArraysWithSingleElements() throws Exception { Map<String, Object> map = Collections.singletonMap("value", "/foo"); Get get = synthesizeAnnotation(map, Get.class, null); assertNotNull(get); assertEquals("value: ", "/foo", get.value()); assertEquals("path: ", "/foo", get.path()); map = Collections.singletonMap("path", "/foo"); get = synthesizeAnnotation(map, Get.class, null); assertNotNull(get); assertEquals("value: ", "/foo", get.value()); assertEquals("path: ", "/foo", get.path()); }
@Test public void synthesizeAnnotationWithoutAttributeAliases() throws Exception { Component component = WebController.class.getAnnotation(Component.class); assertNotNull(component); Component synthesizedComponent = synthesizeAnnotation(component); assertNotNull(synthesizedComponent); assertSame(component, synthesizedComponent); assertEquals("value attribute: ", "webController", synthesizedComponent.value()); }
private void assertAnnotationSynthesisWithImplicitAliasesWithImpliedAliasNamesOmitted( Class<?> clazz, String expected) { ImplicitAliasesWithImpliedAliasNamesOmittedContextConfig config = clazz.getAnnotation( ImplicitAliasesWithImpliedAliasNamesOmittedContextConfig.class); assertNotNull(config); ImplicitAliasesWithImpliedAliasNamesOmittedContextConfig synthesizedConfig = synthesizeAnnotation(config); assertThat(synthesizedConfig, instanceOf(SynthesizedAnnotation.class)); assertEquals("value: ", expected, synthesizedConfig.value()); assertEquals("locations: ", expected, synthesizedConfig.location()); assertEquals("xmlFiles: ", expected, synthesizedConfig.xmlFile()); }
@Test public void synthesizeAnnotationFromDefaultsWithAttributeAliases() throws Exception { ContextConfig contextConfig = synthesizeAnnotation(ContextConfig.class); assertNotNull(contextConfig); assertEquals("value: ", "", contextConfig.value()); assertEquals("location: ", "", contextConfig.location()); }
@Test public void synthesizeAnnotationFromDefaultsWithoutAttributeAliases() throws Exception { AnnotationWithDefaults annotationWithDefaults = synthesizeAnnotation(AnnotationWithDefaults.class); assertNotNull(annotationWithDefaults); assertEquals("text: ", "enigma", annotationWithDefaults.text()); assertTrue("predicate: ", annotationWithDefaults.predicate()); assertArrayEquals("characters: ", new char[] { 'a', 'b', 'c' }, annotationWithDefaults.characters()); }