view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/ModificationDataMatcher.java @ 1027:10dc26b32c35

Update code according to new Java
author nikolai.kulakov@DESKTOP-Q4QCGIH
date Wed, 05 Aug 2020 13:19:53 +0300
parents 7bf4d943d5bb
children
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * 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.
 */

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.List;
import java.util.Map;

import static java.util.Arrays.asList;

public class ModificationDataMatcher extends TypeSafeMatcher<ModificationData> {

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

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

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

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

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

  public ModificationDataMatcher withDescription(@NotNull String description) {
    myDescription = description;
    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;
  }

  public ModificationDataMatcher withParentRevisions(String... parentRevisions) {
    myParentRevisions = asList(parentRevisions);
    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;
  }
}