Skip to content

Commit

Permalink
LogLine is now a Consumer<String>
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoutanov committed Apr 12, 2018
1 parent 782e6ca commit e6f5014
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: "maven-publish"
apply plugin: "com.jfrog.bintray"

group = "com.obsidiandynamics.fulcrum"
version = "0.13.0-SNAPSHOT"
version = "0.13.0"

def envUser = "BINTRAY_USER"
def envKey = "BINTRAY_KEY"
Expand Down
9 changes: 6 additions & 3 deletions func/src/main/java/com/obsidiandynamics/func/LogLine.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.obsidiandynamics.func;

import java.io.*;
import java.util.function.*;

@FunctionalInterface
public interface LogLine {
public interface LogLine extends Consumer<String> {
static LogLine nop() {
return __message -> {};
}
Expand All @@ -12,9 +13,11 @@ static LogLine forPrintStream(PrintStream stream) {
return message -> stream.println(message);
}

void println(String message);
default void println(String message) {
accept(message);
}

default void printf(String format, Object... args) {
println(String.format(format, args));
accept(String.format(format, args));
}
}
17 changes: 12 additions & 5 deletions func/src/test/java/com/obsidiandynamics/func/LogLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@
public final class LogLineTest {
@Test
public void testNopCoverage() {
LogLine.nop().println("message");
LogLine.nop().accept("message");
}

@Test
public void testFormat() {
public void testPrintf() {
final LogLine log = mock(LogLine.class, Answers.CALLS_REAL_METHODS);
log.printf("pi is %.2f", 3.14);
verify(log).println(eq("pi is 3.14"));
log.printf("pi is %.2f", Math.PI);
verify(log).accept(eq("pi is 3.14"));
}

@Test
public void testPrintln() {
final LogLine log = mock(LogLine.class, Answers.CALLS_REAL_METHODS);
log.println("message");
verify(log).accept(eq("message"));
}

@Test
public void testForPrintStream() {
final PrintStream stream = mock(PrintStream.class);
final LogLine log = LogLine.forPrintStream(stream);
log.println("message");
log.accept("message");
verify(stream).println(eq("message"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.*;
import org.junit.runner.*;
import org.junit.runners.*;
import org.mockito.*;

import com.obsidiandynamics.assertion.*;
import com.obsidiandynamics.await.*;
Expand Down Expand Up @@ -98,9 +99,9 @@ public void testGeneralHandler() throws Exception {
assertEquals(1, g1.numMessageHandlers());
assertNotNull(g0.channel());

final LogLine l0 = mock(LogLine.class);
final LogLine l0 = mock(LogLine.class, Answers.CALLS_REAL_METHODS);
doCallRealMethod().when(l0).printf(any(), any());
final LogLine l1 = mock(LogLine.class);
final LogLine l1 = mock(LogLine.class, Answers.CALLS_REAL_METHODS);
doCallRealMethod().when(l1).printf(any(), any());
g0.withDebug(l0);
g1.withDebug(l1);
Expand All @@ -120,7 +121,7 @@ public void testGeneralHandler() throws Exception {
assertEquals(0, g1.numMessageHandlers());

verifyNoMoreInteractions(l0);
verify(l1).println(notNull());
verify(l1).accept(notNull());
}

@Test
Expand All @@ -132,7 +133,7 @@ public void testHandlerError() throws Exception {
throw new Exception("testHandlerError");
}).connect(cluster);

final LogLine l1 = mock(LogLine.class);
final LogLine l1 = mock(LogLine.class, Answers.CALLS_REAL_METHODS);
doCallRealMethod().when(l1).printf(any(), any());
g1.withDebug(l1);

Expand All @@ -146,7 +147,7 @@ public void testHandlerError() throws Exception {
wait.until(() -> {
verify(eh1).onException(notNull(), notNull());
});
verify(l1).println(notNull());
verify(l1).accept(notNull());
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions props/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ def packageName = "fulcrum-props"
version = project(":").version

dependencies {
compile project(":fulcrum-func")

testCompile project(":fulcrum-assert")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.*;
import java.util.function.*;

import com.obsidiandynamics.func.*;

public final class PropsFormat {
private PropsFormat() {}

Expand All @@ -16,7 +18,7 @@ public static int measureKeyWidth(Properties props, Predicate<String> keyPredica
.max(Integer::compare).orElse(0);
}

public static void printStandard(Consumer<String> logLine,
public static void printStandard(LogLine logLine,
Properties props,
int maxKeyPad,
String keyPrefix) {
Expand Down Expand Up @@ -49,15 +51,15 @@ public static Predicate<String> any() {
return s -> true;
}

public static void printProps(Consumer<String> logLine,
public static void printProps(LogLine logLine,
Properties props,
Function<String, String> keyFormat,
Function<String, String> valueFormat,
Predicate<String> keyPredicate) {
for (Enumeration<?> keys = props.propertyNames(); keys.hasMoreElements();) {
final String key = (String) keys.nextElement();
if (keyPredicate.test(key)) {
logLine.accept(keyFormat.apply(key) + ":" + valueFormat.apply(props.getProperty(key)));
logLine.println(keyFormat.apply(key) + ":" + valueFormat.apply(props.getProperty(key)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.Mockito.*;

import org.junit.*;
import org.mockito.*;

import com.obsidiandynamics.assertion.*;
import com.obsidiandynamics.func.*;
Expand Down Expand Up @@ -44,14 +45,14 @@ public void testEnabledNoName() throws Throwable {
@Test
public void testEnabledWithName() throws Throwable {
final CheckedRunnable<?> r = mock(CheckedRunnable.class);
final LogLine logLine = mock(LogLine.class);
final LogLine logLine = mock(LogLine.class, Answers.CALLS_REAL_METHODS);
doCallRealMethod().when(logLine).printf(any(), any());
Testmark.enable().withOptions(LogLine.class, logLine);
assertTrue(Testmark.isEnabled());

Testmark.ifEnabled("name", r);
verify(r).run();
verify(logLine).println(isNotNull());
verify(logLine).accept(isNotNull());
}

@Test
Expand Down

0 comments on commit e6f5014

Please sign in to comment.