LoggerTest.cpp 7.4 KB

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