PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/servlet/test/com/google/inject/servlet/ExtensionSpiTest.java

https://github.com/sonatype/sisu-guice
Java | 293 lines | 256 code | 16 blank | 21 comment | 4 complexity | 75a4060c8556bfff832d14c852400122 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright (C) 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.inject.servlet;
  17. import static com.google.inject.servlet.UriPatternType.REGEX;
  18. import static com.google.inject.servlet.UriPatternType.SERVLET;
  19. import com.google.common.collect.ImmutableList;
  20. import com.google.common.collect.ImmutableMap;
  21. import com.google.inject.Binding;
  22. import com.google.inject.Guice;
  23. import com.google.inject.Injector;
  24. import com.google.inject.Key;
  25. import com.google.inject.name.Names;
  26. import com.google.inject.servlet.ServletSpiVisitor.Params;
  27. import com.google.inject.spi.Element;
  28. import com.google.inject.spi.Elements;
  29. import java.util.Iterator;
  30. import java.util.List;
  31. import junit.framework.TestCase;
  32. /**
  33. * A very basic test that servletmodule works with bindings.
  34. *
  35. * @author sameb@google.com (Sam Berlin)
  36. */
  37. public class ExtensionSpiTest extends TestCase {
  38. private DummyFilterImpl dummyFilter1 = new DummyFilterImpl();
  39. private DummyFilterImpl dummyFilter2 = new DummyFilterImpl();
  40. private DummyFilterImpl dummyFilter3 = new DummyFilterImpl();
  41. private DummyFilterImpl dummyFilter4 = new DummyFilterImpl();
  42. private DummyServlet dummyServlet1 = new DummyServlet();
  43. private DummyServlet dummyServlet2 = new DummyServlet();
  44. private DummyServlet dummyServlet3 = new DummyServlet();
  45. private DummyServlet dummyServlet4 = new DummyServlet();
  46. public final void testSpiOnElements() {
  47. ServletSpiVisitor visitor = new ServletSpiVisitor(false);
  48. int count = 0;
  49. for (Element element : Elements.getElements(new Module())) {
  50. if (element instanceof Binding) {
  51. assertEquals(count++, ((Binding) element).acceptTargetVisitor(visitor));
  52. }
  53. }
  54. validateVisitor(visitor);
  55. }
  56. public final void testSpiOnInjector() {
  57. ServletSpiVisitor visitor = new ServletSpiVisitor(true);
  58. int count = 0;
  59. Injector injector = Guice.createInjector(new Module());
  60. for (Binding binding : injector.getBindings().values()) {
  61. assertEquals(count++, binding.acceptTargetVisitor(visitor));
  62. }
  63. validateVisitor(visitor);
  64. }
  65. private void validateVisitor(ServletSpiVisitor visitor) {
  66. assertEquals(48, visitor.currentCount - visitor.otherCount);
  67. // This is the expected param list, in order..
  68. List<Params> expected =
  69. ImmutableList.of(
  70. new Params("/class", Key.get(DummyFilterImpl.class), ImmutableMap.of(), SERVLET),
  71. new Params("/class/2", Key.get(DummyFilterImpl.class), ImmutableMap.of(), SERVLET),
  72. new Params(
  73. "/key",
  74. Key.get(DummyFilterImpl.class, Names.named("foo")),
  75. ImmutableMap.of(),
  76. SERVLET),
  77. new Params(
  78. "/key/2",
  79. Key.get(DummyFilterImpl.class, Names.named("foo")),
  80. ImmutableMap.of(),
  81. SERVLET),
  82. new Params("/instance", dummyFilter1, ImmutableMap.of(), SERVLET),
  83. new Params("/instance/2", dummyFilter1, ImmutableMap.of(), SERVLET),
  84. new Params(
  85. "/class/keyvalues",
  86. Key.get(DummyFilterImpl.class),
  87. ImmutableMap.of("key", "value"),
  88. SERVLET),
  89. new Params(
  90. "/class/keyvalues/2",
  91. Key.get(DummyFilterImpl.class),
  92. ImmutableMap.of("key", "value"),
  93. SERVLET),
  94. new Params(
  95. "/key/keyvalues",
  96. Key.get(DummyFilterImpl.class, Names.named("foo")),
  97. ImmutableMap.of("key", "value"),
  98. SERVLET),
  99. new Params(
  100. "/key/keyvalues/2",
  101. Key.get(DummyFilterImpl.class, Names.named("foo")),
  102. ImmutableMap.of("key", "value"),
  103. SERVLET),
  104. new Params(
  105. "/instance/keyvalues", dummyFilter2, ImmutableMap.of("key", "value"), SERVLET),
  106. new Params(
  107. "/instance/keyvalues/2", dummyFilter2, ImmutableMap.of("key", "value"), SERVLET),
  108. new Params("/class[0-9]", Key.get(DummyFilterImpl.class), ImmutableMap.of(), REGEX),
  109. new Params("/class[0-9]/2", Key.get(DummyFilterImpl.class), ImmutableMap.of(), REGEX),
  110. new Params(
  111. "/key[0-9]",
  112. Key.get(DummyFilterImpl.class, Names.named("foo")),
  113. ImmutableMap.of(),
  114. REGEX),
  115. new Params(
  116. "/key[0-9]/2",
  117. Key.get(DummyFilterImpl.class, Names.named("foo")),
  118. ImmutableMap.of(),
  119. REGEX),
  120. new Params("/instance[0-9]", dummyFilter3, ImmutableMap.of(), REGEX),
  121. new Params("/instance[0-9]/2", dummyFilter3, ImmutableMap.of(), REGEX),
  122. new Params(
  123. "/class[0-9]/keyvalues",
  124. Key.get(DummyFilterImpl.class),
  125. ImmutableMap.of("key", "value"),
  126. REGEX),
  127. new Params(
  128. "/class[0-9]/keyvalues/2",
  129. Key.get(DummyFilterImpl.class),
  130. ImmutableMap.of("key", "value"),
  131. REGEX),
  132. new Params(
  133. "/key[0-9]/keyvalues",
  134. Key.get(DummyFilterImpl.class, Names.named("foo")),
  135. ImmutableMap.of("key", "value"),
  136. REGEX),
  137. new Params(
  138. "/key[0-9]/keyvalues/2",
  139. Key.get(DummyFilterImpl.class, Names.named("foo")),
  140. ImmutableMap.of("key", "value"),
  141. REGEX),
  142. new Params(
  143. "/instance[0-9]/keyvalues", dummyFilter4, ImmutableMap.of("key", "value"), REGEX),
  144. new Params(
  145. "/instance[0-9]/keyvalues/2", dummyFilter4, ImmutableMap.of("key", "value"), REGEX),
  146. new Params("/class", Key.get(DummyServlet.class), ImmutableMap.of(), SERVLET),
  147. new Params("/class/2", Key.get(DummyServlet.class), ImmutableMap.of(), SERVLET),
  148. new Params(
  149. "/key",
  150. Key.get(DummyServlet.class, Names.named("foo")),
  151. ImmutableMap.of(),
  152. SERVLET),
  153. new Params(
  154. "/key/2",
  155. Key.get(DummyServlet.class, Names.named("foo")),
  156. ImmutableMap.of(),
  157. SERVLET),
  158. new Params("/instance", dummyServlet1, ImmutableMap.of(), SERVLET),
  159. new Params("/instance/2", dummyServlet1, ImmutableMap.of(), SERVLET),
  160. new Params(
  161. "/class/keyvalues",
  162. Key.get(DummyServlet.class),
  163. ImmutableMap.of("key", "value"),
  164. SERVLET),
  165. new Params(
  166. "/class/keyvalues/2",
  167. Key.get(DummyServlet.class),
  168. ImmutableMap.of("key", "value"),
  169. SERVLET),
  170. new Params(
  171. "/key/keyvalues",
  172. Key.get(DummyServlet.class, Names.named("foo")),
  173. ImmutableMap.of("key", "value"),
  174. SERVLET),
  175. new Params(
  176. "/key/keyvalues/2",
  177. Key.get(DummyServlet.class, Names.named("foo")),
  178. ImmutableMap.of("key", "value"),
  179. SERVLET),
  180. new Params(
  181. "/instance/keyvalues", dummyServlet2, ImmutableMap.of("key", "value"), SERVLET),
  182. new Params(
  183. "/instance/keyvalues/2", dummyServlet2, ImmutableMap.of("key", "value"), SERVLET),
  184. new Params("/class[0-9]", Key.get(DummyServlet.class), ImmutableMap.of(), REGEX),
  185. new Params("/class[0-9]/2", Key.get(DummyServlet.class), ImmutableMap.of(), REGEX),
  186. new Params(
  187. "/key[0-9]",
  188. Key.get(DummyServlet.class, Names.named("foo")),
  189. ImmutableMap.of(),
  190. REGEX),
  191. new Params(
  192. "/key[0-9]/2",
  193. Key.get(DummyServlet.class, Names.named("foo")),
  194. ImmutableMap.of(),
  195. REGEX),
  196. new Params("/instance[0-9]", dummyServlet3, ImmutableMap.of(), REGEX),
  197. new Params("/instance[0-9]/2", dummyServlet3, ImmutableMap.of(), REGEX),
  198. new Params(
  199. "/class[0-9]/keyvalues",
  200. Key.get(DummyServlet.class),
  201. ImmutableMap.of("key", "value"),
  202. REGEX),
  203. new Params(
  204. "/class[0-9]/keyvalues/2",
  205. Key.get(DummyServlet.class),
  206. ImmutableMap.of("key", "value"),
  207. REGEX),
  208. new Params(
  209. "/key[0-9]/keyvalues",
  210. Key.get(DummyServlet.class, Names.named("foo")),
  211. ImmutableMap.of("key", "value"),
  212. REGEX),
  213. new Params(
  214. "/key[0-9]/keyvalues/2",
  215. Key.get(DummyServlet.class, Names.named("foo")),
  216. ImmutableMap.of("key", "value"),
  217. REGEX),
  218. new Params(
  219. "/instance[0-9]/keyvalues", dummyServlet4, ImmutableMap.of("key", "value"), REGEX),
  220. new Params(
  221. "/instance[0-9]/keyvalues/2",
  222. dummyServlet4,
  223. ImmutableMap.of("key", "value"),
  224. REGEX));
  225. assertEquals(expected.size(), visitor.actual.size());
  226. Iterator<Params> actualIterator = visitor.actual.iterator();
  227. int i = 0;
  228. for (Params param : expected) {
  229. assertEquals("wrong " + i++ + "th param", param, actualIterator.next());
  230. }
  231. }
  232. private class Module extends ServletModule {
  233. @Override
  234. protected void configureServlets() {
  235. binder().requireExplicitBindings();
  236. filter("/class", "/class/2").through(DummyFilterImpl.class);
  237. filter("/key", "/key/2").through(Key.get(DummyFilterImpl.class, Names.named("foo")));
  238. filter("/instance", "/instance/2").through(dummyFilter1);
  239. filter("/class/keyvalues", "/class/keyvalues/2")
  240. .through(DummyFilterImpl.class, ImmutableMap.of("key", "value"));
  241. filter("/key/keyvalues", "/key/keyvalues/2")
  242. .through(
  243. Key.get(DummyFilterImpl.class, Names.named("foo")), ImmutableMap.of("key", "value"));
  244. filter("/instance/keyvalues", "/instance/keyvalues/2")
  245. .through(dummyFilter2, ImmutableMap.of("key", "value"));
  246. filterRegex("/class[0-9]", "/class[0-9]/2").through(DummyFilterImpl.class);
  247. filterRegex("/key[0-9]", "/key[0-9]/2")
  248. .through(Key.get(DummyFilterImpl.class, Names.named("foo")));
  249. filterRegex("/instance[0-9]", "/instance[0-9]/2").through(dummyFilter3);
  250. filterRegex("/class[0-9]/keyvalues", "/class[0-9]/keyvalues/2")
  251. .through(DummyFilterImpl.class, ImmutableMap.of("key", "value"));
  252. filterRegex("/key[0-9]/keyvalues", "/key[0-9]/keyvalues/2")
  253. .through(
  254. Key.get(DummyFilterImpl.class, Names.named("foo")), ImmutableMap.of("key", "value"));
  255. filterRegex("/instance[0-9]/keyvalues", "/instance[0-9]/keyvalues/2")
  256. .through(dummyFilter4, ImmutableMap.of("key", "value"));
  257. serve("/class", "/class/2").with(DummyServlet.class);
  258. serve("/key", "/key/2").with(Key.get(DummyServlet.class, Names.named("foo")));
  259. serve("/instance", "/instance/2").with(dummyServlet1);
  260. serve("/class/keyvalues", "/class/keyvalues/2")
  261. .with(DummyServlet.class, ImmutableMap.of("key", "value"));
  262. serve("/key/keyvalues", "/key/keyvalues/2")
  263. .with(Key.get(DummyServlet.class, Names.named("foo")), ImmutableMap.of("key", "value"));
  264. serve("/instance/keyvalues", "/instance/keyvalues/2")
  265. .with(dummyServlet2, ImmutableMap.of("key", "value"));
  266. serveRegex("/class[0-9]", "/class[0-9]/2").with(DummyServlet.class);
  267. serveRegex("/key[0-9]", "/key[0-9]/2").with(Key.get(DummyServlet.class, Names.named("foo")));
  268. serveRegex("/instance[0-9]", "/instance[0-9]/2").with(dummyServlet3);
  269. serveRegex("/class[0-9]/keyvalues", "/class[0-9]/keyvalues/2")
  270. .with(DummyServlet.class, ImmutableMap.of("key", "value"));
  271. serveRegex("/key[0-9]/keyvalues", "/key[0-9]/keyvalues/2")
  272. .with(Key.get(DummyServlet.class, Names.named("foo")), ImmutableMap.of("key", "value"));
  273. serveRegex("/instance[0-9]/keyvalues", "/instance[0-9]/keyvalues/2")
  274. .with(dummyServlet4, ImmutableMap.of("key", "value"));
  275. }
  276. }
  277. }