开发者

JUnit test for specific file restricts giving false output

开发者 https://www.devze.com 2023-01-22 00:09 出处:网络
I had requirement where user should add only specific type of links as part o开发者_如何学运维f the attachments. For example, if user wants to upload

I had requirement where user should add only specific type of links as part o开发者_如何学运维f the attachments. For example, if user wants to upload file of type pdf, the url should end with .pdf similarly for document it should be .doc

To check this scenario I had written JUnit test as below

String url="ar.jpg";
String pm="(.*?)\\.(jpg|jpeg|png|gif)$";
Pattern p = Pattern.compile("pm");
Matcher m = p.matcher(url);     
System.out.println("-----exp  "+m.matches());

This test is always returning false.

Is there something wrong with my Pattern.


You have a misprint - "pm" string is passed to compile() method, but pm variable must be passed:

String pm="(.*?)\\.(jpg|jpeg|png|gif)$";
Pattern p = Pattern.compile(pm); // <- here
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号