+ public static Matcher<InputStream> delivers(final byte[] data) {
+ return new TypeSafeMatcher<InputStream>() {
+ byte[] readData = new byte[data.length];
+
+ @Override
+ protected boolean matchesSafely(InputStream inputStream) {
+ int offset = 0;
+ try {
+ while (true) {
+ int r = inputStream.read();
+ if (r == -1) {
+ return offset == data.length;
+ }
+ if (offset == data.length) {
+ return false;
+ }
+ if (data[offset] != (readData[offset] = (byte) r)) {
+ return false;
+ }
+ offset++;
+ }
+ } catch (IOException ioe1) {
+ return false;
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendValue(data);
+ }
+
+ @Override
+ protected void describeMismatchSafely(InputStream item, Description mismatchDescription) {
+ mismatchDescription.appendValue(readData);
+ }
+ };
+ }
+
+ public static Matcher<SimpleFieldSet> matches(final SimpleFieldSet fieldSetToMatch) {
+ return new TypeSafeMatcher<SimpleFieldSet>() {
+ @Override
+ protected boolean matchesSafely(SimpleFieldSet fieldSet) {
+ if (size(fieldSet.keyIterator()) != size(fieldSetToMatch.keyIterator())) {
+ return false;
+ }
+ for (Iterator<String> keys = fieldSetToMatch.keyIterator(); keys.hasNext(); ) {
+ String key = keys.next();
+ if (!equal(fieldSet.get(key), fieldSetToMatch.get(key))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("is ").appendValue(fieldSetToMatch);
+ }
+ };
+ }
+