@Override
  protected void setUp() throws Exception {
    super.setUp();
    converter = container.getInstance(XWorkConverter.class);

    ac = ActionContext.getContext();
    ac.setLocale(Locale.US);
    context = ac.getContextMap();
  }
 // 拦截Action处理的拦截方法
 public String intercept(ActionInvocation invocation) throws Exception {
   // 取得请求相关的ActionContext实例
   ActionContext ctx = invocation.getInvocationContext();
   Map session = ctx.getSession();
   // 取出名为user的Session属性
   String user = (String) session.get("user");
   // 如果没有登录,或者登录所用的用户名不是scott,都返回重新登录
   if (user != null && user.equals("crazyit.org")) {
     return invocation.invoke();
   }
   // 没有登录,将服务器提示设置成一个HttpServletRequest属性
   ctx.put("tip", "您还没有登录,请输入crazyit.org,leegang登录系统");
   // 直接返回login的逻辑视图
   return Action.LOGIN;
 }
  public void testGenericPropertiesFromGetter() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    assertEquals(1, gb.getGetterList().size());
    assertEquals("42.42", stack.findValue("getterList.get(0).toString()"));
    assertEquals(new Double(42.42), stack.findValue("getterList.get(0)"));
    assertEquals(new Double(42.42), gb.getGetterList().get(0));
  }
  public void testGenericProperties() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    String[] value = new String[] {"123.12", "123.45"};
    stack.setValue("doubles", value);
    assertEquals(2, gb.getDoubles().size());
    assertEquals(Double.class, gb.getDoubles().get(0).getClass());
    assertEquals(new Double(123.12), gb.getDoubles().get(0));
    assertEquals(new Double(123.45), gb.getDoubles().get(1));
  }
  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 testGenericPropertiesFromSetter() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    stack.setValue("genericMap[123.12]", "66");
    stack.setValue("genericMap[456.12]", "42");

    assertEquals(2, gb.getGenericMap().size());
    assertEquals("66", stack.findValue("genericMap.get(123.12).toString()"));
    assertEquals("42", stack.findValue("genericMap.get(456.12).toString()"));
    assertEquals(66, stack.findValue("genericMap.get(123.12)"));
    assertEquals(42, stack.findValue("genericMap.get(456.12)"));
    assertEquals(true, stack.findValue("genericMap.containsValue(66)"));
    assertEquals(true, stack.findValue("genericMap.containsValue(42)"));
    assertEquals(true, stack.findValue("genericMap.containsKey(123.12)"));
    assertEquals(true, stack.findValue("genericMap.containsKey(456.12)"));
  }
  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());
  }
  // FIXME: Implement nested Generics such as: List of Generics List, Map of Generic keys/values,
  // etc...
  public void no_testGenericPropertiesWithNestedGenerics() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    stack.setValue("extendedMap[123.12]", new String[] {"1", "2", "3", "4"});
    stack.setValue("extendedMap[456.12]", new String[] {"5", "6", "7", "8", "9"});

    System.out.println("gb.getExtendedMap(): " + gb.getExtendedMap());

    assertEquals(2, gb.getExtendedMap().size());
    System.out.println(stack.findValue("extendedMap"));
    assertEquals(4, stack.findValue("extendedMap.get(123.12).size"));
    assertEquals(5, stack.findValue("extendedMap.get(456.12).size"));

    assertEquals("1", stack.findValue("extendedMap.get(123.12).get(0)"));
    assertEquals("5", stack.findValue("extendedMap.get(456.12).get(0)"));
    assertEquals(Integer.class, stack.findValue("extendedMap.get(123.12).get(0).class"));
    assertEquals(Integer.class, stack.findValue("extendedMap.get(456.12).get(0).class"));

    assertEquals(List.class, stack.findValue("extendedMap.get(123.12).class"));
    assertEquals(List.class, stack.findValue("extendedMap.get(456.12).class"));
  }
  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"));
  }
