Created
January 16, 2022 17:15
-
-
Save stefanofago73/7b2a82f0cae86f4d13100087eec45090 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static it.fago.loggerstein.Loggerstein.$; | |
import static it.fago.loggerstein.Loggerstein.lazy; | |
import static it.fago.loggerstein.Loggerstein.logWith; | |
import static it.fago.loggerstein.Loggerstein.STD_ERR; | |
import static it.fago.loggerstein.Loggerstein.STD_OUT; | |
import org.junit.Test; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import static org.junit.Assert.*; | |
/** | |
* | |
* @author Stefano | |
* | |
*/ | |
public class CoreTest { | |
private Logger logger = LoggerFactory.getLogger(Loggerstein.class); | |
@Test | |
public void testLazy_NewTestBean_ToString() { | |
assertEquals("TESTBEAN[0,,null]", lazy(TestBean::new).toString()); | |
} | |
@Test | |
public void testLazy_NewTestBean_WithValue_ToString() { | |
TestBean bean = new TestBean(); | |
bean.setId(1L); | |
bean.setDescription("Simple Description!"); | |
assertEquals("TESTBEAN[1,Simple Description!,null]", lazy(bean::toString).toString()); | |
} | |
@Test | |
public void testLazy_NewTestBean_Arguments_ToString() { | |
TestBean bean = new TestBean(); | |
bean.setId(1L); | |
bean.setDescription("Simple Description!"); | |
String id = lazy(bean::getId).toString(); | |
String description = lazy(bean::getDescription).toString(); | |
assertEquals("1", id); | |
assertEquals("Simple Description!", description); | |
} | |
@Test | |
public void testLogWith_TestBean_Separated_Arguments() { | |
TestBean bean = new TestBean(); | |
bean.setId(1L); | |
bean.setDescription("Simple Description!"); | |
bean.setData(new String[] { "data1", "data2", "data3", "data4", "data5" }); | |
logWith(logger::info, "This is a TestBean: {}\nid: {}\ndesc: {}\ndata: {}", bean::toString, bean::getId, | |
bean::getDescription, bean::getData); | |
} | |
@Test | |
public void testLogWith_TestBean_Object_Argument() { | |
TestBean bean = new TestBean(); | |
bean.setId(2L); | |
bean.setDescription("Simple Description!"); | |
bean.setData(new String[] { "data1", "data2", "data3", "data4", "data5" }); | |
logWith(logger::info, "This is a TestBean: {}\nid: {}\ndesc: {}\ndata: {}", $(bean), bean::getId, bean::getDescription, bean::getData); | |
} | |
@Test | |
public void testLogWith_TestBean_Object_Array_Argument() { | |
TestBean bean1 = new TestBean(); | |
bean1.setId(1L); | |
bean1.setDescription("Simple1!"); | |
bean1.setData(new String[] { "data1", "data2", "data3", "data4", "data5" }); | |
TestBean bean2 = new TestBean(); | |
bean2.setId(2L); | |
bean2.setDescription("Simple2!"); | |
bean2.setData(new String[] { "data6", "data7", "data8", "data9", "data10" }); | |
TestBean bean3 = new TestBean(); | |
bean3.setId(3L); | |
bean3.setDescription("Simple3!"); | |
bean3.setData(new String[] { "data11", "data12", "data13", "data14", "data15" }); | |
logWith(logger::info, "We now have more TestBean:\n{}\n{}\n{}", $(bean1,bean2,bean3)); | |
} | |
@Test | |
public void testLogWith_TestBean_STD_OUT(){ | |
TestBean bean = new TestBean(); | |
bean.setId(9L); | |
bean.setDescription("Logging with STD_OUT"); | |
bean.setData(new String[] { "data1", "data2", "data3", "data4", "data5" }); | |
logWith(STD_OUT, "This is a TestBean: %s\nid: %s\ndesc: %s\ndata: %s", $(bean), bean::getId, bean::getDescription, bean::getData); | |
} | |
@Test | |
public void testLogWith_TestBean_STD_ERR(){ | |
TestBean bean = new TestBean(); | |
bean.setId(10L); | |
bean.setDescription("Logging with STD_ERR"); | |
bean.setData(new String[] { "data1", "data2", "data3", "data4", "data5" }); | |
logWith(STD_ERR, "This is a TestBean: %s\nid: %s\ndesc: %s\ndata: %s", $(bean), bean::getId, bean::getDescription, bean::getData); | |
} | |
/** | |
* | |
* @author Stefano | |
* | |
*/ | |
public static class TestBean { | |
// | |
private String description=""; | |
// | |
private long id; | |
// | |
private String[] data; | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String[] getData() { | |
return data; | |
} | |
public void setData(String[] data) { | |
this.data = data; | |
} | |
public String toString() { | |
return "TESTBEAN[" + id + "," + description + "," + String.valueOf(data) + "]"; | |
} | |
}// END | |
}//END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.fago.loggerstein; | |
import static java.lang.String.valueOf; | |
import static java.util.Arrays.stream; | |
import java.util.function.BiConsumer; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
/** | |
* | |
* LOGGERSTEIN - A Monstruos Facade for Your Logging System! | |
* | |
*/ | |
public final class Loggerstein { | |
// | |
private static final String[] SURROGATE_FOR_NULL_ARRAY = {}; | |
// | |
private static final String SURROGATE_FOR_NULL = ""; | |
// | |
private static final Supplier<?>[] SURROGATE_FOR_NULL_ARRAY_SUP = new Supplier<?>[]{()->SURROGATE_FOR_NULL_ARRAY}; | |
// | |
public static final BiConsumer<String, Object[]> STD_OUT = System.out::printf; | |
// | |
public static final BiConsumer<String, Object[]> STD_ERR = System.err::printf; | |
// | |
private static final Function<Object,Supplier<?>> TO_SUPPLIER = object->()->object; | |
//====================================================================== | |
// | |
// | |
// | |
//====================================================================== | |
public static final Object lazy(final Supplier<?> arg) { | |
return arg == null ? | |
SURROGATE_FOR_NULL : new Object() { | |
public final String toString() { | |
return valueOf(arg.get()); | |
} | |
}; | |
} | |
public static final Object[] lazy(final Supplier<?>... args) { | |
return args == null ? | |
SURROGATE_FOR_NULL_ARRAY : | |
stream(args) | |
.map(Loggerstein::lazy) | |
.toArray(); | |
} | |
//====================================================================== | |
// | |
// | |
// | |
//====================================================================== | |
public static final Supplier<?> $(final Object arg) { | |
return arg == null ? | |
()->SURROGATE_FOR_NULL : | |
()->arg; | |
} | |
public static final Supplier<?>[] $(final Object ... args) { | |
return args == null? | |
SURROGATE_FOR_NULL_ARRAY_SUP: | |
stream(args) | |
.map(TO_SUPPLIER) | |
.toArray(Supplier[]::new); | |
} | |
//====================================================================== | |
// | |
// | |
// | |
//====================================================================== | |
public static final void logWith( | |
final BiConsumer<String, Object[]> activity, | |
final String msg, | |
final Supplier<?>... args) { | |
activity.accept(msg, lazy(args)); | |
} | |
}// END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment