Add 'log4j2' domain tests to the Log4j 2.x agent
[lttng-ust-java-tests.git] / lttng-ust-java-tests-log4j / src / test / java / org / lttng / ust / agent / integration / events / Log4jLegacyApiIT.java
index 4cfff7b434ad61e95028b0c59d5b514f37ec1878..9cece276040de56af6bdaa965504cd9139bd0c8d 100644 (file)
 
 package org.lttng.ust.agent.integration.events;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
 
 import java.lang.reflect.Field;
+import java.util.Arrays;
 import java.util.List;
 
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.lttng.tools.ILttngSession;
 import org.lttng.tools.ILttngSession.Domain;
 import org.lttng.ust.agent.ILttngHandler;
 import org.lttng.ust.agent.LTTngAgent;
 import org.lttng.ust.agent.utils.Log4jTestUtils;
-import org.lttng.ust.agent.utils.TestPrintRunner;
+import org.lttng.ust.agent.utils.TestPrintExtension;
 
 /**
  * Enabled events test for the LTTng-UST Log4j log handler, using the legacy
  * API.
  */
-@RunWith(TestPrintRunner.class)
+@ExtendWith(TestPrintExtension.class)
 @SuppressWarnings("deprecation")
+@Tag("agent:log4j")
+@Tag("domain:log4j")
 public class Log4jLegacyApiIT {
 
     private static final Domain DOMAIN = Domain.LOG4J;
@@ -55,6 +59,7 @@ public class Log4jLegacyApiIT {
     private static final String EVENT_NAME_B = "EventB";
 
     private ILttngSession session;
+    private LTTngAgent agent;
 
     private Logger loggerA;
     private Logger loggerB;
@@ -62,7 +67,7 @@ public class Log4jLegacyApiIT {
     /**
      * Class setup
      */
-    @BeforeClass
+    @BeforeAll
     public static void log4jClassSetup() {
         Log4jTestUtils.testClassSetup();
     }
@@ -70,7 +75,7 @@ public class Log4jLegacyApiIT {
     /**
      * Class cleanup
      */
-    @AfterClass
+    @AfterAll
     public static void log4jClassCleanup() {
         Log4jTestUtils.testClassCleanup();
     }
@@ -78,10 +83,10 @@ public class Log4jLegacyApiIT {
     /**
      * Test setup
      */
-    @Before
+    @BeforeEach
     public void setup() {
         loggerA = Logger.getLogger(EVENT_NAME_A);
-        LTTngAgent.getLTTngAgent();
+        agent = LTTngAgent.getLTTngAgent();
         loggerB = Logger.getLogger(EVENT_NAME_B);
 
         loggerA.setLevel(Level.ALL);
@@ -93,11 +98,11 @@ public class Log4jLegacyApiIT {
     /**
      * Test cleanup
      */
-    @After
+    @AfterEach
     public void tearDown() {
         session.close();
 
-        LTTngAgent.dispose();
+        agent.dispose();
 
         loggerA = null;
         loggerB = null;
@@ -165,6 +170,26 @@ public class Log4jLegacyApiIT {
         assertEquals(10, handler.getEventCount());
     }
 
+    /**
+     * Test that the "lttng list" commands lists the expected events.
+     */
+    @Test
+    public void testListEvents() {
+        List<String> enabledEvents = session.listEvents();
+        List<String> expectedEvents = Arrays.asList(EVENT_NAME_A, EVENT_NAME_B);
+
+        /*
+         * It doesn't seem possible to forcibly remove Loggers with log4j 1.2.
+         * This, coupled with the way the legacy agent works, makes it so
+         * loggers defined in other tests will always "leak" into this one when
+         * running the whole test suite.
+         *
+         * For this test, simply check that the expected names are present, and
+         * let pass the case where other loggers may the present too.
+         */
+        expectedEvents.forEach(event -> assertTrue(enabledEvents.contains(event)));
+    }
+
     /**
      * Get the singleton Log4j Handler currently managed by the LTTngAgent. It
      * is not public, so we need reflection to access it.
This page took 0.024179 seconds and 4 git commands to generate.