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());
}
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());
}
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());
}
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"));
}
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);
}
}
Aggregations