Search in sources :

Example 6 with FilteredObjectInputStream

use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.

the class LogEventTest method testNanoTimeIsNotSerialized1.

@SuppressWarnings("BanSerializableRead")
@Test
public void testNanoTimeIsNotSerialized1() throws Exception {
    final LogEvent event1 = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    this.getClass().getName()).setLoggerFqcn(// 
    "org.apache.logging.log4j.core.Logger").setLevel(// 
    Level.INFO).setMessage(// 
    new SimpleMessage("Hello, world!")).setThreadName(// 
    "this must be initialized or the test fails").setNanoTime(// 
    12345678L).build();
    final LogEvent copy = new Log4jLogEvent.Builder(event1).build();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(event1);
    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    final ObjectInputStream ois = new FilteredObjectInputStream(bais);
    final LogEvent actual = (LogEvent) ois.readObject();
    assertNotEquals(copy, actual, "Different event: nanoTime");
    assertNotEquals(copy.getNanoTime(), actual.getNanoTime(), "Different nanoTime");
    assertEquals(0, actual.getNanoTime(), "deserialized nanoTime is zero");
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)6 ObjectInputStream (java.io.ObjectInputStream)6 FilteredObjectInputStream (org.apache.logging.log4j.util.FilteredObjectInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)4 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)1 Level (org.apache.logging.log4j.Level)1 LoggingException (org.apache.logging.log4j.LoggingException)1 Marker (org.apache.logging.log4j.Marker)1 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)1 ThrowableProxy (org.apache.logging.log4j.core.impl.ThrowableProxy)1 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)1 FixedPreciseClock (org.apache.logging.log4j.core.time.internal.FixedPreciseClock)1 Message (org.apache.logging.log4j.message.Message)1 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)1 StringMap (org.apache.logging.log4j.util.StringMap)1 Test (org.junit.Test)1