|
4 | 4 |
|
5 | 5 | #include "log4cpp/log4cpp.hpp" |
6 | 6 |
|
7 | | -#ifdef _MSC_VER |
8 | | -// clang-format off |
9 | | -#include <winsock2.h> |
10 | | -#include <windows.h> |
11 | | -// clang-format on |
12 | | - |
13 | | -#endif |
14 | | - |
15 | 7 | #include <gtest/gtest.h> |
16 | 8 |
|
17 | 9 | #include "config/log4cpp.hpp" |
18 | 10 |
|
| 11 | +namespace fs = std::filesystem; |
| 12 | + |
19 | 13 | void parse_json(const std::string &config_file, nlohmann::json &expected_json) { |
20 | 14 | std::ifstream ifs(config_file); |
21 | | - ASSERT_EQ(ifs.is_open(), true); |
| 15 | + ASSERT_TRUE(ifs.is_open()); |
22 | 16 | expected_json = nlohmann::json::parse(ifs); |
23 | 17 | ifs.close(); |
24 | 18 | } |
25 | 19 |
|
26 | | -TEST(configuration_serialize_test, log4cpp_config_serialize_test) { |
27 | | - // Just to load the configuration file |
| 20 | +TEST(configuration_serialize_test, log4cpp_config_roundtrip_test) { |
28 | 21 | auto &log_mgr = log4cpp::supervisor::get_logger_manager(); |
29 | | - ASSERT_NO_THROW(log_mgr.load_config("serialize_test.json")); |
30 | | - std::shared_ptr<log4cpp::logger> logger = log4cpp::logger_manager::get_logger("console_logger"); |
31 | | - const log4cpp::config::log4cpp *config = log_mgr.get_config(); |
32 | | - const std::string actual_json_str = log4cpp::config::log4cpp::serialize(*config); |
33 | | - nlohmann::json expected_json; |
34 | | - parse_json("serialize_test.json", expected_json); |
35 | | - const nlohmann::json actual_json = nlohmann::json::parse(actual_json_str); |
36 | | - EXPECT_EQ(expected_json, actual_json); |
| 22 | + |
| 23 | + for (const auto &entry: fs::directory_iterator(fs::current_path())) { |
| 24 | + if (entry.is_regular_file() && entry.path().extension() == ".json") { |
| 25 | + const std::string filename = entry.path().string(); |
| 26 | + |
| 27 | + // Load original config |
| 28 | + ASSERT_NO_THROW(log_mgr.load_config(filename)); |
| 29 | + const log4cpp::config::log4cpp original_config = *log_mgr.get_config(); |
| 30 | + |
| 31 | + // Serialize to JSON string |
| 32 | + const std::string json_str = log4cpp::config::log4cpp::serialize(original_config); |
| 33 | + |
| 34 | + // Write to a temporary file |
| 35 | + const std::string tmpfile = (fs::temp_directory_path() / entry.path().filename()).string(); |
| 36 | + { |
| 37 | + std::ofstream ofs(tmpfile); |
| 38 | + ofs << json_str; |
| 39 | + } |
| 40 | + |
| 41 | + // Load config back from the temporary file |
| 42 | + ASSERT_NO_THROW(log_mgr.load_config(tmpfile)); |
| 43 | + const log4cpp::config::log4cpp roundtrip_config = *log_mgr.get_config(); |
| 44 | + |
| 45 | + // Compare the two config objects (you may need to implement operator==) |
| 46 | + EXPECT_EQ(original_config, roundtrip_config) << "Roundtrip mismatch in file: " << filename; |
| 47 | + } |
| 48 | + } |
37 | 49 | } |
0 commit comments