configuration_test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "configuration_test.h"
  19. #include "common/configuration.h"
  20. #include <gmock/gmock.h>
  21. #include <cstdio>
  22. #include <fstream>
  23. using ::testing::_;
  24. using namespace hdfs;
  25. namespace hdfs {
  26. TEST(ConfigurationTest, TestDegenerateInputs) {
  27. /* Completely empty stream */
  28. {
  29. std::stringstream stream;
  30. optional<Configuration> config = Configuration::Load("");
  31. EXPECT_FALSE(config && "Empty stream");
  32. }
  33. /* No values */
  34. {
  35. std::string data = "<configuration></configuration>";
  36. optional<Configuration> config = Configuration::Load(data);
  37. EXPECT_TRUE(config && "Blank config");
  38. }
  39. /* Extraneous values */
  40. {
  41. std::string data = "<configuration><spam></spam></configuration>";
  42. optional<Configuration> config = Configuration::Load(data);
  43. EXPECT_TRUE(config && "Extraneous values");
  44. }
  45. }
  46. TEST(ConfigurationTest, TestBasicOperations) {
  47. /* Single value */
  48. {
  49. std::stringstream stream;
  50. simpleConfigStream(stream, "key1", "value1");
  51. optional<Configuration> config = Configuration::Load(stream.str());
  52. EXPECT_TRUE(config && "Parse single value");
  53. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  54. }
  55. /* Multiple values */
  56. {
  57. optional<Configuration> config =
  58. simpleConfig("key1", "value1", "key2", "value2");
  59. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  60. EXPECT_EQ("value2", config->GetWithDefault("key2", ""));
  61. }
  62. /* No defaults */
  63. {
  64. std::stringstream stream;
  65. simpleConfigStream(stream, "key1", "value1");
  66. optional<Configuration> config = Configuration::Load(stream.str());
  67. EXPECT_TRUE(config && "Parse single value");
  68. optional<std::string> value = config->Get("key1");
  69. EXPECT_TRUE((bool)value);
  70. EXPECT_EQ("value1", *value);
  71. EXPECT_FALSE(config->Get("key2"));
  72. }
  73. }
  74. TEST(ConfigurationTest, TestCompactValues) {
  75. {
  76. std::stringstream stream;
  77. stream << "<configuration><property name=\"key1\" "
  78. "value=\"value1\"/></configuration>";
  79. optional<Configuration> config = Configuration::Load(stream.str());
  80. EXPECT_TRUE(config && "Compact value parse");
  81. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  82. }
  83. }
  84. TEST(ConfigurationTest, TestMultipleResources) {
  85. /* Single value */
  86. {
  87. std::stringstream stream;
  88. simpleConfigStream(stream, "key1", "value1");
  89. optional<Configuration> config = Configuration::Load(stream.str());
  90. EXPECT_TRUE(config && "Parse first stream");
  91. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  92. std::stringstream stream2;
  93. simpleConfigStream(stream2, "key2", "value2");
  94. optional<Configuration> config2 =
  95. config->OverlayResourceString(stream2.str());
  96. EXPECT_TRUE(config2 && "Parse second stream");
  97. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  98. EXPECT_EQ("value2", config2->GetWithDefault("key2", ""));
  99. }
  100. }
  101. TEST(ConfigurationTest, TestStringResource) {
  102. /* Single value */
  103. {
  104. std::stringstream stream;
  105. simpleConfigStream(stream, "key1", "value1");
  106. std::string str = stream.str();
  107. optional<Configuration> config = Configuration::Load(stream.str());
  108. EXPECT_TRUE(config && "Parse single value");
  109. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  110. }
  111. }
  112. TEST(ConfigurationTest, TestFinal) {
  113. {
  114. /* Explicitly non-final non-compact value */
  115. std::stringstream stream;
  116. stream << "<configuration><property><name>key1</name><value>value1</"
  117. "value><final>false</final></property></configuration>";
  118. optional<Configuration> config = Configuration::Load(stream.str());
  119. EXPECT_TRUE(config && "Parse first stream");
  120. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  121. std::stringstream stream2;
  122. simpleConfigStream(stream2, "key1", "value2");
  123. optional<Configuration> config2 =
  124. config->OverlayResourceString(stream2.str());
  125. EXPECT_TRUE(config2 && "Parse second stream");
  126. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  127. }
  128. {
  129. /* Explicitly final non-compact value */
  130. std::stringstream stream;
  131. stream << "<configuration><property><name>key1</name><value>value1</"
  132. "value><final>true</final></property></configuration>";
  133. optional<Configuration> config = Configuration::Load(stream.str());
  134. EXPECT_TRUE(config && "Parse first stream");
  135. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  136. std::stringstream stream2;
  137. simpleConfigStream(stream2, "key1", "value2");
  138. optional<Configuration> config2 =
  139. config->OverlayResourceString(stream2.str());
  140. EXPECT_TRUE(config2 && "Parse second stream");
  141. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  142. }
  143. {
  144. /* Explicitly non-final compact value */
  145. std::stringstream stream;
  146. stream << "<configuration><property name=\"key1\" value=\"value1\" "
  147. "final=\"false\"/></configuration>";
  148. optional<Configuration> config = Configuration::Load(stream.str());
  149. EXPECT_TRUE(config && "Parse first stream");
  150. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  151. std::stringstream stream2;
  152. simpleConfigStream(stream2, "key1", "value2");
  153. optional<Configuration> config2 =
  154. config->OverlayResourceString(stream2.str());
  155. EXPECT_TRUE(config2 && "Parse second stream");
  156. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  157. }
  158. {
  159. /* Explicitly final compact value */
  160. std::stringstream stream;
  161. stream << "<configuration><property name=\"key1\" value=\"value1\" "
  162. "final=\"true\"/></configuration>";
  163. optional<Configuration> config = Configuration::Load(stream.str());
  164. EXPECT_TRUE(config && "Parse first stream");
  165. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  166. std::stringstream stream2;
  167. simpleConfigStream(stream2, "key1", "value2");
  168. optional<Configuration> config2 =
  169. config->OverlayResourceString(stream2.str());
  170. EXPECT_TRUE(config2 && "Parse second stream");
  171. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  172. }
  173. {
  174. /* Bogus final value */
  175. std::stringstream stream;
  176. stream << "<configuration><property><name>key1</name><value>value1</"
  177. "value><final>spam</final></property></configuration>";
  178. optional<Configuration> config = Configuration::Load(stream.str());
  179. EXPECT_TRUE(config && "Parse first stream");
  180. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  181. std::stringstream stream2;
  182. simpleConfigStream(stream2, "key1", "value2");
  183. optional<Configuration> config2 =
  184. config->OverlayResourceString(stream2.str());
  185. EXPECT_TRUE(config2 && "Parse second stream");
  186. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  187. }
  188. {
  189. /* Blank final value */
  190. std::stringstream stream;
  191. stream << "<configuration><property><name>key1</name><value>value1</"
  192. "value><final></final></property></configuration>";
  193. optional<Configuration> config = Configuration::Load(stream.str());
  194. EXPECT_TRUE(config && "Parse first stream");
  195. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  196. std::stringstream stream2;
  197. simpleConfigStream(stream2, "key1", "value2");
  198. optional<Configuration> config2 =
  199. config->OverlayResourceString(stream2.str());
  200. EXPECT_TRUE(config2 && "Parse second stream");
  201. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  202. }
  203. }
  204. TEST(ConfigurationTest, TestIntConversions) {
  205. /* No defaults */
  206. {
  207. std::stringstream stream;
  208. simpleConfigStream(stream, "key1", "1");
  209. optional<Configuration> config = Configuration::Load(stream.str());
  210. EXPECT_TRUE(config && "Parse single value");
  211. optional<int64_t> value = config->GetInt("key1");
  212. EXPECT_TRUE((bool)value);
  213. EXPECT_EQ(1, *value);
  214. EXPECT_FALSE(config->GetInt("key2"));
  215. }
  216. {
  217. optional<Configuration> config = simpleConfig("key1", "1");
  218. EXPECT_EQ(1, config->GetIntWithDefault("key1", -1));
  219. }
  220. {
  221. optional<Configuration> config = simpleConfig("key1", "-100");
  222. EXPECT_EQ(-100, config->GetIntWithDefault("key1", -1));
  223. }
  224. {
  225. optional<Configuration> config = simpleConfig("key1", " 1 ");
  226. EXPECT_EQ(1, config->GetIntWithDefault("key1", -1));
  227. }
  228. {
  229. optional<Configuration> config = simpleConfig("key1", "");
  230. EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1));
  231. }
  232. {
  233. optional<Configuration> config = simpleConfig("key1", "spam");
  234. EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1));
  235. }
  236. {
  237. optional<Configuration> config = simpleConfig("key2", "");
  238. EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1));
  239. }
  240. }
  241. TEST(ConfigurationTest, TestDoubleConversions) {
  242. /* No defaults */
  243. {
  244. std::stringstream stream;
  245. simpleConfigStream(stream, "key1", "1");
  246. optional<Configuration> config = Configuration::Load(stream.str());
  247. EXPECT_TRUE(config && "Parse single value");
  248. optional<double> value = config->GetDouble("key1");
  249. EXPECT_TRUE((bool)value);
  250. EXPECT_EQ(1, *value);
  251. EXPECT_FALSE(config->GetDouble("key2"));
  252. }
  253. {
  254. optional<Configuration> config = simpleConfig("key1", "1");
  255. EXPECT_EQ(1, config->GetDoubleWithDefault("key1", -1));
  256. }
  257. {
  258. optional<Configuration> config = simpleConfig("key1", "-100");
  259. EXPECT_EQ(-100, config->GetDoubleWithDefault("key1", -1));
  260. }
  261. {
  262. optional<Configuration> config = simpleConfig("key1", " 1 ");
  263. EXPECT_EQ(1, config->GetDoubleWithDefault("key1", -1));
  264. }
  265. {
  266. optional<Configuration> config = simpleConfig("key1", "");
  267. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  268. }
  269. {
  270. optional<Configuration> config = simpleConfig("key1", "spam");
  271. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  272. }
  273. {
  274. optional<Configuration> config = simpleConfig("key2", "");
  275. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  276. }
  277. { /* Out of range */
  278. optional<Configuration> config = simpleConfig("key2", "1e9999");
  279. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  280. }
  281. }
  282. TEST(ConfigurationTest, TestBoolConversions) {
  283. /* No defaults */
  284. {
  285. std::stringstream stream;
  286. simpleConfigStream(stream, "key1", "true");
  287. optional<Configuration> config = Configuration::Load(stream.str());
  288. EXPECT_TRUE(config && "Parse single value");
  289. optional<bool> value = config->GetBool("key1");
  290. EXPECT_TRUE((bool)value);
  291. EXPECT_EQ(true, *value);
  292. EXPECT_FALSE(config->GetBool("key2"));
  293. }
  294. {
  295. optional<Configuration> config = simpleConfig("key1", "true");
  296. EXPECT_EQ(true, config->GetBoolWithDefault("key1", false));
  297. }
  298. {
  299. optional<Configuration> config = simpleConfig("key1", "tRuE");
  300. EXPECT_EQ(true, config->GetBoolWithDefault("key1", false));
  301. }
  302. {
  303. optional<Configuration> config = simpleConfig("key1", "false");
  304. EXPECT_FALSE(config->GetBoolWithDefault("key1", true));
  305. }
  306. {
  307. optional<Configuration> config = simpleConfig("key1", "FaLsE");
  308. EXPECT_FALSE(config->GetBoolWithDefault("key1", true));
  309. }
  310. {
  311. optional<Configuration> config = simpleConfig("key1", " FaLsE ");
  312. EXPECT_FALSE(config->GetBoolWithDefault("key1", true));
  313. }
  314. {
  315. optional<Configuration> config = simpleConfig("key1", "");
  316. EXPECT_EQ(true, config->GetBoolWithDefault("key1", true));
  317. }
  318. {
  319. optional<Configuration> config = simpleConfig("key1", "spam");
  320. EXPECT_EQ(true, config->GetBoolWithDefault("key1", true));
  321. }
  322. {
  323. optional<Configuration> config = simpleConfig("key1", "");
  324. EXPECT_EQ(true, config->GetBoolWithDefault("key2", true));
  325. }
  326. }
  327. int main(int argc, char *argv[]) {
  328. /*
  329. * The following line must be executed to initialize Google Mock
  330. * (and Google Test) before running the tests.
  331. */
  332. ::testing::InitGoogleMock(&argc, argv);
  333. return RUN_ALL_TESTS();
  334. }
  335. }