Migrate to Junit 5 Jupiter
[lttng-ust-java-tests.git] / lttng-ust-java-tests-common / src / main / java / org / lttng / ust / agent / integration / context / AppContextITBase.java
index 721b6192db5a5d79e92b978c0f1566a3a93bc4df..acf19bdb79fec6c7fcef41c92433bc3e0c8922d5 100644 (file)
 
 package org.lttng.ust.agent.integration.context;
 
-import static org.junit.Assert.assertFalse;
-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.assertFalse;
+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.io.IOException;
 import java.util.List;
 
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+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.context.ContextInfoManager;
-import org.lttng.ust.agent.utils.TestPrintRunner;
+import org.lttng.ust.agent.utils.TestPrintExtension;
 
 /**
  * Base abstract class to implement all sorts of integration tests verifying the
  * presence of enabled application contexts in resulting traces.
  */
-@RunWith(TestPrintRunner.class)
+@ExtendWith(TestPrintExtension.class)
 public abstract class AppContextITBase {
 
     protected static final String EVENT_NAME = "EventName";
 
     protected static final String RETRIEVER_NAME_1 = "Retriever1";
-    protected static final String RETRIEVER_NAME_2 = "Retriever2";
+    protected static final String RETRIEVER_NAME_2 = "some.retriever_2";
 
     private static final String CONTEXT_NAME = ContextInfoRetrieverStubs.CONTEXT_NAME;
 
@@ -58,18 +58,20 @@ public abstract class AppContextITBase {
 
     protected abstract Domain getDomain();
 
+    protected abstract boolean closeHandlers();
+
     protected abstract void sendEventsToLoggers();
 
     /**
      * Base test setup
      */
-    @Before
+    @BeforeEach
     public void testSetup() {
         try {
             cim = ContextInfoManager.getInstance();
         } catch (SecurityException | IOException e) {
             /* The native library is not available! */
-            fail();
+            fail(e.getMessage());
         }
         session = ILttngSession.createSession(null, getDomain());
     }
@@ -77,11 +79,13 @@ public abstract class AppContextITBase {
     /**
      * Base test teardown
      */
-    @After
+    @AfterEach
     public void testTeardown() {
         session.close();
 
-        logHandler.close();
+        if (closeHandlers()) {
+            logHandler.close();
+        }
         logHandler = null;
 
         /* In case some tests fail or forget to unregister their retrievers */
@@ -98,7 +102,10 @@ public abstract class AppContextITBase {
      * trace output.
      */
     private static void testContextPresentInTrace(List<String> traceOutput, String retrieverName, String contextName, String contextValue) {
-        String fullString = "_app_" + retrieverName + "_" + contextName + " = " + contextValue;
+        String traceRetrieverName = convertToNameInTrace(retrieverName);
+        String traceContextName = convertToNameInTrace(contextName);
+
+        String fullString = "_app_" + traceRetrieverName + "_" + traceContextName + " = " + contextValue;
         traceOutput.forEach(line -> assertTrue(line.contains(fullString)));
     }
 
@@ -107,10 +114,21 @@ public abstract class AppContextITBase {
      * trace output
      */
     private static void testContextNotPresentInTrace(List<String> traceOutput, String retrieverName, String contextName) {
-        String fullString = "_app_" + retrieverName + "_" + contextName;
+        String traceRetrieverName = convertToNameInTrace(retrieverName);
+        String traceContextName = convertToNameInTrace(contextName);
+
+        String fullString = "_app_" + traceRetrieverName + "_" + traceContextName;
         traceOutput.forEach(line -> assertFalse(line.contains(fullString)));
     }
 
+    /**
+     * LTTng accepts periods in context names, but ends up printing them as
+     * underscores in the trace, so the metadata grammar remains valid.
+     */
+    private static String convertToNameInTrace(String name) {
+        return name.replace('.', '_');
+    }
+
     /**
      * Test that if no retrievers are declared, no context info is passed at
      * all.
@@ -173,7 +191,7 @@ public abstract class AppContextITBase {
         assertFalse(output.isEmpty());
 
         /* Test that context name is there but value is not */
-        testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME, "{ none = { } } }");
+        testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME, "{ { } } }");
     }
 
     /**
@@ -197,7 +215,7 @@ public abstract class AppContextITBase {
 
         /* Test that context name + value are present */
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ string = \"" + ContextInfoRetrieverStubs.STRING_VALUE + "\" }");
+                "{ \"" + ContextInfoRetrieverStubs.STRING_VALUE + "\" }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -224,7 +242,7 @@ public abstract class AppContextITBase {
 
         /* Test that only retriever-name-2 is present, with no value */
         testContextNotPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME);
-        testContextPresentInTrace(output, RETRIEVER_NAME_2, CONTEXT_NAME, "{ none = { } } }");
+        testContextPresentInTrace(output, RETRIEVER_NAME_2, CONTEXT_NAME, "{ { } } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -252,7 +270,7 @@ public abstract class AppContextITBase {
 
         /* Test that only retriever-name-1 is present, name + value */
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ string = \"" + ContextInfoRetrieverStubs.STRING_VALUE + "\" }");
+                "{ \"" + ContextInfoRetrieverStubs.STRING_VALUE + "\" }");
         testContextNotPresentInTrace(output, RETRIEVER_NAME_2, CONTEXT_NAME);
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
@@ -282,8 +300,8 @@ public abstract class AppContextITBase {
 
         /* Test that both contexts are present, but only retriever-1's has a value */
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ string = \"" + ContextInfoRetrieverStubs.STRING_VALUE + "\" }");
-        testContextPresentInTrace(output, RETRIEVER_NAME_2, CONTEXT_NAME, "{ none = { } } }");
+                "{ \"" + ContextInfoRetrieverStubs.STRING_VALUE + "\" }");
+        testContextPresentInTrace(output, RETRIEVER_NAME_2, CONTEXT_NAME, "{ { } } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -318,7 +336,7 @@ public abstract class AppContextITBase {
         assertTrue(cim.registerContextInfoRetriever(RETRIEVER_NAME_1, ContextInfoRetrieverStubs.NULL_RETRIEVER));
 
         List<String> output = enableContextAndTrace();
-        testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME, "{ none = { } } }");
+        testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME, "{ { } } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -332,7 +350,7 @@ public abstract class AppContextITBase {
 
         List<String> output = enableContextAndTrace();
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ int32 = " + ContextInfoRetrieverStubs.INTEGER_VALUE + " } }");
+                "{ " + ContextInfoRetrieverStubs.INTEGER_VALUE + " } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -346,7 +364,7 @@ public abstract class AppContextITBase {
 
         List<String> output = enableContextAndTrace();
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ int64 = " + ContextInfoRetrieverStubs.LONG_VALUE + " } }");
+                "{ " + ContextInfoRetrieverStubs.LONG_VALUE + " } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -360,7 +378,7 @@ public abstract class AppContextITBase {
 
         List<String> output = enableContextAndTrace();
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ double = " + ContextInfoRetrieverStubs.DOUBLE_VALUE + " } }");
+                "{ " + ContextInfoRetrieverStubs.DOUBLE_VALUE + " } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -374,7 +392,7 @@ public abstract class AppContextITBase {
 
         List<String> output = enableContextAndTrace();
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ string = \"" + ContextInfoRetrieverStubs.CHARACTER_VALUE + "\" } }");
+                "{ \"" + ContextInfoRetrieverStubs.CHARACTER_VALUE + "\" } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -388,7 +406,7 @@ public abstract class AppContextITBase {
 
         List<String> output = enableContextAndTrace();
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ float = " + ContextInfoRetrieverStubs.FLOAT_VALUE + " } }");
+                "{ " + ContextInfoRetrieverStubs.FLOAT_VALUE + " } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -402,7 +420,7 @@ public abstract class AppContextITBase {
 
         List<String> output = enableContextAndTrace();
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ int8 = " + ContextInfoRetrieverStubs.BYTE_VALUE + " } }");
+                "{ " + ContextInfoRetrieverStubs.BYTE_VALUE + " } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -416,7 +434,7 @@ public abstract class AppContextITBase {
 
         List<String> output = enableContextAndTrace();
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ int16 = " + ContextInfoRetrieverStubs.SHORT_VALUE + " } }");
+                "{ " + ContextInfoRetrieverStubs.SHORT_VALUE + " } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -429,7 +447,7 @@ public abstract class AppContextITBase {
         assertTrue(cim.registerContextInfoRetriever(RETRIEVER_NAME_1, ContextInfoRetrieverStubs.BOOLEAN_TRUE_RETRIEVER));
 
         List<String> output = enableContextAndTrace();
-        testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME, "{ int8 = 1 } }");
+        testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME, "{ 1 } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -442,7 +460,7 @@ public abstract class AppContextITBase {
         assertTrue(cim.registerContextInfoRetriever(RETRIEVER_NAME_1, ContextInfoRetrieverStubs.BOOLEAN_FALSE_RETRIEVER));
 
         List<String> output = enableContextAndTrace();
-        testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME, "{ int8 = 0 } }");
+        testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME, "{ 0 } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -456,7 +474,7 @@ public abstract class AppContextITBase {
 
         List<String> output = enableContextAndTrace();
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ string = \"" + ContextInfoRetrieverStubs.STRING_VALUE + "\" } }");
+                "{ \"" + ContextInfoRetrieverStubs.STRING_VALUE + "\" } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
@@ -470,7 +488,89 @@ public abstract class AppContextITBase {
 
         List<String> output = enableContextAndTrace();
         testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
-                "{ string = \"" + ContextInfoRetrieverStubs.OBJECT_VALUE.toString() + "\" } }");
+                "{ \"" + ContextInfoRetrieverStubs.OBJECT_VALUE.toString() + "\" } }");
+
+        assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
+    }
+
+    // ------------------------------------------------------------------------
+    // Tests related to filtering
+    // ------------------------------------------------------------------------
+
+    /**
+     * Test with a filter expression using a context, but not having the actual
+     * context enabled.
+     *
+     * The JNI should still send the context so UST can use it for filtering,
+     * but it should not be present in the resulting trace.
+     */
+    @Test
+    public void testContextFilterExpressionNotEnabled() {
+        assertTrue(cim.registerContextInfoRetriever(RETRIEVER_NAME_1, ContextInfoRetrieverStubs.STRING_RETRIEVER));
+
+        assertTrue(session.enableEvent(EVENT_NAME, null, false,
+                "$app." + RETRIEVER_NAME_1 + ':' + CONTEXT_NAME + "==\"" + ContextInfoRetrieverStubs.STRING_VALUE + '\"'));
+
+        assertTrue(session.start());
+        sendEventsToLoggers();
+        assertTrue(session.stop());
+
+        List<String> output = session.view();
+        assertNotNull(output);
+        assertFalse(output.isEmpty());
+
+        testContextNotPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME);
+
+        assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
+    }
+
+    /**
+     * Test with a filter expression and an enabled context. The filter however
+     * should *exclude* the events, so no events should be present in the
+     * resulting trace.
+     */
+    @Test
+    public void testContextFilterExpressionEnabledNotMatching() {
+        assertTrue(cim.registerContextInfoRetriever(RETRIEVER_NAME_1, ContextInfoRetrieverStubs.STRING_RETRIEVER));
+
+        assertTrue(session.enableEvent(EVENT_NAME, null, false,
+                "$app." + RETRIEVER_NAME_1 + ':' + CONTEXT_NAME + "!=\"" + ContextInfoRetrieverStubs.STRING_VALUE + '\"'));
+
+        assertTrue(session.enableAppContext(RETRIEVER_NAME_1, CONTEXT_NAME));
+        assertTrue(session.start());
+        sendEventsToLoggers();
+        assertTrue(session.stop());
+
+        List<String> output = session.view();
+        assertNotNull(output);
+        assertTrue(output.isEmpty());
+
+        assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
+    }
+
+    /**
+     * Test with a filter expression and an enabled context. The filter however
+     * should match the events, so events with the context info should be
+     * present in the resulting trace.
+     */
+    @Test
+    public void testContextFilterExpressionEnabledMatching() {
+        assertTrue(cim.registerContextInfoRetriever(RETRIEVER_NAME_1, ContextInfoRetrieverStubs.STRING_RETRIEVER));
+
+        assertTrue(session.enableEvent(EVENT_NAME, null, false,
+                "$app." + RETRIEVER_NAME_1 + ':' + CONTEXT_NAME + "==\"" + ContextInfoRetrieverStubs.STRING_VALUE + '\"'));
+
+        assertTrue(session.enableAppContext(RETRIEVER_NAME_1, CONTEXT_NAME));
+        assertTrue(session.start());
+        sendEventsToLoggers();
+        assertTrue(session.stop());
+
+        List<String> output = session.view();
+        assertNotNull(output);
+        assertFalse(output.isEmpty());
+
+        testContextPresentInTrace(output, RETRIEVER_NAME_1, CONTEXT_NAME,
+                "{ \"" + ContextInfoRetrieverStubs.STRING_VALUE + "\" } }");
 
         assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME_1));
     }
This page took 0.027786 seconds and 4 git commands to generate.