Skip to content

Commit e695ac8

Browse files
authored
Merge pull request #1161 from apache/feature/wildcard-packages
Adds additional test cases to match Struts packages
2 parents 7c62bfc + 3be56a4 commit e695ac8

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

core/src/test/java/org/apache/struts2/util/WildcardHelperTest.java

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,60 @@
2424

2525
public class WildcardHelperTest extends XWorkTestCase {
2626

27-
public void testMatch() {
28-
29-
WildcardHelper wild = new WildcardHelper();
30-
HashMap<String, String> matchedPatterns = new HashMap<>();
31-
int[] pattern = wild.compilePattern("wes-rules");
32-
assertEquals(wild.match(matchedPatterns,"wes-rules", pattern), true);
33-
assertEquals(wild.match(matchedPatterns, "rules-wes", pattern), false);
34-
35-
pattern = wild.compilePattern("wes-*");
36-
assertEquals(wild.match(matchedPatterns,"wes-rules", pattern), true);
37-
assertEquals("rules".equals(matchedPatterns.get("1")), true);
38-
assertEquals(wild.match(matchedPatterns, "rules-wes", pattern), false);
39-
40-
pattern = wild.compilePattern("path/**/file");
41-
assertEquals(wild.match(matchedPatterns, "path/to/file", pattern), true);
42-
assertEquals("to".equals(matchedPatterns.get("1")), true);
43-
assertEquals(wild.match(matchedPatterns, "path/to/another/location/of/file", pattern), true);
44-
assertEquals("to/another/location/of".equals(matchedPatterns.get("1")), true);
45-
46-
pattern = wild.compilePattern("path/*/file");
47-
assertEquals(wild.match(matchedPatterns, "path/to/file", pattern), true);
48-
assertEquals("to".equals(matchedPatterns.get("1")), true);
49-
assertEquals(wild.match(matchedPatterns, "path/to/another/location/of/file", pattern), false);
50-
51-
pattern = wild.compilePattern("path/*/another/**/file");
52-
assertEquals(wild.match(matchedPatterns, "path/to/another/location/of/file", pattern), true);
53-
assertEquals("to".equals(matchedPatterns.get("1")), true);
54-
assertEquals("location/of".equals(matchedPatterns.get("2")), true);
55-
}
27+
private WildcardHelper wildcardHelper;
28+
29+
@Override
30+
public void setUp() throws Exception {
31+
super.setUp();
32+
33+
wildcardHelper = new WildcardHelper();
34+
}
35+
36+
public void testMatch() {
37+
HashMap<String, String> matchedPatterns = new HashMap<>();
38+
int[] pattern = wildcardHelper.compilePattern("wes-rules");
39+
assertEquals(wildcardHelper.match(matchedPatterns, "wes-rules", pattern), true);
40+
assertEquals(wildcardHelper.match(matchedPatterns, "rules-wes", pattern), false);
41+
42+
pattern = wildcardHelper.compilePattern("wes-*");
43+
assertEquals(wildcardHelper.match(matchedPatterns, "wes-rules", pattern), true);
44+
assertEquals("rules".equals(matchedPatterns.get("1")), true);
45+
assertEquals(wildcardHelper.match(matchedPatterns, "rules-wes", pattern), false);
46+
47+
pattern = wildcardHelper.compilePattern("path/**/file");
48+
assertEquals(wildcardHelper.match(matchedPatterns, "path/to/file", pattern), true);
49+
assertEquals("to".equals(matchedPatterns.get("1")), true);
50+
assertEquals(wildcardHelper.match(matchedPatterns, "path/to/another/location/of/file", pattern), true);
51+
assertEquals("to/another/location/of".equals(matchedPatterns.get("1")), true);
52+
53+
pattern = wildcardHelper.compilePattern("path/*/file");
54+
assertEquals(wildcardHelper.match(matchedPatterns, "path/to/file", pattern), true);
55+
assertEquals("to".equals(matchedPatterns.get("1")), true);
56+
assertEquals(wildcardHelper.match(matchedPatterns, "path/to/another/location/of/file", pattern), false);
57+
58+
pattern = wildcardHelper.compilePattern("path/*/another/**/file");
59+
assertEquals(wildcardHelper.match(matchedPatterns, "path/to/another/location/of/file", pattern), true);
60+
assertEquals("to".equals(matchedPatterns.get("1")), true);
61+
assertEquals("location/of".equals(matchedPatterns.get("2")), true);
62+
}
63+
64+
public void testMatchStrutsPackages() {
65+
// given
66+
HashMap<String, String> matchedPatterns = new HashMap<>();
67+
int[] pattern = wildcardHelper.compilePattern("org.apache.struts2.*");
68+
69+
// when & then
70+
assertTrue(wildcardHelper.match(matchedPatterns, "org.apache.struts2.XWorkTestCase", pattern));
71+
assertEquals("org.apache.struts2.XWorkTestCase", matchedPatterns.get("0"));
72+
assertEquals("XWorkTestCase", matchedPatterns.get("1"));
73+
74+
assertTrue(wildcardHelper.match(matchedPatterns, "org.apache.struts2.core.SomeClass", pattern));
75+
assertEquals("org.apache.struts2.core.SomeClass", matchedPatterns.get("0"));
76+
assertEquals("core.SomeClass", matchedPatterns.get("1"));
77+
78+
assertTrue(wildcardHelper.match(matchedPatterns, "org.apache.struts2.", pattern));
79+
assertEquals("org.apache.struts2.", matchedPatterns.get("0"));
80+
assertEquals("", matchedPatterns.get("1"));
81+
}
5682

5783
}

0 commit comments

Comments
 (0)