use of com.opensymphony.xwork2.test.StubConfigurationProvider in project struts by apache.
the class DebugTagTest method setStrutsConstant.
/**
* Overwrite the Struts Constant and reload container
*/
private void setStrutsConstant(final Map<String, String> overwritePropeties) {
configurationManager.addContainerProvider(new StubConfigurationProvider() {
@Override
public boolean needsReload() {
return true;
}
@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
for (Map.Entry<String, String> stringStringEntry : overwritePropeties.entrySet()) {
props.setProperty(stringStringEntry.getKey(), stringStringEntry.getValue(), null);
}
}
});
configurationManager.reload();
container = configurationManager.getConfiguration().getContainer();
stack.getActionContext().withContainer(container);
}
use of com.opensymphony.xwork2.test.StubConfigurationProvider in project struts by apache.
the class StrutsBeanSelectionProviderTest method testRegister.
public void testRegister() {
LocalizedTextProvider localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
assertEquals("The form has already been processed or no token was supplied, please try again.", localizedTextProvider.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
loadConfigurationProviders(new StubConfigurationProvider() {
@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
props.setProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES, "testmessages,testmessages2");
props.setProperty(StrutsConstants.STRUTS_LOCALE, "US");
}
});
localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
assertEquals("Replaced message for token tag", localizedTextProvider.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
}
use of com.opensymphony.xwork2.test.StubConfigurationProvider 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.test.StubConfigurationProvider in project struts by apache.
the class SetPropertiesTest method testAddingToCollectionBasedOnPermission.
public void testAddingToCollectionBasedOnPermission() {
final MockObjectTypeDeterminer determiner = new MockObjectTypeDeterminer(Long.class, Bar.class, "id", true);
loadConfigurationProviders(new StubConfigurationProvider() {
@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
builder.factory(ObjectTypeDeterminer.class, new Factory() {
public Object create(Context context) throws Exception {
return determiner;
}
@Override
public Class type() {
return determiner.getClass();
}
}, Scope.SINGLETON);
}
});
Collection barColl = new HashSet();
ValueStack vs = ActionContext.getContext().getValueStack();
ReflectionContextState.setCreatingNullObjects(vs.getContext(), true);
ReflectionContextState.setReportingConversionErrors(vs.getContext(), true);
Foo foo = new Foo();
foo.setBarCollection(barColl);
vs.push(foo);
String bar1Title = "title";
vs.setValue("barCollection(11).title", bar1Title);
assertEquals(1, barColl.size());
Object bar = barColl.iterator().next();
assertTrue(bar instanceof Bar);
assertEquals(((Bar) bar).getTitle(), bar1Title);
assertEquals(((Bar) bar).getId(), new Long(11));
// now test where there is no permission
determiner.setShouldCreateIfNew(false);
String bar2Title = "another title";
vs.setValue("barCollection(22).title", bar1Title);
assertEquals(1, barColl.size());
bar = barColl.iterator().next();
assertTrue(bar instanceof Bar);
assertEquals(((Bar) bar).getTitle(), bar1Title);
assertEquals(((Bar) bar).getId(), new Long(11));
}
use of com.opensymphony.xwork2.test.StubConfigurationProvider in project struts by apache.
the class SetPropertiesTest method doTestAddingToListsWithObjects.
public void doTestAddingToListsWithObjects(final boolean allowAdditions) {
loadConfigurationProviders(new StubConfigurationProvider() {
@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
builder.factory(ObjectTypeDeterminer.class, new Factory() {
public Object create(Context context) throws Exception {
return new MockObjectTypeDeterminer(null, Cat.class, null, allowAdditions);
}
@Override
public Class type() {
return Cat.class;
}
});
}
});
Foo foo = new Foo();
foo.setMoreCats(new ArrayList());
String spielname = "Spielen";
ValueStack vs = ActionContext.getContext().getValueStack();
vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
vs.push(foo);
try {
vs.setValue("moreCats[2].name", spielname);
} catch (IndexOutOfBoundsException e) {
if (allowAdditions) {
throw e;
}
}
Object setCat = null;
if (allowAdditions) {
setCat = foo.getMoreCats().get(2);
assertNotNull(setCat);
assertTrue(setCat instanceof Cat);
assertTrue(((Cat) setCat).getName().equals(spielname));
} else {
assertTrue(foo.getMoreCats() == null || foo.getMoreCats().size() == 0);
}
// has been created
if (allowAdditions) {
spielname = "paws";
vs.setValue("moreCats[0].name", spielname);
setCat = foo.getMoreCats().get(0);
assertNotNull(setCat);
assertTrue(setCat instanceof Cat);
assertTrue(((Cat) setCat).getName().equals(spielname));
}
}
Aggregations