LoggerTest.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2023-02-06
  7. *
  8. * */
  9. #include <classes/TestHelper.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.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. {
  53. try
  54. {
  55. Logger logger{nullptr};
  56. }
  57. catch (const IllegalArgumentException &_exception)
  58. {
  59. throw;
  60. }
  61. },
  62. IllegalArgumentException);
  63. }
  64. TEST_F(LoggerTest, debug)
  65. {
  66. // write to log file
  67. string logName = "output-debug.log";
  68. shared_ptr<IWriter> writer = createFileLogger(logName);
  69. Logger logger{writer};
  70. logger.setLogLevel(LogLevelValue::DEBUG);
  71. logger.debug("1. line!");
  72. logger.info("2. line!");
  73. logger.error("3. line!");
  74. logger.fatal("4. line!");
  75. logger.warn("5. line!");
  76. logger.trace("6. line!");
  77. // get content and check
  78. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  79. string content = getContentFromLogFile(logName);
  80. ASSERT_TRUE(content.find("1. line!") != string::npos);
  81. ASSERT_TRUE(content.find("2. line!") != string::npos);
  82. ASSERT_TRUE(content.find("3. line!") != string::npos);
  83. ASSERT_TRUE(content.find("4. line!") != string::npos);
  84. ASSERT_TRUE(content.find("5. line!") != string::npos);
  85. ASSERT_FALSE(content.find("6. line!") != string::npos);
  86. }
  87. TEST_F(LoggerTest, error)
  88. {
  89. // write to log file
  90. string logName = "output-error.log";
  91. shared_ptr<IWriter> writer = createFileLogger(logName);
  92. Logger logger{writer};
  93. logger.setLogLevel(LogLevelValue::ERR);
  94. logger.debug("1. line!");
  95. logger.info("2. line!");
  96. logger.error("3. line!");
  97. logger.fatal("4. line!");
  98. logger.warn("5. line!");
  99. logger.trace("6. line!");
  100. // get content and check
  101. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  102. string content = getContentFromLogFile(logName);
  103. ASSERT_FALSE(content.find("1. line!") != string::npos);
  104. ASSERT_FALSE(content.find("2. line!") != string::npos);
  105. ASSERT_TRUE(content.find("3. line!") != string::npos);
  106. ASSERT_TRUE(content.find("4. line!") != string::npos);
  107. ASSERT_FALSE(content.find("5. line!") != string::npos);
  108. ASSERT_FALSE(content.find("6. line!") != string::npos);
  109. }
  110. TEST_F(LoggerTest, fatal)
  111. {
  112. // write to log file
  113. string logName = "output-fatal.log";
  114. shared_ptr<IWriter> writer = createFileLogger(logName);
  115. Logger logger{writer};
  116. logger.setLogLevel(LogLevelValue::FATAL);
  117. logger.debug("1. line!");
  118. logger.info("2. line!");
  119. logger.error("3. line!");
  120. logger.fatal("4. line!");
  121. logger.warn("5. line!");
  122. logger.trace("6. line!");
  123. // get content and check
  124. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  125. string content = getContentFromLogFile(logName);
  126. ASSERT_FALSE(content.find("1. line!") != string::npos);
  127. ASSERT_FALSE(content.find("2. line!") != string::npos);
  128. ASSERT_FALSE(content.find("3. line!") != string::npos);
  129. ASSERT_TRUE(content.find("4. line!") != string::npos);
  130. ASSERT_FALSE(content.find("5. line!") != string::npos);
  131. ASSERT_FALSE(content.find("6. line!") != string::npos);
  132. }
  133. TEST_F(LoggerTest, getLogLevel)
  134. {
  135. Logger logger{createFileLogger("output.log")};
  136. ASSERT_EQ(LogLevelValue::INFO, logger.getLogLevel());
  137. }
  138. TEST_F(LoggerTest, info)
  139. {
  140. // write to log file
  141. string logName = "output-info.log";
  142. shared_ptr<IWriter> writer = createFileLogger(logName);
  143. Logger logger{writer};
  144. logger.setLogLevel(LogLevelValue::INFO);
  145. logger.fatal("1. line!");
  146. logger.error("2. line!");
  147. logger.warn("3. line!");
  148. logger.info("4. line!");
  149. logger.debug("5. line!");
  150. logger.trace("6. line!");
  151. // get content and check
  152. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  153. string content = getContentFromLogFile(logName);
  154. ASSERT_TRUE(content.find("1. line!") != string::npos);
  155. ASSERT_TRUE(content.find("2. line!") != string::npos);
  156. ASSERT_TRUE(content.find("3. line!") != string::npos);
  157. ASSERT_TRUE(content.find("4. line!") != string::npos);
  158. ASSERT_FALSE(content.find("5. line!") != string::npos);
  159. ASSERT_FALSE(content.find("6. line!") != string::npos);
  160. }
  161. TEST_F(LoggerTest, setLogLevel)
  162. {
  163. Logger logger{createFileLogger("output.log")};
  164. logger.setLogLevel(LogLevelValue::ERR);
  165. ASSERT_EQ(LogLevelValue::ERR, logger.getLogLevel());
  166. }
  167. TEST_F(LoggerTest, trace)
  168. {
  169. // write to log file
  170. string logName = "output-trace.log";
  171. shared_ptr<IWriter> writer = createFileLogger(logName);
  172. Logger logger{writer};
  173. logger.setLogLevel(LogLevelValue::TRACE);
  174. logger.fatal("1. line!");
  175. logger.error("2. line!");
  176. logger.warn("3. line!");
  177. logger.info("4. line!");
  178. logger.debug("5. line!");
  179. logger.trace("6. line!");
  180. // get content and check
  181. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  182. string content = getContentFromLogFile(logName);
  183. ASSERT_TRUE(content.find("1. line!") != string::npos);
  184. ASSERT_TRUE(content.find("2. line!") != string::npos);
  185. ASSERT_TRUE(content.find("3. line!") != string::npos);
  186. ASSERT_TRUE(content.find("4. line!") != string::npos);
  187. ASSERT_TRUE(content.find("5. line!") != string::npos);
  188. ASSERT_TRUE(content.find("6. line!") != string::npos);
  189. }
  190. TEST_F(LoggerTest, warn)
  191. {
  192. // write to log file
  193. string logName = "output-warn.log";
  194. shared_ptr<IWriter> writer = createFileLogger(logName);
  195. Logger logger{writer};
  196. logger.setLogLevel(LogLevelValue::WARN);
  197. logger.fatal("1. line!");
  198. logger.error("2. line!");
  199. logger.warn("3. line!");
  200. logger.info("4. line!");
  201. logger.debug("5. line!");
  202. logger.trace("6. line!");
  203. // get content and check
  204. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  205. string content = getContentFromLogFile(logName);
  206. ASSERT_TRUE(content.find("1. line!") != string::npos);
  207. ASSERT_TRUE(content.find("2. line!") != string::npos);
  208. ASSERT_TRUE(content.find("3. line!") != string::npos);
  209. ASSERT_FALSE(content.find("4. line!") != string::npos);
  210. ASSERT_FALSE(content.find("5. line!") != string::npos);
  211. ASSERT_FALSE(content.find("6. line!") != string::npos);
  212. }
  213. }