PageRenderTime 696ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/ninja-core/src/test/java/ninja/utils/NinjaPropertiesImplTest.java

https://gitlab.com/kidaa/ninja
Java | 318 lines | 176 code | 85 blank | 57 comment | 0 complexity | bbd914f86f2b0d85d7142f221d50a66c 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.utils;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import javax.inject.Named;
  20. import org.junit.After;
  21. import org.junit.Test;
  22. import com.google.inject.AbstractModule;
  23. import com.google.inject.Guice;
  24. import com.google.inject.Inject;
  25. public class NinjaPropertiesImplTest {
  26. @After
  27. public void tearDown() {
  28. // make sure the external conf property is removed after the test.
  29. System.clearProperty(NinjaProperties.NINJA_EXTERNAL_CONF);
  30. System.clearProperty(NinjaConstant.MODE_KEY_NAME);
  31. }
  32. @Test
  33. public void testSkippingThroughModesWorks() {
  34. // check that mode tests works:
  35. NinjaPropertiesImpl ninjaPropertiesImpl = new NinjaPropertiesImpl(NinjaMode.test);
  36. assertEquals("test_testproperty",
  37. ninjaPropertiesImpl.get("testproperty"));
  38. // check that mode dev works:
  39. ninjaPropertiesImpl = new NinjaPropertiesImpl(NinjaMode.dev);
  40. assertEquals("dev_testproperty",
  41. ninjaPropertiesImpl.get("testproperty"));
  42. assertEquals("secret",
  43. ninjaPropertiesImpl.get(NinjaConstant.applicationSecret));
  44. // and in a completely different mode with no "%"-prefixed key the
  45. // default value use used
  46. ninjaPropertiesImpl = new NinjaPropertiesImpl(NinjaMode.prod);
  47. assertEquals("testproperty_without_prefix",
  48. ninjaPropertiesImpl.get("testproperty"));
  49. assertEquals("secret",
  50. ninjaPropertiesImpl.get(NinjaConstant.applicationSecret));
  51. // tear down
  52. System.clearProperty(NinjaConstant.MODE_KEY_NAME);
  53. }
  54. @Test(expected = RuntimeException.class)
  55. public void testGetOrDie() {
  56. NinjaPropertiesImpl ninjaPropertiesImpl = new NinjaPropertiesImpl(NinjaMode.dev);
  57. assertEquals("dev_testproperty",
  58. ninjaPropertiesImpl.getOrDie("testproperty"));
  59. ninjaPropertiesImpl.getOrDie("a_propert_that_is_not_in_the_file");
  60. }
  61. @Test
  62. public void testGetBooleanParsing() {
  63. NinjaPropertiesImpl ninjaPropertiesImpl = new NinjaPropertiesImpl(NinjaMode.dev);
  64. assertEquals(true, ninjaPropertiesImpl.getBoolean("booleanTestTrue"));
  65. assertEquals(false, ninjaPropertiesImpl.getBoolean("booleanTestFalse"));
  66. assertEquals(null, ninjaPropertiesImpl.getBoolean("booleanNotValid"));
  67. }
  68. @Test(expected = RuntimeException.class)
  69. public void testGetBooleanOrDie() {
  70. NinjaPropertiesImpl ninjaPropertiesImpl = new NinjaPropertiesImpl(NinjaMode.dev);
  71. assertEquals(true,
  72. ninjaPropertiesImpl.getBooleanOrDie("booleanTestTrue"));
  73. ninjaPropertiesImpl.getBooleanOrDie("booleanNotValid");
  74. }
  75. @Test
  76. public void testGetIntegerParsing() {
  77. NinjaPropertiesImpl ninjaPropertiesImpl = new NinjaPropertiesImpl(NinjaMode.dev);
  78. assertEquals(new Integer(123456789),
  79. ninjaPropertiesImpl.getInteger("integerTest"));
  80. assertEquals(null, ninjaPropertiesImpl.getInteger("integerNotValid"));
  81. }
  82. @Test(expected = RuntimeException.class)
  83. public void testGetIntegerDie() {
  84. NinjaPropertiesImpl ninjaPropertiesImpl = new NinjaPropertiesImpl(NinjaMode.dev);
  85. assertEquals(new Integer(123456789),
  86. ninjaPropertiesImpl.getIntegerOrDie("integerTest"));
  87. ninjaPropertiesImpl.getIntegerOrDie("integerNotValid");
  88. }
  89. @Test
  90. public void testPropertiesBoundInGuice() {
  91. final NinjaPropertiesImpl props = new NinjaPropertiesImpl(NinjaMode.dev);
  92. MockService service = Guice.createInjector(new AbstractModule() {
  93. @Override
  94. protected void configure() {
  95. props.bindProperties(binder());
  96. }
  97. }).getInstance(MockService.class);
  98. assertNotNull("Application secret not set by Guice",
  99. service.applicationSecret);
  100. assertEquals("secret", service.applicationSecret);
  101. }
  102. public static class MockService {
  103. @Inject
  104. @Named(NinjaConstant.applicationSecret)
  105. public String applicationSecret;
  106. }
  107. @Test
  108. public void testReferenciningOfPropertiesWorks() {
  109. // instantiate the properties:
  110. NinjaProperties ninjaProperties = new NinjaPropertiesImpl(NinjaMode.dev);
  111. // this is testing if referencing of properties works with external
  112. // configurations
  113. // and application.conf: (fullServerName=${serverName}:${serverPort})
  114. assertEquals("http://myserver.com:80",
  115. ninjaProperties.get("fullServerName"));
  116. }
  117. @Test
  118. public void testLoadingOfExternalConfFile() {
  119. // we can set an external conf file by setting the following system
  120. // property:
  121. System.setProperty(NinjaProperties.NINJA_EXTERNAL_CONF,
  122. "conf/heroku.conf");
  123. // instantiate the properties:
  124. NinjaProperties ninjaProperties = new NinjaPropertiesImpl(NinjaMode.dev);
  125. // we expect that the original application secret gets overwritten:
  126. assertEquals("secretForHeroku",
  127. ninjaProperties.get(NinjaConstant.applicationSecret));
  128. // and make sure other properties of heroku.conf get loaded as well:
  129. assertEquals("some special parameter",
  130. ninjaProperties.get("heroku.special.property"));
  131. // this is testing if referencing of properties works with external
  132. // configurations
  133. // and application.conf (fullServerName=${serverName}:${serverPort})
  134. assertEquals("http://myapp.herokuapp.com:80",
  135. ninjaProperties.get("fullServerName"));
  136. }
  137. @Test
  138. public void testLoadingOfExternalConfigurationFileWorksAndPrefixedConfigurationsSetRead() {
  139. // we can set an external conf file by setting the following system
  140. // property:
  141. System.setProperty(NinjaProperties.NINJA_EXTERNAL_CONF,
  142. "conf/heroku.conf");
  143. // instantiate the properties:
  144. NinjaProperties ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test);
  145. // this is testing if referencing of properties works with external
  146. // configurations
  147. // and application.conf (fullServerName=${serverName}:${serverPort})
  148. // It also will be different as we are in "test" mode:
  149. // "myapp-test" is the important thing here.
  150. assertEquals("http://myapp-test.herokuapp.com:80",
  151. ninjaProperties.get("fullServerName"));
  152. }
  153. @Test
  154. public void testUft8Works() {
  155. NinjaProperties ninjaProperties = new NinjaPropertiesImpl(NinjaMode.dev);
  156. // We test this: utf8Test=this is utf8: öäü
  157. assertEquals("this is utf8: öäü", ninjaProperties.get("utf8Test"));
  158. }
  159. @Test(expected = RuntimeException.class)
  160. public void testExernalConfigLoadingBreaksWhenFileDoesNotExist() {
  161. // we can set an external conf file by setting the following system
  162. // property:
  163. System.setProperty(NinjaProperties.NINJA_EXTERNAL_CONF,
  164. "conf/non_existing.conf");
  165. // instantiate the properties:
  166. NinjaProperties ninjaProperties = new NinjaPropertiesImpl(NinjaMode.dev);
  167. // now a runtime exception must be thrown.
  168. }
  169. @Test
  170. public void testGetWithDefault() {
  171. // instantiate the properties:
  172. NinjaProperties ninjaProperties = new NinjaPropertiesImpl(NinjaMode.dev);
  173. // test default works when property not there:
  174. assertEquals("default", ninjaProperties.getWithDefault(
  175. "non_existsing_property_to_check_defaults", "default"));
  176. // test default works when property is there: => we are int dev mode...
  177. assertEquals("dev_testproperty",
  178. ninjaProperties.getWithDefault("testproperty", "default"));
  179. }
  180. @Test
  181. public void testGetIntegerWithDefault() {
  182. // instantiate the properties:
  183. NinjaProperties ninjaProperties = new NinjaPropertiesImpl(NinjaMode.dev);
  184. // test default works when property not there:
  185. assertEquals(Integer.valueOf(1), ninjaProperties.getIntegerWithDefault(
  186. "non_existsing_property_to_check_defaults", 1));
  187. // test default works when property is there:
  188. assertEquals(Integer.valueOf(123456789),
  189. ninjaProperties.getIntegerWithDefault("integerTest", 1));
  190. }
  191. @Test
  192. public void testGetBooleanWithDefault() {
  193. // instantiate the properties:
  194. NinjaProperties ninjaProperties = new NinjaPropertiesImpl(NinjaMode.dev);
  195. // test default works when property not there:
  196. assertEquals(Boolean.valueOf(true),
  197. ninjaProperties.getBooleanWithDefault(
  198. "non_existsing_property_to_check_defaults", true));
  199. // test default works when property is there:
  200. assertEquals(Boolean.valueOf(true),
  201. ninjaProperties.getBooleanWithDefault("booleanTestTrue", false));
  202. }
  203. @Test
  204. public void testGetStringArrayWorks() {
  205. // instantiate the properties:
  206. NinjaProperties ninjaProperties = new NinjaPropertiesImpl(NinjaMode.dev);
  207. // test default works when property not there:
  208. assertEquals("one", ninjaProperties.get("getOneElementStringArray"));
  209. assertEquals("one",
  210. ninjaProperties.getStringArray("getOneElementStringArray")[0]);
  211. assertEquals("one , me",
  212. ninjaProperties.get("getMultipleElementStringArrayWithSpaces"));
  213. assertEquals(
  214. "one",
  215. ninjaProperties
  216. .getStringArray("getMultipleElementStringArrayWithSpaces")[0]);
  217. assertEquals(
  218. "me",
  219. ninjaProperties
  220. .getStringArray("getMultipleElementStringArrayWithSpaces")[1]);
  221. assertEquals("one,me",
  222. ninjaProperties
  223. .get("getMultipleElementStringArrayWithoutSpaces"));
  224. assertEquals(
  225. "one",
  226. ninjaProperties
  227. .getStringArray("getMultipleElementStringArrayWithoutSpaces")[0]);
  228. assertEquals(
  229. "me",
  230. ninjaProperties
  231. .getStringArray("getMultipleElementStringArrayWithoutSpaces")[1]);
  232. }
  233. }