Add 'log4j2' domain tests to the Log4j 2.x agent
[lttng-ust-java-tests.git] / lttng-ust-java-tests-log4j2 / src / main / java / org / lttng / ust / agent / integration / events / Log4j2ListEventsITBase.java
CommitLineData
9db2c69a
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.integration.events;
20
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertTrue;
23
24import java.util.Arrays;
25import java.util.Collections;
26import java.util.List;
27
28import org.apache.logging.log4j.Logger;
29import org.junit.jupiter.api.AfterAll;
30import org.junit.jupiter.api.AfterEach;
31import org.junit.jupiter.api.BeforeAll;
32import org.junit.jupiter.api.BeforeEach;
33import org.junit.jupiter.api.Test;
34import org.junit.jupiter.api.TestInfo;
35import org.junit.jupiter.api.TestInstance;
36import org.junit.jupiter.api.TestInstance.Lifecycle;
37import org.junit.jupiter.api.extension.ExtendWith;
38import org.lttng.tools.ILttngSession;
39import org.lttng.tools.ILttngSession.Domain;
40import org.lttng.ust.agent.utils.Log4j2TestContext;
41import org.lttng.ust.agent.utils.Log4j2TestUtils;
42import org.lttng.ust.agent.utils.TestPrintExtension;
43
44/**
45 * Test suite for the list events command for the log4j domain
46 */
47@TestInstance(Lifecycle.PER_CLASS)
48@ExtendWith(TestPrintExtension.class)
49public abstract class Log4j2ListEventsITBase {
50
51 protected static final String LOGGER_NAME_1 = "org.lttng.somecomponent";
52 protected static final String LOGGER_NAME_2 = "org.lttng.mycomponent";
53 protected static final String LOGGER_NAME_3 = "org.lttng.myothercomponent-àéç";
54
55 @SuppressWarnings("unused")
56 private Logger logger1;
57 @SuppressWarnings("unused")
58 private Logger logger2;
59 @SuppressWarnings("unused")
60 private Logger logger3;
61
62 private ILttngSession session;
63 private Log4j2TestContext testContext;
64
65 protected abstract Domain getDomain();
66
67 /**
68 * Class setup
69 */
70 @BeforeAll
71 public void log4j2ClassSetup() {
72 Log4j2TestUtils.testClassSetup(getDomain());
73 }
74
75 /**
76 * Class cleanup
77 */
78 @AfterAll
79 public static void log4j2ClassCleanup() {
80 Log4j2TestUtils.testClassCleanup();
81 }
82
83 /**
84 * Create a new session before each test.
85 *
86 * @param testInfo
87 * current test information
88 */
89 @SuppressWarnings("resource")
90 @BeforeEach
91 public void testSetup(TestInfo testInfo) {
92 session = ILttngSession.createSession("Log4j2ListEventsIT", getDomain());
93
94 testContext = new Log4j2TestContext(
95 "log4j2." + getDomain() + testInfo.getDisplayName().replaceAll("[()]", "") + ".xml");
96
97 testContext.beforeTest();
98
99 logger1 = testContext.getLoggerContext().getLogger(LOGGER_NAME_1);
100 logger2 = testContext.getLoggerContext().getLogger(LOGGER_NAME_2);
101 logger3 = testContext.getLoggerContext().getLogger(LOGGER_NAME_3);
102 }
103
104 /**
105 * Close the current session after each test.
106 */
107 @AfterEach
108 public void testTeardown() {
109 session.close();
110 testContext.afterTest();
111 }
112
113 /**
114 * Test with many loggers existing, but none of them having a LTTng handler
115 * attached.
116 */
117 @Test
118 public void testManyLoggersNoneAttached() {
119
120 /* Don't attach anything */
121 List<String> actualEvents = session.listEvents();
122 assertTrue(actualEvents.isEmpty());
123 }
124
125 /**
126 * Test with many loggers existing, but only a subset of them has a LTTng
127 * handler attached.
128 */
129 @Test
130 public void testManyLoggersSomeAttached() {
131
132 List<String> expectedEvents = Arrays.asList(LOGGER_NAME_1);
133 List<String> actualEvents = session.listEvents();
134
135 Collections.sort(expectedEvents);
136 Collections.sort(actualEvents);
137
138 assertEquals(expectedEvents, actualEvents);
139 }
140
141 /**
142 * Test with many loggers existing, and all of them having a LTTng handler
143 * attached.
144 */
145 @Test
146 public void testManyLoggersAllAttached() {
147
148 List<String> expectedEvents = Arrays.asList(LOGGER_NAME_1, LOGGER_NAME_2, LOGGER_NAME_3);
149 List<String> actualEvents = session.listEvents();
150
151 Collections.sort(expectedEvents);
152 Collections.sort(actualEvents);
153
154 assertEquals(expectedEvents, actualEvents);
155 }
156}
This page took 0.027503 seconds and 4 git commands to generate.