[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);
}
[JavaEE] Data Validation的更多相关文章
- [WPF系列]-Data Validation
项目经常前台界面涉及到用户输入时,我们常常会用到数据有效性的验证.在网页中我们之前用js来校验Form中的数据有效性.在WPF中我们如何实现这种验证机制了?答案:INotifyDataErrorInf ...
- Chapter Data Modification & Chapter Data Validation
Chapter Data Modification XF的数据提交,支持单行.集合和多层次的master-details结构的数据. Create 当提交如下数据 <Job> <Id ...
- .NET MVC 学习笔记(五)— Data Validation
.NET MVC 学习笔记(五)—— Data Validation 在实际应用中,我们需要对数据进行增查改删业务,在添加和修改过程中,无论你编写什么样的网页程序,都需要对用户的数据进行验证,以确数据 ...
- Validating HTTP data with Play
Validations ensure that the data has certain values or meets specific requirements. You can use vali ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型
Creating a complex data model 创建复杂数据模型 8 of 9 people found this helpful The Contoso University sampl ...
- 使用Data Annotations进行手动数据验证
Data Annotations是在Asp.Net中用于表单验证的 它通过Attribute直接标记字段的有效性,简单且直观.在非Asp.Net程序中(如控制台程序),我们也可以使用Data Anno ...
- c# Use Properties Instead of Accessible Data Members
advantage of properties: 1 properties can be used in data binding, public data member can not. 2 dat ...
- WPF Input Validation Using MVVM
Data validation is a key part in WPF.Validation is used to alert the user that the data he entered i ...
- 运用模型绑定和web窗体显示和检索数据(Retrieving and displaying data with model binding and web forms)
原文 http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data ...
随机推荐
- JDBC和数据库连接池
JDBC是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成. ● JDBC ● C3P0 ● DRUID 一.JDBC ...
- MySQL客户端导入数据库脚本,字段值出现乱码解决方法
解决方法1:在MySql安装目录下找到my.ini,将[mysql]下的default-character-set=latin1改为default-character-set=utf8,保存,然后重启 ...
- v形 加强版
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- PHP开发心得一
1,php获得服务器时间 $time= date('Y-m-d H:i'); echo $time; 一般写法如上,但发现打印出来的时间小时数总数不对,和机器的时间差几个小时.查资料发现,要设定时区. ...
- SpringBoot项目的mybatis逆向工程
<dependencies> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot& ...
- 使用Unittest做单元测试,addTest()单个case的时候却执行全部的case
参考: http://tieba.baidu.com/p/6008699660 首先造成这个结果的原因是pycharm配置问题 问题验证: 测试代码: import unittest class Te ...
- 梦想MxWeb3D协同设计平台 2018.10.12更新
SDK开发包下载地址: http://www.mxdraw.com/ndetail_10107.html 1. 全新的在线的三维协同设计平台,高效异步方式,基于JavaScript和WebGL技术,前 ...
- 02C语言基本语法
C语言基本语法 注释 //行注释 /* 块注释*/ 标识符 标识符是用来标识变量.函数.任何其他用户自定义项目的名称:一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母.下划 ...
- P1541 乌龟棋 题解(洛谷,动态规划递推)
题目:P1541 乌龟棋 感谢大神的题解(他的写的特别好) 写一下我对他的代码的理解吧(哎,蒟蒻就这能这样...) 代码: #include<bits/stdc++.h> #define ...