org.springframework.core.annotation.AnnotationUtilsTests.ImplicitAliasesContextConfig Java Examples

The following examples show how to use org.springframework.core.annotation.AnnotationUtilsTests.ImplicitAliasesContextConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnnotationAttributesTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void getAliasedStringWithImplicitAliases() {
	String value = "metaverse";
	List<String> aliases = Arrays.asList("value", "location1", "location2", "location3", "xmlFile", "groovyScript");

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("value", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertEquals(value, attributes.getString(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("location1", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertEquals(value, attributes.getString(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("value", value);
	attributes.put("location1", value);
	attributes.put("xmlFile", value);
	attributes.put("groovyScript", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertEquals(value, attributes.getString(alias)));
}
 
Example #2
Source File: AnnotationAttributesTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void getAliasedStringWithImplicitAliases() {
	String value = "metaverse";
	List<String> aliases = Arrays.asList("value", "location1", "location2", "location3", "xmlFile", "groovyScript");

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("value", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertEquals(value, attributes.getString(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("location1", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertEquals(value, attributes.getString(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("value", value);
	attributes.put("location1", value);
	attributes.put("xmlFile", value);
	attributes.put("groovyScript", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertEquals(value, attributes.getString(alias)));
}
 
Example #3
Source File: AnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void getAliasedStringWithImplicitAliases() {
	final String value = "metaverse";
	final List<String> aliases = Arrays.asList("value", "location1", "location2", "location3", "xmlFile", "groovyScript");

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("value", value);
	aliases.stream().forEach(alias -> assertEquals(value, getAliasedStringWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("location1", value);
	aliases.stream().forEach(alias -> assertEquals(value, getAliasedStringWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("value", value);
	attributes.put("location1", value);
	attributes.put("xmlFile", value);
	attributes.put("groovyScript", value);
	aliases.stream().forEach(alias -> assertEquals(value, getAliasedStringWithImplicitAliases(alias)));
}
 
Example #4
Source File: AnnotationAttributesTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getAliasedStringArrayWithImplicitAliases() {
	String[] value = new String[] {"test.xml"};
	List<String> aliases = Arrays.asList("value", "location1", "location2", "location3", "xmlFile", "groovyScript");

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("location1", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("value", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("location1", value);
	attributes.put("value", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("location1", value);
	AnnotationUtils.registerDefaultValues(attributes);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("value", value);
	AnnotationUtils.registerDefaultValues(attributes);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	AnnotationUtils.registerDefaultValues(attributes);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(new String[] {""}, attributes.getStringArray(alias)));
}
 
Example #5
Source File: AnnotationAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void getAliasedStringArrayWithImplicitAliases() {
	String[] value = new String[] {"test.xml"};
	List<String> aliases = Arrays.asList("value", "location1", "location2", "location3", "xmlFile", "groovyScript");

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("location1", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("value", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("location1", value);
	attributes.put("value", value);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("location1", value);
	AnnotationUtils.registerDefaultValues(attributes);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	attributes.put("value", value);
	AnnotationUtils.registerDefaultValues(attributes);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(value, attributes.getStringArray(alias)));

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);
	AnnotationUtils.registerDefaultValues(attributes);
	AnnotationUtils.postProcessAnnotationAttributes(null, attributes, false);
	aliases.stream().forEach(alias -> assertArrayEquals(new String[] {""}, attributes.getStringArray(alias)));
}
 
Example #6
Source File: AbstractAliasAwareAnnotationAttributeExtractorTestCase.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void assertGetAttributeValueForImplicitAliases(Class<?> clazz, String expected) throws Exception {
	Method xmlFile = ImplicitAliasesContextConfig.class.getDeclaredMethod("xmlFile");
	Method groovyScript = ImplicitAliasesContextConfig.class.getDeclaredMethod("groovyScript");
	Method value = ImplicitAliasesContextConfig.class.getDeclaredMethod("value");

	AnnotationAttributeExtractor<?> extractor = createExtractorFor(clazz, expected, ImplicitAliasesContextConfig.class);

	assertThat(extractor.getAttributeValue(value), is(expected));
	assertThat(extractor.getAttributeValue(groovyScript), is(expected));
	assertThat(extractor.getAttributeValue(xmlFile), is(expected));
}
 
Example #7
Source File: MapAnnotationAttributeExtractorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void assertEnrichAndValidateAttributes(Map<String, Object> sourceAttributes, Map<String, Object> expected) {
	Class<? extends Annotation> annotationType = ImplicitAliasesContextConfig.class;

	// Since the ordering of attribute methods returned by the JVM is
	// non-deterministic, we have to rig the attributeAliasesCache in AnnotationUtils
	// so that the tests consistently fail in case enrichAndValidateAttributes() is
	// buggy.
	//
	// Otherwise, these tests would intermittently pass even for an invalid
	// implementation.
	Map<Class<? extends Annotation>, MultiValueMap<String, String>> attributeAliasesCache =
			(Map<Class<? extends Annotation>, MultiValueMap<String, String>>) AnnotationUtilsTests.getCache("attributeAliasesCache");

	// Declare aliases in an order that will cause enrichAndValidateAttributes() to
	// fail unless it considers all aliases in the set of implicit aliases.
	MultiValueMap<String, String> aliases = new LinkedMultiValueMap<String, String>();
	aliases.put("xmlFile", Arrays.asList("value", "groovyScript", "location1", "location2", "location3"));
	aliases.put("groovyScript", Arrays.asList("value", "xmlFile", "location1", "location2", "location3"));
	aliases.put("value", Arrays.asList("xmlFile", "groovyScript", "location1", "location2", "location3"));
	aliases.put("location1", Arrays.asList("xmlFile", "groovyScript", "value", "location2", "location3"));
	aliases.put("location2", Arrays.asList("xmlFile", "groovyScript", "value", "location1", "location3"));
	aliases.put("location3", Arrays.asList("xmlFile", "groovyScript", "value", "location1", "location2"));

	attributeAliasesCache.put(annotationType, aliases);

	MapAnnotationAttributeExtractor extractor = new MapAnnotationAttributeExtractor(sourceAttributes, annotationType, null);
	Map<String, Object> enriched = extractor.getSource();

	assertEquals("attribute map size", expected.size(), enriched.size());
	expected.keySet().stream().forEach( attr ->
		assertThat("for attribute '" + attr + "'", enriched.get(attr), is(expected.get(attr))));
}
 
Example #8
Source File: AnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getAliasedStringWithImplicitAliasesWithMissingAliasedAttributes() {
	final List<String> aliases = Arrays.asList("value", "location1", "location2", "location3", "xmlFile", "groovyScript");
	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);

	exception.expect(IllegalArgumentException.class);
	exception.expectMessage(startsWith("Neither attribute 'value' nor one of its aliases ["));
	aliases.stream().forEach(alias -> exception.expectMessage(containsString(alias)));
	exception.expectMessage(endsWith("] was found in attributes for annotation [" + ImplicitAliasesContextConfig.class.getName() + "]"));
	getAliasedStringWithImplicitAliases("value");
}
 
Example #9
Source File: AnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getAliasedStringArrayWithImplicitAliases() {
	final String[] INPUT = new String[] { "test.xml" };
	final String[] EMPTY = new String[0];
	final List<String> aliases = Arrays.asList("value", "location1", "location2", "location3", "xmlFile", "groovyScript");

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);

	attributes.put("location1", INPUT);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedStringArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("value", INPUT);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedStringArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("location1", INPUT);
	attributes.put("value", INPUT);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedStringArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("location1", INPUT);
	attributes.put("value", EMPTY);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedStringArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("location1", EMPTY);
	attributes.put("value", INPUT);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedStringArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("location1", EMPTY);
	attributes.put("value", EMPTY);
	aliases.stream().forEach(alias -> assertArrayEquals(EMPTY, getAliasedStringArrayWithImplicitAliases(alias)));
}
 
Example #10
Source File: AnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getAliasedStringArrayWithImplicitAliasesWithMissingAliasedAttributes() {
	final List<String> aliases = Arrays.asList("value", "location1", "location2", "location3", "xmlFile", "groovyScript");
	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);

	exception.expect(IllegalArgumentException.class);
	exception.expectMessage(startsWith("Neither attribute 'value' nor one of its aliases ["));
	aliases.stream().forEach(alias -> exception.expectMessage(containsString(alias)));
	exception.expectMessage(endsWith("] was found in attributes for annotation [" + ImplicitAliasesContextConfig.class.getName() + "]"));
	getAliasedStringArrayWithImplicitAliases("value");
}
 
Example #11
Source File: AnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private String[] getAliasedStringArrayWithImplicitAliases(String attributeName) {
	// Note: even though the attributes we test against here are of type
	// String instead of String[], it doesn't matter... since
	// AnnotationAttributes does not validate the actual return type of
	// attributes in the annotation.
	return this.attributes.getAliasedStringArray(attributeName, ImplicitAliasesContextConfig.class, null);
}
 
Example #12
Source File: AnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getAliasedClassArrayWithImplicitAliases() {
	final Class<?>[] INPUT = new Class<?>[] { String.class };
	final Class<?>[] EMPTY = new Class<?>[0];
	final List<String> aliases = Arrays.asList("value", "location1", "location2", "location3", "xmlFile", "groovyScript");

	attributes = new AnnotationAttributes(ImplicitAliasesContextConfig.class);

	attributes.put("location1", INPUT);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedClassArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("value", INPUT);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedClassArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("location1", INPUT);
	attributes.put("value", INPUT);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedClassArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("location1", INPUT);
	attributes.put("value", EMPTY);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedClassArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("location1", EMPTY);
	attributes.put("value", INPUT);
	aliases.stream().forEach(alias -> assertArrayEquals(INPUT, getAliasedClassArrayWithImplicitAliases(alias)));

	attributes.clear();
	attributes.put("location1", EMPTY);
	attributes.put("value", EMPTY);
	aliases.stream().forEach(alias -> assertArrayEquals(EMPTY, getAliasedClassArrayWithImplicitAliases(alias)));
}
 
Example #13
Source File: AnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Class<?>[] getAliasedClassArrayWithImplicitAliases(String attributeName) {
	// Note: even though the attributes we test against here are of type
	// String instead of Class<?>[], it doesn't matter... since
	// AnnotationAttributes does not validate the actual return type of
	// attributes in the annotation.
	return this.attributes.getAliasedClassArray(attributeName, ImplicitAliasesContextConfig.class, null);
}
 
Example #14
Source File: AbstractAliasAwareAnnotationAttributeExtractorTestCase.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void assertGetAttributeValueForImplicitAliases(Class<?> clazz, String expected) throws Exception {
	Method xmlFile = ImplicitAliasesContextConfig.class.getDeclaredMethod("xmlFile");
	Method groovyScript = ImplicitAliasesContextConfig.class.getDeclaredMethod("groovyScript");
	Method value = ImplicitAliasesContextConfig.class.getDeclaredMethod("value");

	AnnotationAttributeExtractor<?> extractor = createExtractorFor(clazz, expected, ImplicitAliasesContextConfig.class);

	assertThat(extractor.getAttributeValue(value), is(expected));
	assertThat(extractor.getAttributeValue(groovyScript), is(expected));
	assertThat(extractor.getAttributeValue(xmlFile), is(expected));
}
 
Example #15
Source File: AnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private String getAliasedStringWithImplicitAliases(String attributeName) {
	return this.attributes.getAliasedString(attributeName, ImplicitAliasesContextConfig.class, null);
}