PageRenderTime 917ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/ninja-test-utilities/src/main/java/ninja/NinjaDaoTestBase.java

https://gitlab.com/kidaa/ninja
Java | 97 lines | 38 code | 13 blank | 46 comment | 0 complexity | e0c2d5cf51105f47e85cdb12a84b9649 MD5 | raw file
  1. /**
  2. * Copyright (C) 2012-2015 the original author or authors.
  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 ninja;
  17. import ninja.jpa.JpaInitializer;
  18. import ninja.jpa.JpaModule;
  19. import ninja.utils.NinjaMode;
  20. import ninja.utils.NinjaModeHelper;
  21. import ninja.utils.NinjaPropertiesImpl;
  22. import org.junit.After;
  23. import org.junit.Before;
  24. import com.google.common.base.Optional;
  25. import com.google.inject.Guice;
  26. import com.google.inject.Injector;
  27. /**
  28. * Base class for testing JPA-based DAOs
  29. *
  30. * How to use: Extend the Class and call getInstace(Class<SomeDao>) method to
  31. * get a real DAO. Then use JUnit assertions to test it (Example:
  32. * assertEquals(0,someDao.getAll().size());)
  33. *
  34. * @author emiguelt
  35. *
  36. */
  37. public abstract class NinjaDaoTestBase {
  38. /**
  39. * Persistence Service initializer
  40. */
  41. private JpaInitializer jpaInitializer;
  42. /**
  43. * Guice Injector to get DAOs
  44. */
  45. private Injector injector;
  46. private NinjaMode ninjaMode;
  47. /**
  48. * Constructor checks if NinjaMode was set in System properties, if not,
  49. * NinjaMode.test is used as default
  50. */
  51. public NinjaDaoTestBase() {
  52. Optional<NinjaMode> mode = NinjaModeHelper
  53. .determineModeFromSystemProperties();
  54. ninjaMode = mode.isPresent() ? mode.get() : NinjaMode.test;
  55. }
  56. /**
  57. * Constructor, receives the test mode to choose the database
  58. *
  59. * @param testMode
  60. */
  61. public NinjaDaoTestBase(NinjaMode testMode) {
  62. ninjaMode = testMode;
  63. }
  64. @Before
  65. public final void initialize() {
  66. NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(ninjaMode);
  67. injector = Guice.createInjector(new JpaModule(ninjaProperties));
  68. jpaInitializer = injector.getInstance(JpaInitializer.class);
  69. jpaInitializer.start();
  70. }
  71. @After
  72. public final void stop() {
  73. jpaInitializer.stop();
  74. }
  75. /**
  76. * Get the DAO instances ready to use
  77. *
  78. * @param clazz
  79. * @return DAO
  80. */
  81. protected <T> T getInstance(Class<T> clazz) {
  82. return injector.getInstance(clazz);
  83. }
  84. }