[JavaEE] Data Validation

來源:互聯網
上載者:User

標籤:name   within   class   iba   add   temp   rate   tar   archive   

When we create Entity and Respority, we also need to do validations to protect our data.

 

In Java, validations are built-in, using decorators. For Typescript, found a useful libaray to do the similar validation as well. Checkout class-validator

 

Entity:

@Entitypublic class Book {    // ======================================    // =             Attributes             =    // ======================================    @Id    @GeneratedValue    private Long id;    @Column(length = 200)    @NotNull    @Size(min = 1, max = 200)    private String title;    @Column(length = 10000)    @Size(min = 1, max = 10000)    private String description;    @Column(name = "unit_cost")    @Min(1)    private Float unitCost;    @Column(length = 50)    @NotNull    @Size(min = 1, max = 50)    private String isbn;    @Column(name = "publication_date")    @Temporal(TemporalType.DATE)    @Past    private Date publicationDate;    ....    }

Testing:

We want to test, if we give title as null, it should throw exception.

@RunWith(Arquillian.class)public class BookRepositoryTest {    @Inject    private BookRepository bookRepository;    // We want the test throw exception    @Test(expected = Exception.class)    public void createInvalidBook() {        Book book = new Book("isbn", null, 12F, 123, Language.ENGLISH, new Date(), "imageURL", "description");        bookRepository.create(book);    }    @Deployment    public static JavaArchive createDeployment() {        return ShrinkWrap.create(JavaArchive.class)                .addClass(BookRepository.class)                .addClass(Book.class)                .addClass(Language.class)                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")                .addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml");    }    @org.junit.Test    public void create() {    }}

 

 

Respority:

@Transactional(SUPPORTS)public class BookRepository {    // ======================================    // =          Injection Points          =    // ======================================    @PersistenceContext(unitName = "bookStorePU")    private EntityManager em;    // ======================================    // =          Business methods          =    // ======================================    public Book find(@NotNull Long id) {        return em.find(Book.class, id);    }    // For creating and deleting methods, we want to use REQUIRED    @Transactional(REQUIRED)    public Book create(@NotNull Book book) {        em.persist(book);        return book;    }}

Testing:

    @Test(expected = Exception.class)    public void findWithInvalidId() {        bookRepository.find(null);    }

 

[JavaEE] Data Validation

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.