PageRenderTime 59ms CodeModel.GetById 35ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks/src/test/java/com/google/sitebricks/routing/PathMatcherTest.java

http://github.com/dhanji/sitebricks
Java | 149 lines | 128 code | 18 blank | 3 comment | 6 complexity | a936746ec7b98c8360de607f572b5461 MD5 | raw file
  1package com.google.sitebricks.routing;
  2
  3import org.testng.annotations.DataProvider;
  4import org.testng.annotations.Test;
  5
  6import java.util.HashMap;
  7import java.util.Map;
  8
  9/**
 10 * @author Dhanji R. Prasanna (dhanji@gmail.com)
 11 */
 12public class PathMatcherTest {
 13    private static final String EXACT_PATHS = "exactPaths";
 14    private static final String SINGLE_VAR_PATHS = "getSingleVarPaths";
 15    private static final String ANTI_VAR_PATHS = "antiVarPaths";
 16    private static final String VARPATHS_MATCHES = "varpathsMatches";
 17    private static final String VARPATHS_ANTIMATCHES = "varpaths_antimatches";
 18
 19    @DataProvider(name = EXACT_PATHS)
 20    public Object[][] getExactPaths() {
 21        return new Object[][] {
 22            { "/wiki", "/wiki",  },
 23            { "/wiki/pensylvania","/wiki/pensylvania", },
 24            { "/12", "/12",  },
 25            { "/", "/",  },
 26        };
 27    }
 28
 29    @Test(dataProvider = EXACT_PATHS)
 30    public final void matchExactUriPath(final String path, final String incoming) {
 31        assert new PathMatcherChain.SimplePathMatcher(path)
 32                .matches(incoming);
 33    }
 34
 35    @SuppressWarnings({"UnusedDeclaration"})
 36    @Test(dataProvider = EXACT_PATHS)
 37    public final void matchGreedy(final String path, final String incoming) {
 38        assert new PathMatcherChain.GreedyPathMatcher("ogog")
 39                .matches(incoming);
 40    }
 41
 42
 43    @DataProvider(name = SINGLE_VAR_PATHS)
 44    public Object[][] getVarPaths() {
 45        return new Object[][] {
 46            { "/wiki/:title", "/wiki/hello",  },
 47            { "/wiki/:title", "/wiki/ashello",  },
 48            { "/wiki/:title", "/wiki/hoolig An+*",  },
 49            { "/wiki/:title/page/:id", "/wiki/hello/page/12",  },
 50            { "/wiki/:title/page/:id", "/wiki/couwdury/page/12424",  },
 51            { "/wiki/:title/page/:id", "/wiki/sokdoasd/page/aoskpaokda",  },
 52            { "/wiki", "/wiki/",  },
 53            { "/wiki/:title", "/wiki/hello/",  },
 54        };
 55    }
 56
 57    @Test(dataProvider = SINGLE_VAR_PATHS)
 58    public final void matchPathTemplate(final String path, final String incoming) {
 59        assert new PathMatcherChain(path)
 60                .matches(incoming);
 61    }
 62
 63
 64    @DataProvider(name = VARPATHS_MATCHES)
 65    public Object[][] getVarPathsAndMatches() {
 66        return new Object[][] {
 67            { "/wiki/:title", "/wiki/hello", new HashMap() {{
 68                put("title", "hello");
 69            }}, },
 70            { "/wiki/:title/:page/:id", "/wiki/hello/page/12", new HashMap() {{
 71                put("title", "hello");
 72                put("page", "page");
 73                put("id", "12");
 74            }}, },
 75            { "/wiki/:title/page/:id", "/wiki/sokdoasd/page/aoskpaokda", new HashMap() {{
 76                put("title", "sokdoasd");
 77                put("id", "aoskpaokda");
 78            }}, },
 79        };
 80    }
 81
 82    @Test(dataProvider = VARPATHS_MATCHES)
 83    public final void findMatchVariables(final String path, final String incoming, Map<String, String> map) {
 84        final Map<String, String> stringMap = new PathMatcherChain(path)
 85                .findMatches(incoming);
 86
 87        assert null != stringMap;
 88        assert stringMap.size() == map.size();
 89        for (Map.Entry<String, String> entry : stringMap.entrySet()) {
 90            assert map.containsKey(entry.getKey());
 91            assert map.get(entry.getKey()).equals(entry.getValue());
 92        }
 93    }
 94
 95    @DataProvider(name = VARPATHS_ANTIMATCHES)
 96    public Object[][] getVarPathsAntiMatches() {
 97        return new Object[][] {
 98            { "/wiki/:title", "/wiki/hello", new HashMap() {{
 99                put("title", "hellol");
100            }}, },
101            { "/wiki/:title/:page/:id", "/wiki/hello/page/12", new HashMap() {{
102                put("title", "hello");
103                put("id", "12");
104            }}, },
105            { "/wiki/:title/page/:id", "/wiki/sokdoasd/page/aoskpaokda", new HashMap() {{
106                put("title", "sokdoasd");
107                put("id", "aoskpaokda");
108                put("pid", "aoskpaokda");
109            }}, },
110        };
111    }
112
113    @Test(dataProvider = VARPATHS_ANTIMATCHES, expectedExceptions = AssertionError.class)
114    public final void notFindMatchVariables(final String path, final String incoming, Map<String, String> map) {
115        final Map<String, String> stringMap = new PathMatcherChain(path)
116                .findMatches(incoming);
117
118        assert null != stringMap;
119        assert stringMap.size() == map.size();
120        for (Map.Entry<String, String> entry : stringMap.entrySet()) {
121            assert map.containsKey(entry.getKey());
122            assert map.get(entry.getKey()).equals(entry.getValue());
123        }
124    }
125
126    @DataProvider(name = ANTI_VAR_PATHS)
127    public Object[][] getAntiVarPaths() {
128        return new Object[][] {
129            { "/wiki/:title", "/clicky/hello",  },
130            { "/wiki/:title/page/:id", "/wiki/hello/dago/12",  },
131            { "/wiki/:title/page/:id", "/wiki/couwdury/1/12424",  },
132            { "/wiki/:title/page/:id", "/wikit/sokdoasd/page/aoskpaokda",  },
133            { "/wiki/:title", "/wikia",  },
134            { "/wiki", "/",  },
135            { "/wiki/fencepost", "/",  },
136            { "/wiki/fencepost/stupid", "/",  },
137            { "/wiki/hicki", "/wiki",  },
138            { "/wiki/:title", "/wiki/",  },
139            { "/wiki/:hickory/dickory", "/wiki/dickory",  },
140            { "/wiki/:title", "/wiki/hello/bye",  },
141        };
142    }
143
144    @Test(dataProvider = ANTI_VAR_PATHS)
145    public final void notMatchPathTemplate(final String path, final String incoming) {
146        assert !new PathMatcherChain(path)
147                .matches(incoming);
148    }
149}