Ejemplo n.º 1
0
  @Test
  public void registerFormatter() {
    // #register-formatter
    Formatters.register(
        LocalTime.class,
        new SimpleFormatter<LocalTime>() {

          private Pattern timePattern = Pattern.compile("([012]?\\d)(?:[\\s:\\._\\-]+([0-5]\\d))?");

          @Override
          public LocalTime parse(String input, Locale l) throws ParseException {
            Matcher m = timePattern.matcher(input);
            if (!m.find()) throw new ParseException("No valid Input", 0);
            int hour = Integer.valueOf(m.group(1));
            int min = m.group(2) == null ? 0 : Integer.valueOf(m.group(2));
            return new LocalTime(hour, min);
          }

          @Override
          public String print(LocalTime localTime, Locale l) {
            return localTime.toString("HH:mm");
          }
        });
    // #register-formatter

    Form<WithLocalTime> form = Form.form(WithLocalTime.class);
    WithLocalTime obj = form.bind(ImmutableMap.of("time", "23:45")).get();
    assertThat(obj.time, equalTo(new LocalTime(23, 45)));
    assertThat(form.fill(obj).field("time").value(), equalTo("23:45"));
  }
Ejemplo n.º 2
0
  @Test
  public void listValidation() {
    Form<UserForm> userForm = Form.form(UserForm.class).bind(ImmutableMap.of("email", "e"));
    assertThat(userForm.errors().get("email"), notNullValue());
    assertThat(
        userForm.errors().get("email").get(0).message(),
        equalTo("This e-mail is already registered."));

    // Run it through the template
    assertThat(
        javaguide.forms.html.view.render(userForm).toString(),
        containsString("<p>This e-mail is already registered.</p>"));
  }
Ejemplo n.º 3
0
  @Test
  public void adhocValidation() {
    Form<javaguide.forms.u3.User> userForm = Form.form(javaguide.forms.u3.User.class);
    Form<javaguide.forms.u3.User> bound =
        userForm.bind(ImmutableMap.of("email", "e", "password", "p"));
    assertThat(bound.hasGlobalErrors(), equalTo(true));
    assertThat(bound.globalError().message(), equalTo("Invalid email or password"));

    // Run it through the template
    assertThat(
        javaguide.forms.html.view.render(bound).toString(),
        containsString("Invalid email or password"));
  }
Ejemplo n.º 4
0
 @Test
 public void fillForm() {
   // User needs a constructor. Give it one.
   class User extends javaguide.forms.u1.User {
     User(String email, String password) {
       this.email = email;
       this.password = password;
     }
   }
   Form<javaguide.forms.u1.User> userForm = Form.form(javaguide.forms.u1.User.class);
   // #fill
   userForm = userForm.fill(new User("*****@*****.**", "secret"));
   // #fill
   assertThat(userForm.field("email").value(), equalTo("*****@*****.**"));
   assertThat(userForm.field("password").value(), equalTo("secret"));
 }
Ejemplo n.º 5
0
 @Test
 public void maxLength() {
   StringBuilder body = new StringBuilder();
   for (int i = 0; i < 1100; i++) {
     body.append("1234567890");
   }
   assertThat(
       status(
           callWithStringBody(
               new MockJavaAction() {
                 // #max-length
                 // Accept only 10KB of data.
                 @BodyParser.Of(value = BodyParser.Text.class, maxLength = 10 * 1024)
                 public Result index() {
                   if (request().body().isMaxSizeExceeded()) {
                     return badRequest("Too much data!");
                   } else {
                     return ok("Got body: " + request().body().asText());
                   }
                 }
                 // #max-length
               },
               fakeRequest(),
               body.toString())),
       equalTo(400));
 }
Ejemplo n.º 6
0
 @Test
 public void bindFromRequest() {
   Result result =
       MockJavaAction.call(
           new Controller1(),
           fakeRequest().withFormUrlEncodedBody(ImmutableMap.of("email", "e", "password", "p")));
   assertThat(contentAsString(result), equalTo("e"));
 }
Ejemplo n.º 7
0
  @Test
  public void usingForm() {
    final // sneaky final
    // #create
    Form<User> userForm = Form.form(User.class);
    // #create

    // #bind
    Map<String, String> anyData = new HashMap();
    anyData.put("email", "*****@*****.**");
    anyData.put("password", "secret");

    User user = userForm.bind(anyData).get();
    // #bind

    assertThat(user.email, equalTo("*****@*****.**"));
    assertThat(user.password, equalTo("secret"));
  }
Ejemplo n.º 8
0
 @Test
 public void dynamicForm() {
   Result result =
       MockJavaAction.call(
           new Controller3(),
           fakeRequest()
               .withFormUrlEncodedBody(ImmutableMap.of("firstname", "a", "lastname", "b")));
   assertThat(contentAsString(result), equalTo("Hello a b"));
 }
Ejemplo n.º 9
0
 @Test
 public void accessRequestBody() {
   assertThat(
       contentAsString(
           call(
               new MockJavaAction() {
                 // #request-body
                 public Result index() {
                   RequestBody body = request().body();
                   return ok("Got body: " + body);
                 }
                 // #request-body
               },
               fakeRequest().withTextBody("foo"))),
       containsString("foo"));
 }
Ejemplo n.º 10
0
 @Test
 public void particularBodyParser() {
   assertThat(
       contentAsString(
           call(
               new MockJavaAction() {
                 // #particular-body-parser
                 @BodyParser.Of(BodyParser.Json.class)
                 public Result index() {
                   RequestBody body = request().body();
                   return ok("Got json: " + body.asJson());
                 }
                 // #particular-body-parser
               },
               fakeRequest().withJsonBody(Json.toJson("foo")))),
       containsString("\"foo\""));
 }
 @Test
 public void negotiateContent() {
   assertThat(
       contentAsString(
           call(
               new MockJavaAction() {
                 // #negotiate-content
                 public Result list() {
                   List<Item> items = Item.find.all();
                   if (request().accepts("text/html")) {
                     return ok(views.html.Application.list.render(items));
                   } else {
                     return ok(Json.toJson(items));
                   }
                 }
                 // #negotiate-content
               },
               fakeRequest().header("Accept", "text/html"),
               mat)),
       equalTo("html list of items"));
 }
Ejemplo n.º 12
0
  @Test
  public void defaultParser() {
    assertThat(
        status(
            call(
                new MockJavaAction() {
                  // #default-parser
                  public Result save() {
                    RequestBody body = request().body();
                    String textBody = body.asText();

                    if (textBody != null) {
                      return ok("Got: " + textBody);
                    } else {
                      return badRequest("Expecting text/plain request body");
                    }
                  }
                  // #default-parser
                },
                fakeRequest().withJsonBody(Json.toJson("foo")))),
        equalTo(400));
  }
Ejemplo n.º 13
0
 @Test
 public void constrants() {
   Form<javaguide.forms.u2.User> userForm = Form.form(javaguide.forms.u2.User.class);
   assertThat(userForm.bind(ImmutableMap.of("password", "p")).hasErrors(), equalTo(true));
 }
Ejemplo n.º 14
0
 @Test
 public void handleErrors() {
   Result result = MockJavaAction.call(new Controller2(), fakeRequest());
   assertThat(contentAsString(result), startsWith("Got user"));
 }