Support for indexing into any Collection/Iterable

Closes gh-907
This commit is contained in:
Juergen Hoeller
2023-07-11 23:07:51 +02:00
parent e048b093b5
commit c20a2e4763
3 changed files with 64 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,8 +17,10 @@
package org.springframework.beans.testfixture.beans;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -47,6 +49,8 @@ public class IndexedTestBean {
private SortedMap sortedMap;
private MyTestBeans myTestBeans;
public IndexedTestBean() {
this(true);
@@ -71,6 +75,7 @@ public class IndexedTestBean {
TestBean tb8 = new TestBean("name8", 0);
TestBean tbX = new TestBean("nameX", 0);
TestBean tbY = new TestBean("nameY", 0);
TestBean tbZ = new TestBean("nameZ", 0);
this.array = new TestBean[] {tb0, tb1};
this.list = new ArrayList<>();
this.list.add(tb2);
@@ -87,6 +92,7 @@ public class IndexedTestBean {
list.add(tbY);
this.map.put("key4", list);
this.map.put("key5[foo]", tb8);
this.myTestBeans = new MyTestBeans(tbZ);
}
@@ -146,4 +152,27 @@ public class IndexedTestBean {
this.sortedMap = sortedMap;
}
public MyTestBeans getMyTestBeans() {
return myTestBeans;
}
public void setMyTestBeans(MyTestBeans myTestBeans) {
this.myTestBeans = myTestBeans;
}
public static class MyTestBeans implements Iterable<TestBean> {
private final Collection<TestBean> testBeans;
public MyTestBeans(TestBean... testBeans) {
this.testBeans = Arrays.asList(testBeans);
}
@Override
public Iterator<TestBean> iterator() {
return this.testBeans.iterator();
}
}
}