From c1dda6a99316a3fc4a3e7629c70d5771783c4c1d Mon Sep 17 00:00:00 2001 From: felmer <felmer> Date: Mon, 6 Dec 2010 13:00:32 +0000 Subject: [PATCH] LMS-1880 useful helper class for testing with jmock SVN: 19011 --- .../cisd/common/test/RecordingMatcher.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 common/source/java/ch/systemsx/cisd/common/test/RecordingMatcher.java diff --git a/common/source/java/ch/systemsx/cisd/common/test/RecordingMatcher.java b/common/source/java/ch/systemsx/cisd/common/test/RecordingMatcher.java new file mode 100644 index 00000000000..4e29464e12d --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/test/RecordingMatcher.java @@ -0,0 +1,64 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * 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 ch.systemsx.cisd.common.test; + +import java.util.ArrayList; +import java.util.List; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +/** + * A {@link Matcher} which always matches the objects to be matched and make them available. + * + * @author Franz-Josef Elmer + */ +public class RecordingMatcher<T> extends BaseMatcher<T> +{ + + private List<T> objects = new ArrayList<T>(); + + /** + * Removes all recored objects. + */ + public void reset() + { + objects.clear(); + } + + /** + * Returns the objects in the order they have been recorded. + */ + public List<T> getRecordedObjects() + { + return objects; + } + + @SuppressWarnings("unchecked") + public boolean matches(Object item) + { + objects.add((T) item); + return true; + } + + public void describeTo(Description description) + { + description.appendText("<recording matcher>"); + } + +} -- GitLab