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