View Javadoc
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  
12  import java.util.Collection;
13  import java.util.Set;
14  import java.util.stream.Collectors;
15  
16  import org.eclipse.jgit.lib.ObjectId;
17  import org.eclipse.jgit.lib.Sets;
18  import org.hamcrest.Description;
19  import org.hamcrest.Matcher;
20  import org.hamcrest.TypeSafeMatcher;
21  
22  /**
23   * Multiple tests check that a collection of ObjectIds contain certain SHA1
24   * (written as strings). This matcher hides the ObjectId to string conversion to
25   * make the assertion more readable:
26   *
27   * assertThat(req.getWantsIds(), hasOnlyObjectIds("123123", "234234"));
28   */
29  class ObjectIdMatcher extends TypeSafeMatcher<Collection<ObjectId>> {
30  
31  	private final Set<ObjectId> expectedOids;
32  
33  	private ObjectIdMatcher(Set<String> oids) {
34  		this.expectedOids = oids.stream().map(ObjectId::fromString)
35  				.collect(Collectors.toSet());
36  	}
37  
38  	@Override
39  	public void describeTo(Description desc) {
40  		desc.appendText("Object ids:");
41  		desc.appendValueList("<", ",", ">", expectedOids);
42  	}
43  
44  	@Override
45  	protected boolean matchesSafely(Collection<ObjectId> resultOids) {
46  		return resultOids.containsAll(expectedOids)
47  				&& expectedOids.containsAll(resultOids);
48  	}
49  
50  	/**
51  	 * Assert that all and only the received {@link ObjectId object ids} are in
52  	 * the expected set.
53  	 * <p>
54  	 * ObjectIds are compared by SHA1.
55  	 *
56  	 * @param oids
57  	 *            Object ids to examine.
58  	 * @return true if examined and specified sets contains exactly the same
59  	 *         elements.
60  	 */
61  	static Matcher<Collection<ObjectId>> hasOnlyObjectIds(
62  			String... oids) {
63  		return new ObjectIdMatcher(Sets.of(oids));
64  	}
65  }