public void testModelOnSession() throws Exception { inter.setScope("session"); inter.setName("king"); User user = new User(); user.setName("King George"); Map session = new HashMap(); ActionContext.getContext().setSession(session); ActionContext.getContext().getSession().put("king", user); ScopedModelDriven action = new MyUserScopedModelDrivenAction(); MockActionInvocation mai = new MockActionInvocation(); MockActionProxy map = new MockActionProxy(); ActionConfig ac = new ActionConfig.Builder("", "", "").build(); map.setConfig(ac); mai.setAction(action); mai.setProxy(map); inter.intercept(mai); inter.destroy(); assertNotNull(action.getModel()); assertNotNull(action.getScopeKey()); assertEquals("king", action.getScopeKey()); Object model = ActionContext.getContext().getSession().get(action.getScopeKey()); assertNotNull(model); assertTrue("Model should be an User object", model instanceof User); assertEquals("King George", ((User) model).getName()); }
public String stack() { ValueStack stack = ActionContext.getContext().getValueStack(); // DefaultTextProvider, NestedActionTest pushed on by the test, and the NestedAction Assert.assertEquals(3, stack.size()); Assert.assertNotNull(stack.findValue(ActionNestingTest.KEY)); Assert.assertEquals( ActionContext.getContext().getValueStack().findValue(ActionNestingTest.KEY), ActionNestingTest.VALUE); Assert.assertEquals( ActionNestingTest.NESTED_VALUE, stack.findValue(ActionNestingTest.NESTED_KEY)); return SUCCESS; }
public String execute() throws Exception { // 创建ActionContext实例 ActionContext ctx = ActionContext.getContext(); // 获取HttpSession中的user属性 String mgrName = (String) ctx.getSession().get(WebConstant.USER); // 获取需要被当前经理处理的全部申请 setApps(mgr.getAppsByMgr(mgrName)); return SUCCESS; }
@Override protected void setUp() throws Exception { super.setUp(); converter = container.getInstance(XWorkConverter.class); ac = ActionContext.getContext(); ac.setLocale(Locale.US); context = ac.getContextMap(); }
public String noStack() { ValueStack stack = ActionContext.getContext().getValueStack(); // Action + DefaultTextProvider on the stack Assert.assertEquals(2, stack.size()); Assert.assertNull(stack.findValue(ActionNestingTest.KEY)); Assert.assertEquals( ActionNestingTest.NESTED_VALUE, stack.findValue(ActionNestingTest.NESTED_KEY)); return SUCCESS; }
public String execute() throws Exception { // 创建ActionContext实例 ActionContext ctx = ActionContext.getContext(); // 获取HttpSession中的user属性 String mgrName = (String) ctx.getSession().get(WebConstant.USER); // 调用业务逻辑方法取得当前员工的全部发薪列表 List<SalaryBean> result = mgr.getSalaryByMgr(mgrName); System.out.println("--------------" + result); setSals(result); return SUCCESS; }
public void testFindConversionErrorMessage() { ModelDrivenAnnotationAction action = new ModelDrivenAnnotationAction(); ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(action); stack.push(action.getModel()); String message = XWorkConverter.getConversionErrorMessage("birth", stack); assertNotNull(message); assertEquals("Invalid date for birth.", message); message = XWorkConverter.getConversionErrorMessage("foo", stack); assertNotNull(message); assertEquals("Invalid field value for field \"foo\".", message); }
// TODO: Fixme... This test does not work with GenericsObjectDeterminer! public void testStringToCollectionConversion() { ValueStack stack = ActionContext.getContext().getValueStack(); Map<String, Object> stackContext = stack.getContext(); stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE); stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE); stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE); AnnotationUser user = new AnnotationUser(); stack.push(user); stack.setValue("list", "asdf"); assertNotNull(user.getList()); assertEquals(1, user.getList().size()); assertEquals(String.class, user.getList().get(0).getClass()); assertEquals("asdf", user.getList().get(0)); }
public void testFindConversionMappingForInterface() { ModelDrivenAnnotationAction2 action = new ModelDrivenAnnotationAction2(); ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(action); stack.push(action.getModel()); Map<String, Object> ognlStackContext = stack.getContext(); ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE); String value = "asdf:123"; Object o = converter.convertValue( ognlStackContext, action.getModel(), null, "barObj", value, Bar.class); assertNotNull(o); assertTrue("class is: " + o.getClass(), o instanceof Bar); Bar b = (Bar) o; assertEquals(value, b.getTitle() + ":" + b.getSomethingElse()); }
public void testResolveModel() throws Exception { ActionContext ctx = ActionContext.getContext(); ctx.setSession(new HashMap()); ObjectFactory factory = ObjectFactory.getObjectFactory(); Object obj = inter.resolveModel(factory, ctx, "java.lang.String", "request", "foo"); assertNotNull(obj); assertTrue(obj instanceof String); assertTrue(obj == ctx.get("foo")); obj = inter.resolveModel(factory, ctx, "java.lang.String", "session", "foo"); assertNotNull(obj); assertTrue(obj instanceof String); assertTrue(obj == ctx.getSession().get("foo")); obj = inter.resolveModel(factory, ctx, "java.lang.String", "session", "foo"); assertNotNull(obj); assertTrue(obj instanceof String); assertTrue(obj == ctx.getSession().get("foo")); }
public void testScopedModelDrivenAction() throws Exception { inter.setScope("request"); ScopedModelDriven action = new MyUserScopedModelDrivenAction(); MockActionInvocation mai = new MockActionInvocation(); MockActionProxy map = new MockActionProxy(); ActionConfig ac = new ActionConfig.Builder("", "", "").build(); map.setConfig(ac); mai.setAction(action); mai.setProxy(map); inter.intercept(mai); inter.destroy(); assertNotNull(action.getModel()); assertNotNull(action.getScopeKey()); assertEquals("com.opensymphony.xwork2.test.User", action.getScopeKey()); Object model = ActionContext.getContext().get(action.getScopeKey()); assertNotNull(model); assertTrue("Model should be an User object", model instanceof User); }
public void testFieldErrorMessageAddedWhenConversionFailsOnModelDriven() { ModelDrivenAnnotationAction action = new ModelDrivenAnnotationAction(); ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(action); stack.push(action.getModel()); Map<String, Object> ognlStackContext = stack.getContext(); ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE); String[] value = new String[] {"invalid date"}; assertEquals( "Conversion should have failed.", OgnlRuntime.NoConversionPossible, converter.convertValue(ognlStackContext, action, null, "birth", value, Date.class)); stack.pop(); stack.pop(); Map conversionErrors = (Map) ognlStackContext.get(ActionContext.CONVERSION_ERRORS); assertNotNull(conversionErrors); assertEquals(1, conversionErrors.size()); assertNotNull(conversionErrors.get("birth")); assertEquals(value, conversionErrors.get("birth")); }
public void testFieldErrorMessageAddedForComplexProperty() { SimpleAnnotationAction action = new SimpleAnnotationAction(); action.setBean(new AnnotatedTestBean()); ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(action); Map<String, Object> ognlStackContext = stack.getContext(); ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE); ognlStackContext.put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, "bean.birth"); String[] value = new String[] {"invalid date"}; assertEquals( "Conversion should have failed.", OgnlRuntime.NoConversionPossible, converter.convertValue( ognlStackContext, action.getBean(), null, "birth", value, Date.class)); stack.pop(); Map conversionErrors = (Map) stack.getContext().get(ActionContext.CONVERSION_ERRORS); assertNotNull(conversionErrors); assertTrue(conversionErrors.size() == 1); assertEquals(value, conversionErrors.get("bean.birth")); }
public String intercept(ActionInvocation actionInvocation) throws Exception { ServletContext context = ServletActionContext.getServletContext(); ActionContext act = actionInvocation.getInvocationContext(); Map<String, Object> application = act.getApplication(); String gingkgoHome = System.getProperty("gingkgo.home"); if (null == gingkgoHome) System.setProperty("gingkgo.home", context.getRealPath("")); // 设置系统模块 if (null == application.get(EbizCommon.EBIZ_APP_MODULES)) { List<Module> modules = moduleService.roGetModuleList(true); Set<String> ms = new HashSet<String>(0); for (Module module : modules) { ms.add(module.getModuleName()); } application.put(EbizCommon.EBIZ_APP_MODULES, ms); } // 设置产品组成 if (null == application.get(EbizCommon.EBIZ_APP_PACKAGE_ITEM)) { List<ProductItem> items = productService.getProductItems(ProductType.Package); application.put(EbizCommon.EBIZ_APP_PACKAGE_ITEM, items); } // 主菜单 if (null == application.get(EbizCommon.EBIZ_APP_MAIN_MENU)) { List<MenuItem> items = new ArrayList<MenuItem>(); items.add(new MenuItem("Desktop", "桌面", "", "ROLE_USER")); items.add(new MenuItem("Products", "产品资源", "", "ROLE_PRODUCT")); items.add(new MenuItem("Order", "订单管理", "", "ROLE_SALES")); items.add(new MenuItem("Operate", "计调操作", "", "ROLE_OPERATOR")); items.add(new MenuItem("Express", "配送管理", "", "ROLE_TRANSPORT")); items.add(new MenuItem("CRM", "客户管理", "", "ROLE_AGENT_MANAGER")); items.add(new MenuItem("Finance", "财务结算", "", "ROLE_FINANCE")); items.add(new MenuItem("Stat", "统计分析", "", "ROLE_SUPERUSER")); items.add(new MenuItem("System", "系统设置", "", "ROLE_SUPERUSER")); items.add(new MenuItem("Config", "设置", "", "ROLE_SUPERUSER")); items.add(new MenuItem("Company", "公司设置", "", "ROLE_SUPERUSER")); for (MenuItem menuItem : items) { List<Shortcut> cuts = shortcutManager.getShortcutByModule(menuItem.getItemName()); for (Shortcut shortcut : cuts) { menuItem .getChild() .add( new MenuItem( shortcut.getModuleName(), shortcut.getDisplayName(), shortcut.getRelativePath(), shortcut.getRoles())); } } application.put(EbizCommon.EBIZ_APP_MAIN_MENU, items); } // 设置产品组成 if (null == application.get(EbizCommon.EBIZ_SYS_CONFIG)) { List<SysConfig> items = configService.getAllConfig(); for (SysConfig config : items) { application.put(config.getName(), config.getValue()); } application.put(EbizCommon.EBIZ_SYS_CONFIG, "OK"); } // XML设置参数 if (null == application.get(EbizCommon.EBIZ_RES_CONFIG)) { try { application.put( EbizCommon.EBIZ_RES_CONFIG, XMLUtility.getInstance(context.getRealPath(XML_PATH))); } catch (ParserConfigurationException pce) { logger.error("", pce); } catch (IOException ioe) { logger.error("", ioe); } catch (SAXException saxe) { logger.error("", saxe); } catch (Exception e) { logger.error("", e); } } ActionContext.getContext().getValueStack().push(this); return actionInvocation.invoke(); }
public void testValueStackWithTypeParameter() { ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(new Foo1()); Bar1 bar = (Bar1) stack.findValue("bar", Bar1.class); assertNotNull(bar); }