|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 Talsma ICT |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package nl.talsmasoftware.context.opentracing; |
| 17 | + |
| 18 | +import io.opentracing.mock.MockSpan; |
| 19 | +import org.hamcrest.BaseMatcher; |
| 20 | +import org.hamcrest.Description; |
| 21 | +import org.hamcrest.Matcher; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collection; |
| 25 | + |
| 26 | +import static org.hamcrest.Matchers.hasItem; |
| 27 | +import static org.hamcrest.Matchers.is; |
| 28 | +import static org.junit.Assert.fail; |
| 29 | + |
| 30 | +/** |
| 31 | + * Hamcrest {@linkplain Matcher} for {@link MockSpan} objects. |
| 32 | + * |
| 33 | + * @author Sjoerd Talsma |
| 34 | + */ |
| 35 | +public class MockSpanMatcher extends BaseMatcher<MockSpan> { |
| 36 | + |
| 37 | + private Collection<Matcher<String>> operationNameMatchers = new ArrayList<Matcher<String>>(); |
| 38 | + private Collection<Matcher<Iterable<? super MockSpan.Reference>>> referenceMatchers = |
| 39 | + new ArrayList<Matcher<Iterable<? super MockSpan.Reference>>>(); |
| 40 | + |
| 41 | + private MockSpanMatcher() { |
| 42 | + } |
| 43 | + |
| 44 | + public static MockSpanMatcher withOperationName(String operationName) { |
| 45 | + return withOperationName(is(operationName)); |
| 46 | + } |
| 47 | + |
| 48 | + public static MockSpanMatcher withOperationName(Matcher<String> operationName) { |
| 49 | + return new MockSpanMatcher().andOperationName(operationName); |
| 50 | + } |
| 51 | + |
| 52 | + public static MockSpanMatcher withReference(Matcher<MockSpan.Reference> reference) { |
| 53 | + return new MockSpanMatcher().andReference(reference); |
| 54 | + } |
| 55 | + |
| 56 | + public MockSpanMatcher andOperationName(String operationName) { |
| 57 | + return andOperationName(is(operationName)); |
| 58 | + } |
| 59 | + |
| 60 | + public MockSpanMatcher andOperationName(Matcher<String> operationName) { |
| 61 | + if (operationName == null) fail("Operation name matcher is <null>."); |
| 62 | + this.operationNameMatchers.add(operationName); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + public MockSpanMatcher andReference(Matcher<MockSpan.Reference> reference) { |
| 67 | + if (reference == null) fail("Refrence matcher is <null>."); |
| 68 | + this.referenceMatchers.add(hasItem(reference)); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean matches(Object item) { |
| 74 | + if (item == null) return true; |
| 75 | + else if (!(item instanceof MockSpan)) return false; |
| 76 | + final MockSpan mockSpan = (MockSpan) item; |
| 77 | + for (Matcher<String> operationName : operationNameMatchers) { |
| 78 | + if (!operationName.matches(mockSpan.operationName())) return false; |
| 79 | + } |
| 80 | + for (Matcher<Iterable<? super MockSpan.Reference>> reference : referenceMatchers) { |
| 81 | + if (!reference.matches(mockSpan.references())) return false; |
| 82 | + } |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void describeTo(Description description) { |
| 88 | + description.appendText("MockSpan"); |
| 89 | + String sep = " with "; |
| 90 | + for (Matcher<String> operationName : operationNameMatchers) { |
| 91 | + operationName.describeTo(description.appendText(sep).appendText("operationName ")); |
| 92 | + sep = " and "; |
| 93 | + } |
| 94 | + for (Matcher<Iterable<? super MockSpan.Reference>> reference : referenceMatchers) { |
| 95 | + reference.describeTo(description.appendText(sep).appendText("references ")); |
| 96 | + sep = " and "; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments