Add oving 6

This commit is contained in:
Andreas Omholt Olsen
2026-02-23 12:10:01 +01:00
parent 1a6645482b
commit 57adabe5be
23 changed files with 1419 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
package oving6.logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayOutputStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class DistributingLoggerTest {
private static final String formatString = "%s: %s (%s)";
private ByteArrayOutputStream infoStream;
private ByteArrayOutputStream warnStream;
private ByteArrayOutputStream errorStream;
private DistributingLogger logger;
private StreamLogger infoLogger;
private StreamLogger warnLogger;
private StreamLogger errorLogger;
@BeforeEach
public void setUp() {
infoStream = new ByteArrayOutputStream();
warnStream = new ByteArrayOutputStream();
errorStream = new ByteArrayOutputStream();
infoLogger = new StreamLogger(infoStream);
warnLogger = new StreamLogger(warnStream);
errorLogger = new StreamLogger(errorStream);
logger = new DistributingLogger(errorLogger, warnLogger, infoLogger);
}
@Test
@DisplayName("Log to INFO")
public void testLogToInfo() {
infoLogger.setFormatString(formatString);
logger.log(ILogger.INFO, "Dette er en info-melding.", null);
assertEquals(String.format(formatString, ILogger.INFO, "Dette er en info-melding.", null),
infoStream.toString().trim(), "Test the format of the info message after logging");
assertEquals("", warnStream.toString(),
"Test the warning stream after logging an info message");
assertEquals("", errorStream.toString(),
"Test the error stream after logging an info message");
}
@Test
@DisplayName("Log to WARNING")
public void testLogToWarn() {
warnLogger.setFormatString(formatString);
logger.log(ILogger.WARNING, "Dette er en advarsel.", null);
assertEquals(String.format(formatString, ILogger.WARNING, "Dette er en advarsel.", null),
warnStream.toString().trim(),
"Test the format of the warning message after logging");
assertEquals("", infoStream.toString(),
"Test the info stream after logging a warning message");
assertEquals("", errorStream.toString(),
"Test the error stream after logging a warning message");
}
@Test
@DisplayName("Log to ERROR")
public void testLogToError() {
Exception exception = new IllegalStateException();
errorLogger.setFormatString(formatString);
logger.log(ILogger.ERROR, "Dette er en feilmelding.", exception);
assertEquals(
String.format(formatString, ILogger.ERROR, "Dette er en feilmelding.", exception),
errorStream.toString().trim(),
"Test the format of the error message after logging");
assertEquals("", warnStream.toString(),
"Test the warning stream after logging an error message");
assertEquals("", infoStream.toString(),
"Test the info stream after logging an error message");
}
@Test
@DisplayName("Change info logger")
public void testChangeInfoLogger() {
ByteArrayOutputStream newInfoStream = new ByteArrayOutputStream();
StreamLogger newInfoLogger = new StreamLogger(newInfoStream);
infoLogger.setFormatString(formatString);
logger.log(ILogger.INFO, "Dette er en info-melding.", null);
assertEquals(String.format(formatString, ILogger.INFO, "Dette er en info-melding.", null),
infoStream.toString().trim(), "Test the format of the info message after logging");
newInfoLogger.setFormatString(formatString);
logger.setLogger(ILogger.INFO, newInfoLogger);
logger.log(ILogger.INFO, "Dette er den andre info-meldingen.", null);
assertEquals(String.format(formatString, ILogger.INFO, "Dette er en info-melding.", null),
infoStream.toString().trim(),
"Test the first info stream after switching the info logger and logging another info message");
assertEquals(
String.format(formatString, ILogger.INFO, "Dette er den andre info-meldingen.",
null),
newInfoStream.toString().trim(),
"Test the second info stream after switching the info logger and logging a new info message");
}
}

View File

