Search in sources :

Example 6 with MutableHttpServletRequest

use of com.manydesigns.elements.servlet.MutableHttpServletRequest in project Portofino by ManyDesigns.

the class BooleanFieldTest method testNotRequiredReadFromRequest.

public void testNotRequiredReadFromRequest() {
    booleanField = new BooleanField(myPropertyAccessor, Mode.EDIT);
    booleanField.setRequired(false);
    assertFalse(booleanField.isRequired());
    MutableHttpServletRequest req = new MutableHttpServletRequest();
    booleanField.setValue(true);
    assertTrue(booleanField.getValue());
    req.setParameter("myBoolean", "");
    booleanField.readFromRequest(req);
    assertNull(booleanField.getValue());
    req.setParameter("myBoolean", "true");
    booleanField.readFromRequest(req);
    assertTrue(booleanField.getValue());
    req.setParameter("myBoolean", "false");
    booleanField.readFromRequest(req);
    assertFalse(booleanField.getValue());
    req.setParameter("myBoolean", (String) null);
    req.setParameter(BooleanField.CHECK_PREFIX + "myBoolean", "true");
    booleanField.readFromRequest(req);
    assertNull(booleanField.getValue());
    booleanField.setValue(true);
    req.setParameter("myBoolean", (String) null);
    req.setParameter(BooleanField.CHECK_PREFIX + "myBoolean", (String) null);
    booleanField.readFromRequest(req);
    assertTrue(booleanField.getValue());
}
Also used : MutableHttpServletRequest(com.manydesigns.elements.servlet.MutableHttpServletRequest)

Example 7 with MutableHttpServletRequest

use of com.manydesigns.elements.servlet.MutableHttpServletRequest in project Portofino by ManyDesigns.

the class DateFieldTest method testDSTSwitch.

@Test(enabled = false)
public void testDSTSwitch() {
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome"));
    setupFields(Mode.EDIT);
    MutableHttpServletRequest request = new MutableHttpServletRequest();
    // Start from string
    // Compatibility
    String strDate = "1973-06-02";
    request.setParameter("date", strDate);
    dateField.readFromRequest(request);
    assertTrue(dateField.validate());
    dateField.writeToObject(this);
    DateTime dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(strDate);
    assertEquals(new DateTime(date.getTime()), dateTime);
    assertEquals(strDate, dateField.getStringValue());
    assertEquals(73, date.getYear());
    assertEquals(5, date.getMonth());
    assertEquals(2, date.getDate());
    assertEquals(0, date.getHours());
    assertEquals(0, date.getMinutes());
    assertEquals(0, date.getSeconds());
    System.out.println(date);
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals(strDate, dateField.getStringValue());
    // Daylight saving test
    strDate = "1973-06-03";
    request.setParameter("date", strDate);
    dateField.readFromRequest(request);
    assertTrue(dateField.validate());
    dateField.writeToObject(this);
    DateTime test = new DateTime(date.getTime());
    assertEquals(1973, test.getYear());
    assertEquals(6, test.getMonthOfYear());
    assertEquals(3, test.getDayOfMonth());
    assertEquals(1, test.getHourOfDay());
    assertEquals(0, test.getMinuteOfHour());
    assertEquals(0, test.getSecondOfMinute());
    assertEquals(73, date.getYear());
    assertEquals(5, date.getMonth());
    assertEquals(3, date.getDate());
    assertEquals(1, date.getHours());
    assertEquals(0, date.getMinutes());
    assertEquals(0, date.getSeconds());
    System.out.println(date);
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals(strDate, dateField.getStringValue());
    // Compatibility
    strDate = "1973-06-04";
    request.setParameter("date", strDate);
    dateField.readFromRequest(request);
    assertTrue(dateField.validate());
    dateField.writeToObject(this);
    dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(strDate);
    assertEquals(new DateTime(date.getTime()), dateTime);
    assertEquals(73, date.getYear());
    assertEquals(5, date.getMonth());
    assertEquals(4, date.getDate());
    assertEquals(0, date.getHours());
    assertEquals(0, date.getMinutes());
    assertEquals(0, date.getSeconds());
    System.out.println(date);
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals(strDate, dateField.getStringValue());
    // Start from date
    date = new Date(114, 1, 5);
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals("2014-02-05", dateField.getStringValue());
    date = new Date(73, 5, 3);
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals("1973-06-03", dateField.getStringValue());
}
Also used : MutableHttpServletRequest(com.manydesigns.elements.servlet.MutableHttpServletRequest) DateTime(org.joda.time.DateTime) Date(java.util.Date) AbstractElementsTest(com.manydesigns.elements.AbstractElementsTest) Test(org.testng.annotations.Test)

