1 /* 2 * Copyright 2008-2012 smartics, Kronseder & Reiner GmbH 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 de.smartics.util.test.matcher.io; 17 18 import java.io.IOException; 19 import java.io.InputStream; 20 import java.io.InputStreamReader; 21 import java.io.Reader; 22 23 import org.apache.commons.io.IOUtils; 24 import org.hamcrest.Factory; 25 import org.hamcrest.Matcher; 26 27 /** 28 * A matcher to check that the contents of two stream contain the identical 29 * information. 30 * 31 * <pre> 32 * {@markupSource "Add Import" 33 * import static de.smartics.util.test.matcher.io.ReadsEqualContentAs.readsEqualContentAs; 34 * } 35 * </pre> 36 * <p> 37 * In the following examples <code>expected</code> and <code>actual</code> are 38 * {@link Reader}s: 39 * </p> 40 * 41 * <pre> 42 * {@markupExample "Assert Example" 43 * import static org.hamcrest.MatcherAssert.assertThat; 44 * 45 * assertThat(actual, readsEqualContentAs(expected)); 46 * } 47 * </pre> 48 * <p> 49 * The following example shows a test spy and the verification of the expected 50 * stream argument: 51 * </p> 52 * 53 * <pre> 54 * {@markupExample "Verify Example" 55 * import static org.mockito.Matchers.argThat; 56 * import static org.mockito.Mockito.verify; 57 * 58 * spy.pass(actual); // Test spy that expects a reader 59 * 60 * verify(spy).pass( 61 * (Reader) argThat(readsEqualContentAs(expected))); 62 * } 63 * </pre> 64 */ 65 public class ReadsEqualContentAs extends AbstractEqualContentAs<Reader> 66 { 67 // ********************************* Fields ********************************* 68 69 // --- constants ------------------------------------------------------------ 70 71 // --- members -------------------------------------------------------------- 72 73 // ****************************** Initializer ******************************* 74 75 // ****************************** Constructors ****************************** 76 77 /** 78 * Default constructor. 79 * 80 * @param expected the other stream to compare to. 81 * @throws IllegalArgumentException on any problem reading from the stream. 82 */ 83 public ReadsEqualContentAs(final Reader expected) 84 throws IllegalArgumentException 85 { 86 this(toStringInternal(expected)); 87 } 88 89 /** 90 * Convenience constructor if the content is served as a String. 91 * 92 * @param expectedContent the expected content. 93 */ 94 public ReadsEqualContentAs(final String expectedContent) 95 { 96 super(expectedContent); 97 } 98 99 // ****************************** Inner Classes ***************************** 100 101 // ********************************* Methods ******************************** 102 103 // --- init ----------------------------------------------------------------- 104 105 private static String toStringInternal(final Reader reader) 106 { 107 try 108 { 109 return IOUtils.toString(reader); 110 } 111 catch (final IOException e) 112 { 113 throw new IllegalArgumentException("Reader cannot be read.", e); 114 } 115 } 116 117 // --- get&set -------------------------------------------------------------- 118 119 // --- business ------------------------------------------------------------- 120 121 /** 122 * {@inheritDoc} 123 * 124 * @see de.smartics.util.test.matcher.io.AbstractEqualContentAs#toString(java.lang.Object) 125 */ 126 @Override 127 protected final String toString(final Reader actual) 128 throws IllegalArgumentException 129 { 130 return toStringInternal(actual); 131 } 132 133 /** 134 * Factory method to create the matcher. 135 * 136 * @param expected the expected stream content. 137 * @return the created matcher. 138 * @throws IllegalArgumentException on any problem reading from the stream. 139 */ 140 @Factory 141 public static Matcher<? super Reader> readsEqualContentAs( 142 final Reader expected) throws IllegalArgumentException 143 { 144 return new ReadsEqualContentAs(expected); 145 } 146 147 /** 148 * Factory method to create the matcher. 149 * 150 * @param expected the expected stream content. 151 * @param encoding the encoding used on the stream. 152 * @return the created matcher. 153 * @throws IllegalArgumentException on any problem reading from the stream. 154 */ 155 @Factory 156 public static Matcher<? super Reader> readsEqualContentAs( 157 final InputStream expected, final String encoding) 158 throws IllegalArgumentException 159 { 160 try 161 { 162 return new ReadsEqualContentAs(new InputStreamReader(expected, encoding)); 163 } 164 catch (final Exception e) 165 { 166 throw new IllegalArgumentException("Cannot parse stream with encoding '" 167 + encoding + "'.", e); 168 } 169 } 170 171 /** 172 * Factory method to create the matcher. 173 * 174 * @param expected the expected stream content. 175 * @return the created matcher. 176 */ 177 @Factory 178 public static Matcher<? super Reader> readsEqualContentAs( 179 final String expected) 180 { 181 return new ReadsEqualContentAs(expected); 182 } 183 184 // --- object basics -------------------------------------------------------- 185 186 }