blob: 1d6fc51268f38492ac13db9d9cab9ce1fc0689ea [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.configuration2;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.commons.configuration2.sync.Synchronizer;
/**
* A test implementation of Synchronizer which allows keeping track about the methods called by the configuration.
*/
public class SynchronizerTestImpl implements Synchronizer {
/**
* An enumeration with the methods of the Synchronizer which can be called.
*/
public enum Methods {
BEGIN_READ, END_READ, BEGIN_WRITE, END_WRITE
}
/** A buffer for registering the methods invoked by clients. */
private final StringBuilder methods = new StringBuilder();
/**
* Adds a method name to the internal buffer. Called by all interface methods.
*
* @param m the method that was invoked
*/
private void append(final Methods m) {
methods.append(m);
}
/**
* {@inheritDoc} Registers this invocation.
*/
@Override
public void beginRead() {
append(Methods.BEGIN_READ);
}
/**
* {@inheritDoc} Registers this invocation.
*/
@Override
public void beginWrite() {
append(Methods.BEGIN_WRITE);
}
/**
* Clears the methods recorded so far.
*/
public void clear() {
methods.setLength(0);
}
/**
* Generates a string with expected methods from the given array.
*
* @param expMethods the array with expected methods
* @return a corresponding string representation
*/
private String constructExpectedMethods(final Methods... expMethods) {
final StringBuilder buf = new StringBuilder();
for (final Methods m : expMethods) {
buf.append(m);
}
return buf.toString();
}
/**
* {@inheritDoc} Registers this invocation.
*/
@Override
public void endRead() {
append(Methods.END_READ);
}
/**
* {@inheritDoc} Registers this invocation.
*/
@Override
public void endWrite() {
append(Methods.END_WRITE);
}
/**
* Verifies that the passed in methods were called in this order.
*
* @param expMethods the expected methods
*/
public void verify(final Methods... expMethods) {
assertEquals(constructExpectedMethods(expMethods), methods.toString());
}
/**
* Verifies that the specified sequence of methods was called somewhere in the interaction with the synchronizer.
*
* @param expMethods the expected methods
*/
public void verifyContains(final Methods... expMethods) {
assertThat(methods.toString(), containsString(constructExpectedMethods(expMethods)));
}
/**
* Verifies that the specified methods were called at the end of the interaction with the synchronizer.
*
* @param expMethods the expected methods
*/
public void verifyEnd(final Methods... expMethods) {
assertThat(methods.toString(), endsWith(constructExpectedMethods(expMethods)));
}
/**
* Verifies that the specified methods were called at the beginning of the interaction with the synchronizer.
*
* @param expMethods the expected methods
*/
public void verifyStart(final Methods... expMethods) {
assertThat(methods.toString(), startsWith(constructExpectedMethods(expMethods)));
}
}