Search in sources :

Example 1 with MockConfiguration

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

the class XmlConfigurationProviderTest method testLoadOrder.

public void testLoadOrder() throws Exception {
    configuration = new MockConfiguration();
    ((MockConfiguration) configuration).selfRegister();
    container = configuration.getContainer();
    XmlConfigurationProvider prov = new StrutsXmlConfigurationProvider("xwork-test-load-order.xml") {

        @Override
        protected Iterator<URL> getConfigurationUrls(String fileName) throws IOException {
            List<URL> urls = new ArrayList<>();
            urls.add(ClassLoaderUtil.getResource("com/opensymphony/xwork2/config/providers/loadorder1/xwork-test-load-order.xml", XmlConfigurationProvider.class));
            urls.add(ClassLoaderUtil.getResource("com/opensymphony/xwork2/config/providers/loadorder2/xwork-test-load-order.xml", XmlConfigurationProvider.class));
            urls.add(ClassLoaderUtil.getResource("com/opensymphony/xwork2/config/providers/loadorder3/xwork-test-load-order.xml", XmlConfigurationProvider.class));
            return urls.iterator();
        }
    };
    prov.setObjectFactory(container.getInstance(ObjectFactory.class));
    prov.setFileManagerFactory(container.getInstance(FileManagerFactory.class));
    prov.init(configuration);
    List<Document> docs = prov.getDocuments();
    assertEquals(3, docs.size());
    assertEquals(1, XmlHelper.getLoadOrder(docs.get(0)).intValue());
    assertEquals(2, XmlHelper.getLoadOrder(docs.get(1)).intValue());
    assertEquals(3, XmlHelper.getLoadOrder(docs.get(2)).intValue());
}
Also used : FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) ArrayList(java.util.ArrayList) MockConfiguration(com.opensymphony.xwork2.config.impl.MockConfiguration) Document(org.w3c.dom.Document) URL(java.net.URL)

Example 2 with MockConfiguration

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

the class DispatcherTest method testInterceptorDestroy.

public void testInterceptorDestroy() throws Exception {
    Mock mockInterceptor = new Mock(Interceptor.class);
    mockInterceptor.matchAndReturn("hashCode", 0);
    mockInterceptor.expect("destroy");
    InterceptorMapping interceptorMapping = new InterceptorMapping("test", (Interceptor) mockInterceptor.proxy());
    InterceptorStackConfig isc = new InterceptorStackConfig.Builder("test").addInterceptor(interceptorMapping).build();
    PackageConfig packageConfig = new PackageConfig.Builder("test").addInterceptorStackConfig(isc).build();
    Map<String, PackageConfig> packageConfigs = new HashMap<String, PackageConfig>();
    packageConfigs.put("test", packageConfig);
    Mock mockContainer = new Mock(Container.class);
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
    String reloadConfigs = container.getInstance(String.class, StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD);
    mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD)), reloadConfigs);
    Mock mockConfiguration = new Mock(Configuration.class);
    mockConfiguration.matchAndReturn("getPackageConfigs", packageConfigs);
    mockConfiguration.matchAndReturn("getContainer", mockContainer.proxy());
    mockConfiguration.expect("destroy");
    ConfigurationManager configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
    configurationManager.setConfiguration((Configuration) mockConfiguration.proxy());
    Dispatcher dispatcher = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), configurationManager);
    dispatcher.init();
    dispatcher.cleanup();
    mockInterceptor.verify();
    mockContainer.verify();
    mockConfiguration.verify();
}
Also used : HashMap(java.util.HashMap) Mock(com.mockobjects.dynamic.Mock) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) MockServletContext(org.springframework.mock.web.MockServletContext) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) ConfigurationManager(com.opensymphony.xwork2.config.ConfigurationManager)

Example 3 with MockConfiguration

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

the class TestNGXWorkTestCase method setUp.

@BeforeTest
protected void setUp() throws Exception {
    configurationManager = XWorkTestCaseHelper.setUp();
    configuration = new MockConfiguration();
    ((MockConfiguration) configuration).selfRegister();
    container = configuration.getContainer();
    actionProxyFactory = container.getInstance(ActionProxyFactory.class);
}
Also used : MockConfiguration(com.opensymphony.xwork2.config.impl.MockConfiguration) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with MockConfiguration

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

the class DispatcherTest method testObjectFactoryDestroy.

