use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.
the class OgnlValueStackTest method testSetNullList.
public void testSetNullList() {
Foo foo = new Foo();
OgnlValueStack vs = createValueStack();
vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
vs.push(foo);
vs.setValue("cats[0].name", "Cat One");
vs.setValue("cats[1].name", "Cat Two");
assertNotNull(foo.getCats());
assertEquals(2, foo.getCats().size());
assertEquals("Cat One", ((Cat) foo.getCats().get(0)).getName());
assertEquals("Cat Two", ((Cat) foo.getCats().get(1)).getName());
// test when both Key and Value types of Map are interfaces but concrete classes are defined in .properties file
vs.setValue("animalMap[3].name", "Cat Three by interface");
vs.setValue("animalMap[6].name", "Cat Six by interface");
assertNotNull(foo.getAnimalMap());
assertEquals(2, foo.getAnimalMap().size());
assertEquals("Cat Three by interface", foo.getAnimalMap().get(3L).getName());
assertEquals("Cat Six by interface", foo.getAnimalMap().get(6L).getName());
vs.setValue("annotatedCats[0].name", "Cat One By Annotation");
vs.setValue("annotatedCats[1].name", "Cat Two By Annotation");
assertNotNull(foo.getAnnotatedCats());
assertEquals(2, foo.getAnnotatedCats().size());
assertEquals("Cat One By Annotation", ((Cat) foo.getAnnotatedCats().get(0)).getName());
assertEquals("Cat Two By Annotation", ((Cat) foo.getAnnotatedCats().get(1)).getName());
vs.setValue("cats[0].foo.cats[1].name", "Deep null cat");
assertNotNull(((Cat) foo.getCats().get(0)).getFoo());
assertNotNull(((Cat) foo.getCats().get(0)).getFoo().getCats());
assertNotNull(((Cat) foo.getCats().get(0)).getFoo().getCats().get(1));
assertEquals("Deep null cat", ((Cat) ((Cat) foo.getCats().get(0)).getFoo().getCats().get(1)).getName());
vs.setValue("annotatedCats[0].foo.annotatedCats[1].name", "Deep null cat by annotation");
assertNotNull(((Cat) foo.getAnnotatedCats().get(0)).getFoo());
assertNotNull(((Cat) foo.getAnnotatedCats().get(0)).getFoo().getAnnotatedCats());
assertNotNull(((Cat) foo.getAnnotatedCats().get(0)).getFoo().getAnnotatedCats().get(1));
assertEquals("Deep null cat by annotation", ((Cat) ((Cat) foo.getAnnotatedCats().get(0)).getFoo().getAnnotatedCats().get(1)).getName());
}
use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.
the class OgnlValueStackTest method testNotFailOnTooLongExpressionWithDefaultProperties.
public void testNotFailOnTooLongExpressionWithDefaultProperties() {
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 was to confirm failure of exceeding the default value. Now the feature should be disabled by default,
// so this test's expectations are now changed.
// Since maxlength is disabled by default, just choose an arbitrary value for test
Integer repeat = Integer.valueOf(256);
OgnlValueStack vs = createValueStack();
try {
vs.findValue(StringUtils.repeat('.', repeat + 1), true);
fail("findValue did not throw any exception (should either fail as invalid expression syntax or security exception) ?");
} catch (Exception ex) {
// If STRUTS_OGNL_EXPRESSION_MAX_LENGTH feature is disabled (default), the parse should fail due to a reason of invalid expression syntax
// with ParseException. Previously when it was enabled the reason for the failure would have been SecurityException.
assertTrue(ex.getCause() instanceof OgnlException);
assertTrue(((OgnlException) ex.getCause()).getReason() instanceof ParseException);
}
}
use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.
the class OgnlValueStackTest method testSuccessFailOnErrorOnInheritedPropertiesWithMethods.
public void testSuccessFailOnErrorOnInheritedPropertiesWithMethods() {
// this shuld not fail as the property is defined on a parent class
OgnlValueStack vs = createValueStack();
Foo foo = new Foo();
BarJunior barjr = new BarJunior();
foo.setBarJunior(barjr);
vs.push(foo);
assertNull(barjr.getTitle());
vs.findValue("getBarJunior().title", true);
}
use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.
the class OgnlValueStackTest method testFailOnTooLongExpressionLongerThan192_ViaOverriddenProperty.
public void testFailOnTooLongExpressionLongerThan192_ViaOverriddenProperty() {
try {
loadConfigurationProviders(new StubConfigurationProvider() {
@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
props.setProperty(StrutsConstants.STRUTS_OGNL_EXPRESSION_MAX_LENGTH, "192");
}
});
Integer repeat = Integer.parseInt(container.getInstance(String.class, StrutsConstants.STRUTS_OGNL_EXPRESSION_MAX_LENGTH));
OgnlValueStack vs = createValueStack();
try {
vs.findValue(StringUtils.repeat('.', repeat + 1), true);
fail("Failed to throw exception on too long expression");
} catch (Exception ex) {
assertTrue(ex.getCause() instanceof OgnlException);
assertTrue(((OgnlException) ex.getCause()).getReason() instanceof SecurityException);
}
} 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 testFailFailOnErrorOnInheritedPropertiesWithMethods.
public void testFailFailOnErrorOnInheritedPropertiesWithMethods() {
OgnlValueStack vs = createValueStack();
Foo foo = new Foo();
BarJunior barjr = new BarJunior();
foo.setBarJunior(barjr);
vs.push(foo);
assertNull(barjr.getTitle());
try {
vs.findValue("getBarJunior().title2", true);
fail("should have failed on missing property");
} catch (Exception e) {
}
}
Aggregations