View Javadoc

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