io.zeebe.test.util.BufferWriterMatcher Maven / Gradle / Ivy
/*
* Copyright © 2017 camunda services GmbH ([email protected])
*
* Licensed 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 io.zeebe.test.util;
import io.zeebe.util.buffer.BufferReader;
import io.zeebe.util.buffer.BufferWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.agrona.concurrent.UnsafeBuffer;
import org.hamcrest.Matcher;
import org.mockito.ArgumentMatcher;
/**
* Note: this matcher does not work when a {@link BufferWriter} is reused throughout a test. Mockito
* only captures the reference, so after the test the {@link BufferWriter} contains the latest
* state.
*
* @author Lindhauer
*/
public class BufferWriterMatcher implements ArgumentMatcher {
protected T reader;
protected List> propertyMatchers = new ArrayList<>();
public BufferWriterMatcher(T reader) {
this.reader = reader;
}
@Override
public boolean matches(BufferWriter argument) {
if (argument == null) {
return false;
}
final UnsafeBuffer buffer = new UnsafeBuffer(new byte[argument.getLength()]);
argument.write(buffer, 0);
reader.wrap(buffer, 0, buffer.capacity());
for (BufferReaderMatch matcher : propertyMatchers) {
if (!matcher.matches(reader)) {
return false;
}
}
return true;
}
public BufferWriterMatcher matching(Function actualProperty, Object expectedValue) {
final BufferReaderMatch match = new BufferReaderMatch<>();
match.propertyExtractor = actualProperty;
if (expectedValue instanceof Matcher) {
match.expectedValueMatcher = (Matcher>) expectedValue;
} else {
match.expectedValue = expectedValue;
}
propertyMatchers.add(match);
return this;
}
public static BufferWriterMatcher writesProperties(
Class readerClass) {
try {
final BufferWriterMatcher matcher = new BufferWriterMatcher<>(readerClass.newInstance());
return matcher;
} catch (Exception e) {
throw new RuntimeException("Could not construct matcher", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy