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;
18  
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.Matchers.containsString;
21  import static org.hamcrest.Matchers.endsWith;
22  import static org.hamcrest.Matchers.startsWith;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  
25  import org.apache.commons.configuration2.sync.Synchronizer;
26  
27  /**
28   * A test implementation of Synchronizer which allows keeping track about the methods called by the configuration.
29   */
30  public class SynchronizerTestImpl implements Synchronizer {
31      /**
32       * An enumeration with the methods of the Synchronizer which can be called.
33       */
34      public enum Methods {
35          BEGIN_READ, END_READ, BEGIN_WRITE, END_WRITE
36      }
37  
38      /** A buffer for registering the methods invoked by clients. */
39      private final StringBuilder methods = new StringBuilder();
40  
41      /**
42       * Adds a method name to the internal buffer. Called by all interface methods.
43       *
44       * @param m the method that was invoked
45       */
46      private void append(final Methods m) {
47          methods.append(m);
48      }
49  
50      /**
51       * {@inheritDoc} Registers this invocation.
52       */
53      @Override
54      public void beginRead() {
55          append(Methods.BEGIN_READ);
56      }
57  
58      /**
59       * {@inheritDoc} Registers this invocation.
60       */
61      @Override
62      public void beginWrite() {
63          append(Methods.BEGIN_WRITE);
64      }
65  
66      /**
67       * Clears the methods recorded so far.
68       */
69      public void clear() {
70          methods.setLength(0);
71      }
72  
73      /**
74       * Generates a string with expected methods from the given array.
75       *
76       * @param expMethods the array with expected methods
77       * @return a corresponding string representation
78       */
79      private String constructExpectedMethods(final Methods... expMethods) {
80          final StringBuilder buf = new StringBuilder();
81          for (final Methods m : expMethods) {
82              buf.append(m);
83          }
84          return buf.toString();
85      }
86  
87      /**
88       * {@inheritDoc} Registers this invocation.
89       */
90      @Override
91      public void endRead() {
92          append(Methods.END_READ);
93      }
94  
95      /**
96       * {@inheritDoc} Registers this invocation.
97       */
98      @Override
99      public void endWrite() {
100         append(Methods.END_WRITE);
101     }
102 
103     /**
104      * Verifies that the passed in methods were called in this order.
105      *
106      * @param expMethods the expected methods
107      */
108     public void verify(final Methods... expMethods) {
109         assertEquals(constructExpectedMethods(expMethods), methods.toString());
110     }
111 
112     /**
113      * Verifies that the specified sequence of methods was called somewhere in the interaction with the synchronizer.
114      *
115      * @param expMethods the expected methods
116      */
117     public void verifyContains(final Methods... expMethods) {
118         assertThat(methods.toString(), containsString(constructExpectedMethods(expMethods)));
119     }
120 
121     /**
122      * Verifies that the specified methods were called at the end of the interaction with the synchronizer.
123      *
124      * @param expMethods the expected methods
125      */
126     public void verifyEnd(final Methods... expMethods) {
127         assertThat(methods.toString(), endsWith(constructExpectedMethods(expMethods)));
128     }
129 
130     /**
131      * Verifies that the specified methods were called at the beginning of the interaction with the synchronizer.
132      *
133      * @param expMethods the expected methods
134      */
135     public void verifyStart(final Methods... expMethods) {
136         assertThat(methods.toString(), startsWith(constructExpectedMethods(expMethods)));
137     }
138 }