view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/ModificationDataMatcher.java @ 609:86e187882960 Gaya-8.0.x

Test for TW-29998
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 28 Jun 2013 12:06:09 +0400
parents ad112e314be5
children 7c017b6aa438
line wrap: on
line source
package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.vcs.ModificationData;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class ModificationDataMatcher extends TypeSafeMatcher<ModificationData> {

  private String myVersion;
  private Map<String, String> myVcsRootProperties = new HashMap<String, String>();
  private Map<String, String> myAttributes = new HashMap<String, String>();

  @Override
  public boolean matchesSafely(ModificationData m) {
    if (myVersion != null && !myVersion.equals(m.getDisplayVersion()))
      return false;
    if (!myVcsRootProperties.isEmpty() && !myVcsRootProperties.equals(m.getVcsRootObject().getProperties()))
      return false;
    if (!myAttributes.isEmpty() && !containsAllAttributes(m.getAttributes()))
      return false;
    return true;
  }

  public void describeTo(Description description) {
    description.appendText("modification");
    if (myVersion != null)
      description.appendText(" with version ").appendValue(myVersion);
    if (!myVcsRootProperties.isEmpty())
      description.appendText(" with vcs root properties ").appendValue(myVcsRootProperties);
    if (!myAttributes.isEmpty())
      description.appendText(" with attributes ").appendValue(myAttributes);
  }

  public static ModificationDataMatcher modificationData() {
    return new ModificationDataMatcher();
  }

  public ModificationDataMatcher withVersion(@NotNull String version) {
    myVersion = version;
    return this;
  }

  public ModificationDataMatcher withVcsRootProperties(@NotNull Map<String, String> properties) {
    myVcsRootProperties.putAll(properties);
    return this;
  }

  public ModificationDataMatcher withAttributes(@NotNull Map<String, String> attributes) {
    myAttributes.putAll(attributes);
    return this;
  }

  private boolean containsAllAttributes(@NotNull Map<String, String> attributes) {
    for (Map.Entry<String, String> e : myAttributes.entrySet()) {
      String expectedValue = e.getValue();
      String actualValue = attributes.get(e.getKey());
      if (expectedValue == null && actualValue != null ||
          expectedValue != null && !e.getValue().equals(actualValue))
        return false;
    }
    return true;
  }
}