Skip to content

Commit

Permalink
IGNITE-23472 Add test for GridTestLog4jLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
chesnokoff committed Nov 1, 2024
1 parent c5f8dee commit 9b6c338
Showing 1 changed file with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.ignite.testframework.junits.logger;

import org.apache.ignite.IgniteLogger;
import org.apache.ignite.internal.logger.IgniteLoggerEx;
import org.apache.ignite.internal.util.lang.RunnableX;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.logging.log4j.Level;
Expand All @@ -29,15 +31,23 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.File;
import java.net.URL;
import java.util.UUID;

import static java.lang.String.format;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Checks that assertion error will be thrown, if logging for the level disabled and log message on this level was invoked.
*/
@RunWith(JUnit4.class)
public class GridTestLog4jLoggerSelfTest {

private static final String LOG_PATH_TEST = "modules/log4j2/src/test/config/log4j2-test.xml";

/** Logger message. */
private static final String LOG_MESSAGE = "TEST MESSAGE";

Expand All @@ -64,6 +74,78 @@ public static void afterTests() {
assertEquals(defaultRootLevel, LoggerContext.getContext(false).getConfiguration().getRootLogger().getLevel());
}

@Test
public void testFileConstructor() throws Exception {
File xml = GridTestUtils.resolveIgnitePath(LOG_PATH_TEST);

assert xml != null;
assert xml.exists();

IgniteLogger log = new GridTestLog4jLogger(xml).getLogger(getClass());

System.out.println(log.toString());

assertTrue(log.toString().contains("GridTestLog4jLogger"));
assertTrue(log.toString().contains(xml.getPath()));

((IgniteLoggerEx)log).setApplicationAndNode(null, UUID.randomUUID());

checkLog(log);
}

@Test
public void testUrlConstructor() throws Exception {
File xml = GridTestUtils.resolveIgnitePath(LOG_PATH_TEST);

assert xml != null;
assert xml.exists();

URL url = xml.toURI().toURL();
IgniteLogger log = new GridTestLog4jLogger(url).getLogger(getClass());

System.out.println(log.toString());

assertTrue(log.toString().contains("GridTestLog4jLogger"));
assertTrue(log.toString().contains(url.getPath()));

((IgniteLoggerEx)log).setApplicationAndNode(null, UUID.randomUUID());

checkLog(log);
}

/**
* @throws Exception If failed.
*/
@Test
public void testPathConstructor() throws Exception {
IgniteLogger log = new GridTestLog4jLogger(LOG_PATH_TEST).getLogger(getClass());

System.out.println(log.toString());

assertTrue(log.toString().contains("GridTestLog4jLogger"));
assertTrue(log.toString().contains(LOG_PATH_TEST));

((IgniteLoggerEx)log).setApplicationAndNode(null, UUID.randomUUID());

checkLog(log);
}


/**
* Tests log4j logging SPI.
*/
private void checkLog(IgniteLogger log) {
assert !log.isDebugEnabled();
assert log.isInfoEnabled();

log.debug("This is 'debug' message.");
log.info("This is 'info' message.");
log.warning("This is 'warning' message.");
log.warning("This is 'warning' message.", new Exception("It's a test warning exception"));
log.error("This is 'error' message.");
log.error("This is 'error' message.", new Exception("It's a test error exception"));
}

/** */
@Test
public void testDebug() {
Expand Down

0 comments on commit 9b6c338

Please sign in to comment.