use of org.apache.struts.config.FormPropertyConfig in project sonar-java by SonarSource.
the class ActionServlet method processFormBeanConfigClass.
/**
* <p>Checks if the current beanConfig is using the correct class based on
* the class of its ancestor form bean config.</p>
*
* @param beanConfig The form bean to check.
* @param moduleConfig The config for the current module.
* @return The form bean config using the correct class as determined by
* the config's ancestor and its own overridden value.
* @throws UnavailableException if an instance of the form bean config
* class cannot be created.
* @throws ServletException on class creation error
*/
protected FormBeanConfig processFormBeanConfigClass(FormBeanConfig beanConfig, ModuleConfig moduleConfig) throws ServletException {
String ancestor = beanConfig.getExtends();
if (ancestor == null) {
// Nothing to do, then
return beanConfig;
}
// Make sure that this bean is of the right class
FormBeanConfig baseConfig = moduleConfig.findFormBeanConfig(ancestor);
if (baseConfig == null) {
throw new UnavailableException("Unable to find " + "form bean '" + ancestor + "' to extend.");
}
// Was our bean's class overridden already?
if (beanConfig.getClass().equals(FormBeanConfig.class)) {
// Ensure that our bean is using the correct class
if (!baseConfig.getClass().equals(beanConfig.getClass())) {
// Replace the bean with an instance of the correct class
FormBeanConfig newBeanConfig = null;
String baseConfigClassName = baseConfig.getClass().getName();
try {
newBeanConfig = (FormBeanConfig) RequestUtils.applicationInstance(baseConfigClassName);
// copy the values
BeanUtils.copyProperties(newBeanConfig, beanConfig);
FormPropertyConfig[] fpc = beanConfig.findFormPropertyConfigs();
for (int i = 0; i < fpc.length; i++) {
newBeanConfig.addFormPropertyConfig(fpc[i]);
}
} catch (Exception e) {
handleCreationException(baseConfigClassName, e);
}
// replace beanConfig with newBeanConfig
moduleConfig.removeFormBeanConfig(beanConfig);
moduleConfig.addFormBeanConfig(newBeanConfig);
beanConfig = newBeanConfig;
}
}
return beanConfig;
}
use of org.apache.struts.config.FormPropertyConfig in project sonar-java by SonarSource.
the class DynaActionForm method reset.
/**
* <p>Reset the properties to their <code>initial</code> value if their
* <code>reset</code> configuration is set to true or if
* <code>reset</code> is set to a list of HTTP request methods that
* includes the method of given <code>request</code> object.</p>
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
String name = getDynaClass().getName();
if (name == null) {
return;
}
FormBeanConfig config = mapping.getModuleConfig().findFormBeanConfig(name);
if (config == null) {
return;
}
// look for properties we should reset
FormPropertyConfig[] props = config.findFormPropertyConfigs();
for (int i = 0; i < props.length; i++) {
String resetValue = props[i].getReset();
// skip this property if there's no reset value
if ((resetValue == null) || (resetValue.length() <= 0)) {
continue;
}
boolean reset = Boolean.valueOf(resetValue).booleanValue();
if (!reset) {
// check for the request method
// use a StringTokenizer with the default delimiters + a comma
StringTokenizer st = new StringTokenizer(resetValue, ", \t\n\r\f");
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.equalsIgnoreCase(request.getMethod())) {
reset = true;
break;
}
}
}
if (reset) {
set(props[i].getName(), props[i].initial());
}
}
}
use of org.apache.struts.config.FormPropertyConfig in project sonar-java by SonarSource.
the class TestActionServlet method setUp.
// ------------------------------------------------- setUp() and tearDown()
/**
* Set up instance variables required by this test case.
*/
public void setUp() throws Exception {
actionServlet = new ActionServlet();
actionServlet.initInternal();
ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
moduleConfig = factoryObject.createModuleConfig("");
// Setup the base form
baseFormBean = new FormBeanConfig();
baseFormBean.setName("baseForm");
baseFormBean.setType("org.apache.struts.action.DynaActionForm");
// Set up id, name, and score
FormPropertyConfig property = new FormPropertyConfig();
property.setName("id");
property.setType("java.lang.String");
baseFormBean.addFormPropertyConfig(property);
property = new FormPropertyConfig();
property.setName("name");
property.setType("java.lang.String");
baseFormBean.addFormPropertyConfig(property);
property = new FormPropertyConfig();
property.setName("score");
property.setType("java.lang.String");
baseFormBean.addFormPropertyConfig(property);
// Setup the exception handler
baseException = new ExceptionConfig();
baseException.setType("java.lang.NullPointerException");
baseException.setKey("msg.exception.npe");
// Setup the forward config
baseForward = new ActionForward("success", "/succes.jsp", false);
// Setup the action config
baseAction = new ActionMapping();
baseAction.setPath("/index");
baseAction.setType("org.apache.struts.actions.DummyAction");
baseAction.setName("someForm");
baseAction.setInput("/input.jsp");
baseAction.addForwardConfig(new ActionForward("next", "/next.jsp", false));
baseAction.addForwardConfig(new ActionForward("prev", "/prev.jsp", false));
ExceptionConfig exceptionConfig = new ExceptionConfig();
exceptionConfig.setType("java.sql.SQLException");
exceptionConfig.setKey("msg.exception.sql");
baseAction.addExceptionConfig(exceptionConfig);
// Nothing is registered to our module config until they are needed
}
use of org.apache.struts.config.FormPropertyConfig in project sonar-java by SonarSource.
the class TestDynaActionFormClass method testConfigProperties.
// Check the configured FormPropertyConfig element properties
public void testConfigProperties() {
for (int i = 0; i < dynaProperties.length; i++) {
FormPropertyConfig dynaProperty = beanConfig.findFormPropertyConfig(dynaProperties[i].getName());
assertNotNull("Found dynaProperty " + dynaProperties[i].getName(), dynaProperty);
assertEquals("dynaProperty name for " + dynaProperties[i].getName(), dynaProperties[i].getName(), dynaProperty.getName());
assertEquals("dynaProperty type for " + dynaProperties[i].getName(), dynaProperties[i].getType(), dynaProperty.getType());
assertEquals("dynaProperty initial for " + dynaProperties[i].getName(), dynaProperties[i].getInitial(), dynaProperty.getInitial());
}
}
use of org.apache.struts.config.FormPropertyConfig in project sonar-java by SonarSource.
the class TestDynaActionFormClass method testConfigDuplicate.
// Check attempts to add a duplicate property name
public void testConfigDuplicate() {
FormPropertyConfig prop = null;
assertNull("booleanProperty is found", prop);
try {
beanConfig.addFormPropertyConfig(new FormPropertyConfig("booleanProperty", "java.lang.String", ""));
fail("Adding duplicate property not prevented");
} catch (IllegalArgumentException e) {
// Expected result
;
}
}
Aggregations