org.springframework.tests.sample.beans.IndexedTestBean Java Examples

The following examples show how to use org.springframework.tests.sample.beans.IndexedTestBean. 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: AbstractPropertyAccessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyNonMatchingType() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	Collection<String> coll = new ArrayList<>();
	coll.add("coll1");
	accessor.setPropertyValue("collection", coll);
	List<String> set = new LinkedList<>();
	set.add("set1");
	accessor.setPropertyValue("set", set);
	List<String> sortedSet = new ArrayList<>();
	sortedSet.add("sortedSet1");
	accessor.setPropertyValue("sortedSet", sortedSet);
	Set<String> list = new HashSet<>();
	list.add("list1");
	accessor.setPropertyValue("list", list);
	assertEquals(1, target.getCollection().size());
	assertTrue(target.getCollection().containsAll(coll));
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().containsAll(set));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().containsAll(sortedSet));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().containsAll(list));
}
 
Example #2
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSpecificEditorForNestedIndexedField() {
	IndexedTestBean tb = new IndexedTestBean();
	tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
	tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "array[0].nestedIndexedBean.list.name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("list" + text);
		}
		@Override
		public String getAsText() {
			return ((String) getValue()).substring(4);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
	pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
	binder.bind(pvs);
	assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
	assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
	assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
	assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
 
Example #3
Source File: AbstractPropertyAccessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyWithArrayValue() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	Collection<String> coll = new HashSet<>();
	coll.add("coll1");
	accessor.setPropertyValue("collection", coll.toArray());
	List<String> set = new LinkedList<>();
	set.add("set1");
	accessor.setPropertyValue("set", set.toArray());
	List<String> sortedSet = new ArrayList<>();
	sortedSet.add("sortedSet1");
	accessor.setPropertyValue("sortedSet", sortedSet.toArray());
	Set<String> list = new HashSet<>();
	list.add("list1");
	accessor.setPropertyValue("list", list.toArray());
	assertEquals(1, target.getCollection().size());
	assertTrue(target.getCollection().containsAll(coll));
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().containsAll(set));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().containsAll(sortedSet));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().containsAll(list));
}
 
Example #4
Source File: CustomEditorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testIndexedPropertiesWithListPropertyEditor() {
	IndexedTestBean bean = new IndexedTestBean();
	BeanWrapper bw = new BeanWrapperImpl(bean);
	bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			List<TestBean> result = new ArrayList<>();
			result.add(new TestBean("list" + text, 99));
			setValue(result);
		}
	});
	bw.setPropertyValue("list", "1");
	assertEquals("list1", ((TestBean) bean.getList().get(0)).getName());
	bw.setPropertyValue("list[0]", "test");
	assertEquals("test", bean.getList().get(0));
}
 
Example #5
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBindingWithResortedList() {
	IndexedTestBean tb = new IndexedTestBean();
	DataBinder binder = new DataBinder(tb, "tb");
	MutablePropertyValues pvs = new MutablePropertyValues();
	TestBean tb1 = new TestBean("tb1", 99);
	TestBean tb2 = new TestBean("tb2", 99);
	pvs.add("list[0]", tb1);
	pvs.add("list[1]", tb2);
	binder.bind(pvs);
	assertEquals(tb1.getName(), binder.getBindingResult().getFieldValue("list[0].name"));
	assertEquals(tb2.getName(), binder.getBindingResult().getFieldValue("list[1].name"));
	tb.getList().set(0, tb2);
	tb.getList().set(1, tb1);
	assertEquals(tb2.getName(), binder.getBindingResult().getFieldValue("list[0].name"));
	assertEquals(tb1.getName(), binder.getBindingResult().getFieldValue("list[1].name"));
}
 
Example #6
Source File: AbstractPropertyAccessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyWithStringValue() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	List<String> set = new LinkedList<>();
	set.add("set1");
	accessor.setPropertyValue("set", "set1");
	List<String> sortedSet = new ArrayList<>();
	sortedSet.add("sortedSet1");
	accessor.setPropertyValue("sortedSet", "sortedSet1");
	Set<String> list = new HashSet<>();
	list.add("list1");
	accessor.setPropertyValue("list", "list1");
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().containsAll(set));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().containsAll(sortedSet));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().containsAll(list));
}
 
