use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.
the class LogEventTest method testSerialization.
@SuppressWarnings("BanSerializableRead")
@Test
public void testSerialization() 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!")).build();
final Exception parent = new IllegalStateException("Test");
final Throwable child = new LoggingException("This is a test", parent);
final LogEvent event2 = //
Log4jLogEvent.newBuilder().setLoggerName(//
this.getClass().getName()).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(//
Level.INFO).setMessage(//
new SimpleMessage("Hello, world!")).setThrown(//
child).build();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(event1);
oos.writeObject(event2);
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new FilteredObjectInputStream(bais);
try {
ois.readObject();
} catch (final IOException ioe) {
fail("Exception processing event1");
}
try {
ois.readObject();
} catch (final IOException ioe) {
fail("Exception processing event2");
}
}
use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.
the class RingBufferLogEventTest method testSerializationDeserialization.
@SuppressWarnings("BanSerializableRead")
@Test
public void testSerializationDeserialization() throws IOException, ClassNotFoundException {
final RingBufferLogEvent evt = new RingBufferLogEvent();
final String loggerName = "logger.name";
final Marker marker = null;
final String fqcn = "f.q.c.n";
final Level level = Level.TRACE;
final Message data = new SimpleMessage("message");
final Throwable t = new InternalError("not a real error");
final ContextStack contextStack = null;
final String threadName = "main";
final StackTraceElement location = null;
evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(12345, 678), new DummyNanoClock(1));
((StringMap) evt.getContextData()).putValue("key", "value");
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(evt);
final ObjectInputStream in = new FilteredObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
final RingBufferLogEvent other = (RingBufferLogEvent) in.readObject();
assertEquals(loggerName, other.getLoggerName());
assertEquals(marker, other.getMarker());
assertEquals(fqcn, other.getLoggerFqcn());
assertEquals(level, other.getLevel());
assertEquals(data, other.getMessage());
assertNull("null after serialization", other.getThrown());
assertEquals(new ThrowableProxy(t), other.getThrownProxy());
assertEquals(evt.getContextData(), other.getContextData());
assertEquals(contextStack, other.getContextStack());
assertEquals(threadName, other.getThreadName());
assertEquals(location, other.getSource());
assertEquals(12345, other.getTimeMillis());
assertEquals(678, other.getInstant().getNanoOfMillisecond());
}
use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.
the class MutableLogEventTest method deserialize.
@SuppressWarnings("BanSerializableRead")
private Log4jLogEvent deserialize(final byte[] binary) throws IOException, ClassNotFoundException {
final ByteArrayInputStream inArr = new ByteArrayInputStream(binary);
final ObjectInputStream in = useObjectInputStream ? new ObjectInputStream(inArr) : new FilteredObjectInputStream(inArr);
final Log4jLogEvent result = (Log4jLogEvent) in.readObject();
return result;
}
use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.
the class Log4jLogEventTest method deserialize.
@SuppressWarnings("BanSerializableRead")
private Log4jLogEvent deserialize(final byte[] binary) throws IOException, ClassNotFoundException {
final ByteArrayInputStream inArr = new ByteArrayInputStream(binary);
final ObjectInputStream in = new FilteredObjectInputStream(inArr);
final Log4jLogEvent result = (Log4jLogEvent) in.readObject();
return result;
}
use of org.apache.logging.log4j.util.FilteredObjectInputStream in project logging-log4j2 by apache.
the class LogEventTest method testNanoTimeIsNotSerialized2.
@SuppressWarnings("BanSerializableRead")
@Test
public void testNanoTimeIsNotSerialized2() 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!")).setThreadId(// this must be initialized or the test fails
1).setThreadName(//
"this must be initialized or the test fails").setThreadPriority(// this must be initialized or the test fails
2).setNanoTime(//
0).build();
final LogEvent event2 = 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();
assertEquals(event2, actual, "both zero nanoTime");
}
Aggregations