You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Markward Schubert <ma...@gmail.com> on 2012/09/05 10:51:04 UTC

Question about unit testing

Hi folks!

I am sure this is a trivial question, but i am simply too dumb to figure
this out.
We have a DopDownChoice, which is prefilled by some modelvalue, given to
the page.
Until now i was doing some testing about the model-state before and after
submitting, which then was correct.
The problem in the browser was, that the rendered DropDownChoice did  not
show the preselected value correctly. It could not recognize the equality
between some member of the available choices and the model's choice.

Actually it turned out that it was a problem with the model object's
equals.

Now my question is:

What is the most elegant way to unit test the correct rendering of the
preselection of the DropDownChoice or similar components?
As said, simply dealing with sumbmit and stuff worked correctly.


Regards,

Markward

Re: Question about unit testing

Posted by Martin Makundi <ma...@koodaripalvelut.com>.
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 <ma...@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-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>

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


Re: Question about unit testing

Posted by Markward Schubert <ma...@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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Question about unit testing

Posted by Martin Makundi <ma...@koodaripalvelut.com>.
https://cwiki.apache.org/WICKET/type-safe-testing-in-wicket.html

=)

2012/9/5 Markward Schubert <ma...@gmail.com>:
> Hi folks!
>
> I am sure this is a trivial question, but i am simply too dumb to figure
> this out.
> We have a DopDownChoice, which is prefilled by some modelvalue, given to
> the page.
> Until now i was doing some testing about the model-state before and after
> submitting, which then was correct.
> The problem in the browser was, that the rendered DropDownChoice did  not
> show the preselected value correctly. It could not recognize the equality
> between some member of the available choices and the model's choice.
>
> Actually it turned out that it was a problem with the model object's
> equals.
>
> Now my question is:
>
> What is the most elegant way to unit test the correct rendering of the
> preselection of the DropDownChoice or similar components?
> As said, simply dealing with sumbmit and stuff worked correctly.
>
>
> Regards,
>
> Markward

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