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

net.sf.cotta.test.assertion.StringAssert Maven / Gradle / Ivy

The newest version!
package net.sf.cotta.test.assertion;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.core.AllOf;
import org.junit.matchers.JUnitMatchers;

import java.util.ArrayList;
import java.util.List;

public class StringAssert extends BaseAssert {
  public StringAssert(String value) {
    super(value);
  }

  public StringAssert(byte[] value) {
    this(value == null ? null : new String(value));
  }

  public StringAssert isNotEmpty() {
    not(empty());
    return this;
  }

  public StringAssert isEmpty() {
    matches(empty());
    return this;
  }

  private BaseMatcher empty() {
    return new BaseMatcher() {
      public boolean matches(Object o) {
        String string = (String) o;
        return string != null && string.length() == 0;
      }

      public void describeTo(Description description) {
        description.appendText("empty string");
      }
    };
  }

  public StringAssert contains(String... expectedValues) {
    Matcher matcher = expectedValues.length == 1 ?
            JUnitMatchers.containsString(expectedValues[0]) :
            allof(expectedValues);
    matches(matcher);
    return this;
  }

  private Matcher allof(String[] expectedValues) {
    List> matchers = new ArrayList>(expectedValues.length);
    for (String item : expectedValues) {
      matchers.add(JUnitMatchers.containsString(item));
    }
    return new AllOf(matchers);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy