use of com.manydesigns.elements.servlet.MutableHttpServletRequest in project Portofino by ManyDesigns.
the class CrudActionTest method testBlobs.
public void testBlobs() throws Exception {
MutableHttpServletRequest req = new MutableHttpServletRequest();
ElementsThreadLocals.setMultipart(req);
req.getServletContext().setInitParameter("portofino.api.root", "http://fake");
req.makeMultipart();
Column column = DatabaseLogic.findColumnByName(persistence.getModel(), "jpetstore", "PUBLIC", "PRODUCT", "DESCN");
Annotation ann = new Annotation(column, FileBlob.class.getName());
column.getAnnotations().add(ann);
persistence.initModel();
CrudAction crudAction = new CrudAction() {
public void commitTransaction() {
super.commitTransaction();
session.beginTransaction();
}
@NotNull
@Override
protected ClassAccessor filterAccordingToPermissions(ClassAccessor classAccessor) {
// Let's ignore Shiro
return classAccessor;
}
@Override
protected String getUrlEncoding() {
return PortofinoProperties.URL_ENCODING_DEFAULT;
}
};
CrudConfiguration configuration = new CrudConfiguration();
configuration.setDatabase("jpetstore");
configuration.setQuery("from product");
String metaFilenamePattern = "blob-{0}.properties";
String dataFilenamePattern = "blob-{0}.data";
crudAction.blobManager = new HierarchicalBlobManager(new File(System.getProperty("java.io.tmpdir")), metaFilenamePattern, dataFilenamePattern);
CrudProperty property = new CrudProperty();
property.setName("productid");
property.setEnabled(true);
property.setInsertable(true);
property.setUpdatable(true);
configuration.getProperties().add(property);
property = new CrudProperty();
property.setName("category");
property.setEnabled(true);
property.setInsertable(true);
property.setUpdatable(true);
configuration.getProperties().add(property);
property = new CrudProperty();
property.setName("descn");
property.setEnabled(true);
property.setInsertable(true);
property.setUpdatable(true);
configuration.getProperties().add(property);
property = new CrudProperty();
property.setName("name");
property.setEnabled(true);
property.setInsertable(true);
property.setUpdatable(true);
ann = new Annotation(column, Required.class.getName());
ann.getProperties().add(new Property("value", "true"));
property.getAnnotations().add(ann);
configuration.getProperties().add(property);
configuration.persistence = persistence;
configuration.init();
ActionInstance actionInstance = new ActionInstance(null, null, new ActionDescriptor(), CrudAction.class);
actionInstance.setConfiguration(configuration);
actionInstance.getParameters().add("1");
ActionContext actionContext = new ActionContext();
actionContext.setRequest(req);
actionContext.setActionPath("");
actionContext.setServletContext(req.getServletContext());
req.setParameter("productid", "1");
Map category = (Map) persistence.getSession("jpetstore").createQuery("from category").list().get(0);
req.setParameter("category", (String) category.get("catid"));
crudAction.persistence = persistence;
crudAction.setContext(actionContext);
crudAction.setActionInstance(actionInstance);
crudAction.init();
crudAction.setupForm(Mode.CREATE);
Field descnField = crudAction.getForm().findFieldByPropertyName("descn");
assertNotNull(descnField);
assertTrue(descnField instanceof FileBlobField);
File tmpFile = File.createTempFile("blob", "blob");
DiskFileItem fileItem = new DiskFileItem("descn", "application/octet-stream", false, tmpFile.getName(), 0, tmpFile.getParentFile()) {
@Override
public void delete() {
// Do nothing as we want to reuse this
}
};
OutputStream os = fileItem.getOutputStream();
IOUtils.write("some test data", os, req.getCharacterEncoding());
req.addFileItem("descn", fileItem);
req.setParameter("descn_operation", AbstractBlobField.UPLOAD_MODIFY);
crudAction.httpPostMultipart();
assertFalse(crudAction.form.validate());
AbstractBlobField blobField = (AbstractBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
assertEquals(tmpFile.getName(), blobField.getValue().getFilename());
assertEquals(fileItem.getSize(), blobField.getValue().getSize());
try {
crudAction.getBlobManager().loadMetadata(new Blob(blobField.getValue().getCode()));
fail("The blob was saved despite validation failing");
} catch (Exception e) {
}
crudAction.object = null;
req.setParameter(blobField.getCodeInputName(), blobField.getValue().getCode());
req.setParameter("name", "name");
req.setParameter("productid", "1");
req.setParameter("category", "BIRDS");
crudAction.httpPostMultipart();
assertTrue(crudAction.form.validate());
blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
// This is necessary because the crud might reload the form
crudAction.blobManager.loadMetadata(blobField.getValue());
assertEquals(tmpFile.getName(), blobField.getValue().getFilename());
assertEquals(fileItem.getSize(), blobField.getValue().getSize());
try {
crudAction.blobManager.loadMetadata(new Blob(blobField.getValue().getCode()));
} catch (IOException e) {
e.printStackTrace();
fail("The blob was not saved");
}
crudAction.httpPutMultipart();
assertTrue(crudAction.form.validate());
blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
// This is necessary because the crud might reload the form
crudAction.blobManager.loadMetadata(blobField.getValue());
assertEquals(tmpFile.getName(), blobField.getValue().getFilename());
String oldBlobCode = blobField.getValue().getCode();
assertEquals(fileItem.getSize(), blobField.getValue().getSize());
req.setParameter("descn_operation", FileBlobField.UPLOAD_MODIFY);
req.setFileItem("descn", fileItem);
crudAction.httpPutMultipart();
assertTrue(crudAction.form.validate());
blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
// This is necessary because the crud might reload the form
crudAction.blobManager.loadMetadata(blobField.getValue());
assertEquals(tmpFile.getName(), blobField.getValue().getFilename());
String newBlobCode = blobField.getValue().getCode();
assertNotEquals(oldBlobCode, newBlobCode);
crudAction.blobManager.loadMetadata(new Blob(newBlobCode));
try {
crudAction.blobManager.loadMetadata(new Blob(oldBlobCode));
fail("The blob " + oldBlobCode + " should have been deleted");
} catch (IOException e) {
// Ok
}
Session session = persistence.getSession("jpetstore");
session.flush();
Object id = ((Map) crudAction.object).get("productid");
int qres = session.createSQLQuery("update product set descn = 'illegal' where productid = :id").setParameter("id", id).executeUpdate();
assertEquals(1, qres);
session.flush();
session.getTransaction().commit();
session.clear();
session.beginTransaction();
// Force loading the object from the DB
crudAction.getParameters().add(id.toString());
crudAction.parametersAcquired();
crudAction.setupForm(Mode.VIEW);
crudAction.form.readFromObject(crudAction.object);
BlobUtils.loadBlobs(crudAction.form, crudAction.getBlobManager(), false);
blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
assertNotNull(blobField.getBlobError());
assertNull(blobField.getValue().getFilename());
qres = session.createSQLQuery("update product set descn = :blobCode where productid = :id").setParameter("id", id).setParameter("blobCode", newBlobCode).executeUpdate();
assertEquals(1, qres);
session.flush();
session.getTransaction().commit();
session.clear();
session.beginTransaction();
// Force reload
crudAction.parametersAcquired();
crudAction.httpDelete(Collections.emptyList());
try {
crudAction.blobManager.loadMetadata(new Blob(newBlobCode));
fail("The blob " + newBlobCode + " should have been deleted");
} catch (IOException e) {
// Ok
}
}
use of com.manydesigns.elements.servlet.MutableHttpServletRequest in project Portofino by ManyDesigns.
the class BooleanFieldTest method testRequiredReadFromRequest.
public void testRequiredReadFromRequest() {
booleanField = new BooleanField(myPropertyAccessor, Mode.EDIT);
booleanField.setRequired(true);
assertTrue(booleanField.isRequired());
MutableHttpServletRequest req = new MutableHttpServletRequest();
booleanField.setValue(true);
assertTrue(booleanField.getValue());
req.setParameter("myBoolean", "");
booleanField.readFromRequest(req);
assertFalse(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);
assertFalse(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 DateSearchFieldTest method testRange.
public void testRange() {
setupFields();
MutableHttpServletRequest request = new MutableHttpServletRequest();
String minDate = "1999-01-01";
String maxDate = "1999-02-01";
request.setParameter("date_min", minDate);
request.setParameter("date_max", maxDate);
dateField.readFromRequest(request);
assertTrue(dateField.validate());
date = (Date) dateField.getMinValue();
DateTime dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(minDate);
assertEquals(new DateTime(date.getTime()), dateTime);
assertEquals(minDate, dateField.getMinStringValue());
date = (Date) dateField.getMaxValue();
dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(maxDate);
assertEquals(new DateTime(date.getTime()), dateTime);
assertEquals(maxDate, dateField.getMaxStringValue());
BaseCriteria criteria = new BaseCriteria();
dateField.configureCriteria(criteria);
assertEquals(dateField.getMinValue(), ((BaseCriteria.BetweenCriterion) criteria.get(0)).getMin());
assertEquals(dateField.getMaxValue(), ((BaseCriteria.BetweenCriterion) criteria.get(0)).getMax());
// Invers
request.setParameter("date_min", maxDate);
request.setParameter("date_max", minDate);
dateField.readFromRequest(request);
assertTrue(dateField.validate());
date = (Date) dateField.getMinValue();
dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(minDate);
assertEquals(new DateTime(date.getTime()), dateTime);
assertEquals(maxDate, dateField.getMinStringValue());
date = (Date) dateField.getMaxValue();
dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(maxDate);
assertEquals(new DateTime(date.getTime()), dateTime);
assertEquals(minDate, dateField.getMaxStringValue());
criteria = new BaseCriteria();
dateField.configureCriteria(criteria);
assertEquals(dateField.getMinValue(), ((BaseCriteria.BetweenCriterion) criteria.get(0)).getMin());
assertEquals(dateField.getMaxValue(), ((BaseCriteria.BetweenCriterion) criteria.get(0)).getMax());
}
use of com.manydesigns.elements.servlet.MutableHttpServletRequest in project Portofino by ManyDesigns.
the class DateSearchFieldTest method testDSTSwitch.
@Test(enabled = false)
public void testDSTSwitch() {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome"));
setupFields();
MutableHttpServletRequest request = new MutableHttpServletRequest();
// Start from string
// Compatibility
String strDate = "1973-06-02";
request.setParameter("date_min", strDate);
dateField.readFromRequest(request);
assertTrue(dateField.validate());
date = (Date) dateField.getMinValue();
DateTime dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(strDate);
assertEquals(new DateTime(date.getTime()), dateTime);
assertEquals(strDate, dateField.getMinStringValue());
assertEquals(73, date.getYear());
assertEquals(5, date.getMonth());
assertEquals(2, date.getDate());
assertEquals(0, date.getHours());
assertEquals(0, date.getMinutes());
assertEquals(0, date.getSeconds());
/* TODO DateSearchField works differently from DateField
dateField.setMinValue(date);
assertEquals(date, dateField.getMinValue());
assertEquals(strDate, dateField.getMinStringValue());
*/
// Daylight saving test
strDate = "1973-06-03";
request.setParameter("date_min", strDate);
dateField.readFromRequest(request);
assertTrue(dateField.validate());
date = (Date) dateField.getMinValue();
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());
/* TODO DateSearchField works differently from DateField
dateField.setMinValue(date);
assertEquals(date, dateField.getMinValue());
assertEquals(strDate, dateField.getMinStringValue());*/
// Compatibility
strDate = "1973-06-04";
request.setParameter("date_min", strDate);
dateField.readFromRequest(request);
assertTrue(dateField.validate());
date = (Date) dateField.getMinValue();
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());
/* TODO DateSearchField works differently from DateField
dateField.setMinValue(date);
assertEquals(date, dateField.getMinValue());
assertEquals(strDate, dateField.getMinStringValue());*/
// Start from date
/* TODO DateSearchField works differently from DateField
date = new Date(114, 1, 5);
dateField.setMinValue(date);
assertEquals(date, dateField.getMinValue());
assertEquals("2014-02-05", dateField.getMinStringValue());
date = new Date(73, 5, 3);
dateField.setMinValue(date);
assertEquals(date, dateField.getMinValue());
assertEquals("1973-06-03", dateField.getMinStringValue());*/
}
use of com.manydesigns.elements.servlet.MutableHttpServletRequest in project Portofino by ManyDesigns.
the class PersistenceTest method testDateWithDTSSwitch.
// Disable because it fails on the CI server where the timezone is different
@Test(enabled = false)
public void testDateWithDTSSwitch() {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome"));
// 1973-06-03 DTS switch at midnight in tz Europe/Rome
Date problematicDate = new Date(73, 5, 3);
Map<String, Object> order = new HashMap<>();
order.put("orderid", new BigInteger("1"));
order.put("userid", "x");
order.put("orderdate", problematicDate);
order.put("shipaddr1", "x");
order.put("shipaddr2", "x");
order.put("shipcity", "x");
order.put("shipstate", "x");
order.put("shipzip", "x");
order.put("shipcountry", "x");
order.put("billaddr1", "x");
order.put("billaddr2", "x");
order.put("billcity", "x");
order.put("billstate", "x");
order.put("billzip", "x");
order.put("billcountry", "x");
order.put("courier", "x");
order.put("totalprice", new BigDecimal(1.0));
order.put("billtofirstname", "x");
order.put("billtolastname", "x");
order.put("shiptofirstname", "x");
order.put("shiptolastname", "x");
order.put("creditcard", "x");
order.put("exprdate", "x");
order.put("cardtype", "x");
order.put("locale", "x");
Session session = persistence.getSession("jpetstore");
session.save("orders", order);
FormBuilder fb = new FormBuilder(persistence.getTableAccessor("jpetstore", "orders"));
Form form = fb.configFields("orderdate").build();
DateField dateField = (DateField) form.findFieldByPropertyName("orderdate");
dateField.readFromObject(order);
assertEquals("dd-MM-yyyy", ElementsProperties.getConfiguration().getString("fields.date.format"));
String strDate = "03-06-1973";
assertEquals(strDate, dateField.getStringValue());
MutableHttpServletRequest request = new MutableHttpServletRequest();
request.setParameter("date", strDate);
dateField.readFromRequest(request);
assertTrue(dateField.validate());
dateField.writeToObject(order);
session.update("orders", order);
session.flush();
// Forget about order
session.clear();
order = (Map) session.get("orders", new BigInteger("1"));
// Convert to java.util.Date from java.sql.Date
Date date = new Date(((Date) order.get("orderdate")).getTime());
assertEquals(73, date.getYear());
assertEquals(5, date.getMonth());
assertEquals(3, date.getDate());
assertEquals(1, date.getHours());
assertEquals(0, date.getMinutes());
assertEquals(0, date.getSeconds());
dateField.readFromObject(order);
assertEquals(strDate, dateField.getStringValue());
}
Aggregations