Search in sources :

Example 6 with XWorkConverter

use of com.opensymphony.xwork2.conversion.impl.XWorkConverter in project struts by apache.

the class SetPropertiesTest method testSetCollectionByConverterFromCollection.

public void testSetCollectionByConverterFromCollection() {
    Foo foo = new Foo();
    ValueStack vs = ActionContext.getContext().getValueStack();
    vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    XWorkConverter c = (XWorkConverter) ((OgnlTypeConverterWrapper) Ognl.getTypeConverter(vs.getContext())).getTarget();
    c.registerConverter(Cat.class.getName(), new FooBarConverter());
    vs.push(foo);
    HashSet s = new HashSet();
    s.add("1");
    s.add("2");
    vs.setValue("cats", s);
    assertNotNull(foo.getCats());
    assertEquals(2, foo.getCats().size());
    assertEquals(Cat.class, foo.getCats().get(0).getClass());
    assertEquals(Cat.class, foo.getCats().get(1).getClass());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) Foo(com.opensymphony.xwork2.util.Foo) Cat(com.opensymphony.xwork2.util.Cat) FooBarConverter(com.opensymphony.xwork2.conversion.impl.FooBarConverter) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter)

Example 7 with XWorkConverter

use of com.opensymphony.xwork2.conversion.impl.XWorkConverter in project struts by apache.

the class NotURLClassLoader method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    result = new EmbeddedJSPResult();
    request = EasyMock.createNiceMock(HttpServletRequest.class);
    response = new MockHttpServletResponse();
    context = new MockServletContext();
    config = new MockServletConfig(context);
    final Map params = new HashMap();
    HttpSession session = EasyMock.createNiceMock(HttpSession.class);
    EasyMock.replay(session);
    EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
    EasyMock.expect(request.getParameterMap()).andReturn(params).anyTimes();
    EasyMock.expect(request.getParameter("username")).andAnswer(() -> ActionContext.getContext().getParameters().get("username").getValue());
    EasyMock.expect(request.getAttribute("something")).andReturn("somethingelse").anyTimes();
    EasyMock.replay(request);
    // mock value stack
    ValueStack valueStack = EasyMock.createNiceMock(ValueStack.class);
    EasyMock.expect(valueStack.getActionContext()).andReturn(ActionContext.getContext()).anyTimes();
    EasyMock.replay(valueStack);
    // mock converter
    XWorkConverter converter = new DummyConverter();
    DefaultFileManager fileManager = new DefaultFileManager();
    fileManager.setReloadingConfigs(false);
    // mock container
    Container container = EasyMock.createNiceMock(Container.class);
    EasyMock.expect(container.getInstance(XWorkConverter.class)).andReturn(converter).anyTimes();
    TextParser parser = new OgnlTextParser();
    EasyMock.expect(container.getInstance(TextParser.class)).andReturn(parser).anyTimes();
    EasyMock.expect(container.getInstanceNames(FileManager.class)).andReturn(new HashSet<>()).anyTimes();
    EasyMock.expect(container.getInstance(FileManager.class)).andReturn(fileManager).anyTimes();
    UrlHelper urlHelper = new DefaultUrlHelper();
    EasyMock.expect(container.getInstance(UrlHelper.class)).andReturn(urlHelper).anyTimes();
    FileManagerFactory fileManagerFactory = new DummyFileManagerFactory();
    EasyMock.expect(container.getInstance(FileManagerFactory.class)).andReturn(fileManagerFactory).anyTimes();
    EasyMock.replay(container);
    ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).withServletRequest(request).withServletResponse(response).withServletContext(context).withContainer(container).withValueStack(valueStack).bind();
}
Also used : OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) UrlHelper(org.apache.struts2.views.util.UrlHelper) ValueStack(com.opensymphony.xwork2.util.ValueStack) TextParser(com.opensymphony.xwork2.util.TextParser) OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) MockServletConfig(org.springframework.mock.web.MockServletConfig) MockServletContext(org.springframework.mock.web.MockServletContext) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) FileManager(com.opensymphony.xwork2.FileManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Container(com.opensymphony.xwork2.inject.Container) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HashSet(java.util.HashSet)

Example 8 with XWorkConverter

use of com.opensymphony.xwork2.conversion.impl.XWorkConverter in project struts by apache.

the class OgnlValueStack method readResolve.

private Object readResolve() {
    // TODO: this should be done better
    ActionContext ac = ActionContext.getContext();
    Container cont = ac.getContainer();
    XWorkConverter xworkConverter = cont.getInstance(XWorkConverter.class);
    CompoundRootAccessor accessor = (CompoundRootAccessor) cont.getInstance(PropertyAccessor.class, CompoundRoot.class.getName());
    TextProvider prov = cont.getInstance(TextProvider.class, "system");
    final boolean allowStaticMethod = BooleanUtils.toBoolean(cont.getInstance(String.class, StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS));
    final boolean allowStaticField = BooleanUtils.toBoolean(cont.getInstance(String.class, StrutsConstants.STRUTS_ALLOW_STATIC_FIELD_ACCESS));
    OgnlValueStack aStack = new OgnlValueStack(xworkConverter, accessor, prov, allowStaticMethod, allowStaticField);
    aStack.setOgnlUtil(cont.getInstance(OgnlUtil.class));
    aStack.setRoot(xworkConverter, accessor, this.root, allowStaticMethod, allowStaticField);
    return aStack;
}
Also used : Container(com.opensymphony.xwork2.inject.Container) CompoundRootAccessor(com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter) ActionContext(com.opensymphony.xwork2.ActionContext) TextProvider(com.opensymphony.xwork2.TextProvider)

Aggregations

XWorkConverter (com.opensymphony.xwork2.conversion.impl.XWorkConverter)6 ValueStack (com.opensymphony.xwork2.util.ValueStack)6 ActionContext (com.opensymphony.xwork2.ActionContext)3 Container (com.opensymphony.xwork2.inject.Container)3 FooBarConverter (com.opensymphony.xwork2.conversion.impl.FooBarConverter)2 Cat (com.opensymphony.xwork2.util.Cat)2 Foo (com.opensymphony.xwork2.util.Foo)2 OgnlTextParser (com.opensymphony.xwork2.util.OgnlTextParser)2 TextParser (com.opensymphony.xwork2.util.TextParser)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 FileManager (com.opensymphony.xwork2.FileManager)1 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1 TextProvider (com.opensymphony.xwork2.TextProvider)1 CompoundRootAccessor (com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor)1 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1