Example 8 with MutableHttpServletRequest

use of com.manydesigns.elements.servlet.MutableHttpServletRequest in project Portofino by ManyDesigns.

the class JodaTimeFieldTest method testDSTSwitch.

@Test(enabled = false)
public void testDSTSwitch() throws NoSuchFieldException {
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome"));
    setupFields(Mode.EDIT);
    MutableHttpServletRequest request = new MutableHttpServletRequest();
    // Start from string
    // Compatibility
    String strDate = "1973-06-02";
    request.setParameter("date", strDate);
    dateField.readFromRequest(request);
    assertTrue(dateField.validate());
    dateField.writeToObject(this);
    DateTime dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(strDate);
    assertEquals(date, dateTime);
    assertEquals(strDate, dateField.getStringValue());
    assertEquals(1973, date.getYear());
    assertEquals(6, date.getMonthOfYear());
    assertEquals(2, date.getDayOfMonth());
    assertEquals(0, date.getHourOfDay());
    assertEquals(0, date.getMinuteOfHour());
    assertEquals(0, date.getSecondOfMinute());
    System.out.println(date);
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals(strDate, dateField.getStringValue());
    // Daylight saving test
    strDate = "1973-06-03";
    request.setParameter("date", strDate);
    dateField.readFromRequest(request);
    assertTrue(dateField.validate());
    dateField.writeToObject(this);
    assertEquals(1973, date.getYear());
    assertEquals(6, date.getMonthOfYear());
    assertEquals(3, date.getDayOfMonth());
    assertEquals(1, date.getHourOfDay());
    assertEquals(0, date.getMinuteOfHour());
    assertEquals(0, date.getSecondOfMinute());
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals(strDate, dateField.getStringValue());
    // Compatibility
    strDate = "1973-06-04";
    request.setParameter("date", strDate);
    dateField.readFromRequest(request);
    assertTrue(dateField.validate());
    dateField.writeToObject(this);
    dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(strDate);
    assertEquals(date, dateTime);
    assertEquals(1973, date.getYear());
    assertEquals(6, date.getMonthOfYear());
    assertEquals(4, date.getDayOfMonth());
    assertEquals(0, date.getHourOfDay());
    assertEquals(0, date.getMinuteOfHour());
    assertEquals(0, date.getSecondOfMinute());
    System.out.println(date);
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals(strDate, dateField.getStringValue());
    // Start from date
    date = new DateTime().withYear(2014).withMonthOfYear(2).withDayOfMonth(5);
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals("2014-02-05", dateField.getStringValue());
    date = new DateTime().withYear(1973).withMonthOfYear(6).withDayOfMonth(3);
    dateField.readFromObject(this);
    assertEquals(date, dateField.getValue());
    assertEquals("1973-06-03", dateField.getStringValue());
}
Also used : MutableHttpServletRequest(com.manydesigns.elements.servlet.MutableHttpServletRequest) DateTime(org.joda.time.DateTime) AbstractElementsTest(com.manydesigns.elements.AbstractElementsTest) Test(org.testng.annotations.Test)