Example #12
0
 @Override
 protected void processKeyEvent(KeyEvent evt) {
   if (evt.getID() == KeyEvent.KEY_PRESSED) {
     ActionContext ac = VFSBrowser.getActionContext();
     int row = parentDirectories.getSelectedIndex();
     switch (evt.getKeyCode()) {
       case KeyEvent.VK_DOWN:
         evt.consume();
         if (row < parentDirectories.getSize().height - 1)
           parentDirectories.setSelectedIndex(++row);
         break;
       case KeyEvent.VK_LEFT:
         if ((evt.getModifiers() & InputEvent.ALT_MASK) > 0) {
           evt.consume();
           browser.previousDirectory();
         } else super.processEvent(evt);
         break;
       case KeyEvent.VK_RIGHT:
         if ((evt.getModifiers() & InputEvent.ALT_MASK) > 0) {
           evt.consume();
           browser.nextDirectory();
         } else super.processEvent(evt);
         break;
       case KeyEvent.VK_TAB:
         evt.consume();
         if ((evt.getModifiers() & InputEvent.SHIFT_MASK) > 0) browser.focusOnDefaultComponent();
         else table.requestFocus();
         break;
       case KeyEvent.VK_UP:
         evt.consume();
         if (row > 0) {
           parentDirectories.setSelectedIndex(--row);
         }
         break;
       case KeyEvent.VK_BACK_SPACE:
         evt.consume();
         EditAction up = ac.getAction("vfs.browser.up");
         ac.invokeAction(evt, up);
         break;
       case KeyEvent.VK_F5:
         evt.consume();
         EditAction reload = ac.getAction("vfs.browser.reload");
         ac.invokeAction(evt, reload);
         break;
       case KeyEvent.VK_ENTER:
         evt.consume();
         if (row != -1) {
           // basically the same handling as in ParentMouseHandler#mouseReleased
           Object obj = parentDirectories.getModel().getElementAt(row);
           if (obj instanceof VFSFile) {
             VFSFile dirEntry = (VFSFile) obj;
             browser.setDirectory(dirEntry.getPath());
             if (browser.getMode() == VFSBrowser.BROWSER) focusOnFileView();
           }
         }
         break;
         /* These actions don't work because they look at the EntryTable for the current selected
         * 	item. We need actions that look at the parentDirectoryList item instead.
         *
         			case KeyEvent.VK_DELETE:
         				evt.consume();
         				ea = ac.getAction("vfs.browser.delete");
         				ac.invokeAction(evt, ea);
         				break;
         			case KeyEvent.CTRL_MASK | KeyEvent.VK_N:
         				evt.consume();
         				ea = ac.getAction("vfs.browser.new-file");
         				ac.invokeAction(evt, ea);
         				break;
         			case KeyEvent.VK_INSERT:
         				evt.consume();
         				ea = ac.getAction("vfs.browser.new-directory");
         				ac.invokeAction(evt, ea);
         				break; */
     }
   } else if (evt.getID() == KeyEvent.KEY_TYPED) {
     if (evt.isControlDown() || evt.isAltDown() || evt.isMetaDown()) {
       evt.consume();
       return;
     }
     switch (evt.getKeyChar()) {
       case '~':
         evt.consume();
         if (browser.getMode() == VFSBrowser.BROWSER)
           browser.setDirectory(System.getProperty("user.home"));
         break;
       case '/':
         evt.consume();
         if (browser.getMode() == VFSBrowser.BROWSER) browser.rootDirectory();
         break;
       case '-':
         evt.consume();
         if (browser.getMode() == VFSBrowser.BROWSER) {
           browser.setDirectory(browser.getView().getBuffer().getDirectory());
         }
         break;
     }
   }
   if (!evt.isConsumed()) super.processKeyEvent(evt);
 }
 @Override
 protected void tearDown() throws Exception {
   ActionContext.setContext(null);
 }
 public void testValueStackWithTypeParameter() {
   ValueStack stack = ActionContext.getContext().getValueStack();
   stack.push(new Foo1());
   Bar1 bar = (Bar1) stack.findValue("bar", Bar1.class);
   assertNotNull(bar);
 }