org.codehaus.btm
btm
diff --git a/spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java b/spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java
new file mode 100644
index 0000000000..525ec20083
--- /dev/null
+++ b/spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2012-2015 the original author or authors.
+ *
+ * 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 org.springframework.boot.test.assertj;
+
+import org.assertj.core.api.Condition;
+import org.hamcrest.Matcher;
+import org.hamcrest.StringDescription;
+
+import org.springframework.util.Assert;
+
+/**
+ * Adapter class allowing a Hamcrest {@link Matcher} to be used as an AssertJ
+ * {@link Condition}.
+ *
+ * Usually used with the {@code is} method of {@code assertThat}, for example:
+ *
+ *
+ * assertThat("1234").is(Matched.when(startsWith("12")));
+ *
+ *
+ * @param The type of object that the condition accepts
+ * @author Phillip Webb
+ * @since 1.4
+ */
+public final class Matched extends Condition {
+
+ private final Matcher extends T> matcher;
+
+ private Matched(Matcher extends T> matcher) {
+ Assert.notNull(matcher, "Matcher must not be null");
+ this.matcher = matcher;
+ }
+
+ @Override
+ public boolean matches(final T value) {
+ if (this.matcher.matches(value)) {
+ return true;
+ }
+ StringDescription description = new StringDescription();
+ this.matcher.describeTo(description);
+ describedAs(description.toString());
+ return false;
+ }
+
+ public static Condition when(Matcher extends T> matcher) {
+ return by(matcher);
+ }
+
+ public static Condition by(Matcher extends T> matcher) {
+ return new Matched(matcher);
+ }
+
+}
diff --git a/spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java b/spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java
new file mode 100644
index 0000000000..a07a7501d5
--- /dev/null
+++ b/spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2012-2015 the original author or authors.
+ *
+ * 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 org.springframework.boot.test.assertj;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.startsWith;
+
+/**
+ * Tests for {@link Matched}.
+ *
+ * @author Phillip Webb
+ */
+public class MatchedTests {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void byMatcherMatches() {
+ assertThat("1234").is(Matched.by(startsWith("12")));
+ }
+
+ @Test
+ public void byMatcherDoesNotMatch() {
+ this.thrown.expect(AssertionError.class);
+ this.thrown.expectMessage("a string starting with \"23\"");
+ assertThat("1234").is(Matched.by(startsWith("23")));
+ }
+
+ @Test
+ public void whenMatcherMatches() {
+ assertThat("1234").is(Matched.when(startsWith("12")));
+ }
+
+ @Test
+ public void whenMatcherDoesNotMatch() {
+ this.thrown.expect(AssertionError.class);
+ this.thrown.expectMessage("a string starting with \"23\"");
+ assertThat("1234").is(Matched.when(startsWith("23")));
+ }
+
+}