@@ -0,0 +1,86 @@
package oving6.logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class FilteringLoggerTest {
private ByteArrayOutputStream stream;
private StreamLogger subLogger;
@BeforeEach
public void setUp() {
stream = new ByteArrayOutputStream();
subLogger = new StreamLogger(stream);
}
@Test
@DisplayName("Create logger without severities")
public void testFilteringLoggerNoSeverities() {
FilteringLogger logger = new FilteringLogger(subLogger);
assertFalse(logger.isLogging(ILogger.INFO),
"Test logger without severity for info-logging");
assertFalse(logger.isLogging(ILogger.WARNING),
"Test logger without severity for warning-logging");
assertFalse(logger.isLogging(ILogger.ERROR),
"Test logger without severity for error-logging");
}
@Test
@DisplayName("Create logger with error and info")
public void testFilteringLoggerErrorAndInfo() {
FilteringLogger logger = new FilteringLogger(subLogger, ILogger.ERROR, ILogger.INFO);
assertTrue(logger.isLogging(ILogger.INFO), "Test error and info logger for info-logging");
assertFalse(logger.isLogging(ILogger.WARNING),
"Test error and info logger for warning-logging");
assertTrue(logger.isLogging(ILogger.ERROR), "Test error and info logger for error-logging");
}
@Test
@DisplayName("Create logger with warning, set error")
public void testFilteringLoggerWarningSetIsLoggingError() {
FilteringLogger logger = new FilteringLogger(subLogger, ILogger.WARNING);
assertFalse(logger.isLogging(ILogger.INFO), "Test warning logger for info-logging");
assertTrue(logger.isLogging(ILogger.WARNING), "Test warning logger for warning-logging");
assertFalse(logger.isLogging(ILogger.ERROR), "Test warning logger for error-logging");
logger.setIsLogging(ILogger.ERROR, true);
assertFalse(logger.isLogging(ILogger.INFO),
"Set error-logging for warning logger and test for info-logging");
assertTrue(logger.isLogging(ILogger.WARNING),
"Set error-logging for warning logger and test for warning-logging");
assertTrue(logger.isLogging(ILogger.ERROR),
"Set error-logging for warning logger and test for error-logging");
}
@Test
@DisplayName("Logger with severity ERROR")
public void testErrorLogging() {
IllegalStateException exception = new IllegalStateException();
FilteringLogger logger = new FilteringLogger(subLogger, ILogger.ERROR);
String formatString = "%s: %s (%s)";
subLogger.setFormatString(formatString);
logger.log(ILogger.ERROR, "Noe er feil!", exception);
assertEquals(String.format(formatString, ILogger.ERROR, "Noe er feil!", exception),
stream.toString().trim(), "Test the format of error message after logging");
}
@Test
@DisplayName("Logger with severity INFO in ERROR logger")
public void testInfoToErrorLogger() {
IllegalStateException exception = new IllegalStateException();
FilteringLogger logger = new FilteringLogger(subLogger, ILogger.ERROR);
String formatString = "%s: %s (%s)";
subLogger.setFormatString(formatString);
logger.log(ILogger.INFO, "Noe er feil!", exception);
assertEquals("", stream.toString(),
"Test the stream after logging info message in error logger");
}
}

View File

@@ -0,0 +1,49 @@
package oving6.logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class StreamLoggerTest {
private static final String formatString = "%s: %s (%s)";
private static final String melding = "En melding ble logget!";
private ByteArrayOutputStream stream;
private StreamLogger logger;
@BeforeEach
public void setUp() {
stream = new ByteArrayOutputStream();
logger = new StreamLogger(stream);
}
@Test
@DisplayName("Logs info message")
public void testLog() {
logger.log(ILogger.INFO, melding, null);
assertTrue(stream.toString().contains(melding),
"Test that the stream contains the message after it has been logged");
assertTrue(stream.toString().contains(ILogger.INFO),
"Test that the stream contains a message of type info after it has been logged");
}
@Test
@DisplayName("Logs exception")
public void testLogException() {
assertThrows(IllegalArgumentException.class, () -> {
logger.setFormatString(null);
}, "Test that IllegalArgumentException is thrown when the format is null");
IllegalStateException exception = new IllegalStateException();
logger.setFormatString(formatString);
logger.log(ILogger.INFO, melding, exception);
assertEquals(String.format(formatString, ILogger.INFO, melding, exception),
stream.toString().trim(),
"Test the format of the message that was logged with an exception");
}
}