Skip to content

Commit e989b26

Browse files
committed
Test for cyclic type declaration handling in TypeDescriptor
Issue: SPR-9735 (cherry picked from commit 7fa3e65)
1 parent 3c3e07e commit e989b26

File tree

1 file changed

+174
-22
lines changed

1 file changed

+174
-22
lines changed

spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

Lines changed: 174 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import java.util.Collection;
2525
import java.util.Collections;
2626
import java.util.HashMap;
27+
import java.util.Iterator;
2728
import java.util.LinkedHashMap;
2829
import java.util.LinkedHashSet;
2930
import java.util.List;
31+
import java.util.ListIterator;
3032
import java.util.Map;
3133
import java.util.Properties;
3234
import java.util.concurrent.atomic.AtomicInteger;
@@ -1854,6 +1856,28 @@ public void SPR11609() {
18541856
assertEquals(1, exp.getValue(sec));
18551857
}
18561858

1859+
@Test
1860+
public void SPR9735() {
1861+
Item item = new Item();
1862+
item.setName("parent");
1863+
1864+
Item item1 = new Item();
1865+
item1.setName("child1");
1866+
1867+
Item item2 = new Item();
1868+
item2.setName("child2");
1869+
1870+
item.add(item1);
1871+
item.add(item2);
1872+
1873+
ExpressionParser parser = new SpelExpressionParser();
1874+
EvaluationContext context = new StandardEvaluationContext();
1875+
Expression exp = parser.parseExpression("#item[0].name");
1876+
context.setVariable("item", item);
1877+
1878+
assertEquals("child1", exp.getValue(context));
1879+
}
1880+
18571881

18581882
private static enum ABC { A, B, C }
18591883

