configuration_test.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef TESTS_CONFIGURATION_H_
  19. #define TESTS_CONFIGURATION_H_
  20. #include "common/configuration.h"
  21. #include "common/configuration_loader.h"
  22. #include <cstdio>
  23. #include <fstream>
  24. #include <istream>
  25. #include <gmock/gmock.h>
  26. namespace hdfs {
  27. template <typename T, typename U>
  28. void simpleConfigStreamProperty(std::stringstream& out, T key, U value) {
  29. out << "<property>"
  30. << "<name>" << key << "</name>"
  31. << "<value>" << value << "</value>"
  32. << "</property>";
  33. }
  34. template <typename T, typename U, typename... Args>
  35. void simpleConfigStreamProperty(std::stringstream& out, T key, U value,
  36. Args... args) {
  37. simpleConfigStreamProperty(out, key, value);
  38. simpleConfigStreamProperty(out, args...);
  39. }
  40. template <typename... Args>
  41. void simpleConfigStream(std::stringstream& out, Args... args) {
  42. out << "<configuration>";
  43. simpleConfigStreamProperty(out, args...);
  44. out << "</configuration>";
  45. }
  46. template <typename... Args>
  47. optional<Configuration> simpleConfig(Args... args) {
  48. std::stringstream stream;
  49. simpleConfigStream(stream, args...);
  50. optional<Configuration> parse = ConfigurationLoader().Load<Configuration>(stream.str());
  51. EXPECT_TRUE((bool)parse);
  52. return parse;
  53. }
  54. template <typename... Args>
  55. void writeSimpleConfig(const std::string& filename, Args... args) {
  56. std::stringstream stream;
  57. simpleConfigStream(stream, args...);
  58. std::ofstream out;
  59. out.open(filename);
  60. out << stream.rdbuf();
  61. }
  62. }
  63. #endif