use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.
the class OgnlValueStackTest method testGetBarAsString.
public void testGetBarAsString() {
Foo foo = new Foo();
Bar bar = new Bar();
bar.setTitle("bar");
bar.setSomethingElse(123);
foo.setBar(bar);
OgnlValueStack vs = createValueStack();
vs.push(foo);
String output = (String) vs.findValue("bar", String.class);
assertEquals("bar:123", output);
}
use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.
the class OgnlValueStackTest method testSetBarAsString.
public void testSetBarAsString() {
Foo foo = new Foo();
OgnlValueStack vs = createValueStack();
vs.push(foo);
vs.setValue("bar", "bar:123");
assertEquals("bar", foo.getBar().getTitle());
assertEquals(123, foo.getBar().getSomethingElse());
}
use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.
the class OgnlValueStackTest method testNotFailOnTooLongValueWithDefaultProperties.
public void testNotFailOnTooLongValueWithDefaultProperties() {
try {
loadConfigurationProviders(new DefaultPropertiesProvider());
Object defaultMaxLengthFromConfiguration = container.getInstance(String.class, StrutsConstants.STRUTS_OGNL_EXPRESSION_MAX_LENGTH);
if (defaultMaxLengthFromConfiguration != null) {
assertTrue("non-null defaultMaxLengthFromConfiguration not a String ?", defaultMaxLengthFromConfiguration instanceof String);
assertTrue("non-null defaultMaxLengthFromConfiguration not empty string by default ?", ((String) defaultMaxLengthFromConfiguration).length() == 0);
} else {
assertNull("defaultMaxLengthFromConfiguration not null ?", defaultMaxLengthFromConfiguration);
}
// Original test logic is unchanged (testing that values can be larger than maximum expression length), but since the feature is disabled by
// default we will now have to enable it with an arbitrary value, test, and reset it to disabled.
// Since maxlength is disabled by default, just choose an arbitrary value for test
Integer repeat = Integer.valueOf(256);
// Apply a non-default value for expressionMaxLength (as it should be disabled by default)
try {
ognlUtil.applyExpressionMaxLength(repeat.toString());
} catch (Exception ex) {
fail("applyExpressionMaxLength did not accept maxlength string " + repeat.toString() + " ?");
}
OgnlValueStack vs = createValueStack();
Dog dog = new Dog();
vs.push(dog);
String value = StringUtils.repeat('.', repeat + 1);
vs.setValue("name", value);
assertEquals(value, dog.getName());
} finally {
// Reset expressionMaxLength value to default (disabled)
ognlUtil.applyExpressionMaxLength(null);
}
}
use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.
the class OgnlValueStackTest method testSetNullMap.
public void testSetNullMap() {
Foo foo = new Foo();
OgnlValueStack vs = createValueStack();
vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
vs.push(foo);
vs.setValue("catMap['One'].name", "Cat One");
vs.setValue("catMap['Two'].name", "Cat Two");
assertNotNull(foo.getCatMap());
assertEquals(2, foo.getCatMap().size());
assertEquals("Cat One", ((Cat) foo.getCatMap().get("One")).getName());
assertEquals("Cat Two", ((Cat) foo.getCatMap().get("Two")).getName());
vs.setValue("catMap['One'].foo.catMap['Two'].name", "Deep null cat");
assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo());
assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo().getCatMap());
assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo().getCatMap().get("Two"));
assertEquals("Deep null cat", ((Cat) ((Cat) foo.getCatMap().get("One")).getFoo().getCatMap().get("Two")).getName());
}
use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.
the class OgnlValueStackTest method testGetComplexBarAsString.
public void testGetComplexBarAsString() {
// children foo->foo->foo
Foo foo = new Foo();
Foo foo2 = new Foo();
foo.setChild(foo2);
Foo foo3 = new Foo();
foo2.setChild(foo3);
// relatives
Foo fooA = new Foo();
foo.setRelatives(new Foo[] { fooA });
Foo fooB = new Foo();
foo2.setRelatives(new Foo[] { fooB });
Foo fooC = new Foo();
foo3.setRelatives(new Foo[] { fooC });
// the bar
Bar bar = new Bar();
bar.setTitle("bar");
bar.setSomethingElse(123);
// now place the bar all over
foo.setBar(bar);
foo2.setBar(bar);
foo3.setBar(bar);
fooA.setBar(bar);
fooB.setBar(bar);
fooC.setBar(bar);
OgnlValueStack vs = createValueStack();
vs.push(foo);
vs.getContext().put("foo", foo);
assertEquals("bar:123", vs.findValue("#foo.bar", String.class));
assertEquals("bar:123", vs.findValue("bar", String.class));
assertEquals("bar:123", vs.findValue("child.bar", String.class));
assertEquals("bar:123", vs.findValue("child.child.bar", String.class));
assertEquals("bar:123", vs.findValue("relatives[0].bar", String.class));
assertEquals("bar:123", vs.findValue("child.relatives[0].bar", String.class));
assertEquals("bar:123", vs.findValue("child.child.relatives[0].bar", String.class));
vs.push(vs.findValue("child"));
assertEquals("bar:123", vs.findValue("bar", String.class));
assertEquals("bar:123", vs.findValue("child.bar", String.class));
assertEquals("bar:123", vs.findValue("relatives[0].bar", String.class));
assertEquals("bar:123", vs.findValue("child.relatives[0].bar", String.class));
}
Aggregations