123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-20
- * Changed: 2023-03-25
- *
- * */
- #include <classes/TestHelper.hpp>
- #include <gtest/gtest.h>
- #include <ls-std/ls-std-core.hpp>
- #include <ls-std/ls-std-io.hpp>
- using ls::std::core::IllegalArgumentException;
- using ls::std::core::interface_type::IWriter;
- using ls::std::io::File;
- using ls::std::io::FileOutputStream;
- using ls::std::io::FileReader;
- using ls::std::io::Logger;
- using ls::std::io::LogLevelValue;
- using ls::std::test::TestHelper;
- using std::dynamic_pointer_cast;
- using std::make_shared;
- using std::shared_ptr;
- using std::string;
- using testing::Test;
- namespace
- {
- class LoggerTest : public Test
- {
- public:
- LoggerTest() = default;
- ~LoggerTest() override = default;
- static shared_ptr<IWriter> createFileLogger(const string &_logName)
- {
- string path = TestHelper::getResourcesFolderLocation() + _logName;
- File file{path};
- if (!file.exists())
- {
- file.createNewFile();
- }
- shared_ptr<IWriter> writer = make_shared<FileOutputStream>(file);
- return writer;
- }
- static string getContentFromLogFile(const string &_logName)
- {
- File file{TestHelper::getResourcesFolderLocation() + _logName};
- FileReader reader{file};
- string content{reader.read()};
- file.remove();
- return content;
- }
- };
- TEST_F(LoggerTest, constructor_no_writer_reference)
- {
- EXPECT_THROW(
- {
- try
- {
- Logger logger{nullptr};
- }
- catch (const IllegalArgumentException &_exception)
- {
- throw;
- }
- },
- IllegalArgumentException);
- }
- TEST_F(LoggerTest, debug)
- {
- // write to log file
- string logName = "output-debug.log";
- shared_ptr<IWriter> writer = createFileLogger(logName);
- Logger logger{writer};
- logger.setLogLevel(LogLevelValue::DEBUG);
- logger.debug("1. line!");
- logger.info("2. line!");
- logger.error("3. line!");
- logger.fatal("4. line!");
- logger.warn("5. line!");
- logger.trace("6. line!");
- // get content and check
- dynamic_pointer_cast<FileOutputStream>(writer)->close();
- string content = getContentFromLogFile(logName);
- ASSERT_TRUE(content.find("1. line!") != string::npos);
- ASSERT_TRUE(content.find("2. line!") != string::npos);
- ASSERT_TRUE(content.find("3. line!") != string::npos);
- ASSERT_TRUE(content.find("4. line!") != string::npos);
- ASSERT_TRUE(content.find("5. line!") != string::npos);
- ASSERT_FALSE(content.find("6. line!") != string::npos);
- }
- TEST_F(LoggerTest, error)
- {
- // write to log file
- string logName = "output-error.log";
- shared_ptr<IWriter> writer = createFileLogger(logName);
- Logger logger{writer};
- logger.setLogLevel(LogLevelValue::ERR);
- logger.debug("1. line!");
- logger.info("2. line!");
- logger.error("3. line!");
- logger.fatal("4. line!");
- logger.warn("5. line!");
- logger.trace("6. line!");
- // get content and check
- dynamic_pointer_cast<FileOutputStream>(writer)->close();
- string content = getContentFromLogFile(logName);
- ASSERT_FALSE(content.find("1. line!") != string::npos);
- ASSERT_FALSE(content.find("2. line!") != string::npos);
- ASSERT_TRUE(content.find("3. line!") != string::npos);
- ASSERT_TRUE(content.find("4. line!") != string::npos);
- ASSERT_FALSE(content.find("5. line!") != string::npos);
- ASSERT_FALSE(content.find("6. line!") != string::npos);
- }
- TEST_F(LoggerTest, fatal)
- {
- // write to log file
- string logName = "output-fatal.log";
- shared_ptr<IWriter> writer = createFileLogger(logName);
- Logger logger{writer};
- logger.setLogLevel(LogLevelValue::FATAL);
- logger.debug("1. line!");
- logger.info("2. line!");
- logger.error("3. line!");
- logger.fatal("4. line!");
- logger.warn("5. line!");
- logger.trace("6. line!");
- // get content and check
- dynamic_pointer_cast<FileOutputStream>(writer)->close();
- string content = getContentFromLogFile(logName);
- ASSERT_FALSE(content.find("1. line!") != string::npos);
- ASSERT_FALSE(content.find("2. line!") != string::npos);
- ASSERT_FALSE(content.find("3. line!") != string::npos);
- ASSERT_TRUE(content.find("4. line!") != string::npos);
- ASSERT_FALSE(content.find("5. line!") != string::npos);
- ASSERT_FALSE(content.find("6. line!") != string::npos);
- }
- TEST_F(LoggerTest, getLogLevel)
- {
- Logger logger{createFileLogger("output.log")};
- ASSERT_EQ(LogLevelValue::INFO, logger.getLogLevel().getValue());
- }
- TEST_F(LoggerTest, info)
- {
- // write to log file
- string logName = "output-info.log";
- shared_ptr<IWriter> writer = createFileLogger(logName);
- Logger logger{writer};
- logger.setLogLevel(LogLevelValue::INFO);
- logger.fatal("1. line!");
- logger.error("2. line!");
- logger.warn("3. line!");
- logger.info("4. line!");
- logger.debug("5. line!");
- logger.trace("6. line!");
- // get content and check
- dynamic_pointer_cast<FileOutputStream>(writer)->close();
- string content = getContentFromLogFile(logName);
- ASSERT_TRUE(content.find("1. line!") != string::npos);
- ASSERT_TRUE(content.find("2. line!") != string::npos);
- ASSERT_TRUE(content.find("3. line!") != string::npos);
- ASSERT_TRUE(content.find("4. line!") != string::npos);
- ASSERT_FALSE(content.find("5. line!") != string::npos);
- ASSERT_FALSE(content.find("6. line!") != string::npos);
- }
- TEST_F(LoggerTest, setLogLevel)
- {
- Logger logger{createFileLogger("output.log")};
- logger.setLogLevel(LogLevelValue::ERR);
- ASSERT_EQ(LogLevelValue::ERR, logger.getLogLevel().getValue());
- }
- TEST_F(LoggerTest, trace)
- {
- // write to log file
- string logName = "output-trace.log";
- shared_ptr<IWriter> writer = createFileLogger(logName);
- Logger logger{writer};
- logger.setLogLevel(LogLevelValue::TRACE);
- logger.fatal("1. line!");
- logger.error("2. line!");
- logger.warn("3. line!");
- logger.info("4. line!");
- logger.debug("5. line!");
- logger.trace("6. line!");
- // get content and check
- dynamic_pointer_cast<FileOutputStream>(writer)->close();
- string content = getContentFromLogFile(logName);
- ASSERT_TRUE(content.find("1. line!") != string::npos);
- ASSERT_TRUE(content.find("2. line!") != string::npos);
- ASSERT_TRUE(content.find("3. line!") != string::npos);
- ASSERT_TRUE(content.find("4. line!") != string::npos);
- ASSERT_TRUE(content.find("5. line!") != string::npos);
- ASSERT_TRUE(content.find("6. line!") != string::npos);
- }
- TEST_F(LoggerTest, warn)
- {
- // write to log file
- string logName = "output-warn.log";
- shared_ptr<IWriter> writer = createFileLogger(logName);
- Logger logger{writer};
- logger.setLogLevel(LogLevelValue::WARN);
- logger.fatal("1. line!");
- logger.error("2. line!");
- logger.warn("3. line!");
- logger.info("4. line!");
- logger.debug("5. line!");
- logger.trace("6. line!");
- // get content and check
- dynamic_pointer_cast<FileOutputStream>(writer)->close();
- string content = getContentFromLogFile(logName);
- ASSERT_TRUE(content.find("1. line!") != string::npos);
- ASSERT_TRUE(content.find("2. line!") != string::npos);
- ASSERT_TRUE(content.find("3. line!") != string::npos);
- ASSERT_FALSE(content.find("4. line!") != string::npos);
- ASSERT_FALSE(content.find("5. line!") != string::npos);
- ASSERT_FALSE(content.find("6. line!") != string::npos);
- }
- }
|