Put common setup code in the utils classes
[lttng-ust-java-tests.git] / lttng-ust-java-tests-log4j / src / test / java / org / lttng / ust / agent / utils / Log4jTestUtils.java
CommitLineData
2b408e85
AM
1/*
2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
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
4821eac9 19package org.lttng.ust.agent.utils;
8576633f 20
eca27046
AM
21import static org.junit.Assert.assertTrue;
22import static org.junit.Assert.fail;
23
8576633f
AM
24import java.io.IOException;
25
26import org.apache.log4j.Level;
27import org.apache.log4j.Logger;
eca27046
AM
28import org.lttng.tools.LttngToolsHelper;
29import org.lttng.tools.ILttngSession.Domain;
4821eac9 30import org.lttng.ust.agent.log4j.LttngLogAppender;
8576633f 31
8a0613fa
AM
32/**
33 * Utility methods for log4j tests
34 */
4821eac9 35public final class Log4jTestUtils {
8576633f
AM
36
37 private Log4jTestUtils() {
38 }
39
eca27046
AM
40 /**
41 * Setup method common to most log4j tests. To be called in a @BeforeClass.
42 */
43 public static void testClassSetup() {
44 /* Make sure we can find the JNI library and lttng-tools */
45 checkForLog4jLibrary();
46 assertTrue("lttng-tools is not working properly.", LttngUtils.checkForLttngTools(Domain.LOG4J));
47
48 LttngToolsHelper.destroyAllSessions();
49 }
50
51 /**
52 * Teardown method common to most log4j tests. To be called in a @AfterClass.
53 */
54 public static void testClassCleanup() {
55 LttngToolsHelper.deleteAllTraces();
56 }
57
4821eac9
AM
58 /**
59 * Check the the Log4j native library is available, effectively allowing
60 * LTTng Log4j appenders to be used.
4821eac9 61 */
eca27046 62 private static void checkForLog4jLibrary() {
4821eac9
AM
63 try {
64 LttngLogAppender testAppender = new LttngLogAppender();
65 testAppender.close();
66 } catch (SecurityException | IOException e) {
eca27046 67 fail(e.getMessage());
4821eac9 68 }
4821eac9
AM
69 }
70
71 /**
72 * Send 10 dummy events through the provided logger
73 *
74 * @param logger
75 * The logger to use to send events
76 */
77 public static void send10Events(Logger logger) {
8576633f
AM
78 // Levels/priorities are DEBUG, ERROR, FATAL, INFO, TRACE, WARN
79 logger.debug("Debug message. Lost among so many.");
80 logger.debug("Debug message with a throwable", new IOException());
81 logger.error("Error messsage. This might be bad.");
82 logger.error("Error message with a throwable", new IOException());
83 logger.fatal("A fatal message. You are already dead.");
84 logger.info("A info message. Lol, who cares.");
85 logger.trace("A trace message. No, no *that* trace");
86 logger.warn("A warn message. Yellow underline.");
87 logger.log(Level.DEBUG, "A debug message using .log()");
88 logger.log(Level.ERROR, "A error message using .log()");
89 }
90}
This page took 0.026268 seconds and 4 git commands to generate.