Migrate to Junit 5 Jupiter
[lttng-ust-java-tests.git] / lttng-ust-java-tests-log4j2 / src / test / java / org / lttng / ust / agent / utils / Log4j2TestUtils.java
CommitLineData
eca1a136
MJ
1/*
2 * Copyright (C) 2022, EfficiOS Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19package org.lttng.ust.agent.utils;
20
7a4f0255
MJ
21import static org.junit.jupiter.api.Assertions.assertTrue;
22import static org.junit.jupiter.api.Assertions.fail;
eca1a136
MJ
23
24import java.io.IOException;
25
26import org.apache.logging.log4j.Level;
27import org.apache.logging.log4j.Logger;
28import org.lttng.tools.ILttngSession.Domain;
29import org.lttng.tools.LttngToolsHelper;
30import org.lttng.ust.agent.log4j2.LttngLogAppender;
31
32
33/**
34 * Utility methods for log4j 2.x tests
35 */
36public final class Log4j2TestUtils {
37
38 private Log4j2TestUtils() {
39 }
40
41 /**
42 * Setup method common to most log4j tests. To be called in a @BeforeClass.
43 */
44 public static void testClassSetup() {
45 /* Make sure we can find the JNI library and lttng-tools */
46 checkForLog4jLibrary();
7a4f0255 47 assertTrue(LttngUtils.checkForLttngTools(Domain.LOG4J), "lttng-tools is not working properly.");
eca1a136
MJ
48
49 LttngToolsHelper.destroyAllSessions();
50 }
51
52 /**
53 * Teardown method common to most log4j tests. To be called in a @AfterClass.
54 */
55 public static void testClassCleanup() {
56 LttngToolsHelper.deleteAllTraces();
57 }
58
59 /**
60 * Check the the Log4j native library is available, effectively allowing
61 * LTTng Log4j appenders to be used.
62 */
63 private static void checkForLog4jLibrary() {
64 try {
65 LttngLogAppender testAppender = LttngLogAppender.createAppender("checkForLttngTools", null, null);
66 testAppender.close();
67 } catch (SecurityException | IOException e) {
68 fail(e.getMessage());
69 }
70 }
71
72 /**
73 * Send 10 dummy events through the provided logger
74 *
75 * @param logger
76 * The logger to use to send events
77 */
78 public static void send10Events(Logger logger) {
79 // Levels/priorities are DEBUG, ERROR, FATAL, INFO, TRACE, WARN
80 logger.debug("Debug message. Lost among so many.");
81 logger.debug("Debug message with a throwable", new IOException());
82 logger.error("Error messsage. This might be bad.");
83 logger.error("Error message with a throwable", new IOException());
84 logger.fatal("A fatal message. You are already dead.");
85 logger.info("A info message. Lol, who cares.");
86 logger.trace("A trace message. No, no *that* trace");
87 logger.warn("A warn message. Yellow underline.");
88 logger.log(Level.DEBUG, "A debug message using .log()");
89 logger.log(Level.ERROR, "A error message using .log()");
90 }
91}
This page took 0.024902 seconds and 4 git commands to generate.