This is what you need, works for any component:

/**
   * @param <T>
   * @param formComponent
   * @return T
   */
  @SuppressWarnings("unchecked")
  public static <T> T getConvertedValue(FormComponent<T> formComponent) {
    try {
      if (formComponent instanceof AbstractSingleSelectChoice) {
        return (T)
abstractSingleSelectChoiceConverterMethod.invoke(formComponent,
formComponent.getValue());
      }

      if (formComponent instanceof RadioGroupWithVisibleConverter) {
        String rawInput = ((RadioGroupWithVisibleConverter<T>)
formComponent).getRawInput();
        if (rawInput != null) {
          return ((RadioGroupWithVisibleConverter<T>)
formComponent).convertValue(new String[] { rawInput });
        }

        T t = formComponent.getConvertedInput();

        if (t != null) {
          return t;
        }

        return formComponent.getModelObject();
      }

      String value = getRawInputForReal(formComponent);

      if ("[-NO-RAW-INPUT-]".equals(value)) {
        return formComponent.getModelObject();
      }

      if (Utils.isEmpty(value) &&
(!String.class.equals(formComponent.getType()))) {
        value = null;
      }

      return (T)
formComponent.getConverter(formComponent.getType()).convertToObject(value,
formComponent.getLocale());
    } catch (ConversionException e) {
      ValidationError error = new ValidationError();
      if (e.getResourceKey() != null)
      {
        error.addMessageKey(e.getResourceKey());
      }
      String simpleName = Classes.simpleName(formComponent.getType());
      error.addMessageKey("IConverter." + simpleName);
      error.addMessageKey("IConverter");
      error.setVariable("type", simpleName);

      final Locale locale = e.getLocale();
      if (locale != null)
      {
        error.setVariable("locale", locale);
        error.setVariable("input", Utils.noNull(formComponent.getRawInput()));
      }
      error.setVariable("exception", e);
      Format format = e.getFormat();
      if (format instanceof SimpleDateFormat)
      {
        error.setVariable("format",
((SimpleDateFormat)format).toLocalizedPattern());
      }

      Map<String, Object> variables = e.getVariables();
      if (variables != null)
      {
        error.getVariables().putAll(variables);
      }

      formComponent.error((IValidationError)error);

      return formComponent.getModelObject(); // Default value
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

**
Martin

2012/9/5 Markward Schubert <markward.schub...@gmail.com>:
> Thanks for the link!
>
> This is really interesting and I think I will adapt this.
> Unfortunately I am not sure if this addresses my original Problem.
>
> What I need is a kind of code, like this:
>
> // start page...
> tester.startComponentInPage(MyFormPanel.....)
>
> // verify that after initial page rendering, the selected item in the
> DropDown equals the pageModel's
> assertEquals(pageModel.myChoice.getLabel(),getDropDownChoice().currentlyRenderedSelectedChoiceItem());
>
> As described before, I had a problem in the equals. This resulted in
> myChoice being let's say "red" and the DropDown containing "red" "black"
> "blue", but "please select" was rendered in the DDC, because myChoice("red")
> was not equal to dropChoice("red").
>
> Sorry for the pseudo code :-)
>
> So the DDC could not find out that the "red" of my model was the same as the
> "red" in the choices list.
> Of course this would be a stupid bug, but i would like to protect my code
> against fellows changing the equals behavior.
>
>
>
>
>
> --
> View this message in context: 
> http://apache-wicket.1842946.n4.nabble.com/Question-about-unit-testing-tp4651766p4651768.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to