Example #7
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testInnerSpecificEditorForNestedIndexedField() {
	IndexedTestBean tb = new IndexedTestBean();
	tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
	tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list[0].name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("list" + text);
		}
		@Override
		public String getAsText() {
			return ((String) getValue()).substring(4);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
	pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
	binder.bind(pvs);
	assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
	assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
	assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
	assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
 
Example #8
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testBindingWithResortedList() {
	IndexedTestBean tb = new IndexedTestBean();
	DataBinder binder = new DataBinder(tb, "tb");
	MutablePropertyValues pvs = new MutablePropertyValues();
	TestBean tb1 = new TestBean("tb1", 99);
	TestBean tb2 = new TestBean("tb2", 99);
	pvs.add("list[0]", tb1);
	pvs.add("list[1]", tb2);
	binder.bind(pvs);
	assertEquals(tb1.getName(), binder.getBindingResult().getFieldValue("list[0].name"));
	assertEquals(tb2.getName(), binder.getBindingResult().getFieldValue("list[1].name"));
	tb.getList().set(0, tb2);
	tb.getList().set(1, tb1);
	assertEquals(tb2.getName(), binder.getBindingResult().getFieldValue("list[0].name"));
	assertEquals(tb1.getName(), binder.getBindingResult().getFieldValue("list[1].name"));
}
 
Example #9
Source File: BindTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bindTagWithIndexedProperties() throws JspException {
	PageContext pc = createPageContext();
	IndexedTestBean tb = new IndexedTestBean();
	Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
	errors.rejectValue("array[0]", "code1", "message1");
	errors.rejectValue("array[0]", "code2", "message2");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.array[0]");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
	assertTrue("Value is TestBean", status.getValue() instanceof TestBean);
	assertTrue("Correct value", "name0".equals(((TestBean) status.getValue()).getName()));
	assertTrue("Correct isError", status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 2);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
	assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
	assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
	assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0]));
	assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
}
 
Example #10
Source File: BindTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bindTagWithMappedProperties() throws JspException {
	PageContext pc = createPageContext();
	IndexedTestBean tb = new IndexedTestBean();
	Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
	errors.rejectValue("map[key1]", "code1", "message1");
	errors.rejectValue("map[key1]", "code2", "message2");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.map[key1]");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "map[key1]".equals(status.getExpression()));
	assertTrue("Value is TestBean", status.getValue() instanceof TestBean);
	assertTrue("Correct value", "name4".equals(((TestBean) status.getValue()).getName()));
	assertTrue("Correct isError", status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 2);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
	assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
	assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
	assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0]));
	assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
}
 
Example #11
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSpecificEditorForNestedIndexedField() {
	IndexedTestBean tb = new IndexedTestBean();
	tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
	tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "array[0].nestedIndexedBean.list.name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("list" + text);
		}
		@Override
		public String getAsText() {
			return ((String) getValue()).substring(4);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
	pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
	binder.bind(pvs);
	assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
	assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
	assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
	assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
 
Example #12
Source File: PropertyResourceConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPropertyOverrideConfigurerWithNestedPropertyAndDotInBeanName() {
	BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
	factory.registerBeanDefinition("my.tb", def);

	PropertyOverrideConfigurer poc;
	poc = new PropertyOverrideConfigurer();
	Properties props = new Properties();
	props.setProperty("my.tb_array[0].age", "99");
	props.setProperty("my.tb_list[1].name", "test");
	poc.setProperties(props);
	poc.setBeanNameSeparator("_");
	poc.postProcessBeanFactory(factory);

	IndexedTestBean tb = (IndexedTestBean) factory.getBean("my.tb");
	assertEquals(99, tb.getArray()[0].getAge());
	assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
}
 
Example #13
Source File: PropertyResourceConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPropertyOverrideConfigurerWithNestedProperty() {
	BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
	factory.registerBeanDefinition("tb", def);

	PropertyOverrideConfigurer poc;
	poc = new PropertyOverrideConfigurer();
	Properties props = new Properties();
	props.setProperty("tb.array[0].age", "99");
	props.setProperty("tb.list[1].name", "test");
	poc.setProperties(props);
	poc.postProcessBeanFactory(factory);

	IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
	assertEquals(99, tb.getArray()[0].getAge());
	assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
}
 
Example #14
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAnnotationOrderedResourceInjection() {
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	IndexedTestBean itb = new IndexedTestBean();
	bf.registerSingleton("indexedTestBean", itb);
	FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean();
	bf.registerSingleton("nestedTestBean1", ntb1);
	FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean();
	bf.registerSingleton("nestedTestBean2", ntb2);

	OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());
	assertSame(tb, bean.getTestBean3());
	assertSame(tb, bean.getTestBean4());
	assertSame(itb, bean.getIndexedTestBean());
	assertEquals(2, bean.getNestedTestBeans().length);
	assertSame(ntb2, bean.getNestedTestBeans()[0]);
	assertSame(ntb1, bean.getNestedTestBeans()[1]);
	assertEquals(2, bean.nestedTestBeansField.length);
	assertSame(ntb2, bean.nestedTestBeansField[0]);
	assertSame(ntb1, bean.nestedTestBeansField[1]);
}
 
