LoggerTest.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2022-11-09
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_io.hpp>
  12. #include "TestHelper.hpp"
  13. using namespace ls::std::core;
  14. using namespace ls::std::core::interface_type;
  15. using namespace ls::std::io;
  16. using namespace ::std;
  17. using namespace ls_std_test;
  18. namespace
  19. {
  20. class LoggerTest : public ::testing::Test
  21. {
  22. protected:
  23. LoggerTest() = default;
  24. ~LoggerTest() override = default;
  25. void SetUp() override
  26. {}
  27. void TearDown() override
  28. {}
  29. static shared_ptr<IWriter> createFileLogger(const string &_logName)
  30. {
  31. string path = TestHelper::getResourcesFolderLocation() + _logName;
  32. File file{path};
  33. if (!file.exists())
  34. {
  35. file.createNewFile();
  36. }
  37. shared_ptr<IWriter> writer = dynamic_pointer_cast<IWriter>(make_shared<FileOutputStream>(file));
  38. return writer;
  39. }
  40. static string getContentFromLogFile(const string &_logName)
  41. {
  42. File file{TestHelper::getResourcesFolderLocation() + _logName};
  43. FileReader reader{file};
  44. string content{reader.read()};
  45. file.remove();
  46. return content;
  47. }
  48. };
  49. TEST_F(LoggerTest, constructor_no_writer_reference)
  50. {
  51. EXPECT_THROW({
  52. try
  53. {
  54. Logger logger{nullptr};
  55. }
  56. catch (const IllegalArgumentException &_exception)
  57. {
  58. throw;
  59. }
  60. }, IllegalArgumentException);
  61. }
  62. TEST_F(LoggerTest, debug)
  63. {
  64. // write to log file
  65. string logName = "output_debug.log";
  66. shared_ptr<IWriter> writer = createFileLogger(logName);
  67. Logger logger{writer};
  68. logger.setLogLevel(LogLevelValue::DEBUG);
  69. logger.debug("1. line!");
  70. logger.info("2. line!");
  71. logger.error("3. line!");
  72. logger.fatal("4. line!");
  73. logger.warn("5. line!");
  74. logger.trace("6. line!");
  75. // get content and check
  76. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  77. string content = getContentFromLogFile(logName);
  78. ASSERT_TRUE(content.find("1. line!") != string::npos);
  79. ASSERT_TRUE(content.find("2. line!") != string::npos);
  80. ASSERT_TRUE(content.find("3. line!") != string::npos);
  81. ASSERT_TRUE(content.find("4. line!") != string::npos);
  82. ASSERT_TRUE(content.find("5. line!") != string::npos);
  83. ASSERT_FALSE(content.find("6. line!") != string::npos);
  84. }
  85. TEST_F(LoggerTest, error)
  86. {
  87. // write to log file
  88. string logName = "output_error.log";
  89. shared_ptr<IWriter> writer = createFileLogger(logName);
  90. Logger logger{writer};
  91. logger.setLogLevel(LogLevelValue::ERR);
  92. logger.debug("1. line!");
  93. logger.info("2. line!");
  94. logger.error("3. line!");
  95. logger.fatal("4. line!");
  96. logger.warn("5. line!");
  97. logger.trace("6. line!");
  98. // get content and check
  99. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  100. string content = getContentFromLogFile(logName);
  101. ASSERT_FALSE(content.find("1. line!") != string::npos);
  102. ASSERT_FALSE(content.find("2. line!") != string::npos);
  103. ASSERT_TRUE(content.find("3. line!") != string::npos);
  104. ASSERT_TRUE(content.find("4. line!") != string::npos);
  105. ASSERT_FALSE(content.find("5. line!") != string::npos);
  106. ASSERT_FALSE(content.find("6. line!") != string::npos);
  107. }
  108. TEST_F(LoggerTest, fatal)
  109. {
  110. // write to log file
  111. string logName = "output_fatal.log";
  112. shared_ptr<IWriter> writer = createFileLogger(logName);
  113. Logger logger{writer};
  114. logger.setLogLevel(LogLevelValue::FATAL);
  115. logger.debug("1. line!");
  116. logger.info("2. line!");
  117. logger.error("3. line!");
  118. logger.fatal("4. line!");
  119. logger.warn("5. line!");
  120. logger.trace("6. line!");
  121. // get content and check
  122. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  123. string content = getContentFromLogFile(logName);
  124. ASSERT_FALSE(content.find("1. line!") != string::npos);
  125. ASSERT_FALSE(content.find("2. line!") != string::npos);
  126. ASSERT_FALSE(content.find("3. line!") != string::npos);
  127. ASSERT_TRUE(content.find("4. line!") != string::npos);
  128. ASSERT_FALSE(content.find("5. line!") != string::npos);
  129. ASSERT_FALSE(content.find("6. line!") != string::npos);
  130. }
  131. TEST_F(LoggerTest, getLogLevel)
  132. {
  133. Logger logger{createFileLogger("output.log")};
  134. ASSERT_EQ(LogLevelValue::INFO, logger.getLogLevel());
  135. }
  136. TEST_F(LoggerTest, info)
  137. {
  138. // write to log file
  139. string logName = "output_info.log";
  140. shared_ptr<IWriter> writer = createFileLogger(logName);
  141. Logger logger{writer};
  142. logger.setLogLevel(LogLevelValue::INFO);
  143. logger.fatal("1. line!");
  144. logger.error("2. line!");
  145. logger.warn("3. line!");
  146. logger.info("4. line!");
  147. logger.debug("5. line!");
  148. logger.trace("6. line!");
  149. // get content and check
  150. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  151. string content = getContentFromLogFile(logName);
  152. ASSERT_TRUE(content.find("1. line!") != string::npos);
  153. ASSERT_TRUE(content.find("2. line!") != string::npos);
  154. ASSERT_TRUE(content.find("3. line!") != string::npos);
  155. ASSERT_TRUE(content.find("4. line!") != string::npos);
  156. ASSERT_FALSE(content.find("5. line!") != string::npos);
  157. ASSERT_FALSE(content.find("6. line!") != string::npos);
  158. }
  159. TEST_F(LoggerTest, setLogLevel)
  160. {
  161. Logger logger{createFileLogger("output.log")};
  162. logger.setLogLevel(LogLevelValue::ERR);
  163. ASSERT_EQ(LogLevelValue::ERR, logger.getLogLevel());
  164. }
  165. TEST_F(LoggerTest, trace)
  166. {
  167. // write to log file
  168. string logName = "output_trace.log";
  169. shared_ptr<IWriter> writer = createFileLogger(logName);
  170. Logger logger{writer};
  171. logger.setLogLevel(LogLevelValue::TRACE);
  172. logger.fatal("1. line!");
  173. logger.error("2. line!");
  174. logger.warn("3. line!");
  175. logger.info("4. line!");
  176. logger.debug("5. line!");
  177. logger.trace("6. line!");
  178. // get content and check
  179. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  180. string content = getContentFromLogFile(logName);
  181. ASSERT_TRUE(content.find("1. line!") != string::npos);
  182. ASSERT_TRUE(content.find("2. line!") != string::npos);
  183. ASSERT_TRUE(content.find("3. line!") != string::npos);
  184. ASSERT_TRUE(content.find("4. line!") != string::npos);
  185. ASSERT_TRUE(content.find("5. line!") != string::npos);
  186. ASSERT_TRUE(content.find("6. line!") != string::npos);
  187. }
  188. TEST_F(LoggerTest, warn)
  189. {
  190. // write to log file
  191. string logName = "output_warn.log";
  192. shared_ptr<IWriter> writer = createFileLogger(logName);
  193. Logger logger{writer};
  194. logger.setLogLevel(LogLevelValue::WARN);
  195. logger.fatal("1. line!");
  196. logger.error("2. line!");
  197. logger.warn("3. line!");
  198. logger.info("4. line!");
  199. logger.debug("5. line!");
  200. logger.trace("6. line!");
  201. // get content and check
  202. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  203. string content = getContentFromLogFile(logName);
  204. ASSERT_TRUE(content.find("1. line!") != string::npos);
  205. ASSERT_TRUE(content.find("2. line!") != string::npos);
  206. ASSERT_TRUE(content.find("3. line!") != string::npos);
  207. ASSERT_FALSE(content.find("4. line!") != string::npos);
  208. ASSERT_FALSE(content.find("5. line!") != string::npos);
  209. ASSERT_FALSE(content.find("6. line!") != string::npos);
  210. }
  211. }