[JavaEE] Data Validation
2024-09-08 11:26:43
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);
}
最新文章
- 提高前端开发效率必备AngularJS (基础)
- Bootstrap~学习笔记索引
- linux系统启动时更改MAC地址
- 【leetcode】Distinct Subsequences(hard)
- 使用MyBatis Generator自动创建代码
- socket编程概述
- Android 线程模型
- 常用oracle查询总结
- BitmapFactory 加载图片到内存
- hive函数总结-字符串函数
- 推荐一本好书给即将走入工作的程序员and程序媴
- KeCode对照表(键盘按键的获取)
- 【剑指offer】删除字符也出现在一个字符串
- spark-RDD源码分析
- Android-SD卡相关操作
- 学习ActiveMQ(一):安装与启动
- H5 文本属性
- deepin使用笔记-解决蓝牙设备开机自动开启的问题
- Python Mock的入门(转)
- 国内淘宝镜像 cnpm转npm
热门文章
- Windows Azure中文博客 Windows Azure入门教学系列 (一): 创建第一个WebRole程序
- Cesium加载影像
- 关于defineProperty
- VMWare虚拟机中Ubuntu 16.04 (linux无桌面)配置静态IP上网
- HTML form without CSRF protection,HTML表单没有CSRF保护
- PKI中常用编码:ASN.1 DER BER Base64
- poi导出word时设置兼容性
- 【笔记JS/HTML/CSS】CSS3实现鼠标滑动显示动画(transition、transform)
- (转)淘淘商城系列——导入商品数据到索引库——Service层
- 安卓app测试之cpu监控