Example #15
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyWithStringValue() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	List<String> set = new LinkedList<>();
	set.add("set1");
	accessor.setPropertyValue("set", "set1");
	List<String> sortedSet = new ArrayList<>();
	sortedSet.add("sortedSet1");
	accessor.setPropertyValue("sortedSet", "sortedSet1");
	Set<String> list = new HashSet<>();
	list.add("list1");
	accessor.setPropertyValue("list", "list1");
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().containsAll(set));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().containsAll(sortedSet));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().containsAll(list));
}
 
Example #16
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyWithIntegerValue() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	Collection<Integer> coll = new HashSet<>();
	coll.add(0);
	accessor.setPropertyValue("collection", new Integer(0));
	List<Integer> set = new LinkedList<>();
	set.add(1);
	accessor.setPropertyValue("set", new Integer(1));
	List<Integer> sortedSet = new ArrayList<>();
	sortedSet.add(2);
	accessor.setPropertyValue("sortedSet", new Integer(2));
	Set<Integer> list = new HashSet<>();
	list.add(3);
	accessor.setPropertyValue("list", new Integer(3));
	assertEquals(1, target.getCollection().size());
	assertTrue(target.getCollection().containsAll(coll));
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().containsAll(set));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().containsAll(sortedSet));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().containsAll(list));
}
 
Example #17
Source File: AbstractPropertyAccessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyWithIntegerValue() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	Collection<Integer> coll = new HashSet<>();
	coll.add(0);
	accessor.setPropertyValue("collection", new Integer(0));
	List<Integer> set = new LinkedList<>();
	set.add(1);
	accessor.setPropertyValue("set", new Integer(1));
	List<Integer> sortedSet = new ArrayList<>();
	sortedSet.add(2);
	accessor.setPropertyValue("sortedSet", new Integer(2));
	Set<Integer> list = new HashSet<>();
	list.add(3);
	accessor.setPropertyValue("list", new Integer(3));
	assertEquals(1, target.getCollection().size());
	assertTrue(target.getCollection().containsAll(coll));
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().containsAll(set));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().containsAll(sortedSet));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().containsAll(list));
}
 
Example #18
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyWithArrayValue() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	Collection<String> coll = new HashSet<>();
	coll.add("coll1");
	accessor.setPropertyValue("collection", coll.toArray());
	List<String> set = new LinkedList<>();
	set.add("set1");
	accessor.setPropertyValue("set", set.toArray());
	List<String> sortedSet = new ArrayList<>();
	sortedSet.add("sortedSet1");
	accessor.setPropertyValue("sortedSet", sortedSet.toArray());
	Set<String> list = new HashSet<>();
	list.add("list1");
	accessor.setPropertyValue("list", list.toArray());
	assertEquals(1, target.getCollection().size());
	assertTrue(target.getCollection().containsAll(coll));
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().containsAll(set));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().containsAll(sortedSet));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().containsAll(list));
}
 
