LoggerTest.cpp 7.5 KB

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