Skip to content

Commit

Permalink
Use root logger for capturing
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Nov 2, 2024
1 parent 07673a4 commit b9e0cfd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/test/java/net/dv8tion/jda/test/JDABuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

package net.dv8tion.jda.test;

import ch.qos.logback.classic.Logger;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.dv8tion.jda.internal.JDAImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
Expand All @@ -44,7 +42,7 @@ void testCreateWithAllIntents()
TestJDABuilder builder = new TestJDABuilder(ALL_INTENTS);
builder.applyIntents();

List<String> logs = captureLogging((Logger) JDAImpl.LOG, builder::checkIntents);
List<String> logs = captureLogging(builder::checkIntents);
assertThat(logs).isEmpty();
}

Expand All @@ -54,7 +52,7 @@ void testCreateWithMinimalIntents()
TestJDABuilder builder = new TestJDABuilder(0);
builder.applyIntents();

List<String> logs = captureLogging((Logger) JDAImpl.LOG, builder::checkIntents);
List<String> logs = captureLogging(builder::checkIntents);

EnumSet<CacheFlag> flags = EnumSet.allOf(CacheFlag.class);
flags.removeIf(flag -> flag.getRequiredIntent() == null);
Expand All @@ -68,7 +66,7 @@ void testCreateWithAllCacheAllIntents()
builder.applyIntents();
builder.enableCache(EnumSet.allOf(CacheFlag.class));

List<String> logs = captureLogging((Logger) JDAImpl.LOG, builder::checkIntents);
List<String> logs = captureLogging(builder::checkIntents);
assertThat(logs).isEmpty();
}

Expand Down Expand Up @@ -128,7 +126,7 @@ void testDefaultMinimal()
EnumSet<CacheFlag> flags = EnumSet.allOf(CacheFlag.class);
flags.removeIf(flag -> flag.getRequiredIntent() == null || defaultDisabled.contains(flag));

List<String> logs = captureLogging((Logger) JDAImpl.LOG, builder::checkIntents);
List<String> logs = captureLogging(builder::checkIntents);
checkMissingIntentWarnings(logs, flags);
}

Expand All @@ -139,7 +137,7 @@ void testChunkingWithMissingIntent()
builder.applyIntents();
builder.setChunkingFilter(ChunkingFilter.ALL);

List<String> logs = captureLogging((Logger) JDAImpl.LOG, builder::checkIntents);
List<String> logs = captureLogging(builder::checkIntents);
assertThat(logs).contains("Member chunking is disabled due to missing GUILD_MEMBERS intent.");
}

Expand Down
17 changes: 14 additions & 3 deletions src/test/java/net/dv8tion/jda/test/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,31 @@

package net.dv8tion.jda.test;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

public class TestHelper
{
public static List<String> captureLogging(Runnable task)
{
return captureLogging(LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), task);
}

public static List<String> captureLogging(Logger logger, Runnable task)
{
assertThat(logger).isInstanceOf(ch.qos.logback.classic.Logger.class);
ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) logger;

ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
listAppender.start();
logger.addAppender(listAppender);
logbackLogger.addAppender(listAppender);
try
{
task.run();
Expand All @@ -39,7 +50,7 @@ public static List<String> captureLogging(Logger logger, Runnable task)
}
finally
{
logger.detachAppender(listAppender);
logbackLogger.detachAppender(listAppender);
listAppender.stop();
}
}
Expand Down

0 comments on commit b9e0cfd

Please sign in to comment.