LoggerTest.cpp 7.6 KB

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