Example #19
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyNonMatchingType() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	Collection<String> coll = new ArrayList<>();
	coll.add("coll1");
	accessor.setPropertyValue("collection", coll);
	List<String> set = new LinkedList<>();
	set.add("set1");
	accessor.setPropertyValue("set", set);
	List<String> sortedSet = new ArrayList<>();
	sortedSet.add("sortedSet1");
	accessor.setPropertyValue("sortedSet", sortedSet);
	Set<String> list = new HashSet<>();
	list.add("list1");
	accessor.setPropertyValue("list", list);
	assertEquals(1, target.getCollection().size());
	assertTrue(target.getCollection().containsAll(coll));
	assertEquals(1, target.getSet().size());
	assertTrue(target.getSet().containsAll(set));
	assertEquals(1, target.getSortedSet().size());
	assertTrue(target.getSortedSet().containsAll(sortedSet));
	assertEquals(1, target.getList().size());
	assertTrue(target.getList().containsAll(list));
}
 
Example #20
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testOptionalResourceInjection() {
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	IndexedTestBean itb = new IndexedTestBean();
	bf.registerSingleton("indexedTestBean", itb);
	NestedTestBean ntb1 = new NestedTestBean();
	bf.registerSingleton("nestedTestBean1", ntb1);
	NestedTestBean ntb2 = new NestedTestBean();
	bf.registerSingleton("nestedTestBean2", ntb2);

	OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());
	assertSame(tb, bean.getTestBean3());
	assertSame(tb, bean.getTestBean4());
	assertSame(itb, bean.getIndexedTestBean());
	assertEquals(2, bean.getNestedTestBeans().length);
	assertSame(ntb1, bean.getNestedTestBeans()[0]);
	assertSame(ntb2, bean.getNestedTestBeans()[1]);
	assertEquals(2, bean.nestedTestBeansField.length);
	assertSame(ntb1, bean.nestedTestBeansField[0]);
	assertSame(ntb2, bean.nestedTestBeansField[1]);
}
 
Example #21
Source File: PropertyResourceConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPropertyOverrideConfigurerWithConvertProperties() {
	BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
	factory.registerBeanDefinition("tb", def);

	ConvertingOverrideConfigurer bfpp = new ConvertingOverrideConfigurer();
	Properties props = new Properties();
	props.setProperty("tb.array[0].name", "99");
	props.setProperty("tb.list[1].name", "test");
	bfpp.setProperties(props);
	bfpp.postProcessBeanFactory(factory);

	IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
	assertEquals("X99", tb.getArray()[0].getName());
	assertEquals("Xtest", ((TestBean) tb.getList().get(1)).getName());
}
 
Example #22
Source File: PropertyResourceConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPropertyOverrideConfigurerWithNestedProperty() {
	BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
	factory.registerBeanDefinition("tb", def);

	PropertyOverrideConfigurer poc;
	poc = new PropertyOverrideConfigurer();
	Properties props = new Properties();
	props.setProperty("tb.array[0].age", "99");
	props.setProperty("tb.list[1].name", "test");
	poc.setProperties(props);
	poc.postProcessBeanFactory(factory);

	IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
	assertEquals(99, tb.getArray()[0].getAge());
	assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
}
 
Example #23
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void setMapPropertyWithUnmodifiableMap() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			if (!StringUtils.hasLength(text)) {
				throw new IllegalArgumentException();
			}
			setValue(new TestBean(text));
		}
	});

	Map<Integer, String> inputMap = new HashMap<>();
	inputMap.put(1, "rod");
	inputMap.put(2, "rob");
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("map", Collections.unmodifiableMap(inputMap));
	accessor.setPropertyValues(pvs);
	assertEquals("rod", ((TestBean) target.getMap().get(1)).getName());
	assertEquals("rob", ((TestBean) target.getMap().get(2)).getName());
}
 
Example #24
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testOptionalResourceInjection() {
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	IndexedTestBean itb = new IndexedTestBean();
	bf.registerSingleton("indexedTestBean", itb);
	NestedTestBean ntb1 = new NestedTestBean();
	bf.registerSingleton("nestedTestBean1", ntb1);
	NestedTestBean ntb2 = new NestedTestBean();
	bf.registerSingleton("nestedTestBean2", ntb2);

	OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());
	assertSame(tb, bean.getTestBean3());
	assertSame(tb, bean.getTestBean4());
	assertSame(itb, bean.getIndexedTestBean());
	assertEquals(2, bean.getNestedTestBeans().length);
	assertSame(ntb1, bean.getNestedTestBeans()[0]);
	assertSame(ntb2, bean.getNestedTestBeans()[1]);
	assertEquals(2, bean.nestedTestBeansField.length);
	assertSame(ntb1, bean.nestedTestBeansField[0]);
	assertSame(ntb2, bean.nestedTestBeansField[1]);
}
 
