001    /*
002     * Created on Jan 26, 2011
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     * 
007     * http://www.apache.org/licenses/LICENSE-2.0
008     * 
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     * 
013     * Copyright @2011 the original author or authors.
014     */
015    package org.fest.assertions.internal;
016    
017    import static java.lang.String.format;
018    
019    import static org.fest.assertions.error.ShouldHaveEqualContent.shouldHaveEqualContent;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.util.List;
024    
025    import org.fest.assertions.core.AssertionInfo;
026    import org.fest.util.VisibleForTesting;
027    
028    /**
029     * Reusable assertions for <code>{@link InputStream}</code>s.
030     * 
031     * @author Matthieu Baechler
032     */
033    public class InputStreams {
034    
035      private static final InputStreams INSTANCE = new InputStreams();
036    
037      /**
038       * Returns the singleton instance of this class.
039       * @return the singleton instance of this class.
040       */
041      public static InputStreams instance() {
042        return INSTANCE;
043      }
044    
045      @VisibleForTesting
046      Diff diff = new Diff();
047      @VisibleForTesting
048      Failures failures = Failures.instance();
049    
050      @VisibleForTesting
051      InputStreams() {}
052    
053      /**
054       * Asserts that the given InputStreams have equal content.
055       * 
056       * @param info contains information about the assertion.
057       * @param actual the "actual" InputStream.
058       * @param expected the "expected" InputStream.
059       * @throws NullPointerException if {@code expected} is {@code null}.
060       * @throws AssertionError if {@code actual} is {@code null}.
061       * @throws AssertionError if the given InputStreams do not have equal content.
062       * @throws InputStreamsException if an I/O error occurs.
063       */
064      public void assertEqualContent(AssertionInfo info, InputStream actual, InputStream expected) {
065        if (expected == null) throw new NullPointerException("The InputStream to compare to should not be null");
066        assertNotNull(info, actual);
067        try {
068          List<String> diffs = diff.diff(actual, expected);
069          if (diffs.isEmpty()) return;
070          throw failures.failure(info, shouldHaveEqualContent(actual, expected, diffs));
071        } catch (IOException e) {
072          String msg = format("Unable to compare contents of InputStreams :<%s> and:<%s>", actual, expected);
073          throw new InputStreamsException(msg, e);
074        }
075      }
076    
077      private static void assertNotNull(AssertionInfo info, InputStream stream) {
078        Objects.instance().assertNotNull(info, stream);
079      }
080    }