Browse Source

[trac976] Ensure unit tests reset back to original logging state

Stephen Morris 14 years ago
parent
commit
dd46abf659

+ 1 - 10
src/lib/log/tests/logger_level_unittest.cc

@@ -26,14 +26,10 @@ using namespace isc;
 using namespace isc::log;
 using namespace std;
 
-namespace {
-string ROOT_NAME("logleveltest");
-}
-
 class LoggerLevelTest : public ::testing::Test {
 protected:
     LoggerLevelTest() {
-        LoggerManager::init(ROOT_NAME);
+        // Logger initialization is done in main()
     }
     ~LoggerLevelTest() {
         LoggerManager::reset();
@@ -62,11 +58,6 @@ TEST_F(LoggerLevelTest, Creation) {
 }
 
 TEST(LoggerLevel, getSeverity) {
-    // Should initialize logger as getSeverity() may output
-    // a message.  This gives a properly-qualified logger
-    // name.
-    LoggerManager::init(ROOT_NAME);
-
     EXPECT_EQ(DEBUG, getSeverity("DEBUG"));
     EXPECT_EQ(DEBUG, getSeverity("debug"));
     EXPECT_EQ(DEBUG, getSeverity("DeBuG"));

+ 1 - 5
src/lib/log/tests/logger_manager_unittest.cc

@@ -40,15 +40,11 @@ using namespace isc;
 using namespace isc::log;
 using namespace std;
 
-namespace {
-string ROOT_NAME("logmgrtest");
-}
-
 /// \brief LoggerManager Test
 class LoggerManagerTest : public ::testing::Test {
 public:
     LoggerManagerTest() {
-        LoggerManager::init(ROOT_NAME);
+        // Initialization of logging is done in main()
     }
 
     ~LoggerManagerTest() {

+ 23 - 2
src/lib/log/tests/logger_name_unittest.cc

@@ -21,9 +21,30 @@
 using namespace isc;
 using namespace isc::log;
 
+// Test class.  To avoid disturbing the root logger configuration in other
+// tests in the suite, the root logger name is saved in the constructor and
+// restored in the destructor.  However, this is a bit chicken and egg, as the
+// functions used to do the save and restore are those being tested...
+//
+// Note that the root name is originally set by the initialization of the
+// logging configuration done in main().
+
+class LoggerNameTest : public ::testing::Test {
+public:
+    LoggerNameTest() {
+        name_ = getRootLoggerName();
+    }
+    ~LoggerNameTest() {
+        setRootLoggerName(name_);
+    }
+
+private:
+    std::string     name_;  ///< Saved name
+};
+
 // Check setting and getting of root name
 
-TEST(LoggerNameTest, RootNameSetGet) {
+TEST_F(LoggerNameTest, RootNameSetGet) {
     const std::string name1 = "test1";
     const std::string name2 = "test2";
 
@@ -44,7 +65,7 @@ TEST(LoggerNameTest, RootNameSetGet) {
 
 // Check expansion of name
 
-TEST(LoggerNameTest, ExpandLoggerName) {
+TEST_F(LoggerNameTest, ExpandLoggerName) {
     const std::string ROOT = "example";
     const std::string NAME = "something";
     const std::string FULL_NAME = ROOT + "." + NAME;

+ 2 - 6
src/lib/log/tests/logger_unittest.cc

@@ -26,10 +26,6 @@ using namespace isc;
 using namespace isc::log;
 using namespace std;
 
-namespace {
-string ROOT_NAME = "loggertest";
-}
-
 /// \brief Logger Test
 ///
 /// As the logger is only a shell around the implementation, this tests also
@@ -38,7 +34,7 @@ string ROOT_NAME = "loggertest";
 class LoggerTest : public ::testing::Test {
 public:
     LoggerTest() {
-        LoggerManager::init(ROOT_NAME);
+        // Initialization of logging is done in main()
     }
     ~LoggerTest() {
         LoggerManager::reset();
@@ -54,7 +50,7 @@ TEST_F(LoggerTest, Name) {
     Logger logger("alpha");
 
     // ... and check the name
-    EXPECT_EQ(ROOT_NAME + string(".alpha"), logger.getName());
+    EXPECT_EQ(getRootLoggerName() + string(".alpha"), logger.getName());
 }
 
 // This test attempts to get two instances of a logger with the same name