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