();
+ sources.addFirst(new MapPropertySource("test", map));
+ return map;
+ }
+
+ private static int getSeparatorIndex(String pair) {
+ int colonIndex = pair.indexOf(":");
+ int equalIndex = pair.indexOf("=");
+ if (colonIndex == -1) {
+ return equalIndex;
+ }
+ if (equalIndex == -1) {
+ return colonIndex;
+ }
+ return Math.min(colonIndex, equalIndex);
+ }
+
+}
diff --git a/spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java b/spring-boot/src/test/java/org/springframework/boot/testutil/Matched.java
similarity index 84%
rename from spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java
rename to spring-boot/src/test/java/org/springframework/boot/testutil/Matched.java
index 525ec20083..4ab4ee81b8 100644
--- a/spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java
+++ b/spring-boot/src/test/java/org/springframework/boot/testutil/Matched.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2015 the original author or authors.
+ * Copyright 2012-2016 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.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.springframework.boot.test.assertj;
+package org.springframework.boot.testutil;
import org.assertj.core.api.Condition;
import org.hamcrest.Matcher;
@@ -25,16 +25,9 @@ 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 {
diff --git a/spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java b/spring-boot/src/test/java/org/springframework/boot/testutil/MatchedTests.java
similarity index 93%
rename from spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java
rename to spring-boot/src/test/java/org/springframework/boot/testutil/MatchedTests.java
index a07a7501d5..bb1cc2306c 100644
--- a/spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java
+++ b/spring-boot/src/test/java/org/springframework/boot/testutil/MatchedTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2015 the original author or authors.
+ * Copyright 2012-2016 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.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.springframework.boot.test.assertj;
+package org.springframework.boot.testutil;
import org.junit.Rule;
import org.junit.Test;
diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/util/OutputCapture.java b/spring-boot/src/test/java/org/springframework/boot/testutil/OutputCapture.java
similarity index 70%
rename from spring-boot-cli/src/test/java/org/springframework/boot/cli/util/OutputCapture.java
rename to spring-boot/src/test/java/org/springframework/boot/testutil/OutputCapture.java
index 41f19e2c18..89b1faf5b0 100644
--- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/util/OutputCapture.java
+++ b/spring-boot/src/test/java/org/springframework/boot/testutil/OutputCapture.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2014 the original author or authors.
+ * Copyright 2012-2016 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.
@@ -14,21 +14,31 @@
* limitations under the License.
*/
-package org.springframework.boot.cli.util;
+package org.springframework.boot.testutil;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.List;
+import org.hamcrest.Matcher;
+import org.junit.Assert;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
+import org.springframework.boot.ansi.AnsiOutput;
+import org.springframework.boot.ansi.AnsiOutput.Enabled;
+
+import static org.hamcrest.Matchers.allOf;
+
/**
- * Capture output from System.out and System.err.
+ * Internal JUnit {@code @Rule} to capture output from System.out and System.err.
*
* @author Phillip Webb
+ * @author Andy Wilkinson
*/
public class OutputCapture implements TestRule {
@@ -38,6 +48,8 @@ public class OutputCapture implements TestRule {
private ByteArrayOutputStream copy;
+ private List> matchers = new ArrayList>();
+
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@@ -48,13 +60,22 @@ public class OutputCapture implements TestRule {
base.evaluate();
}
finally {
- releaseOutput();
+ try {
+ if (!OutputCapture.this.matchers.isEmpty()) {
+ String output = OutputCapture.this.toString();
+ Assert.assertThat(output, allOf(OutputCapture.this.matchers));
+ }
+ }
+ finally {
+ releaseOutput();
+ }
}
}
};
}
protected void captureOutput() {
+ AnsiOutput.setEnabled(Enabled.NEVER);
this.copy = new ByteArrayOutputStream();
this.captureOut = new CaptureOutputStream(System.out, this.copy);
this.captureErr = new CaptureOutputStream(System.err, this.copy);
@@ -63,6 +84,7 @@ public class OutputCapture implements TestRule {
}
protected void releaseOutput() {
+ AnsiOutput.setEnabled(Enabled.DETECT);
System.setOut(this.captureOut.getOriginal());
System.setErr(this.captureErr.getOriginal());
this.copy = null;
@@ -84,6 +106,15 @@ public class OutputCapture implements TestRule {
return this.copy.toString();
}
+ /**
+ * Verify that the output is matched by the supplied {@code matcher}. Verification is
+ * performed after the test method has executed.
+ * @param matcher the matcher
+ */
+ public void expect(Matcher super String> matcher) {
+ this.matchers.add(matcher);
+ }
+
private static class CaptureOutputStream extends OutputStream {
private final PrintStream original;
@@ -122,6 +153,7 @@ public class OutputCapture implements TestRule {
this.copy.flush();
this.original.flush();
}
+
}
}