Example #25
Source File: PropertyResourceConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPropertyOverrideConfigurerWithInvalidPropertiesFile() {
	BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
	factory.registerBeanDefinition("tb", def);

	PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
	poc.setLocations(TEST_PROPS, XTEST_PROPS);
	poc.setIgnoreResourceNotFound(true);
	poc.postProcessBeanFactory(factory);

	IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
	assertEquals(99, tb.getArray()[0].getAge());
	assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
}
 
Example #26
Source File: PropertyResourceConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPropertyOverrideConfigurerWithPropertiesFile() {
	BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
	factory.registerBeanDefinition("tb", def);

	PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
	poc.setLocation(TEST_PROPS);
	poc.postProcessBeanFactory(factory);

	IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
	assertEquals(99, tb.getArray()[0].getAge());
	assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
}
 
Example #27
Source File: BindTagTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
	PageContext pc = createPageContext();
	IndexedTestBean tb = new IndexedTestBean();
	DataBinder binder = new ServletRequestDataBinder(tb, "tb");
	binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {
		@Override
		public String getAsText() {
			return "something";
		}
	});
	Errors errors = binder.getBindingResult();
	errors.rejectValue("array[0]", "code1", "message1");
	errors.rejectValue("array[0]", "code2", "message2");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.array[0]");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
	// because of the custom editor getValue() should return a String
	assertTrue("Value is TestBean", status.getValue() instanceof String);
	assertTrue("Correct value", "something".equals(status.getValue()));
}
 
Example #28
Source File: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testAutowireByConstructor() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	ConstructorDependenciesBean rod1 = (ConstructorDependenciesBean) xbf.getBean("rod1");
	TestBean kerry = (TestBean) xbf.getBean("kerry2");
	// should have been autowired
	assertEquals(kerry, rod1.getSpouse1());
	assertEquals(0, rod1.getAge());
	assertEquals(null, rod1.getName());

	ConstructorDependenciesBean rod2 = (ConstructorDependenciesBean) xbf.getBean("rod2");
	TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
	TestBean kerry2 = (TestBean) xbf.getBean("kerry2");
	// should have been autowired
	assertEquals(kerry2, rod2.getSpouse1());
	assertEquals(kerry1, rod2.getSpouse2());
	assertEquals(0, rod2.getAge());
	assertEquals(null, rod2.getName());

	ConstructorDependenciesBean rod = (ConstructorDependenciesBean) xbf.getBean("rod3");
	IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
	// should have been autowired
	assertEquals(kerry, rod.getSpouse1());
	assertEquals(kerry, rod.getSpouse2());
	assertEquals(other, rod.getOther());
	assertEquals(0, rod.getAge());
	assertEquals(null, rod.getName());

	xbf.getBean("rod4", ConstructorDependenciesBean.class);
	// should have been autowired
	assertEquals(kerry, rod.getSpouse1());
	assertEquals(kerry, rod.getSpouse2());
	assertEquals(other, rod.getOther());
	assertEquals(0, rod.getAge());
	assertEquals(null, rod.getName());
}
 
Example #29
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void setMapPropertyNonMatchingType() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	Map<String, String> map = new TreeMap<>();
	map.put("key", "value");
	accessor.setPropertyValue("map", map);
	Map<String, String> sortedMap = new TreeMap<>();
	sortedMap.put("sortedKey", "sortedValue");
	accessor.setPropertyValue("sortedMap", sortedMap);
	assertEquals(1, target.getMap().size());
	assertEquals("value", target.getMap().get("key"));
	assertEquals(1, target.getSortedMap().size());
	assertEquals("sortedValue", target.getSortedMap().get("sortedKey"));
}
 
Example #30
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testBindingNullToEmptyCollection() {
	IndexedTestBean tb = new IndexedTestBean();
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class, true));
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("set", null);
	binder.bind(pvs);

	assertTrue(tb.getSet() instanceof TreeSet);
	assertTrue(tb.getSet().isEmpty());
}