View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration2.builder;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  import java.util.List;
26  
27  import org.apache.commons.configuration2.event.EventListener;
28  import org.apache.commons.configuration2.event.EventType;
29  
30  /**
31   * A test implementation of an event listener for configuration builders. This class is used by some unit tests. It
32   * collects the events received by the listener and provides some methods for querying them.
33   */
34  public class BuilderEventListenerImpl implements EventListener<ConfigurationBuilderEvent> {
35      /** A list with the received events. */
36      private final List<ConfigurationBuilderEvent> events = new LinkedList<>();
37  
38      /** An iterator for inspecting the received events. */
39      private Iterator<ConfigurationBuilderEvent> iterator;
40  
41      /**
42       * Checks that no further events have been received by this listener.
43       */
44      public void assertNoMoreEvents() {
45          assertFalse(initIterator().hasNext());
46      }
47  
48      /**
49       * Ensures that the iterator for received events has been initialized.
50       *
51       * @return the iterator to be used
52       */
53      private Iterator<ConfigurationBuilderEvent> initIterator() {
54          if (iterator == null) {
55              iterator = events.iterator();
56          }
57          return iterator;
58      }
59  
60      /**
61       * Checks whether the next received event is of the specified event type and returns it. Causes the test to fail if
62       * there are no more events or the next event is of a different event type.
63       *
64       * @param eventType the expected event type
65       * @param <T> the type of the received event
66       * @return the next received event
67       */
68      public <T extends ConfigurationBuilderEvent> T nextEvent(final EventType<T> eventType) {
69          final Iterator<ConfigurationBuilderEvent> it = initIterator();
70          assertTrue(it.hasNext());
71          final ConfigurationBuilderEvent nextEvent = it.next();
72          assertEquals(eventType, nextEvent.getEventType());
73          // Safe cast because of the comparison of the event type
74          @SuppressWarnings("unchecked")
75          final T resultEvent = (T) nextEvent;
76          return resultEvent;
77      }
78  
79      /**
80       * {@inheritDoc} This implementation just records the event.
81       */
82      @Override
83      public void onEvent(final ConfigurationBuilderEvent event) {
84          events.add(event);
85      }
86  }