Search in sources :

Example 36 with ConfigurationProvider

use of com.opensymphony.xwork2.config.ConfigurationProvider in project struts by apache.

the class FetchMetadataInterceptorTest method testSetExemptedPathsInjectionIndirectly.

public void testSetExemptedPathsInjectionIndirectly() throws Exception {
    // Perform a multi-step test to confirm (indirectly) that the method parameter injection of setExemptedPaths() for
    // the FetchMetadataInterceptor is functioning as expected, when configured appropriately.
    // Ensure we're using the specific test configuration, not the default simple configuration.
    XmlConfigurationProvider configurationProvider = new StrutsXmlConfigurationProvider("struts-testing.xml");
    container.inject(configurationProvider);
    loadConfigurationProviders(configurationProvider);
    // The test configuration in "struts-testing.xml" should define a "default" package.  That "default" package should contain a "defaultInterceptorStack" containing
    // a "fetchMetadata" interceptor parameter "fetchMetadata.setExemptedPaths".  If the parameter method injection is working correctly for the FetchMetadataInterceptor,
    // the exempted paths should be set appropriately for the interceptor instances, once the configuration is loaded into the container.
    final PackageConfig defaultPackageConfig = configuration.getPackageConfig("default");
    final InterceptorStackConfig defaultInterceptorStackConfig = (InterceptorStackConfig) defaultPackageConfig.getInterceptorConfig("defaultInterceptorStack");
    final Collection<InterceptorMapping> defaultInterceptorStackInterceptors = defaultInterceptorStackConfig.getInterceptors();
    assertFalse("'defaultInterceptorStack' interceptors in struts-testing.xml is empty ?", defaultInterceptorStackInterceptors.isEmpty());
    InterceptorMapping configuredFetchMetadataInterceptorMapping = null;
    Iterator<InterceptorMapping> interceptorIterator = defaultInterceptorStackInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping not present after loading 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping loaded from 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    FetchMetadataInterceptor configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, "foo");
    request.setContextPath("/foo");
    assertEquals("Expected interceptor to NOT accept this request [/foo]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    request.setContextPath("/fetchMetadataExemptedGlobal");
    assertNotEquals("Expected interceptor to accept this request [/fetchMetadataExemptedGlobal]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    request.setContextPath("/someOtherPath");
    assertNotEquals("Expected interceptor to accept this request [/someOtherPath]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    // The test configuration in "struts-testing.xml" should also contain three actions configured differently for the "fetchMetadata" interceptor.
    // "fetchMetadataExempted" has an override exemption matching its action name, "fetchMetadataNotExempted" has an override exemption NOT matching its action name,
    // and "fetchMetadataExemptedGlobal" has an action name matching an exemption defined in "defaultInterceptorStack".
    final RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration();
    final ActionConfig fetchMetadataExemptedActionConfig = runtimeConfiguration.getActionConfig("/", "fetchMetadataExempted");
    final ActionConfig fetchMetadataNotExemptedActionConfig = runtimeConfiguration.getActionConfig("/", "fetchMetadataNotExempted");
    final ActionConfig fetchMetadataExemptedGlobalActionConfig = runtimeConfiguration.getActionConfig("/", "fetchMetadataExemptedGlobal");
    assertNotNull("'fetchMetadataExempted' action config not present in 'struts-testing.xml' ?", fetchMetadataExemptedActionConfig);
    assertNotNull("'fetchMetadataNotExempted' action config not present in 'struts-testing.xml' ?", fetchMetadataExemptedActionConfig);
    assertNotNull("'fetchMetadataExemptedGlobal' action config not present in 'struts-testing.xml' ?", fetchMetadataExemptedActionConfig);
    // Test fetchMetadata interceptor for the "fetchMetadataExempted" action.
    Collection<InterceptorMapping> currentActionInterceptors = fetchMetadataExemptedActionConfig.getInterceptors();
    assertFalse("'fetchMetadataExempted' interceptors in struts-testing.xml is empty ?", currentActionInterceptors.isEmpty());
    configuredFetchMetadataInterceptorMapping = null;
    interceptorIterator = currentActionInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping for action 'fetchMetadataExempted' not present in 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping for action 'fetchMetadataExempted' in 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, fetchMetadataExemptedActionConfig.getName());
    request.setContextPath("/" + fetchMetadataExemptedActionConfig.getName());
    assertNotEquals("Expected interceptor to accept this request [" + "/" + fetchMetadataExemptedActionConfig.getName() + "]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    // Test fetchMetadata interceptor for the "fetchMetadataNotExempted" action.
    currentActionInterceptors = fetchMetadataNotExemptedActionConfig.getInterceptors();
    assertFalse("'fetchMetadataNotExempted' interceptors in struts-testing.xml is empty ?", currentActionInterceptors.isEmpty());
    configuredFetchMetadataInterceptorMapping = null;
    interceptorIterator = currentActionInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping for action 'fetchMetadataNotExempted' not present in 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping 'fetchMetadataExempted' in 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, fetchMetadataNotExemptedActionConfig.getName());
    request.setContextPath("/" + fetchMetadataNotExemptedActionConfig.getName());
    assertEquals("Expected interceptor to NOT accept this request [" + "/" + fetchMetadataNotExemptedActionConfig.getName() + "]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    // Test fetchMetadata interceptor for the "fetchMetadataExemptedGlobal" action.
    currentActionInterceptors = fetchMetadataExemptedGlobalActionConfig.getInterceptors();
    assertFalse("'fetchMetadataExemptedGlobal' interceptors in struts-testing.xml is empty ?", currentActionInterceptors.isEmpty());
    configuredFetchMetadataInterceptorMapping = null;
    interceptorIterator = currentActionInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping for action 'fetchMetadataExemptedGlobal' not present in 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping 'fetchMetadataExemptedGlobal' in 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, fetchMetadataExemptedGlobalActionConfig.getName());
    request.setContextPath("/" + fetchMetadataExemptedGlobalActionConfig.getName());
    assertNotEquals("Expected interceptor to accept this request [" + "/" + fetchMetadataExemptedGlobalActionConfig.getName() + "]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) RuntimeConfiguration(com.opensymphony.xwork2.config.RuntimeConfiguration)

Example 37 with ConfigurationProvider

use of com.opensymphony.xwork2.config.ConfigurationProvider in project struts by apache.

the class XmlConfigurationProviderResultsTest method testResultTypes.

public void testResultTypes() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-results.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // setup expectations
    ResultTypeConfig chainResult = new ResultTypeConfig.Builder("chain", ActionChainResult.class.getName()).build();
    ResultTypeConfig mockResult = new ResultTypeConfig.Builder("mock", MockResult.class.getName()).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map resultTypes = pkg.getResultTypeConfigs();
    // assertions
    assertEquals(2, resultTypes.size());
    assertEquals("chain", pkg.getDefaultResultType());
    assertEquals(chainResult, resultTypes.get("chain"));
    assertEquals(mockResult, resultTypes.get("mock"));
}
Also used : ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) ResultTypeConfig(com.opensymphony.xwork2.config.entities.ResultTypeConfig) Map(java.util.Map) HashMap(java.util.HashMap) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 38 with ConfigurationProvider

use of com.opensymphony.xwork2.config.ConfigurationProvider in project struts by apache.

the class XmlConfigurationProviderResultsTest method testResultNames.

public void testResultNames() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-result-names.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map<String, ActionConfig> actionConfigs = pkg.getActionConfigs();
    // assertions
    assertNotNull(actionConfigs);
    Map<String, ResultConfig> resultConfigs = actionConfigs.get("noname").getResults();
    assertEquals(1, resultConfigs.size());
    assertTrue(resultConfigs.containsKey(Action.SUCCESS));
    resultConfigs = actionConfigs.get("success").getResults();
    assertEquals(1, resultConfigs.size());
    assertTrue(resultConfigs.containsKey(Action.SUCCESS));
    resultConfigs = actionConfigs.get("empty").getResults();
    assertEquals(1, resultConfigs.size());
    assertTrue(resultConfigs.containsKey(Action.SUCCESS));
    resultConfigs = actionConfigs.get("comma").getResults();
    assertEquals(1, resultConfigs.size());
    assertTrue(resultConfigs.containsKey(" , "));
    resultConfigs = actionConfigs.get("error-input").getResults();
    assertEquals(2, resultConfigs.size());
    assertTrue(resultConfigs.containsKey(Action.ERROR));
    assertTrue(resultConfigs.containsKey(Action.INPUT));
    resultConfigs = actionConfigs.get("error-input2").getResults();
    assertEquals(2, resultConfigs.size());
    assertTrue(resultConfigs.containsKey(Action.ERROR));
    assertTrue(resultConfigs.containsKey(Action.INPUT));
    resultConfigs = actionConfigs.get("noname-error-input").getResults();
    assertEquals(3, resultConfigs.size());
    assertTrue(resultConfigs.containsKey(Action.SUCCESS));
    assertTrue(resultConfigs.containsKey(Action.ERROR));
    assertTrue(resultConfigs.containsKey(Action.INPUT));
    resultConfigs = actionConfigs.get("noname-error-input2").getResults();
    assertEquals(3, resultConfigs.size());
    assertTrue(resultConfigs.containsKey(Action.SUCCESS));
    assertTrue(resultConfigs.containsKey(Action.ERROR));
    assertTrue(resultConfigs.containsKey(Action.INPUT));
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 39 with ConfigurationProvider

use of com.opensymphony.xwork2.config.ConfigurationProvider in project struts by apache.

the class XmlConfigurationProviderResultsTest method testActions.

public void testActions() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-results.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    HashMap<String, String> parameters = new HashMap<>();
    HashMap<String, ResultConfig> results = new HashMap<>();
    results.put("chainDefaultTypedResult", new ResultConfig.Builder("chainDefaultTypedResult", ActionChainResult.class.getName()).build());
    results.put("mockTypedResult", new ResultConfig.Builder("mockTypedResult", MockResult.class.getName()).build());
    Map<String, String> resultParams = new HashMap<>();
    resultParams.put("actionName", "bar.vm");
    results.put("specificLocationResult", new ResultConfig.Builder("specificLocationResult", ActionChainResult.class.getName()).addParams(resultParams).build());
    resultParams = new HashMap<>();
    resultParams.put("actionName", "foo.vm");
    results.put("defaultLocationResult", new ResultConfig.Builder("defaultLocationResult", ActionChainResult.class.getName()).addParams(resultParams).build());
    resultParams = new HashMap<>();
    resultParams.put("foo", "bar");
    results.put("noDefaultLocationResult", new ResultConfig.Builder("noDefaultLocationResult", ActionChainResult.class.getName()).addParams(resultParams).build());
    ActionConfig expectedAction = new ActionConfig.Builder("default", "Bar", SimpleAction.class.getName()).addParams(parameters).addResultConfigs(results).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map<String, ActionConfig> actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(1, actionConfigs.size());
    ActionConfig action = actionConfigs.get("Bar");
    assertEquals(expectedAction, action);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) MockResult(com.opensymphony.xwork2.mock.MockResult) HashMap(java.util.HashMap) ActionChainResult(com.opensymphony.xwork2.ActionChainResult) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 40 with ConfigurationProvider

use of com.opensymphony.xwork2.config.ConfigurationProvider in project struts by apache.

the class XmlConfigurationProviderTest method testProvider.

private void testProvider(String configFile) throws Exception {
    ConfigurationProvider provider = buildConfigurationProvider(configFile);
    container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
    assertFalse(provider.needsReload());
    String fullPath = ClassLoaderUtil.getResource(configFile, ConfigurationProvider.class).toString();
    int startIndex = fullPath.indexOf(":file:/");
    int endIndex = fullPath.indexOf("!/");
    String jar = fullPath.substring(startIndex + (":file:/".length() - 1), endIndex).replaceAll("%20", " ");
    File file = new File(jar);
    assertTrue("File [" + file + "] doesn't exist!", file.exists());
    changeFileTime(jar, file);
    assertFalse(provider.needsReload());
}
Also used : ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) File(java.io.File)

Aggregations

ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)41 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)22 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)21 Map (java.util.Map)12 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)11 XmlConfigurationProvider (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider)10 HashMap (java.util.HashMap)9 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)7 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)6 File (java.io.File)6 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)5 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)5 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)5 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)4 MockResult (com.opensymphony.xwork2.mock.MockResult)4 DefaultUnknownHandlerManager (com.opensymphony.xwork2.DefaultUnknownHandlerManager)3 SimpleAction (com.opensymphony.xwork2.SimpleAction)3 UnknownHandlerManager (com.opensymphony.xwork2.UnknownHandlerManager)3 Configuration (com.opensymphony.xwork2.config.Configuration)3 RuntimeConfiguration (com.opensymphony.xwork2.config.RuntimeConfiguration)3