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

/configuration/src/test/java/io/airlift/configuration/TestConfig.java

https://gitlab.com/CORP-RESELLER/airlift
Java | 141 lines | 113 code | 12 blank | 16 comment | 1 complexity | 023e12ab39a4053897d73de9c8f26758 MD5 | raw file
  1. /*
  2. * Copyright 2010 Proofpoint, 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 io.airlift.configuration;
  17. import com.google.common.collect.ImmutableMap;
  18. import com.google.common.collect.ImmutableMap.Builder;
  19. import com.google.inject.Binder;
  20. import com.google.inject.CreationException;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Injector;
  23. import com.google.inject.Module;
  24. import com.google.inject.spi.Message;
  25. import org.testng.annotations.BeforeMethod;
  26. import org.testng.annotations.Test;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Map.Entry;
  31. import static org.testng.Assert.assertEquals;
  32. import static org.testng.Assert.fail;
  33. public class TestConfig
  34. {
  35. private ImmutableMap<String,String> properties;
  36. @Test
  37. public void testConfig()
  38. {
  39. Injector injector = createInjector(properties, createModule(Config1.class, null));
  40. verifyConfig(injector.getInstance(Config1.class));
  41. }
  42. @Test
  43. public void testPrefixConfigTypes()
  44. {
  45. Injector injector = createInjector(prefix("prefix", properties), createModule(Config1.class, "prefix"));
  46. verifyConfig(injector.getInstance(Config1.class));
  47. }
  48. private void verifyConfig(Config1 config)
  49. {
  50. assertEquals("a string", config.getStringOption());
  51. assertEquals(true, config.getBooleanOption());
  52. assertEquals(Boolean.TRUE, config.getBoxedBooleanOption());
  53. assertEquals(Byte.MAX_VALUE, config.getByteOption());
  54. assertEquals(Byte.valueOf(Byte.MAX_VALUE), config.getBoxedByteOption());
  55. assertEquals(Short.MAX_VALUE, config.getShortOption());
  56. assertEquals(Short.valueOf(Short.MAX_VALUE), config.getBoxedShortOption());
  57. assertEquals(Integer.MAX_VALUE, config.getIntegerOption());
  58. assertEquals(Integer.valueOf(Integer.MAX_VALUE), config.getBoxedIntegerOption());
  59. assertEquals(Long.MAX_VALUE, config.getLongOption());
  60. assertEquals(Long.valueOf(Long.MAX_VALUE), config.getBoxedLongOption());
  61. assertEquals(Float.MAX_VALUE, config.getFloatOption(), 0);
  62. assertEquals(Float.MAX_VALUE, config.getBoxedFloatOption());
  63. assertEquals(Double.MAX_VALUE, config.getDoubleOption(), 0);
  64. assertEquals(Double.MAX_VALUE, config.getBoxedDoubleOption());
  65. assertEquals(MyEnum.FOO, config.getMyEnumOption());
  66. assertEquals(config.getValueClassOption().getValue(), "a value class");
  67. }
  68. @Test
  69. public void testDetectsNoConfigAnnotations()
  70. {
  71. try {
  72. Injector injector = createInjector(Collections.<String, String>emptyMap(), createModule(ConfigWithNoAnnotations.class, null));
  73. injector.getInstance(ConfigWithNoAnnotations.class);
  74. fail("Expected exception due to missing @Config annotations");
  75. }
  76. catch (CreationException e) {
  77. // do nothing
  78. }
  79. }
  80. private Injector createInjector(Map<String, String> properties, Module module)
  81. {
  82. ConfigurationFactory configurationFactory = new ConfigurationFactory(properties);
  83. List<Message> messages = new ConfigurationValidator(configurationFactory, null).validate(module);
  84. return Guice.createInjector(new ConfigurationModule(configurationFactory), module, new ValidationErrorModule(messages));
  85. }
  86. private <T> Module createModule(final Class<T> configClass, final String prefix)
  87. {
  88. Module module = new Module() {
  89. @Override
  90. public void configure(Binder binder)
  91. {
  92. ConfigurationModule.bindConfig(binder).prefixedWith(prefix).to(configClass);
  93. }
  94. };
  95. return module;
  96. }
  97. @BeforeMethod
  98. protected void setUp()
  99. throws Exception
  100. {
  101. properties = ImmutableMap.<String, String>builder()
  102. .put("stringOption", "a string")
  103. .put("booleanOption", "true")
  104. .put("boxedBooleanOption", "true")
  105. .put("byteOption", Byte.toString(Byte.MAX_VALUE))
  106. .put("boxedByteOption", Byte.toString(Byte.MAX_VALUE))
  107. .put("shortOption", Short.toString(Short.MAX_VALUE))
  108. .put("boxedShortOption", Short.toString(Short.MAX_VALUE))
  109. .put("integerOption", Integer.toString(Integer.MAX_VALUE))
  110. .put("boxedIntegerOption", Integer.toString(Integer.MAX_VALUE))
  111. .put("longOption", Long.toString(Long.MAX_VALUE))
  112. .put("boxedLongOption", Long.toString(Long.MAX_VALUE))
  113. .put("floatOption", Float.toString(Float.MAX_VALUE))
  114. .put("boxedFloatOption", Float.toString(Float.MAX_VALUE))
  115. .put("doubleOption", Double.toString(Double.MAX_VALUE))
  116. .put("boxedDoubleOption", Double.toString(Double.MAX_VALUE))
  117. .put("myEnumOption", MyEnum.FOO.toString())
  118. .put("valueClassOption", "a value class")
  119. .build();
  120. }
  121. private Map<String, String> prefix(String prefix, Map<String, String> properties)
  122. {
  123. Builder<String, String> builder = ImmutableMap.builder();
  124. for (Entry<String, String> entry : properties.entrySet()) {
  125. builder.put(prefix + "." + entry.getKey(), entry.getValue());
  126. }
  127. return builder.build();
  128. }
  129. }