|
| 1 | +/* |
| 2 | + * Copyright 2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core.mapping.event; |
| 17 | + |
| 18 | +import static org.hamcrest.core.StringStartsWith.*; |
| 19 | +import static org.junit.Assert.*; |
| 20 | + |
| 21 | +import ch.qos.logback.classic.Level; |
| 22 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 23 | +import ch.qos.logback.core.read.ListAppender; |
| 24 | + |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import com.mongodb.BasicDBObject; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Christoph Strobl |
| 33 | + */ |
| 34 | +public class LoggingEventListenerTests { |
| 35 | + |
| 36 | + LoggingEventListener listener; |
| 37 | + ListAppender<ILoggingEvent> appender; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() { |
| 41 | + |
| 42 | + // set log level for LoggingEventListener to "info" and set up an appender capturing events. |
| 43 | + ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory |
| 44 | + .getLogger(LoggingEventListener.class); |
| 45 | + logger.setLevel(Level.toLevel("info")); |
| 46 | + |
| 47 | + appender = new ListAppender(); |
| 48 | + logger.addAppender(appender); |
| 49 | + appender.start(); |
| 50 | + |
| 51 | + listener = new LoggingEventListener(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test // DATAMONGO-1645 |
| 55 | + public void shouldSerializeAfterConvertEventCorrectly() { |
| 56 | + |
| 57 | + listener.onAfterConvert(new AfterConvertEvent<Object>(new BasicDBObject("foo", new Foo()), this, "collection")); |
| 58 | + |
| 59 | + assertThat(appender.list.get(0).getFormattedMessage(), startsWith("onAfterConvert: { \"foo\"")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test // DATAMONGO-1645 |
| 63 | + public void shouldSerializeBeforeSaveEventEventCorrectly() { |
| 64 | + |
| 65 | + listener.onBeforeSave(new BeforeSaveEvent<Object>(new Foo(), new BasicDBObject("foo", new Foo()), "collection")); |
| 66 | + |
| 67 | + assertThat(appender.list.get(0).getFormattedMessage(), |
| 68 | + startsWith("onBeforeSave: org.springframework.data.mongodb.core.")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test // DATAMONGO-1645 |
| 72 | + public void shouldSerializeAfterSaveEventEventCorrectly() { |
| 73 | + |
| 74 | + listener.onAfterSave(new AfterSaveEvent<Object>(new Foo(), new BasicDBObject("foo", new Foo()), "collection")); |
| 75 | + |
| 76 | + assertThat(appender.list.get(0).getFormattedMessage(), |
| 77 | + startsWith("onAfterSave: org.springframework.data.mongodb.core.")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test // DATAMONGO-1645 |
| 81 | + public void shouldSerializeBeforeDeleteEventEventCorrectly() { |
| 82 | + |
| 83 | + listener |
| 84 | + .onBeforeDelete(new BeforeDeleteEvent<Object>(new BasicDBObject("foo", new Foo()), Object.class, "collection")); |
| 85 | + |
| 86 | + assertThat(appender.list.get(0).getFormattedMessage(), startsWith("onBeforeDelete: { \"foo\"")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test // DATAMONGO-1645 |
| 90 | + public void shouldSerializeAfterDeleteEventEventCorrectly() { |
| 91 | + |
| 92 | + listener |
| 93 | + .onAfterDelete(new AfterDeleteEvent<Object>(new BasicDBObject("foo", new Foo()), Object.class, "collection")); |
| 94 | + |
| 95 | + assertThat(appender.list.get(0).getFormattedMessage(), startsWith("onAfterDelete: { \"foo\"")); |
| 96 | + } |
| 97 | + |
| 98 | + static class Foo { |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments