PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/dependency-injection/src/test/java/com/iluwatar/dependency/injection/GuiceWizardTest.java

https://gitlab.com/akkhil2012/java-design-patterns
Java | 100 lines | 41 code | 18 blank | 41 comment | 2 complexity | 23cd3215d22aca2b6559a082fc3f15bc MD5 | raw file
  1. /**
  2. * The MIT License
  3. * Copyright (c) 2014 Ilkka Seppälä
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. package com.iluwatar.dependency.injection;
  24. import com.google.inject.AbstractModule;
  25. import com.google.inject.Guice;
  26. import com.google.inject.Injector;
  27. import org.junit.Test;
  28. import static org.mockito.Mockito.times;
  29. import static org.mockito.Mockito.verify;
  30. import static org.mockito.Mockito.verifyNoMoreInteractions;
  31. /**
  32. * Date: 12/10/15 - 8:57 PM
  33. *
  34. * @author Jeroen Meulemeester
  35. */
  36. public class GuiceWizardTest extends StdOutTest {
  37. /**
  38. * Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
  39. * through the constructor parameter
  40. */
  41. @Test
  42. public void testSmokeEveryThingThroughConstructor() throws Exception {
  43. final Tobacco[] tobaccos = {
  44. new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
  45. };
  46. for (final Tobacco tobacco : tobaccos) {
  47. final GuiceWizard guiceWizard = new GuiceWizard(tobacco);
  48. guiceWizard.smoke();
  49. // Verify if the wizard is smoking the correct tobacco ...
  50. verify(getStdOutMock(), times(1)).println("GuiceWizard smoking " + tobacco.getClass().getSimpleName());
  51. // ... and nothing else is happening.
  52. verifyNoMoreInteractions(getStdOutMock());
  53. }
  54. }
  55. /**
  56. * Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
  57. * through the Guice google inject framework
  58. */
  59. @Test
  60. public void testSmokeEveryThingThroughInjectionFramework() throws Exception {
  61. @SuppressWarnings("unchecked")
  62. final Class<? extends Tobacco>[] tobaccos = new Class[]{
  63. OldTobyTobacco.class, RivendellTobacco.class, SecondBreakfastTobacco.class
  64. };
  65. for (final Class<? extends Tobacco> tobaccoClass : tobaccos) {
  66. // Configure the tobacco in the injection framework ...
  67. final Injector injector = Guice.createInjector(new AbstractModule() {
  68. @Override
  69. protected void configure() {
  70. bind(Tobacco.class).to(tobaccoClass);
  71. }
  72. });
  73. // ... and create a new wizard with it
  74. final GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
  75. guiceWizard.smoke();
  76. // Verify if the wizard is smoking the correct tobacco ...
  77. verify(getStdOutMock(), times(1)).println("GuiceWizard smoking " + tobaccoClass.getSimpleName());
  78. // ... and nothing else is happening.
  79. verifyNoMoreInteractions(getStdOutMock());
  80. }
  81. }
  82. }