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

adding generics to avoid casting the getRawLogs() #7

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
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ The following examples show how to add the framework to a test, provide it with
Logger logger = LogManager.getLogger("acme.Gizmo");

@Rule
public JUnit4LogCollector collector = new JUnit4LogCollector(logger);
public JUnit4LogCollector<LogEvent> collector = new JUnit4LogCollector<>(logger);

@Test
public void testGizmo() {
Gizmo gizmo = new Gizmo();
gizmo.run();

List<LogEvent> rawLogs = (List<LogEvent>) collector.getRawLogs();
List<LogEvent> rawLogs = collector.getRawLogs();
assertTrue(rawLogs.stream().noneMatch(l -> l.getLevel() == Level.ERROR));
}
```
Expand All @@ -57,14 +57,14 @@ public void testGizmo() {
public class GizmoTest {
Logger logger = LogManager.getLogger("acme.Gizmo");

JUnit5LogCollector collector = new JUnit5LogCollector(logger);
JUnit5LogCollector<LogEvent> collector = new JUnit5LogCollector<>(logger);

@Test
void testGizmo() {
Gizmo gizmo = new Gizmo();
gizmo.run();

List<LogEvent> rawLogs = (List<LogEvent>) collector.getRawLogs();
List<LogEvent> rawLogs = collector.getRawLogs();
assertTrue(rawLogs.stream().noneMatch(l -> l.getLevel() == Level.ERROR));
}
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public class GizmoTest {
Gizmo gizmo = new Gizmo();
gizmo.run();

List<LogEvent> rawLogs = (List<LogEvent>) TestNGLogCollector.getRawLogs();
List<LogEvent> rawLogs = TestNGLogCollector.getRawLogs(LoggingEvent.class);
assertTrue(rawLogs.stream().noneMatch(l -> l.getLevel() == Level.ERROR));
}
}
Expand Down Expand Up @@ -127,6 +127,5 @@ testCompile 'dk.bitcraft:LogCollector:0.9.0'
- Come up with a better name for the framework.
- Improve Javadoc and documentation.
- Discover the intricacies of the various logging frameworks in real-world settings and adapt to them.
- Avoid the ugly cast in `getRawLogs`.
- Detect SLF4j's `NOPLogger` and replace it with `SimpleLogger`
- Any ideas? Get in touch!
4 changes: 2 additions & 2 deletions src/main/java/dk/bitcraft/lc/CollectorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.util.List;

interface CollectorImpl {
interface CollectorImpl<LOG> {
void setup();
void remove();
List<String> getResult();
List<?> getRawLogs();
List<LOG> getRawLogs();
}
7 changes: 4 additions & 3 deletions src/main/java/dk/bitcraft/lc/JUnit4LogCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import java.util.Optional;


public class JUnit4LogCollector extends ExternalResource {
private CollectorImpl collector;
public class JUnit4LogCollector<LOG> extends ExternalResource {
private CollectorImpl<LOG> collector;

@SuppressWarnings("unchecked")
public JUnit4LogCollector(Object logger) {
collector = Arrays.stream(Frameworks.values())
.map(v -> v.getCollector(logger))
Expand All @@ -33,7 +34,7 @@ public List<String> getLogs() {
return collector.getResult();
}

public List<?> getRawLogs() {
public List<LOG> getRawLogs() {
return collector.getRawLogs();
}
}
7 changes: 4 additions & 3 deletions src/main/java/dk/bitcraft/lc/JUnit5LogCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import java.util.List;
import java.util.Optional;

public class JUnit5LogCollector {
private CollectorImpl collector;
public class JUnit5LogCollector<LOG> {
private CollectorImpl<LOG> collector;

@SuppressWarnings("unchecked")
public JUnit5LogCollector(Object logger) {
collector = Arrays.stream(Frameworks.values())
.map(v -> v.getCollector(logger))
Expand All @@ -28,7 +29,7 @@ public List<String> getLogs() {
return collector.getResult();
}

public List<?> getRawLogs() {
public List<LOG> getRawLogs() {
return collector.getRawLogs();
}
}
4 changes: 2 additions & 2 deletions src/main/java/dk/bitcraft/lc/JavaUtilLoggingCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import static java.util.stream.Collectors.toList;

public class JavaUtilLoggingCollector implements CollectorImpl {
public class JavaUtilLoggingCollector implements CollectorImpl<LogRecord> {

private final Logger logger;
private ListHandler handler;
Expand Down Expand Up @@ -39,7 +39,7 @@ public List<String> getResult() {
}

@Override
public List<?> getRawLogs() {
public List<LogRecord> getRawLogs() {
return handler.records.stream().collect(toList());
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dk/bitcraft/lc/Log4j2Collector.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import static java.util.stream.Collectors.toList;

class Log4j2Collector implements CollectorImpl {
class Log4j2Collector implements CollectorImpl<LogEvent> {
private final Logger logger;
ListAppender appender;

Expand Down Expand Up @@ -48,7 +48,7 @@ public List<String> getResult() {
}

@Override
public List<?> getRawLogs() {
public List<LogEvent> getRawLogs() {
return appender.events.stream().collect(toList());
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/dk/bitcraft/lc/LogBackCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

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

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

import static java.util.stream.Collectors.toList;

class LogBackCollector implements CollectorImpl {
class LogBackCollector implements CollectorImpl<ILoggingEvent> {
private final Logger logger;

private final static String appenderName = UUID.randomUUID().toString();
Expand Down Expand Up @@ -41,7 +40,7 @@ public List<String> getResult() {
}

@Override
public List<?> getRawLogs() {
public List<ILoggingEvent> getRawLogs() {
return appender.list.stream().collect(toList());
}
}
4 changes: 2 additions & 2 deletions src/main/java/dk/bitcraft/lc/SLF4JSimpleCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* <p>SLF4J's SimpleLogger works by printing directly to System.err, so this collector assumes control of System.err and captures its output.</p>
* <p>Any other usage of <code>System.err</code> will be included among the captured log statements.</p>
*/
public class SLF4JSimpleCollector implements CollectorImpl {
public class SLF4JSimpleCollector implements CollectorImpl<String> {

private PrintStream systemErr;
private CapturingPrintStream capturingPrintStream;
Expand All @@ -38,7 +38,7 @@ public List<String> getResult() {
}

@Override
public List<?> getRawLogs() {
public List<String> getRawLogs() {
return getResult();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dk/bitcraft/lc/TestNGLogCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static List<String> getLogs() {
return collector.getResult();
}

public static List<?> getRawLogs() {
public static <LOG> List<LOG> getRawLogs(Class<LOG> clazz) {
return collector.getRawLogs();
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/dk/bitcraft/lc/JavaUtilLoggingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class JavaUtilLoggingTest {
@Rule
public JUnit4LogCollector collector = new JUnit4LogCollector(Logger.getLogger("test.logger"));
public JUnit4LogCollector<LogRecord> collector = new JUnit4LogCollector<>(Logger.getLogger("test.logger"));

@Test
public void test() {
Expand All @@ -29,7 +29,7 @@ public void test() {
assertThat(logs.get(0)).contains("This is an warning!");
assertThat(logs.get(1)).contains("This is another warning!");

List<LogRecord> rawLogs = (List<LogRecord>) collector.getRawLogs();
List<LogRecord> rawLogs = collector.getRawLogs();
assertTrue(rawLogs.stream().allMatch(e -> e.getLevel() == Level.WARNING));
}
}
4 changes: 2 additions & 2 deletions src/test/java/dk/bitcraft/lc/Log4j2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class Log4j2Test {
@Rule
public JUnit4LogCollector collector = new JUnit4LogCollector(LogManager.getLogger("test.logger"));
public JUnit4LogCollector<LogEvent> collector = new JUnit4LogCollector<>(LogManager.getLogger("test.logger"));

@Test
public void test() {
Expand All @@ -28,7 +28,7 @@ public void test() {
assertThat(collector.getLogs()).hasSize(3)
.contains("This is an error!", "This is another error!", "This is a third error!");

List<LogEvent> rawLogs = (List<LogEvent>) collector.getRawLogs();
List<LogEvent> rawLogs = collector.getRawLogs();
assertThat(rawLogs).hasSize(3);

assertTrue(rawLogs.stream().allMatch(l -> l.getLevel() == Level.ERROR));
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/dk/bitcraft/lc/LogbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class LogbackTest {
@Rule
public JUnit4LogCollector collector = new JUnit4LogCollector(LoggerFactory.getLogger("test.logger"));
public JUnit4LogCollector<LoggingEvent> collector = new JUnit4LogCollector<>(LoggerFactory.getLogger("test.logger"));

@Test
public void test() {
Expand All @@ -32,7 +32,7 @@ public void test() {
assertThat(collector.getLogs()).hasSize(1);
assertThat(collector.getLogs().get(0)).contains("This should be collected!");

List<LoggingEvent> rawLogs = (List<LoggingEvent>) collector.getRawLogs();
List<LoggingEvent> rawLogs = collector.getRawLogs();
assertTrue(rawLogs.stream().allMatch(e -> e.getLevel() == Level.WARN));
}
}
2 changes: 1 addition & 1 deletion test-environments/slf4j/src/test/java/test/RuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RuleTest {
final static Logger logger = LoggerFactory.getLogger(RuleTest.class);

@Rule
public JUnit4LogCollector collector = new JUnit4LogCollector(logger);
public JUnit4LogCollector<String> collector = new JUnit4LogCollector<>(logger);


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void sometimesFails() {
log.warn("This is a warning.");
log.info("This is purely informational.");

List<LoggingEvent> logs = (List<LoggingEvent>) TestNGLogCollector.getRawLogs();
List<LoggingEvent> logs = TestNGLogCollector.getRawLogs(LoggingEvent.class);

assertThat(logs)
.hasSize(2)
Expand Down