Add tests for the "lttng list" command
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Tue, 8 Sep 2015 23:39:24 +0000 (19:39 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Wed, 9 Sep 2015 20:50:50 +0000 (16:50 -0400)
Should cover https://bugs.lttng.org/issues/933

Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
lttng-tools-java/.settings/org.eclipse.jdt.core.prefs
lttng-tools-java/src/main/java/org/lttng/tools/ILttngSession.java
lttng-tools-java/src/main/java/org/lttng/tools/LttngCommandLineSession.java
lttng-ust-java-tests/.settings/org.eclipse.jdt.core.prefs
lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/ListEventsITBase.java [new file with mode: 0644]
lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/jul/JulListEventsIT.java [new file with mode: 0644]
lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/log4j/Log4jListEventsIT.java [new file with mode: 0644]

index 3ef0c309a70cf58886a824711ecdf6abc16f9cc1..f1acd28c51b07e2f6919cbdfc444cc6f21d7e862 100644 (file)
@@ -107,7 +107,7 @@ org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
 org.eclipse.jdt.core.compiler.source=1.8
 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
-org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0
+org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=16
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16
@@ -371,7 +371,7 @@ org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constan
 org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert
 org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert
 org.eclipse.jdt.core.formatter.join_lines_in_comments=true
-org.eclipse.jdt.core.formatter.join_wrapped_lines=true
+org.eclipse.jdt.core.formatter.join_wrapped_lines=false
 org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false
 org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false
 org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false
index b6d0aedefec830494eaae9d11f27e0baefa362c2..a28a4aa555a90221f5c3b84fb687b5937024d186 100644 (file)
@@ -19,6 +19,7 @@
 package org.lttng.tools;
 
 import java.util.List;
+import java.util.Set;
 
 /**
  * Java representation of a LTTng tracing session.
@@ -149,6 +150,14 @@ public interface ILttngSession extends AutoCloseable {
      */
     boolean disableAllEvents();
 
+    /**
+     * Get a list of events currently available (exposed by applications) in the
+     * session's domain.
+     *
+     * @return The list of available events
+     */
+    Set<String> listEvents();
+
     /**
      * Start tracing
      *
index e2b1ff6b6ce56acdc97041cbc7540eb3cc06df83..171cffbf08ac3559e2c9d365a04a3ff8fda05bf0 100644 (file)
@@ -23,6 +23,7 @@ import static org.lttng.tools.utils.ShellUtils.executeCommand;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Set;
 import java.util.UUID;
 import java.util.stream.Collectors;
 
@@ -136,6 +137,16 @@ class LttngCommandLineSession implements ILttngSession {
                 "lttng", "disable-event", domain.flag(), "-a", "-s", sessionName));
     }
 
+    @Override
+    public Set<String> listEvents() {
+        List<String> output = ShellUtils.getOutputFromCommand(true, Arrays.asList("lttng", "list", domain.flag()));
+        return output.stream()
+                .map(e -> e.trim())
+                .filter(e -> e.startsWith("- "))
+                .map(e -> e.substring(2))
+                .collect(Collectors.toSet());
+    }
+
     @Override
     public boolean start() {
         /*
index 3ef0c309a70cf58886a824711ecdf6abc16f9cc1..f1acd28c51b07e2f6919cbdfc444cc6f21d7e862 100644 (file)
@@ -107,7 +107,7 @@ org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
 org.eclipse.jdt.core.compiler.source=1.8
 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
-org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0
+org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=16
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16
@@ -371,7 +371,7 @@ org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constan
 org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert
 org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert
 org.eclipse.jdt.core.formatter.join_lines_in_comments=true
-org.eclipse.jdt.core.formatter.join_wrapped_lines=true
+org.eclipse.jdt.core.formatter.join_wrapped_lines=false
 org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false
 org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false
 org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false
diff --git a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/ListEventsITBase.java b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/ListEventsITBase.java
new file mode 100644 (file)
index 0000000..ec4e0c1
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.lttng.ust.agent.integration.events;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.lttng.tools.ILttngSession;
+
+/**
+ * Base class for the list events command tests
+ */
+public abstract class ListEventsITBase {
+
+    protected static final String LOGGER_NAME_1 = "org.lttng";
+    protected static final String LOGGER_NAME_2 = "org.lttng.mycomponent";
+    protected static final String LOGGER_NAME_3 = "org.lttng.myothercomponent";
+
+    private ILttngSession session;
+
+    /**
+     * Common test setup
+     */
+    @Before
+    public void testSetup() {
+        session = ILttngSession.createSession(null, getDomain());
+    }
+
+    /**
+     * Common test teardown
+     */
+    @After
+    public void testTeardown() {
+        session.close();
+    }
+
+    protected abstract ILttngSession.Domain getDomain();
+
+    protected abstract void attachHandlerToLogger(int handlerIndex, int loggerIndex);
+
+    protected abstract void detachHandlerFromLogger(int handlerIndex, int loggerIndex);
+
+    /**
+     * Test with many loggers existing, but none of them having a LTTng handler
+     * attached.
+     */
+    @Test
+    public void testManyLoggersNoneAttached() {
+        /* Don't attach anything */
+        Set<String> actualEvents = session.listEvents();
+        assertTrue(actualEvents.isEmpty());
+    }
+
+    /**
+     * Test with many loggers existing, but only a subset of them has a LTTng
+     * handler attached.
+     */
+    @Test
+    public void testManyLoggersSomeAttached() {
+        attachHandlerToLogger(1, 1);
+
+        Set<String> expectedEvents = new HashSet<>(Arrays.asList(LOGGER_NAME_1));
+        Set<String> actualEvents = session.listEvents();
+
+        assertEquals(expectedEvents, actualEvents);
+    }
+
+    /**
+     * Test with many loggers existing, and all of them having a LTTng handler
+     * attached.
+     */
+    @Test
+    public void testManyLoggersAllAttached() {
+        attachHandlerToLogger(1, 1);
+        attachHandlerToLogger(2, 2);
+        attachHandlerToLogger(2, 3);
+
+        Set<String> expectedEvents = new HashSet<>(Arrays.asList(LOGGER_NAME_1, LOGGER_NAME_2, LOGGER_NAME_3));
+        Set<String> actualEvents = session.listEvents();
+
+        assertEquals(expectedEvents, actualEvents);
+    }
+
+    /**
+     * Test with some loggers having had handlers attached but then detached.
+     */
+    @Test
+    public void testLoggersSomeDetached() {
+        attachHandlerToLogger(1, 1);
+        attachHandlerToLogger(2, 2);
+
+        attachHandlerToLogger(2, 3);
+        detachHandlerFromLogger(2, 3);
+        /* Only loggers 1 and 2 will be attached */
+
+        Set<String> expectedEvents = new HashSet<>(Arrays.asList(LOGGER_NAME_1, LOGGER_NAME_2));
+        Set<String> actualEvents = session.listEvents();
+
+        assertEquals(expectedEvents, actualEvents);
+    }
+
+    /**
+     * Test with all loggers having had handlers attached and then detached.
+     */
+    @Test
+    public void testLoggersAllDetached() {
+        attachHandlerToLogger(1, 1);
+        attachHandlerToLogger(2, 2);
+        attachHandlerToLogger(2, 3);
+        detachHandlerFromLogger(1, 1);
+        detachHandlerFromLogger(2, 2);
+        detachHandlerFromLogger(2, 3);
+
+        Set<String> actualEvents = session.listEvents();
+        assertTrue(actualEvents.isEmpty());
+    }
+}
diff --git a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/jul/JulListEventsIT.java b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/jul/JulListEventsIT.java
new file mode 100644 (file)
index 0000000..2025a97
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.lttng.ust.agent.integration.events.jul;
+
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.util.logging.Handler;
+import java.util.logging.Logger;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.lttng.tools.ILttngSession.Domain;
+import org.lttng.tools.ILttngSession;
+import org.lttng.tools.LttngToolsHelper;
+import org.lttng.ust.agent.integration.events.ListEventsITBase;
+import org.lttng.ust.agent.jul.LttngLogHandler;
+import org.lttng.ust.agent.utils.LttngUtils;
+
+/**
+ * Test suite for the list events command for the JUL domain
+ */
+public class JulListEventsIT extends ListEventsITBase {
+
+    private Logger[] loggers;
+    private Handler[] handlers;
+
+    /**
+     * Test class setup
+     */
+    @BeforeClass
+    public static void testClassSetup() {
+        /* Skip tests if we can't find the JNI library or lttng-tools */
+        assumeTrue(LttngUtils.checkForJulLibrary());
+        assumeTrue(LttngUtils.checkForLttngTools(Domain.JUL));
+
+        LttngToolsHelper.destroyAllSessions();
+    }
+
+    /**
+     * Test setup
+     *
+     * @throws SecurityException
+     * @throws IOException
+     */
+    @Before
+    public void julSetup() throws SecurityException, IOException {
+        loggers = new Logger[] {
+                Logger.getLogger(LOGGER_NAME_1),
+                Logger.getLogger(LOGGER_NAME_2),
+                Logger.getLogger(LOGGER_NAME_3)
+        };
+
+        handlers = new Handler[] {
+                new LttngLogHandler(),
+                new LttngLogHandler()
+        };
+    }
+
+    /**
+     * Test teardown. Detach and close all log handlers.
+     */
+    @After
+    public void julTeardown() {
+        for (Logger logger : loggers) {
+            for (Handler handler : handlers) {
+                logger.removeHandler(handler);
+            }
+        }
+
+        for (Handler handler : handlers) {
+            handler.close();
+        }
+        handlers = null;
+        loggers = null;
+    }
+
+    @Override
+    protected ILttngSession.Domain getDomain() {
+        return ILttngSession.Domain.JUL;
+    }
+
+    @Override
+    protected void attachHandlerToLogger(int handlerIndex, int loggerIndex) {
+        loggers[loggerIndex - 1].addHandler(handlers[handlerIndex - 1]);
+    }
+
+    @Override
+    protected void detachHandlerFromLogger(int handlerIndex, int loggerIndex) {
+        loggers[loggerIndex - 1].removeHandler(handlers[handlerIndex - 1]);
+    }
+
+}
diff --git a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/log4j/Log4jListEventsIT.java b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/log4j/Log4jListEventsIT.java
new file mode 100644 (file)
index 0000000..2dc566e
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.lttng.ust.agent.integration.events.log4j;
+
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+
+import org.apache.log4j.Appender;
+import org.apache.log4j.Logger;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.lttng.tools.ILttngSession.Domain;
+import org.lttng.tools.ILttngSession;
+import org.lttng.tools.LttngToolsHelper;
+import org.lttng.ust.agent.integration.events.ListEventsITBase;
+import org.lttng.ust.agent.log4j.LttngLogAppender;
+import org.lttng.ust.agent.utils.LttngUtils;
+
+/**
+ * Test suite for the list events command for the log4j domain
+ */
+public class Log4jListEventsIT extends ListEventsITBase {
+
+    private Logger[] loggers;
+    private Appender[] appenders;
+
+    /**
+     * Test class setup
+     */
+    @BeforeClass
+    public static void testClassSetup() {
+        /* Skip tests if we can't find the JNI library or lttng-tools */
+        assumeTrue(LttngUtils.checkForLog4jLibrary());
+        assumeTrue(LttngUtils.checkForLttngTools(Domain.LOG4J));
+
+        LttngToolsHelper.destroyAllSessions();
+    }
+
+    /**
+     * Test setup
+     *
+     * @throws SecurityException
+     * @throws IOException
+     */
+    @Before
+    public void log4jSetup() throws SecurityException, IOException {
+        loggers = new Logger[] {
+                Logger.getLogger(LOGGER_NAME_1),
+                Logger.getLogger(LOGGER_NAME_2),
+                Logger.getLogger(LOGGER_NAME_3)
+        };
+
+        appenders = new Appender[] {
+                new LttngLogAppender(),
+                new LttngLogAppender()
+        };
+    }
+
+    /**
+     * Test teardown. Detach and close all log handlers.
+     */
+    @After
+    public void log4jTeardown() {
+        for (Logger logger : loggers) {
+            for (Appender appender : appenders) {
+                logger.removeAppender(appender);
+            }
+        }
+
+        for (Appender appender : appenders) {
+            appender.close();
+        }
+        appenders = null;
+        loggers = null;
+    }
+
+    @Override
+    protected ILttngSession.Domain getDomain() {
+        return ILttngSession.Domain.LOG4J;
+    }
+
+    @Override
+    protected void attachHandlerToLogger(int handlerIndex, int loggerIndex) {
+        loggers[loggerIndex - 1].addAppender(appenders[handlerIndex - 1]);
+    }
+
+    @Override
+    protected void detachHandlerFromLogger(int handlerIndex, int loggerIndex) {
+        loggers[loggerIndex - 1].removeAppender(appenders[handlerIndex - 1]);
+    }
+
+}
This page took 0.029704 seconds and 4 git commands to generate.