zoukankan      html  css  js  c++  java
  • [JavaEE] Data Validation

    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:

    @Entity
    public 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);
        }
  • 相关阅读:
    看所访问网站的IP
    FLEX是什么及与FLASH的关系的介绍
    [设计模式]Head First Design Patterns[转]
    ASP.NET 2.0移动开发入门之使用模拟器
    在一篇文章的基础上总结了一下(接口,组合,继承,组件)
    抽象类与接口的区别 Java描述(转)
    C#:代表(delegate)和事件(event) (转)
    C#分页控件(自己做的)
    一个站点控制另一个站点的文件(添加,删除)用Webservices
    .net remoting程序开发入门篇
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9401910.html
Copyright © 2011-2022 走看看