public void testBeanValidation() throws Exception { Artist a = new Artist(); a.setName("TOO OLD ARTIST"); a.setAge(120); XPersistence.getManager().persist(a); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof javax.validation.ConstraintViolationException) { javax.validation.ConstraintViolationException vex = (javax.validation.ConstraintViolationException) ex.getCause(); assertEquals("1 invalid value is expected", 1, vex.getConstraintViolations().size()); ConstraintViolation violation = vex.getConstraintViolations().iterator().next(); assertEquals("Bean", "Artist", violation.getRootBeanClass().getSimpleName()); String expectedMessage = "es".equals(Locale.getDefault().getLanguage()) ? "tiene que ser menor o igual que 90" : "must be less than or equal to 90"; assertEquals("Message text", expectedMessage, violation.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
public void testCreateReferencesFromDescriptionsList() throws Exception { execute("CRUD.new"); // Verifying initial state String[][] familyValues = { {"", ""}, {"1", "SOFTWARE"}, {"2", "HARDWARE"}, {"3", "SERVICIOS"} }; assertValidValues("family.number", familyValues); execute("Reference.createNew", "model=Family2,keyProperty=xava.Product2.family.number"); assertAction("NewCreation.saveNew"); assertAction("NewCreation.cancel"); execute("NewCreation.cancel"); execute("Reference.createNew", "model=Family2,keyProperty=xava.Product2.family.number"); assertAction("NewCreation.saveNew"); assertAction("NewCreation.cancel"); execute("NewCreation.saveNew"); assertError("Value for Number in Family is required"); assertError("Value for Description in Family is required"); setValue("Family2", "number", "1"); setValue("Family2", "description", "JUNIT TEST"); execute("NewCreation.saveNew"); assertError("Impossible to create: an object with that key already exists"); setValue("Family2", "number", "66"); execute("NewCreation.saveNew"); assertNoErrors(); // Test just added String[][] familyValuesUpdated = { {"", ""}, {"1", "SOFTWARE"}, {"2", "HARDWARE"}, {"3", "SERVICIOS"}, {"66", "JUNIT TEST"} }; assertValidValues("family.number", familyValuesUpdated); assertValue("family.number", "66"); // The just created family is automatically selected // Delete it Family2 f = XPersistence.getManager().find(Family2.class, 66); XPersistence.getManager().remove(f); }
private void createProduct(int number, String description, int zone) throws Exception { Product2 p = new Product2(); p.setNumber(number); p.setDescription(description); Family2 f = new Family2(); f.setNumber(1); p.setFamily(f); Subfamily2 sf = new Subfamily2(); sf.setNumber(1); p.setSubfamily(sf); Warehouse w = new Warehouse(); w.setNumber(1); w.setZoneNumber(zone); p.setWarehouse(w); p.setUnitPrice(new BigDecimal("1.00")); XPersistence.getManager().persist(p); XPersistence.commit(); }
public void testRequiredAsBeanValidationAnnotation() throws Exception { DrivingLicence dl = new DrivingLicence(); dl.setType("X"); dl.setLevel(1); dl.setDescription(""); // This is annotated with @Required XPersistence.getManager().persist(dl); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof ConstraintViolationException) { ConstraintViolationException cex = (ConstraintViolationException) ex.getCause(); assertEquals("1 constraint violation expected", 1, cex.getConstraintViolations().size()); ConstraintViolation v = cex.getConstraintViolations().iterator().next(); assertEquals("Property", "description", v.getPropertyPath().toString()); assertEquals("Message text", "{required}", v.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
private void setDefaultSchema(HttpServletRequest request) { String hibernateDefaultSchemaTab = (String) request.getSession().getAttribute("xava_hibernateDefaultSchemaTab"); if (hibernateDefaultSchemaTab != null) { request.getSession().removeAttribute("xava_hibernateDefaultSchemaTab"); XHibernate.setDefaultSchema(hibernateDefaultSchemaTab); } String jpaDefaultSchemaTab = (String) request.getSession().getAttribute("xava_jpaDefaultSchemaTab"); if (jpaDefaultSchemaTab != null) { request.getSession().removeAttribute("xava_jpaDefaultSchemaTab"); XPersistence.setDefaultSchema(jpaDefaultSchemaTab); } }
public void testPropertyValidatorsAsBeanValidationAnnotation() throws Exception { Product p = new Product(); p.setNumber(66); p.setDescription("MOTO"); p.setFamilyNumber(1); p.setSubfamilyNumber(1); p.setWarehouseKey(new Warehouse()); p.setUnitPrice(new BigDecimal("900")); XPersistence.getManager().persist(p); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof ConstraintViolationException) { ConstraintViolationException cex = (ConstraintViolationException) ex.getCause(); assertEquals("1 constraint violation expected", 1, cex.getConstraintViolations().size()); ConstraintViolation v = cex.getConstraintViolations().iterator().next(); assertEquals("Property", "description", v.getPropertyPath().toString()); assertEquals("Message text", "", v.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
public void testEntityValidatorsAsHibernateAnnotation() throws Exception { Product p = new Product(); p.setNumber(66); p.setDescription("BUENO, BONITO, BARATO"); // It's cheap ('BARATO') thus... p.setFamilyNumber(1); p.setSubfamilyNumber(1); p.setWarehouseKey(new Warehouse()); p.setUnitPrice(new BigDecimal("900")); // ... it cannot cost 900 (max 100) XPersistence.getManager().persist(p); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof ConstraintViolationException) { ConstraintViolationException cex = (ConstraintViolationException) ex.getCause(); assertEquals("1 constraint violation expected", 1, cex.getConstraintViolations().size()); ConstraintViolation v = cex.getConstraintViolations().iterator().next(); assertEquals("Bean", "Product", v.getRootBean().getClass().getSimpleName()); assertEquals("Message text", "", v.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
private void deleteProduct(long number) throws Exception { Product2 k = XPersistence.getManager().find(Product2.class, number); XPersistence.getManager().remove(k); XPersistence.commit(); }
public void testImagesGallery() throws Exception { // We remove oid from product 1 in order to test that images gallery works well in the first // attemp. Product2.findByNumber(1).setPhotos(""); XPersistence.commit(); // Verifying product 1 has no images assertTrue("At least 2 products are required to run this test", getListRowCount() >= 2); execute("Mode.detailAndFirst"); assertValue("number", "1"); execute("Gallery.edit", "galleryProperty=photos"); assertNoErrors(); assertMessage("No images"); assertNoAction("Gallery.maximizeImage"); assertNoAction("Gallery.minimizeImage"); assertNoAction("Gallery.removeImage"); assertEquals( "Images count does not match", 0, getForm().getInputsByName("xava.GALLERY.images").size()); // Canceling the adding of and image execute("Gallery.addImage"); assertNoErrors(); String imageUrl = System.getProperty("user.dir") + "/test-images/foto_javi.jpg"; setFileValue("newImage", imageUrl); execute("LoadImageIntoGallery.cancel"); assertNoErrors(); assertNoAction("Gallery.maximizeImage"); assertNoAction("Gallery.minimizeImage"); assertNoAction("Gallery.removeImage"); assertEquals( "Images count does not match", 0, getForm().getInputsByName("xava.GALLERY.images").size()); // Adding one image execute("Gallery.addImage"); assertNoErrors(); imageUrl = System.getProperty("user.dir") + "/test-images/foto_javi.jpg"; setFileValue("newImage", imageUrl); execute("LoadImageIntoGallery.loadImage"); assertNoErrors(); assertMessage("Image added to the gallery"); assertAction("Gallery.maximizeImage"); assertNoAction("Gallery.minimizeImage"); assertAction("Gallery.removeImage"); assertEquals( "Images count does not match", 1, getForm().getInputsByName("xava.GALLERY.images").size()); // Returning to the main entity execute("Gallery.return"); // execute("CRUD.save"); It's not needed explicit saving of the main entity assertNoErrors(); // Verifying that product 2 has no images execute("Navigation.next"); assertValue("number", "2"); execute("Gallery.edit", "galleryProperty=photos"); assertNoErrors(); assertMessage("No images"); assertNoAction("Gallery.maximizeImage"); assertNoAction("Gallery.minimizeImage"); assertNoAction("Gallery.removeImage"); assertEquals( "Images count does not match", 0, getForm().getInputsByName("xava.GALLERY.images").size()); execute("Gallery.return"); // Verifying that product 1 has the added image execute("CRUD.new"); setValue("number", "1"); execute("CRUD.refresh"); assertNoErrors(); execute("Gallery.edit", "galleryProperty=photos"); assertNoErrors(); assertNoMessages(); assertAction("Gallery.maximizeImage"); assertNoAction("Gallery.minimizeImage"); assertAction("Gallery.removeImage"); assertEquals( "Images count does not match", 1, getForm().getInputsByName("xava.GALLERY.images").size()); String imageOid = getForm().getInputByName("xava.GALLERY.images").getValueAttribute(); // Maximizing the image execute("Gallery.maximizeImage", "oid=" + imageOid); assertNoErrors(); assertNoAction("Gallery.maximizeImage"); assertAction("Gallery.minimizeImage"); assertNoAction("Gallery.removeImage"); // Minimizing the image execute("Gallery.minimizeImage"); assertNoErrors(); assertAction("Gallery.maximizeImage"); assertNoAction("Gallery.minimizeImage"); assertAction("Gallery.removeImage"); assertEquals( "Images count does not match", 1, getForm().getInputsByName("xava.GALLERY.images").size()); // Verifying read-only execute("Gallery.return"); execute("EditableOnOff.setOff"); execute("Gallery.edit", "galleryProperty=photos"); assertNoErrors(); assertNoMessages(); assertNoAction("Gallery.addImage"); assertAction("Gallery.maximizeImage"); assertNoAction("Gallery.minimizeImage"); assertNoAction("Gallery.removeImage"); assertEquals( "Images count does not match", 1, getForm().getInputsByName("xava.GALLERY.images").size()); execute("Return.return"); execute("EditableOnOff.setOn"); // Removing the image execute("Gallery.edit", "galleryProperty=photos"); assertEquals( "Images count does not match", 1, getForm().getInputsByName("xava.GALLERY.images").size()); execute("Gallery.removeImage", "oid=" + imageOid); assertNoErrors(); assertNoAction("Gallery.maximizeImage"); assertNoAction("Gallery.minimizeImage"); assertNoAction("Gallery.removeImage"); assertEquals( "Images count does not match", 0, getForm().getInputsByName("xava.GALLERY.images").size()); // Verifying that product 1 has no images execute("Gallery.return"); execute("CRUD.new"); setValue("number", "1"); execute("CRUD.refresh"); assertNoErrors(); execute("Gallery.edit", "galleryProperty=photos"); assertNoErrors(); assertMessage("No images"); assertEquals( "Images count does not match", 0, getForm().getInputsByName("xava.GALLERY.images").size()); }
public static Product2 findByNumber(long number) throws NoResultException { Query query = XPersistence.getManager().createQuery("from Product2 as o where o.number = :number"); query.setParameter("number", new Long(number)); return (Product2) query.getSingleResult(); }
protected void tearDown() throws Exception { XPersistence.commit(); }
static { XPersistence.setPersistenceUnit("junit"); DataSourceConnectionProvider.setUseHibernateConnection(true); }