Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent NPE when discriminator is not set #893

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public void start() {
if (discriminator == null) {
addError("Missing discriminator. Aborting");
errors++;
}
if (!discriminator.isStarted()) {
} else if (!discriminator.isStarted()) {
addError("Discriminator has not started successfully. Aborting");
errors++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.core.appender;

import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import ch.qos.logback.core.Appender;
import ch.qos.logback.core.ContextBase;
import ch.qos.logback.core.sift.DefaultDiscriminator;
import ch.qos.logback.core.sift.SiftingAppenderBase;
import ch.qos.logback.core.status.Status;

/**
* Basic tests of SiftingAppender.
*/
public class SiftingAppenderTest extends AbstractAppenderTest<Object> {

@Test
public void testNoAppenderConfiuration() {
final ContextBase context = new ContextBase();
final SiftingAppenderBase<Object> appender = getAppender();
appender.setContext(context);

appender.start();

Assertions.assertEquals(2, context.getStatusManager().getCount());
final List<Status> statuses = context.getStatusManager().getCopyOfStatusList();
Assertions.assertEquals("Missing discriminator. Aborting", statuses.get(0).getMessage());
Assertions.assertEquals("AppenderFactory has not been set. Aborting", statuses.get(1).getMessage());
}

@Test
public void testAppenderConfiurationWhenDiscriminatorIsNotStarted() {
final ContextBase context = new ContextBase();
final SiftingAppenderBase<Object> appender = getAppender();
appender.setContext(context);
appender.setDiscriminator(new DefaultDiscriminator<>());

appender.start();

Assertions.assertEquals(2, context.getStatusManager().getCount());
final List<Status> statuses = context.getStatusManager().getCopyOfStatusList();
Assertions.assertEquals("Discriminator has not started successfully. Aborting", statuses.get(0).getMessage());
Assertions.assertEquals("AppenderFactory has not been set. Aborting", statuses.get(1).getMessage());
}

@Override
protected SiftingAppenderBase<Object> getAppender() {
return new SiftingAppenderBase<Object>() {

@Override
protected long getTimestamp(Object event) {
return 0;
}

@Override
protected boolean eventMarksEndOfLife(Object event) {
return false;
}
};
}

@Disabled("SiftingAppender does not call 'super.stop()', and I don't know why")
@Test
public void testConfiguredAppender() {

}

@Override
protected Appender<Object> getConfiguredAppender() {
throw new UnsupportedOperationException("Implement it while fixing 'testConfiguredAppender'");
}
}