configuration_test.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 "common/configuration_loader.h"
  21. #include <gmock/gmock.h>
  22. #include <cstdio>
  23. #include <fstream>
  24. using ::testing::_;
  25. using namespace hdfs;
  26. namespace hdfs {
  27. TEST(ConfigurationTest, TestDegenerateInputs) {
  28. /* Completely empty stream */
  29. {
  30. std::stringstream stream;
  31. ConfigurationLoader config_loader;
  32. config_loader.ClearSearchPath();
  33. optional<Configuration> config = config_loader.Load<Configuration>("");
  34. EXPECT_FALSE(config && "Empty stream");
  35. }
  36. /* No values */
  37. {
  38. std::string data = "<configuration></configuration>";
  39. ConfigurationLoader config_loader;
  40. config_loader.ClearSearchPath();
  41. optional<Configuration> config = config_loader.Load<Configuration>(data);
  42. EXPECT_TRUE(config && "Blank config");
  43. }
  44. /* Extraneous values */
  45. {
  46. std::string data = "<configuration><spam></spam></configuration>";
  47. ConfigurationLoader config_loader;
  48. config_loader.ClearSearchPath();
  49. optional<Configuration> config = config_loader.Load<Configuration>(data);
  50. EXPECT_TRUE(config && "Extraneous values");
  51. }
  52. }
  53. TEST(ConfigurationTest, TestBasicOperations) {
  54. /* Single value */
  55. {
  56. std::stringstream stream;
  57. simpleConfigStream(stream, "key1", "value1");
  58. ConfigurationLoader config_loader;
  59. config_loader.ClearSearchPath();
  60. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  61. EXPECT_TRUE(config && "Parse single value");
  62. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  63. }
  64. /* Multiple values */
  65. {
  66. optional<Configuration> config =
  67. simpleConfig("key1", "value1", "key2", "value2");
  68. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  69. EXPECT_EQ("value2", config->GetWithDefault("key2", ""));
  70. }
  71. /* Case-insensitive */
  72. {
  73. std::stringstream stream;
  74. simpleConfigStream(stream, "key1", "value1");
  75. ConfigurationLoader config_loader;
  76. config_loader.ClearSearchPath();
  77. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  78. EXPECT_TRUE(config && "Parse single value");
  79. EXPECT_EQ("value1", config->GetWithDefault("KEY1", ""));
  80. }
  81. /* No defaults */
  82. {
  83. std::stringstream stream;
  84. simpleConfigStream(stream, "key1", "value1");
  85. ConfigurationLoader config_loader;
  86. config_loader.ClearSearchPath();
  87. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  88. EXPECT_TRUE(config && "Parse single value");
  89. optional<std::string> value = config->Get("key1");
  90. EXPECT_TRUE((bool)value);
  91. EXPECT_EQ("value1", *value);
  92. EXPECT_FALSE(config->Get("key2"));
  93. }
  94. }
  95. TEST(ConfigurationTest, TestCompactValues) {
  96. {
  97. std::stringstream stream;
  98. stream << "<configuration><property name=\"key1\" "
  99. "value=\"value1\"/></configuration>";
  100. ConfigurationLoader config_loader;
  101. config_loader.ClearSearchPath();
  102. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  103. EXPECT_TRUE(config && "Compact value parse");
  104. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  105. }
  106. }
  107. TEST(ConfigurationTest, TestMultipleResources) {
  108. /* Single value */
  109. {
  110. std::stringstream stream;
  111. simpleConfigStream(stream, "key1", "value1");
  112. ConfigurationLoader config_loader;
  113. config_loader.ClearSearchPath();
  114. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  115. EXPECT_TRUE(config && "Parse first stream");
  116. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  117. std::stringstream stream2;
  118. simpleConfigStream(stream2, "key2", "value2");
  119. optional<Configuration> config2 =
  120. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  121. EXPECT_TRUE(config2 && "Parse second stream");
  122. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  123. EXPECT_EQ("value2", config2->GetWithDefault("key2", ""));
  124. }
  125. }
  126. TEST(ConfigurationTest, TestStringResource) {
  127. /* Single value */
  128. {
  129. std::stringstream stream;
  130. simpleConfigStream(stream, "key1", "value1");
  131. std::string str = stream.str();
  132. ConfigurationLoader config_loader;
  133. config_loader.ClearSearchPath();
  134. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  135. EXPECT_TRUE(config && "Parse single value");
  136. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  137. }
  138. }
  139. TEST(ConfigurationTest, TestValueOverlay) {
  140. /* Incremental updates */
  141. {
  142. ConfigurationLoader loader;
  143. std::stringstream stream;
  144. stream << "<configuration>"
  145. "<property><name>key1</name><value>value1</value><final>false</final></property>"
  146. "<property><name>final2</name><value>value2</value><final>true</final></property>"
  147. "</configuration>";
  148. optional<Configuration> config = loader.Load<Configuration>(stream.str());
  149. EXPECT_TRUE(config && "Parse single value");
  150. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  151. EXPECT_EQ("value2", config->GetWithDefault("final2", ""));
  152. config = loader.OverlayValue(config.value(), "key3", "value3");
  153. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  154. EXPECT_EQ("value2", config->GetWithDefault("final2", ""));
  155. EXPECT_EQ("value3", config->GetWithDefault("key3", ""));
  156. config = loader.OverlayValue(config.value(), "final2", "value4");
  157. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  158. EXPECT_EQ("value2", config->GetWithDefault("final2", ""));
  159. EXPECT_EQ("value3", config->GetWithDefault("key3", ""));
  160. // Case insensitive overlay
  161. config = loader.OverlayValue(config.value(), "KEY3", "value3a");
  162. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  163. EXPECT_EQ("value2", config->GetWithDefault("final2", ""));
  164. EXPECT_EQ("value3a", config->GetWithDefault("key3", ""));
  165. }
  166. }
  167. TEST(ConfigurationTest, TestFinal) {
  168. {
  169. /* Explicitly non-final non-compact value */
  170. std::stringstream stream;
  171. stream << "<configuration><property><name>key1</name><value>value1</"
  172. "value><final>false</final></property></configuration>";
  173. ConfigurationLoader config_loader;
  174. config_loader.ClearSearchPath();
  175. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  176. EXPECT_TRUE(config && "Parse first stream");
  177. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  178. std::stringstream stream2;
  179. simpleConfigStream(stream2, "key1", "value2");
  180. optional<Configuration> config2 =
  181. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  182. EXPECT_TRUE(config2 && "Parse second stream");
  183. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  184. }
  185. {
  186. /* Explicitly final non-compact value */
  187. std::stringstream stream;
  188. stream << "<configuration><property><name>key1</name><value>value1</"
  189. "value><final>true</final></property></configuration>";
  190. ConfigurationLoader config_loader;
  191. config_loader.ClearSearchPath();
  192. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  193. EXPECT_TRUE(config && "Parse first stream");
  194. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  195. std::stringstream stream2;
  196. simpleConfigStream(stream2, "key1", "value2");
  197. optional<Configuration> config2 =
  198. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  199. EXPECT_TRUE(config2 && "Parse second stream");
  200. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  201. }
  202. {
  203. /* Explicitly non-final compact value */
  204. std::stringstream stream;
  205. stream << "<configuration><property name=\"key1\" value=\"value1\" "
  206. "final=\"false\"/></configuration>";
  207. ConfigurationLoader config_loader;
  208. config_loader.ClearSearchPath();
  209. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  210. EXPECT_TRUE(config && "Parse first stream");
  211. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  212. std::stringstream stream2;
  213. simpleConfigStream(stream2, "key1", "value2");
  214. optional<Configuration> config2 =
  215. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  216. EXPECT_TRUE(config2 && "Parse second stream");
  217. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  218. }
  219. {
  220. /* Explicitly final compact value */
  221. std::stringstream stream;
  222. stream << "<configuration><property name=\"key1\" value=\"value1\" "
  223. "final=\"true\"/></configuration>";
  224. ConfigurationLoader config_loader;
  225. config_loader.ClearSearchPath();
  226. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  227. EXPECT_TRUE(config && "Parse first stream");
  228. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  229. std::stringstream stream2;
  230. simpleConfigStream(stream2, "key1", "value2");
  231. optional<Configuration> config2 =
  232. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  233. EXPECT_TRUE(config2 && "Parse second stream");
  234. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  235. }
  236. {
  237. /* Bogus final value */
  238. std::stringstream stream;
  239. stream << "<configuration><property><name>key1</name><value>value1</"
  240. "value><final>spam</final></property></configuration>";
  241. ConfigurationLoader config_loader;
  242. config_loader.ClearSearchPath();
  243. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  244. EXPECT_TRUE(config && "Parse first stream");
  245. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  246. std::stringstream stream2;
  247. simpleConfigStream(stream2, "key1", "value2");
  248. optional<Configuration> config2 =
  249. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  250. EXPECT_TRUE(config2 && "Parse second stream");
  251. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  252. }
  253. {
  254. /* Blank final value */
  255. std::stringstream stream;
  256. stream << "<configuration><property><name>key1</name><value>value1</"
  257. "value><final></final></property></configuration>";
  258. ConfigurationLoader config_loader;
  259. config_loader.ClearSearchPath();
  260. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  261. EXPECT_TRUE(config && "Parse first stream");
  262. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  263. std::stringstream stream2;
  264. simpleConfigStream(stream2, "key1", "value2");
  265. optional<Configuration> config2 =
  266. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  267. EXPECT_TRUE(config2 && "Parse second stream");
  268. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  269. }
  270. }
  271. TEST(ConfigurationTest, TestFileReads)
  272. {
  273. // Single stream
  274. {
  275. TempFile tempFile;
  276. writeSimpleConfig(tempFile.GetFileName(), "key1", "value1");
  277. ConfigurationLoader config_loader;
  278. config_loader.ClearSearchPath();
  279. optional<Configuration> config =
  280. config_loader.LoadFromFile<Configuration>(tempFile.GetFileName());
  281. EXPECT_TRUE(config && "Parse first stream");
  282. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  283. }
  284. // Multiple files
  285. {
  286. TempFile tempFile;
  287. writeSimpleConfig(tempFile.GetFileName(), "key1", "value1");
  288. ConfigurationLoader loader;
  289. optional<Configuration> config =
  290. loader.LoadFromFile<Configuration>(tempFile.GetFileName());
  291. ASSERT_TRUE(config && "Parse first stream");
  292. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  293. TempFile tempFile2;
  294. writeSimpleConfig(tempFile2.GetFileName(), "key2", "value2");
  295. optional<Configuration> config2 =
  296. loader.OverlayResourceFile(*config, tempFile2.GetFileName());
  297. ASSERT_TRUE(config2 && "Parse second stream");
  298. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  299. EXPECT_EQ("value2", config2->GetWithDefault("key2", ""));
  300. }
  301. // Try to add a directory
  302. {
  303. TempDir tempDir;
  304. ConfigurationLoader config_loader;
  305. config_loader.ClearSearchPath();
  306. optional<Configuration> config = config_loader.LoadFromFile<Configuration>(tempDir.path);
  307. EXPECT_FALSE(config && "Add directory as file resource");
  308. }
  309. // Search path splitting
  310. {
  311. ConfigurationLoader loader;
  312. loader.SetSearchPath("foo:/bar:baz/:/fioux/:/bar/bar/bong");
  313. // Paths will have / appended to them if not already present
  314. EXPECT_EQ("foo/:/bar/:baz/:/fioux/:/bar/bar/bong/", loader.GetSearchPath());
  315. }
  316. // Search path
  317. {
  318. TempDir tempDir1;
  319. TempFile tempFile1(tempDir1.path + "/file1.xml");
  320. writeSimpleConfig(tempFile1.GetFileName(), "key1", "value1");
  321. TempDir tempDir2;
  322. TempFile tempFile2(tempDir2.path + "/file2.xml");
  323. writeSimpleConfig(tempFile2.GetFileName(), "key2", "value2");
  324. TempDir tempDir3;
  325. TempFile tempFile3(tempDir3.path + "/file3.xml");
  326. writeSimpleConfig(tempFile3.GetFileName(), "key3", "value3");
  327. ConfigurationLoader loader;
  328. loader.SetSearchPath(tempDir1.path + ":" + tempDir2.path + ":" + tempDir3.path);
  329. optional<Configuration> config1 = loader.LoadFromFile<Configuration>("file1.xml");
  330. EXPECT_TRUE(config1 && "Parse first stream");
  331. optional<Configuration> config2 = loader.OverlayResourceFile(*config1, "file2.xml");
  332. EXPECT_TRUE(config2 && "Parse second stream");
  333. optional<Configuration> config3 = loader.OverlayResourceFile(*config2, "file3.xml");
  334. EXPECT_TRUE(config3 && "Parse third stream");
  335. EXPECT_EQ("value1", config3->GetWithDefault("key1", ""));
  336. EXPECT_EQ("value2", config3->GetWithDefault("key2", ""));
  337. EXPECT_EQ("value3", config3->GetWithDefault("key3", ""));
  338. }
  339. }
  340. TEST(ConfigurationTest, TestDefaultConfigs) {
  341. // Search path
  342. {
  343. TempDir tempDir;
  344. TempFile coreSite(tempDir.path + "/core-site.xml");
  345. writeSimpleConfig(coreSite.GetFileName(), "key1", "value1");
  346. ConfigurationLoader loader;
  347. loader.SetSearchPath(tempDir.path);
  348. optional<Configuration> config = loader.LoadDefaultResources<Configuration>();
  349. EXPECT_TRUE(config && "Parse streams");
  350. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  351. }
  352. }
  353. TEST(ConfigurationTest, TestIntConversions) {
  354. /* No defaults */
  355. {
  356. std::stringstream stream;
  357. simpleConfigStream(stream, "key1", "1");
  358. ConfigurationLoader config_loader;
  359. config_loader.ClearSearchPath();
  360. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  361. EXPECT_TRUE(config && "Parse single value");
  362. optional<int64_t> value = config->GetInt("key1");
  363. EXPECT_TRUE((bool)value);
  364. EXPECT_EQ(1, *value);
  365. EXPECT_FALSE(config->GetInt("key2"));
  366. }
  367. {
  368. optional<Configuration> config = simpleConfig("key1", "1");
  369. EXPECT_EQ(1, config->GetIntWithDefault("key1", -1));
  370. }
  371. {
  372. optional<Configuration> config = simpleConfig("key1", "-100");
  373. EXPECT_EQ(-100, config->GetIntWithDefault("key1", -1));
  374. }
  375. {
  376. optional<Configuration> config = simpleConfig("key1", " 1 ");
  377. EXPECT_EQ(1, config->GetIntWithDefault("key1", -1));
  378. }
  379. {
  380. optional<Configuration> config = simpleConfig("key1", "");
  381. EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1));
  382. }
  383. {
  384. optional<Configuration> config = simpleConfig("key1", "spam");
  385. EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1));
  386. }
  387. {
  388. optional<Configuration> config = simpleConfig("key2", "");
  389. EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1));
  390. }
  391. }
  392. TEST(ConfigurationTest, TestDoubleConversions) {
  393. /* No defaults */
  394. {
  395. std::stringstream stream;
  396. simpleConfigStream(stream, "key1", "1");
  397. ConfigurationLoader config_loader;
  398. config_loader.ClearSearchPath();
  399. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  400. EXPECT_TRUE(config && "Parse single value");
  401. optional<double> value = config->GetDouble("key1");
  402. EXPECT_TRUE((bool)value);
  403. EXPECT_EQ(1, *value);
  404. EXPECT_FALSE(config->GetDouble("key2"));
  405. }
  406. {
  407. optional<Configuration> config = simpleConfig("key1", "1");
  408. EXPECT_EQ(1, config->GetDoubleWithDefault("key1", -1));
  409. }
  410. {
  411. optional<Configuration> config = simpleConfig("key1", "-100");
  412. EXPECT_EQ(-100, config->GetDoubleWithDefault("key1", -1));
  413. }
  414. {
  415. optional<Configuration> config = simpleConfig("key1", " 1 ");
  416. EXPECT_EQ(1, config->GetDoubleWithDefault("key1", -1));
  417. }
  418. {
  419. optional<Configuration> config = simpleConfig("key1", "");
  420. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  421. }
  422. {
  423. optional<Configuration> config = simpleConfig("key1", "spam");
  424. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  425. }
  426. {
  427. optional<Configuration> config = simpleConfig("key2", "");
  428. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  429. }
  430. { /* Out of range */
  431. optional<Configuration> config = simpleConfig("key2", "1e9999");
  432. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  433. }
  434. }
  435. TEST(ConfigurationTest, TestBoolConversions) {
  436. /* No defaults */
  437. {
  438. std::stringstream stream;
  439. simpleConfigStream(stream, "key1", "true");
  440. ConfigurationLoader config_loader;
  441. config_loader.ClearSearchPath();
  442. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  443. EXPECT_TRUE(config && "Parse single value");
  444. optional<bool> value = config->GetBool("key1");
  445. EXPECT_TRUE((bool)value);
  446. EXPECT_EQ(true, *value);
  447. EXPECT_FALSE(config->GetBool("key2"));
  448. }
  449. {
  450. optional<Configuration> config = simpleConfig("key1", "true");
  451. EXPECT_EQ(true, config->GetBoolWithDefault("key1", false));
  452. }
  453. {
  454. optional<Configuration> config = simpleConfig("key1", "tRuE");
  455. EXPECT_EQ(true, config->GetBoolWithDefault("key1", false));
  456. }
  457. {
  458. optional<Configuration> config = simpleConfig("key1", "false");
  459. EXPECT_FALSE(config->GetBoolWithDefault("key1", true));
  460. }
  461. {
  462. optional<Configuration> config = simpleConfig("key1", "FaLsE");
  463. EXPECT_FALSE(config->GetBoolWithDefault("key1", true));
  464. }
  465. {
  466. optional<Configuration> config = simpleConfig("key1", " FaLsE ");
  467. EXPECT_FALSE(config->GetBoolWithDefault("key1", true));
  468. }
  469. {
  470. optional<Configuration> config = simpleConfig("key1", "");
  471. EXPECT_EQ(true, config->GetBoolWithDefault("key1", true));
  472. }
  473. {
  474. optional<Configuration> config = simpleConfig("key1", "spam");
  475. EXPECT_EQ(true, config->GetBoolWithDefault("key1", true));
  476. }
  477. {
  478. optional<Configuration> config = simpleConfig("key1", "");
  479. EXPECT_EQ(true, config->GetBoolWithDefault("key2", true));
  480. }
  481. }
  482. TEST(ConfigurationTest, TestUriConversions) {
  483. /* No defaults */
  484. {
  485. std::stringstream stream;
  486. simpleConfigStream(stream, "key1", "hdfs:///");
  487. ConfigurationLoader config_loader;
  488. config_loader.ClearSearchPath();
  489. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  490. EXPECT_TRUE(config && "Parse single value");
  491. optional<URI> value = config->GetUri("key1");
  492. EXPECT_TRUE((bool)value);
  493. EXPECT_EQ("hdfs:///", value->str());
  494. EXPECT_FALSE(config->GetUri("key2"));
  495. }
  496. {
  497. optional<Configuration> config = simpleConfig("key1", "hdfs:///");
  498. EXPECT_EQ("hdfs:///", config->GetUriWithDefault("key1", "http:///").str());
  499. }
  500. {
  501. optional<Configuration> config = simpleConfig("key1", " hdfs:/// ");
  502. EXPECT_EQ("hdfs:///", config->GetUriWithDefault("key1", "http:///").str());
  503. }
  504. {
  505. optional<Configuration> config = simpleConfig("key1", "");
  506. EXPECT_EQ("", config->GetUriWithDefault("key1", "http:///").str());
  507. }
  508. {
  509. optional<Configuration> config = simpleConfig("key1", "%%"); // invalid URI
  510. EXPECT_EQ("http:///", config->GetUriWithDefault("key1", "http:///").str());
  511. }
  512. {
  513. optional<Configuration> config = simpleConfig("key2", "hdfs:///");
  514. EXPECT_EQ("http:///", config->GetUriWithDefault("key1", "http:///").str());
  515. }
  516. }
  517. int main(int argc, char *argv[]) {
  518. /*
  519. * The following line must be executed to initialize Google Mock
  520. * (and Google Test) before running the tests.
  521. */
  522. ::testing::InitGoogleMock(&argc, argv);
  523. return RUN_ALL_TESTS();
  524. }
  525. }