Example 9 with MutableHttpServletRequest

use of com.manydesigns.elements.servlet.MutableHttpServletRequest in project Portofino by ManyDesigns.

the class ElementsTestSupport method setUpRequest.

public void setUpRequest() {
    req = new MutableHttpServletRequest(servletContext);
    req.setContextPath("");
    req.setRequestURI("");
    // Uniform locales
    req.locales.clear();
    req.locales.add(Locale.forLanguageTag("en"));
    req.locales.add(Locale.forLanguageTag("it"));
}
Also used : MutableHttpServletRequest(com.manydesigns.elements.servlet.MutableHttpServletRequest)

Example 10 with MutableHttpServletRequest

use of com.manydesigns.elements.servlet.MutableHttpServletRequest in project Portofino by ManyDesigns.

the class AbstractCrudAction method readSearchFormFromRequest.

/**
 * Populates the search form from request parameters.
 * <ul>
 *     <li>If <code>searchString</code> is blank, then the form is read from the request
 *     (by {@link SearchForm#readFromRequest(HttpServletRequest)}) and <code>searchString</code>
 *     is generated accordingly.</li>
 *     <li>Else, <code>searchString</code> is interpreted as a query string and the form is populated from it.</li>
 * </ul>
 */
protected void readSearchFormFromRequest() {
    if (StringUtils.isBlank(searchString)) {
        searchForm.readFromRequest(context.getRequest());
        searchString = searchForm.toSearchString(getUrlEncoding());
        if (searchString.length() == 0) {
            searchString = null;
        }
    } else {
        MutableHttpServletRequest dummyRequest = new MutableHttpServletRequest();
        String[] parts = searchString.split(",");
        Pattern pattern = Pattern.compile("(.*)=(.*)");
        for (String part : parts) {
            Matcher matcher = pattern.matcher(part);
            if (matcher.matches()) {
                String key = matcher.group(1);
                String value = matcher.group(2);
                logger.debug("Matched part: {}={}", key, value);
                try {
                    dummyRequest.addParameter(URLDecoder.decode(key, getUrlEncoding()), URLDecoder.decode(value, getUrlEncoding()));
                } catch (UnsupportedEncodingException e) {
                    logger.error("Unsupported encoding when parsing search string", e);
                }
            } else {
                logger.debug("Could not match part: {}", part);
            }
        }
        searchForm.readFromRequest(dummyRequest);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MutableHttpServletRequest(com.manydesigns.elements.servlet.MutableHttpServletRequest)

Aggregations

MutableHttpServletRequest (com.manydesigns.elements.servlet.MutableHttpServletRequest)10 DateTime (org.joda.time.DateTime)4 AbstractElementsTest (com.manydesigns.elements.AbstractElementsTest)3 Test (org.testng.annotations.Test)3 FileObject (org.apache.commons.vfs2.FileObject)2 Session (org.hibernate.Session)2 FileBlob (com.manydesigns.elements.annotations.FileBlob)1 Blob (com.manydesigns.elements.blobs.Blob)1 HierarchicalBlobManager (com.manydesigns.elements.blobs.HierarchicalBlobManager)1 AbstractBlobField (com.manydesigns.elements.fields.AbstractBlobField)1 DateField (com.manydesigns.elements.fields.DateField)1 Field (com.manydesigns.elements.fields.Field)1 FileBlobField (com.manydesigns.elements.fields.FileBlobField)1 BaseCriteria (com.manydesigns.elements.fields.search.BaseCriteria)1 Form (com.manydesigns.elements.forms.Form)1 FormBuilder (com.manydesigns.elements.forms.FormBuilder)1 ClassAccessor (com.manydesigns.elements.reflection.ClassAccessor)1 ActionDescriptor (com.manydesigns.portofino.actions.ActionDescriptor)1 Annotation (com.manydesigns.portofino.model.Annotation)1 Property (com.manydesigns.portofino.model.Property)1