/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

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