View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.util.filter;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertFalse;
8   import static org.junit.Assert.assertTrue;
9   
10  import org.junit.Test;
11  
12  public class RegexStringFilterTest {
13  
14      @Test
15      public void testFilterAbsoluteWithExtension() {
16  	String pattern = "C:/workspace/project/X.java";
17  	verifyFilterFalse("same relative path w/ extension", pattern, "X.java");
18  	verifyFilterFalse("same relative path w/o extension", pattern, "X");
19  	verifyFilterFalse("different relative path w/ extension", pattern, "Y.java");
20  	verifyFilterFalse("different relative path w/o extension", pattern, "Y");
21  	verifyFilterTrue("same absolute path w/ extension", pattern, "C:/workspace/project/X.java");
22  	verifyFilterFalse("same absolute path w/o extension", pattern, "C:/workspace/project/X");
23  	verifyFilterFalse("different absolute path w/ extension", pattern, "C:/workspace/project/Y.java");
24  	verifyFilterFalse("different absolute path w/o extension", pattern, "C:/workspace/project/Y");
25      }
26  
27      @Test
28      public void testFilterAbsoluteWithoutExtension() {
29  	String pattern = "C:/workspace/project/X";
30  	verifyFilterFalse("same relative path w/ extension", pattern, "X.java");
31  	verifyFilterFalse("same relative path w/o extension", pattern, "X");
32  	verifyFilterFalse("different relative path w/ extension", pattern, "Y.java");
33  	verifyFilterFalse("different relative path w/o extension", pattern, "Y");
34  	verifyFilterFalse("same absolute path w/ extension", pattern, "C:/workspace/project/X.java");
35  	verifyFilterTrue("same absolute path w/o extension", pattern, "C:/workspace/project/X");
36  	verifyFilterFalse("different absolute path w/ extension", pattern, "C:/workspace/project/Y.java");
37  	verifyFilterFalse("different absolute path w/o extension", pattern, "C:/workspace/project/Y");
38      }
39  
40      @Test
41      public void testFilterRelativeWithExtension() {
42  	String pattern = ".*X.java";
43  	verifyFilterTrue("same relative path w/ extension", pattern, "X.java");
44  	verifyFilterFalse("same relative path w/o extension", pattern, "X");
45  	verifyFilterFalse("different relative path w/ extension", pattern, "Y.java");
46  	verifyFilterFalse("different relative path w/o extension", pattern, "Y");
47  	verifyFilterTrue("same absolute path w/ extension", pattern, "C:/workspace/project/X.java");
48  	verifyFilterFalse("same absolute path w/o extension", pattern, "C:/workspace/project/X");
49  	verifyFilterFalse("different absolute path w/ extension", pattern, "C:/workspace/project/Y.java");
50  	verifyFilterFalse("different absolute path w/o extension", pattern, "C:/workspace/project/Y");
51      }
52  
53      @Test
54      public void testFilterRelativeWithoutExtension() {
55  	String pattern = ".*X";
56  	verifyFilterFalse("same relative path w/ extension", pattern, "X.java");
57  	verifyFilterTrue("same relative path w/o extension", pattern, "X");
58  	verifyFilterFalse("different relative path w/ extension", pattern, "Y.java");
59  	verifyFilterFalse("different relative path w/o extension", pattern, "Y");
60  	verifyFilterFalse("same absolute path w/ extension", pattern, "C:/workspace/project/X.java");
61  	verifyFilterTrue("same absolute path w/o extension", pattern, "C:/workspace/project/X");
62  	verifyFilterFalse("different absolute path w/ extension", pattern, "C:/workspace/project/Y.java");
63  	verifyFilterFalse("different absolute path w/o extension", pattern, "C:/workspace/project/Y");
64      }
65  
66      @Test
67      public void testEndsWith() {
68  	// These patterns cannot be optimized to use String.endsWith
69  	verifyEndsWith("no literal path", ".*", null);
70  	verifyEndsWith("not ends with", "x", null);
71  	verifyEndsWith("glob on end", ".*XXX.*", null);
72  	verifyEndsWith("special character \\", ".*X\\Y", null);
73  	verifyEndsWith("special character [", ".*X[Y", null);
74  	verifyEndsWith("special character (", ".*X(Y", null);
75  	verifyEndsWith("special character *", ".*X*Y", null);
76  	verifyEndsWith("special character ?", ".*X?Y", null);
77  	verifyEndsWith("special character +", ".*X+Y", null);
78  	verifyEndsWith("special character |", ".*X|Y", null);
79  	verifyEndsWith("special character {", ".*X{Y", null);
80  	verifyEndsWith("special character $", ".*X$Y", null);
81  	verifyEndsWith("too many .", ".*X.Y.java", null);
82  
83  	// These patterns can be optimized to use String.endsWith
84  	verifyEndsWith("unescaped .", ".*X.java", "X.java");
85  	verifyEndsWith("escaped .", ".*X\\.java", "X.java");
86  	verifyEndsWith("no extension", ".*X", "X");
87  	verifyEndsWith("begin anchor, unescaped .", "^.*X.java", "X.java");
88  	verifyEndsWith("begin anchor, escaped .", "^.*X\\.java", "X.java");
89  	verifyEndsWith("begin anchor, no extension", "^.*X", "X");
90  	verifyEndsWith("end anchor, unescaped .", ".*X.java$", "X.java");
91  	verifyEndsWith("end anchor, escaped .", ".*X\\.java$", "X.java");
92  	verifyEndsWith("end anchor, no extension", ".*X$", "X");
93  	verifyEndsWith("begin and end anchors, unescaped .", "^.*X.java$", "X.java");
94  	verifyEndsWith("begin and end anchors, escaped .", "^.*X\\.java$", "X.java");
95  	verifyEndsWith("begin and end anchors, no extension", "^.*X$", "X");
96      }
97  
98      private static void verifyFilterTrue(final String message, final String pattern, final String file) {
99  	assertTrue("filter: " + message, new RegexStringFilter(pattern).filter(file));
100     }
101 
102     private static void verifyFilterFalse(final String message, final String pattern, final String file) {
103 	assertFalse("filter: " + message, new RegexStringFilter(pattern).filter(file));
104     }
105 
106     private static void verifyEndsWith(final String message, final String pattern, final String endsWith) {
107 	assertEquals("endsWith: " + message, endsWith, new RegexStringFilter(pattern).getEndsWith());
108     }
109 
110     public static junit.framework.Test suite() {
111 	return new junit.framework.JUnit4TestAdapter(RegexStringFilterTest.class);
112     }
113 }