All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.assertj.api.AbstractInputStreamAssert Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2012-2016 the original author or authors.
 */
package com.fitbur.assertj.api;

import java.io.InputStream;

import com.fitbur.assertj.internal.InputStreams;
import com.fitbur.assertj.internal.InputStreamsException;
import com.fitbur.assertj.util.VisibleForTesting;

/**
 * Base class for all implementations of assertions for {@link InputStream}s.
 * @param  the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation"
 *          for more details.
 * @param  the type of the "actual" value.
 *
 * @author Matthieu Baechler
 * @author Mikhail Mazursky
 */
public abstract class AbstractInputStreamAssert, A extends InputStream>
    extends AbstractAssert {

  @VisibleForTesting
  InputStreams inputStreams = InputStreams.instance();

  protected AbstractInputStreamAssert(A actual, Class selfType) {
    super(actual, selfType);
  }

  /**
   * Verifies that the content of the actual {@code InputStream} is equal to the content of the given one.
   *
   * @param expected the given {@code InputStream} to compare the actual {@code InputStream} to.
   * @return {@code this} assertion object.
   * @throws NullPointerException if the given {@code InputStream} is {@code null}.
   * @throws AssertionError if the actual {@code InputStream} is {@code null}.
   * @throws AssertionError if the content of the actual {@code InputStream} is not equal to the content of the given one.
   * @throws InputStreamsException if an I/O error occurs.
   *
   * @deprecated use {@link #hasSameContentAs()} instead
   */
  @Deprecated
  public S hasContentEqualTo(InputStream expected) {
    inputStreams.assertSameContentAs(info, actual, expected);
    return myself;
  }

  /**
   * Verifies that the content of the actual {@code InputStream} is equal to the content of the given one.
   * 

* Example: *

 // assertion will pass
   * assertThat(new ByteArrayInputStream(new byte[] {0xa})).hasSameContentAs(new ByteArrayInputStream(new byte[] {0xa}));
   *
   * // assertions will fail
   * assertThat(new ByteArrayInputStream(new byte[] {0xa})).hasSameContentAs(new ByteArrayInputStream(new byte[] {}));
   * assertThat(new ByteArrayInputStream(new byte[] {0xa})).hasSameContentAs(new ByteArrayInputStream(new byte[] {0xa, 0xc, 0xd}));
* * @param expected the given {@code InputStream} to compare the actual {@code InputStream} to. * @return {@code this} assertion object. * @throws NullPointerException if the given {@code InputStream} is {@code null}. * @throws AssertionError if the actual {@code InputStream} is {@code null}. * @throws AssertionError if the content of the actual {@code InputStream} is not equal to the content of the given one. * @throws InputStreamsException if an I/O error occurs. */ public S hasSameContentAs(InputStream expected) { inputStreams.assertSameContentAs(info, actual, expected); return myself; } }