public void testObjectFactoryDestroy() throws Exception {
    ConfigurationManager cm = new ConfigurationManager(Container.DEFAULT_NAME);
    Dispatcher du = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), cm);
    Mock mockConfiguration = new Mock(Configuration.class);
    cm.setConfiguration((Configuration) mockConfiguration.proxy());
    Mock mockContainer = new Mock(Container.class);
    String reloadConfigs = container.getInstance(String.class, StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD);
    mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD)), reloadConfigs);
    final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
    destroyedObjectFactory.setContainer((Container) mockContainer.proxy());
    mockContainer.expectAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), destroyedObjectFactory);
    mockConfiguration.expectAndReturn("getContainer", mockContainer.proxy());
    mockConfiguration.expectAndReturn("getContainer", mockContainer.proxy());
    mockConfiguration.expect("destroy");
    mockConfiguration.matchAndReturn("getPackageConfigs", new HashMap<String, PackageConfig>());
    du.init();
    assertFalse(destroyedObjectFactory.destroyed);
    du.cleanup();
    assertTrue(destroyedObjectFactory.destroyed);
    mockConfiguration.verify();
    mockContainer.verify();
}
Also used : ConfigurationManager(com.opensymphony.xwork2.config.ConfigurationManager) MockServletContext(org.springframework.mock.web.MockServletContext) Mock(com.mockobjects.dynamic.Mock) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 5 with MockConfiguration

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

the class StrutsJavaConfigurationProviderTest method testRegister.

@Test
public void testRegister() throws Exception {
    final ConstantConfig constantConfig = new ConstantConfig();
    constantConfig.setDevMode(true);
    final String expectedUnknownHandler = "expectedUnknownHandler";
    StrutsJavaConfiguration javaConfig = new StrutsJavaConfiguration() {

        @Override
        public List<String> unknownHandlerStack() {
            return Collections.singletonList(expectedUnknownHandler);
        }

        @Override
        public List<ConstantConfig> constants() {
            return Collections.singletonList(constantConfig);
        }

        @Override
        public List<BeanConfig> beans() {
            return Arrays.asList(new BeanConfig(TestBean.class, "struts"), new BeanConfig(TestBean.class, "struts.static", TestBean.class, Scope.PROTOTYPE, true, true), new BeanConfig(TestBean.class, "struts.test.bean", TestBean.class));
        }

        @Override
        public Optional<BeanSelectionConfig> beanSelection() {
            return Optional.of(new BeanSelectionConfig(TestBeanSelectionProvider.class, "testBeans"));
        }
    };
    StrutsJavaConfigurationProvider provider = new StrutsJavaConfigurationProvider(javaConfig);
    Configuration configuration = new MockConfiguration();
    provider.init(configuration);
    ContainerBuilder builder = new ContainerBuilder();
    LocatableProperties props = new LocatableProperties();
    provider.register(builder, props);
    // constant
    Assert.assertEquals(String.valueOf(constantConfig.getDevMode()), props.get(StrutsConstants.STRUTS_DEVMODE));
    // unknown-handler-stack
    Assert.assertNotNull(configuration.getUnknownHandlerStack());
    Assert.assertEquals(1, configuration.getUnknownHandlerStack().size());
    Assert.assertEquals(expectedUnknownHandler, configuration.getUnknownHandlerStack().get(0).getName());
    // bean
    Container container = builder.create(true);
    TestBean testBean = container.getInstance(TestBean.class);
    Assert.assertNotNull(testBean);
    testBean = container.getInstance(TestBean.class, "struts");
    Assert.assertNotNull(testBean);
    // bean selection
    Set<String> names = container.getInstanceNames(TestBean.class);
    Assert.assertTrue(names.contains("struts"));
    Assert.assertTrue(names.contains("struts.test.bean"));
}
Also used : BeanConfig(org.apache.struts2.config.entities.BeanConfig) MockConfiguration(com.opensymphony.xwork2.config.impl.MockConfiguration) Configuration(com.opensymphony.xwork2.config.Configuration) Container(com.opensymphony.xwork2.inject.Container) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) TestBean(com.opensymphony.xwork2.TestBean) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) MockConfiguration(com.opensymphony.xwork2.config.impl.MockConfiguration) BeanSelectionConfig(org.apache.struts2.config.entities.BeanSelectionConfig) ConstantConfig(org.apache.struts2.config.entities.ConstantConfig) Test(org.junit.Test)

Aggregations

MockConfiguration (com.opensymphony.xwork2.config.impl.MockConfiguration)4 Mock (com.mockobjects.dynamic.Mock)2 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)2 ConfigurationManager (com.opensymphony.xwork2.config.ConfigurationManager)2 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)2 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1 TestBean (com.opensymphony.xwork2.TestBean)1 Configuration (com.opensymphony.xwork2.config.Configuration)1 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)1 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)1 Container (com.opensymphony.xwork2.inject.Container)1 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)1 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 BeanConfig (org.apache.struts2.config.entities.BeanConfig)1 BeanSelectionConfig (org.apache.struts2.config.entities.BeanSelectionConfig)1