You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectIdMatcher.java 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (C) 2018, Google LLC. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport;
  11. import java.util.Collection;
  12. import java.util.Set;
  13. import java.util.stream.Collectors;
  14. import org.eclipse.jgit.lib.ObjectId;
  15. import org.eclipse.jgit.lib.Sets;
  16. import org.hamcrest.Description;
  17. import org.hamcrest.Factory;
  18. import org.hamcrest.Matcher;
  19. import org.hamcrest.TypeSafeMatcher;
  20. /**
  21. * Multiple tests check that a collection of ObjectIds contain certain SHA1
  22. * (written as strings). This matcher hides the ObjectId to string conversion to
  23. * make the assertion more readable:
  24. *
  25. * assertThat(req.getWantsIds(), hasOnlyObjectIds("123123", "234234"));
  26. */
  27. class ObjectIdMatcher extends TypeSafeMatcher<Collection<ObjectId>> {
  28. private final Set<ObjectId> expectedOids;
  29. private ObjectIdMatcher(Set<String> oids) {
  30. this.expectedOids = oids.stream().map(ObjectId::fromString)
  31. .collect(Collectors.toSet());
  32. }
  33. @Override
  34. public void describeTo(Description desc) {
  35. desc.appendText("Object ids:");
  36. desc.appendValueList("<", ",", ">", expectedOids);
  37. }
  38. @Override
  39. protected boolean matchesSafely(Collection<ObjectId> resultOids) {
  40. return resultOids.containsAll(expectedOids)
  41. && expectedOids.containsAll(resultOids);
  42. }
  43. /**
  44. * Assert that all and only the received {@link ObjectId object ids} are in
  45. * the expected set.
  46. * <p>
  47. * ObjectIds are compared by SHA1.
  48. *
  49. * @param oids
  50. * Object ids to examine.
  51. * @return true if examined and specified sets contains exactly the same
  52. * elements.
  53. */
  54. @Factory
  55. static Matcher<Collection<ObjectId>> hasOnlyObjectIds(
  56. String... oids) {
  57. return new ObjectIdMatcher(Sets.of(oids));
  58. }
  59. }