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