aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ObjectIdMatcher.java
blob: 77e8afb2b96d5822b4e8ad137833efeb954f2979 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright (C) 2018, Google LLC. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.transport;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Sets;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

/**
 * Multiple tests check that a collection of ObjectIds contain certain SHA1
 * (written as strings). This matcher hides the ObjectId to string conversion to
 * make the assertion more readable:
 *
 * assertThat(req.getWantsIds(), hasOnlyObjectIds("123123", "234234"));
 */
class ObjectIdMatcher extends TypeSafeMatcher<Collection<ObjectId>> {

	private final Set<ObjectId> expectedOids;

	private ObjectIdMatcher(Set<String> oids) {
		this.expectedOids = oids.stream().map(ObjectId::fromString)
				.collect(Collectors.toSet());
	}

	@Override
	public void describeTo(Description desc) {
		desc.appendText("Object ids:");
		desc.appendValueList("<", ",", ">", expectedOids);
	}

	@Override
	protected boolean matchesSafely(Collection<ObjectId> resultOids) {
		return resultOids.containsAll(expectedOids)
				&& expectedOids.containsAll(resultOids);
	}

	/**
	 * Assert that all and only the received {@link ObjectId object ids} are in
	 * the expected set.
	 * <p>
	 * ObjectIds are compared by SHA1.
	 *
	 * @param oids
	 *            Object ids to examine.
	 * @return true if examined and specified sets contains exactly the same
	 *         elements.
	 */
	static Matcher<Collection<ObjectId>> hasOnlyObjectIds(
			String... oids) {
		return new ObjectIdMatcher(Sets.of(oids));
	}
}