@@ -1866,21 +1890,21 @@ public static class BooleanHolder {
18661890

18671891
private boolean primitiveProperty = true;
18681892

1869-
public Boolean isSimpleProperty() {
1870-
return simpleProperty;
1871-
}
1872-
18731893
public void setSimpleProperty(Boolean simpleProperty) {
18741894
this.simpleProperty = simpleProperty;
18751895
}
18761896

1877-
public boolean isPrimitiveProperty() {
1878-
return primitiveProperty;
1897+
public Boolean isSimpleProperty() {
1898+
return this.simpleProperty;
18791899
}
18801900

18811901
public void setPrimitiveProperty(boolean primitiveProperty) {
18821902
this.primitiveProperty = primitiveProperty;
18831903
}
1904+
1905+
public boolean isPrimitiveProperty() {
1906+
return this.primitiveProperty;
1907+
}
18841908
}
18851909

18861910

@@ -1933,13 +1957,13 @@ public static class Spr10486 {
19331957

19341958
private String name = "name";
19351959

1936-
public String getName() {
1937-
return name;
1938-
}
1939-
19401960
public void setName(String name) {
19411961
this.name = name;
19421962
}
1963+
1964+
public String getName() {
1965+
return this.name;
1966+
}
19431967
}
19441968

19451969

@@ -1951,25 +1975,22 @@ public String isSomething() {
19511975
}
19521976

19531977

1954-
static class TestClass2 { // SPR-9194
1978+
static class TestClass2 { // SPR-9194
19551979

19561980
String string;
19571981

1958-
19591982
public TestClass2(String string) {
19601983
this.string = string;
19611984
}
19621985

1963-
@Override
1964-
public int hashCode() {
1965-
return 0;
1986+
public boolean equals(Object other) {
1987+
return (this == other || (other instanceof TestClass2 &&
1988+
this.string.equals(((TestClass2) other).string)));
19661989
}
19671990

1968-
public boolean equals(Object o) {
1969-
if (o instanceof TestClass2) {
1970-
return string.equals(((TestClass2) o).string);
1971-
}
1972-
return false;
1991+
@Override
1992+
public int hashCode() {
1993+
return this.string.hashCode();
19731994
}
19741995
}
19751996

@@ -1983,12 +2004,12 @@ public int echo(int invocation) {
19832004
}
19842005

19852006
public int parameter() {
1986-
return counter.incrementAndGet();
2007+
return this.counter.incrementAndGet();
19872008
}
19882009

19892010
@Override
19902011
public Object resolve(EvaluationContext context, String beanName) throws AccessException {
1991-
return beanName.equals("bean") ? this : null;
2012+
return (beanName.equals("bean") ? this : null);
19922013
}
19932014
}
19942015

@@ -1999,4 +2020,135 @@ public static class MapWithConstant extends HashMap {
19992020
public static final int X = 1;
20002021
}
20012022

2023+
2024+
public class Item implements List<Item> {
2025+
2026+
private String name;
2027+
2028+
private List<Item> children = new ArrayList<Item>();
2029+
2030+
public void setName(String name) {
2031+
this.name = name;
2032+
}
2033+
2034+
public String getName() {
2035+
return this.name;
2036+
}
2037+
2038+
@Override
2039+
public int size() {
2040+
return this.children.size();
2041+
}
2042+
2043+
@Override
2044+
public boolean isEmpty() {
2045+
return this.children.isEmpty();
2046+
}
2047+
2048+
@Override
2049+
public boolean contains(Object o) {
2050+
return this.children.contains(o);
2051+
}
2052+
2053+
@Override
2054+
public Iterator<Item> iterator() {
2055+
return this.children.iterator();
2056+
}
2057+
2058+
@Override
2059+
public Object[] toArray() {
2060+
return this.children.toArray();
2061+
}
2062+
2063+
@Override
2064+
public <T> T[] toArray(T[] a) {
2065+
return this.children.toArray(a);
2066+
}
2067+
2068+
@Override
2069+
public boolean add(Item e) {
2070+
return this.children.add(e);
2071+
}
2072+
2073+
@Override
2074+
public boolean remove(Object o) {
2075+
return this.children.remove(o);
2076+
}
2077+
2078+
@Override
2079+
public boolean containsAll(Collection<?> c) {
2080+
return this.children.containsAll(c);
2081+
}
2082+
2083+
@Override
2084+
public boolean addAll(Collection<? extends Item> c) {
2085+
return this.children.addAll(c);
2086+
}
2087+
2088+
@Override
2089+
public boolean addAll(int index, Collection<? extends Item> c) {
2090+
return this.children.addAll(index, c);
2091+
}
2092+
2093+
@Override
2094+
public boolean removeAll(Collection<?> c) {
2095+
return this.children.removeAll(c);
2096+
}
2097+
2098+
@Override
2099+
public boolean retainAll(Collection<?> c) {
2100+
return this.children.retainAll(c);
2101+
}
2102+
2103+
@Override
2104+
public void clear() {
2105+
this.children.clear();
2106+
}
2107+
2108+
@Override
2109+
public Item get(int index) {
2110+
return this.children.get(index);
2111+
}
2112+
2113+
@Override
2114+
public Item set(int index, Item element) {
2115+
return this.children.set(index, element);
2116+
}
2117+
2118+
@Override
2119+
public void add(int index, Item element) {
2120+
this.children.add(index, element);
2121+
}
2122+
2123+
@Override
2124+
public Item remove(int index) {
2125+
return this.children.remove(index);
2126+
}
2127+
2128+
@Override
2129+
public int indexOf(Object o) {
2130+
return this.children.indexOf(o);
2131+
}
2132+
2133+
@Override
2134+
public int lastIndexOf(Object o) {
2135+
return this.children.lastIndexOf(o);
2136+
}
2137+
2138+
@Override
2139+
public ListIterator<Item> listIterator() {
2140+
return this.children.listIterator();
2141+
}
2142+
2143+
@Override
2144+
public ListIterator<Item> listIterator(int index) {
2145+
return this.children.listIterator(index);
2146+
}
2147+
2148+
@Override
2149+
public List<Item> subList(int fromIndex, int toIndex) {
2150+
return this.children.subList(fromIndex, toIndex);
2151+
}
2152+
}
2153+
20022154
}

0 commit comments

Comments
 (0)