/projects/nakedobjects-4.0.0/plugins/headless/junit/src/test/java/org/nakedobjects/plugins/headless/junit/SaveObjectsTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 90 lines · 73 code · 15 blank · 2 comment · 0 complexity · 741503f41f41697ea03329d158356bd8 MD5 · raw file

  1. package org.nakedobjects.plugins.headless.junit;
  2. import static org.hamcrest.CoreMatchers.equalTo;
  3. import static org.hamcrest.CoreMatchers.is;
  4. import static org.junit.Assert.assertThat;
  5. import static org.junit.Assert.fail;
  6. import static org.nakedobjects.metamodel.commons.matchers.NofMatchers.classEqualTo;
  7. import org.junit.Test;
  8. import org.nakedobjects.metamodel.facets.object.validate.ValidateObjectFacetViaValidateMethod;
  9. import org.nakedobjects.plugins.headless.applib.InvalidException;
  10. import org.nakedobjects.plugins.headless.applib.ViewObject;
  11. import org.nakedobjects.plugins.headless.junit.sample.domain.Customer;
  12. public class SaveObjectsTest extends AbstractTest {
  13. @SuppressWarnings("unchecked")
  14. private ViewObject<Customer> asViewObject(final Customer proxiedNewCustomer) {
  15. return (ViewObject<Customer>) proxiedNewCustomer;
  16. }
  17. @Test
  18. public void invokingSaveThroughProxyMakesTransientObjectPersistent() {
  19. final Customer newCustomer = getDomainObjectContainer().newTransientInstance(Customer.class);
  20. assertThat(getDomainObjectContainer().isPersistent(newCustomer),is(false));
  21. final Customer newCustomerViewObject = getHeadlessViewer().view(newCustomer);
  22. newCustomerViewObject.setCustomerNumber(123);
  23. newCustomerViewObject.setLastName("Smith");
  24. newCustomerViewObject.setMandatoryAssociation(countryGbrDO);
  25. newCustomerViewObject.setMandatoryValue("foo");
  26. newCustomerViewObject.setMaxLengthField("abc");
  27. newCustomerViewObject.setRegExCaseInsensitiveField("ABCd");
  28. newCustomerViewObject.setRegExCaseSensitiveField("abcd");
  29. final ViewObject<Customer> proxyNewCustomer = asViewObject(newCustomerViewObject);
  30. proxyNewCustomer.save();
  31. assertThat(getDomainObjectContainer().isPersistent(newCustomer),
  32. is(true));
  33. }
  34. @Test
  35. public void invokingSaveOnThroughProxyOnAlreadyPersistedObjectJustUpdatesIt() {
  36. // just to get into valid state
  37. custJsDO.setCustomerNumber(123);
  38. custJsDO.setLastName("Smith");
  39. custJsDO.setMandatoryAssociation(countryGbrDO);
  40. custJsDO.setMandatoryValue("foo");
  41. custJsDO.setMaxLengthField("abc");
  42. custJsDO.setRegExCaseInsensitiveField("ABCd");
  43. custJsDO.setRegExCaseSensitiveField("abcd");
  44. assertThat(getDomainObjectContainer().isPersistent(custJsDO), is(true));
  45. final ViewObject<Customer> proxyNewCustomer = asViewObject(custJsVO);
  46. proxyNewCustomer.save();
  47. assertThat(getDomainObjectContainer().isPersistent(custJsDO), is(true));
  48. }
  49. @Test
  50. public void whenValidateMethodThenCanVetoSave() {
  51. final Customer newCustomer = getDomainObjectContainer().newTransientInstance(Customer.class);
  52. // just to get into valid state
  53. newCustomer.setCustomerNumber(123);
  54. newCustomer.setLastName("Smith");
  55. newCustomer.setMandatoryAssociation(countryGbrDO);
  56. newCustomer.setMandatoryValue("foo");
  57. newCustomer.setMaxLengthField("abc");
  58. newCustomer.setRegExCaseInsensitiveField("ABCd");
  59. newCustomer.setRegExCaseSensitiveField("abcd");
  60. final Customer newCustomerViewObject = getHeadlessViewer().view(newCustomer);
  61. newCustomer.validate = "No shakes";
  62. final ViewObject<Customer> proxyNewCustomer = asViewObject(newCustomerViewObject);
  63. try {
  64. assertThat(getDomainObjectContainer().isPersistent(newCustomer),
  65. is(false));
  66. proxyNewCustomer.save();
  67. fail("An InvalidImperativelyException should have been thrown");
  68. } catch (final InvalidException ex) {
  69. assertThat(ex.getAdvisorClass(), classEqualTo(ValidateObjectFacetViaValidateMethod.class));
  70. assertThat(getDomainObjectContainer().isPersistent(newCustomer),
  71. is(false)); // not saved
  72. assertThat(ex.getMessage(), equalTo("No shakes"));
  73. }
  74. }
  75. }