PageRenderTime 616ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/guice-3.0-rc2-src/extensions/servlet/test/com/google/inject/servlet/EdslTest.java

#
Java | 106 lines | 66 code | 18 blank | 22 comment | 0 complexity | 77d0b7cf8903644300190fb1baa3e29e MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (C) 2008 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 com.google.inject.AbstractModule;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Injector;
  20. import com.google.inject.Key;
  21. import com.google.inject.Module;
  22. import com.google.inject.Singleton;
  23. import com.google.inject.Stage;
  24. import java.util.HashMap;
  25. import junit.framework.TestCase;
  26. /**
  27. * Sanity checks the EDSL and resultant bound module(s).
  28. *
  29. * @author Dhanji R. Prasanna (dhanji gmail com)
  30. */
  31. public class EdslTest extends TestCase {
  32. public final void testExplicitBindingsWorksWithGuiceServlet() {
  33. Injector injector = Guice.createInjector(
  34. new AbstractModule() {
  35. @Override
  36. protected void configure() {
  37. binder().requireExplicitBindings();
  38. }
  39. }, new ServletModule() {
  40. @Override protected void configureServlets() {
  41. bind(DummyServlet.class).in(Singleton.class);
  42. serve("/*").with(DummyServlet.class);
  43. }
  44. });
  45. assertNotNull(injector.getInstance(DummyServlet.class));
  46. }
  47. public final void testConfigureServlets() {
  48. //the various possible config calls--
  49. Module webModule = new ServletModule() {
  50. @Override
  51. protected void configureServlets() {
  52. filter("/*").through(DummyFilterImpl.class);
  53. filter("*.html").through(DummyFilterImpl.class);
  54. filter("/*").through(Key.get(DummyFilterImpl.class));
  55. filter("/*").through(new DummyFilterImpl());
  56. filter("*.html").through(DummyFilterImpl.class,
  57. new HashMap<String, String>());
  58. filterRegex("/person/[0-9]*").through(DummyFilterImpl.class);
  59. filterRegex("/person/[0-9]*").through(DummyFilterImpl.class,
  60. new HashMap<String, String>());
  61. filterRegex("/person/[0-9]*").through(Key.get(DummyFilterImpl.class));
  62. filterRegex("/person/[0-9]*").through(Key.get(DummyFilterImpl.class),
  63. new HashMap<String, String>());
  64. filterRegex("/person/[0-9]*").through(new DummyFilterImpl());
  65. filterRegex("/person/[0-9]*").through(new DummyFilterImpl(),
  66. new HashMap<String, String>());
  67. serve("/1/*").with(DummyServlet.class);
  68. serve("/2/*").with(Key.get(DummyServlet.class));
  69. serve("/3/*").with(new DummyServlet());
  70. serve("/4/*").with(DummyServlet.class, new HashMap<String, String>());
  71. serve("*.htm").with(Key.get(DummyServlet.class));
  72. serve("*.html").with(Key.get(DummyServlet.class),
  73. new HashMap<String, String>());
  74. serveRegex("/person/[0-8]*").with(DummyServlet.class);
  75. serveRegex("/person/[0-9]*").with(DummyServlet.class,
  76. new HashMap<String, String>());
  77. serveRegex("/person/[0-6]*").with(Key.get(DummyServlet.class));
  78. serveRegex("/person/[0-9]/2/*").with(Key.get(DummyServlet.class),
  79. new HashMap<String, String>());
  80. serveRegex("/person/[0-5]*").with(new DummyServlet());
  81. serveRegex("/person/[0-9]/3/*").with(new DummyServlet(),
  82. new HashMap<String, String>());
  83. }
  84. };
  85. //verify that it doesn't blow up!
  86. Guice.createInjector(Stage.TOOL, webModule);
  87. }
  88. }