下面列出了怎么用org.hamcrest.collection.IsIterableWithSize的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
void shouldCompareMapsAndReturnResultForEmptyTables()
{
List<List<EntryComparisonResult>> result = ComparisonUtils.compareListsOfMaps(List.of(Map.of(FIRST_COLUMN, 1)),
List.of(Map.of(SECOND_COLUMN, 1)));
List<EntryComparisonResult> firstRowResult = result.get(0);
EntryComparisonResult result1 = firstRowResult.get(0);
EntryComparisonResult result2 = firstRowResult.get(1);
Assertions.assertAll(COMPARISON_RESULT,
() -> Assertions.assertFalse(result1.isPassed()),
() -> Assertions.assertEquals(FIRST_COLUMN, result1.getKey()),
() -> Assertions.assertEquals(BigDecimal.ONE, result1.getLeft()),
() -> Assertions.assertNull(result1.getRight()),
() -> Assertions.assertFalse(result2.isPassed()),
() -> Assertions.assertEquals(SECOND_COLUMN, result2.getKey()),
() -> Assertions.assertNull(result2.getLeft()),
() -> Assertions.assertEquals(BigDecimal.ONE, result2.getRight()));
assertThat(firstRowResult, IsIterableWithSize.iterableWithSize(2));
}
/**
* Tests if the {@link FilteredUriBuilder} can add filter parameters.
*/
@Test
public final void addFilterParameters() {
final Map<String, Iterable<String>> filters = new HashMap<>();
filters.put(
"driver",
Arrays.asList(
"bridge"
)
);
MatcherAssert.assertThat(
"Did not added filter parameters to URI",
new FilteredUriBuilder(
new URIBuilder(), filters
).getQueryParams(),
new IsIterableWithSize<>(
new IsEqual<>(1)
)
);
}
/**
* Tests if {@link ReadJsonArray} can read an array from response.
* @throws IOException If something wrong happens
*/
@Test
public void readArray() throws IOException {
MatcherAssert.assertThat(
"Could not read JSON array from response",
new ReadJsonArray(
new Fake()
).handleResponse(
new Response(
HttpStatus.SC_OK,
Json.createArrayBuilder()
.add(
Json.createObjectBuilder()
.add("first", "First item")
).add(
Json.createObjectBuilder()
.add("second", "Second item")
).build().toString()
)
),
new IsIterableWithSize<>(
new IsEqual<>(2)
)
);
}
@Test
public void peersNoEmissaryServer() {
// TODO Look at behavior here, we should probably throw the exception and not catch and return this value
// setup
Namespace.unbind("EmissaryServer");
// test
Response response = target("peers").request().get();
// verify
assertThat(response.getStatus(), equalTo(200));
PeersResponseEntity entity = response.readEntity(PeersResponseEntity.class);
assertThat(entity.getLocal().getHost(), equalTo("Namespace lookup error, host unknown"));
assertThat(entity.getErrors(), IsIterableWithSize.iterableWithSize(0));
assertThat(entity.getLocal().getPeers(), equalTo(PEERS));
}
@Test
public void noServerBound() throws Exception {
// setup
// Namespace.unbind("EmissaryServer");
// test
Response response = target("places").request().get();
// verify
assertThat(response.getStatus(), equalTo(200));
PlacesResponseEntity entity = response.readEntity(PlacesResponseEntity.class);
assertThat(entity.getLocal(), equalTo(null));
assertThat(entity.getCluster(), equalTo(null));
assertThat(entity.getErrors(), IsIterableWithSize.iterableWithSize(1));
assertThat(entity.getErrors(), equalTo(UNBOUND_SERVER_ERR));
}
@Test
public void testDynamicClassLoaderService() throws MalformedURLException {
// this will check that the java service loader works on a classloader that is mutable
new JarRuntimeInfo((URL) null, null, null);
// given
ExtensibleUrlClassLoader urlClassLoader = new ExtensibleUrlClassLoader(new URL[0]);
// 2 comp installer
assertThat(ServiceLoader.load(ComponentInstaller.class, urlClassLoader),
IsIterableWithSize.<ComponentInstaller> iterableWithSize(2));
// when
urlClassLoader.addURL(new URL("mvn:org.talend.components/multiple-runtime-comp/0.18.0"));
// then
// 3 comp installer
assertThat(ServiceLoader.load(ComponentInstaller.class, urlClassLoader),
IsIterableWithSize.<ComponentInstaller> iterableWithSize(3));
}
/**
* See https://github.com/SciCrunch/SciGraph/wiki/MappingToOWL#equivalence-axioms
*/
@Test
public void testEquivalentToIntersectionOf() {
Node x = getNode("http://example.org/x");
Node y = getNode("http://example.org/y");
Node z = getNode("http://example.org/z");
assertThat("equivalence is symmetric and holds between all members.",
GraphUtil.getRelationships(x, y, OwlRelationships.OWL_EQUIVALENT_CLASS, false),
is(IsIterableWithSize.<Relationship> iterableWithSize(1)));
assertThat("equivalence is symmetric and holds between all members.",
GraphUtil.getRelationships(x, z, OwlRelationships.OWL_EQUIVALENT_CLASS, false),
is(IsIterableWithSize.<Relationship> iterableWithSize(1)));
assertThat("equivalence is symmetric and holds between all members.",
GraphUtil.getRelationships(y, z, OwlRelationships.OWL_EQUIVALENT_CLASS, false),
is(IsIterableWithSize.<Relationship> iterableWithSize(1)));
}
@Test
public void transform_string_to_enum () {
List<String> days = Lists.newArrayList(
"WEDNESDAY",
"SUNDAY",
"MONDAY",
"TUESDAY",
"WEDNESDAY");
Function<String, Day> valueOfFunction = Enums.valueOfFunction(Day.class);
Iterable<Day> daysAsEnums = Iterables.transform(days, valueOfFunction);
assertThat(daysAsEnums, IsIterableWithSize.<Day>iterableWithSize(5));
assertThat(daysAsEnums, IsIterableContainingInOrder.
<Day>contains(
Day.WEDNESDAY,
Day.SUNDAY,
Day.MONDAY,
Day.TUESDAY,
Day.WEDNESDAY));
}
@Test
public void transform_string_to_enum_string_converter () {
List<String> days = Lists.newArrayList(
"WEDNESDAY",
"SUNDAY",
"MONDAY",
"TUESDAY",
"WEDNESDAY");
Function<String, Day> valueOfFunction = Enums.stringConverter(Day.class);
Iterable<Day> daysAsEnums = Iterables.transform(days, valueOfFunction);
assertThat(daysAsEnums, IsIterableWithSize.<Day>iterableWithSize(5));
assertThat(daysAsEnums, IsIterableContainingInOrder.
<Day>contains(
Day.WEDNESDAY,
Day.SUNDAY,
Day.MONDAY,
Day.TUESDAY,
Day.WEDNESDAY));
}
/**
* UnixDocker can list {@link Volumes}.
* @throws Exception If something goes wrong.
*/
@Test
public void listVolumes() throws Exception {
final Docker docker = new UnixDocker(
Paths.get("/var/run/docker.sock").toFile()
);
MatcherAssert.assertThat(
docker.volumes(),
new IsIterableWithSize<>(new GreaterOrEqual<>(0))
);
}
/**
* LocalUnixDocker can list {@link Volumes}.
* @throws Exception If something goes wrong.
*/
@Test
public void listVolumes() throws Exception {
final Docker docker = new LocalDocker(
Paths.get("/var/run/docker.sock").toFile()
);
MatcherAssert.assertThat(
docker.volumes(),
new IsIterableWithSize<>(new GreaterOrEqual<>(0))
);
}
@Test
public void places() throws Exception {
Namespace.bind("EmissaryServer", server);
Namespace.bind("PickupPlace", "pickupPlace");
Namespace.bind("PickupClient", "pickupClient");
Namespace.bind("ThisWontBeAdded", "miss");
Namespace.bind("ProcessingPlace", "processingPlace");
// test
Response response = target("places").request().get();
// verify
assertThat(response.getStatus(), equalTo(200));
PlacesResponseEntity entity = response.readEntity(PlacesResponseEntity.class);
assertThat(entity.getLocal().getHost(), equalTo("localhost:8001"));
assertThat(entity.getLocal().getPlaces(), IsIterableWithSize.iterableWithSize(3));
assertThat(entity.getLocal().getPlaces(), equalTo(EXPECTED_PLACES));
assertThat(entity.getErrors(), IsIterableWithSize.iterableWithSize(0));
assertThat(entity.getCluster(), equalTo(null));
// cleanup
Namespace.unbind("EmissaryServer");
Namespace.unbind("PickupPlace");
Namespace.unbind("PickupClient");
Namespace.unbind("ThisWontBeAdded");
Namespace.unbind("ProcessingPlace");
}
@Test
public void placesNoPlacesClientsBound() throws Exception {
Namespace.bind("EmissaryServer", server);
// test
Response response = target("places").request().get();
// verify
assertThat(response.getStatus(), equalTo(200));
PlacesResponseEntity entity = response.readEntity(PlacesResponseEntity.class);
assertThat(entity.getLocal().getHost(), equalTo("localhost:8001"));
assertThat(entity.getLocal().getPlaces(), IsIterableWithSize.iterableWithSize(0));
assertThat(entity.getErrors(), IsIterableWithSize.iterableWithSize(0));
assertThat(entity.getCluster(), equalTo(null));
}
@Test
public void testGetPeersWithPort() throws IOException {
// test
Set<String> peers = PeersCommand.getPeers(HostAndPort.fromString(""), true);
// verify
assertThat(peers, IsIterableContainingInOrder.contains("localhost:7001", "localhost:8001", "localhost:9001"));
assertThat(peers, IsIterableWithSize.iterableWithSize(3));
}
@Test
public void testGetPeers() throws IOException {
// test
Set<String> peers = PeersCommand.getPeers(HostAndPort.fromString(""), false);
// verify
assertThat(peers, IsIterableContainingInOrder.contains("localhost"));
assertThat(peers, IsIterableWithSize.iterableWithSize(1));
}
@Test
public void testGetPeersIgnoreHost() throws IOException {
// test
Set<String> peers = PeersCommand.getPeers(HostAndPort.fromString("localhost"), false);
// verify
assertThat(peers, IsIterableWithSize.iterableWithSize(0));
}
@Test
public void testGetPeersIgnoreHostAndPort() throws IOException {
// test
Set<String> peers = PeersCommand.getPeers(HostAndPort.fromString("localhost:8001"), true);
// verify
assertThat(peers, IsIterableContainingInOrder.contains("localhost:7001", "localhost:9001"));
assertThat(peers, IsIterableWithSize.iterableWithSize(2));
}
@Test
@SuppressWarnings("unchecked")
public void reportTotalPagedLength() throws Exception {
final Iterable<String> first = new IterableOf<>("A", "five");
final Iterable<String> second = new IterableOf<>("word", "long");
final Iterable<String> third = new IterableOf<>("sentence");
final Iterable<Iterable<String>> service = new IterableOf<>(
first, second, third
);
final Iterator<Iterable<String>> pages = service.iterator();
MatcherAssert.assertThat(
"length must be equal to total number of elements",
new IterableOf<>(
() -> pages.next().iterator(),
page -> new Ternary<>(
() -> pages.hasNext(),
() -> pages.next().iterator(),
() -> new IteratorOf<String>()
).value()
),
new IsIterableWithSize<>(
new IsEqual<>(
new LengthOf(
new Joined<>(first, second, third)
).intValue()
)
)
);
}
@Test
public void testSubclass() {
Node subclass = getNode("http://example.org/subclass");
Node superclass = getNode("http://example.org/superclass");
assertThat("classes should be labeled as such", subclass.hasLabel(OwlLabels.OWL_CLASS) && superclass.hasLabel(OwlLabels.OWL_CLASS));
assertThat("subclass should be a directed relationship",
GraphUtil.getRelationships(subclass, superclass, OwlRelationships.RDFS_SUBCLASS_OF),
is(IsIterableWithSize.<Relationship> iterableWithSize(1)));
}
@Test
public void testSubclass() {
Node subp = getNode("http://example.org/subproperty");
Node superp = getNode("http://example.org/superproperty");
assertThat("subclass should be a directed relationship",
GraphUtil.getRelationships(subp, superp, OwlRelationships.RDFS_SUB_PROPERTY_OF),
is(IsIterableWithSize.<Relationship> iterableWithSize(1)));
}
@Test
public void testSubclass() {
Node subp = getNode("http://example.org/subproperty");
Node superp = getNode("http://example.org/superproperty");
assertThat("subclass should be a directed relationship",
GraphUtil.getRelationships(subp, superp, OwlRelationships.RDFS_SUB_PROPERTY_OF),
is(IsIterableWithSize.<Relationship> iterableWithSize(1)));
}
@Test
public void evidenceIsAdded() {
assertThat(graph.getVertices(), IsIterableWithSize.<Vertex>iterableWithSize(5));
assertThat(graph.getEdges(), IsIterableWithSize.<Edge>iterableWithSize(1));
aspect.invoke(graph);
assertThat(graph.getVertices(), IsIterableWithSize.<Vertex>iterableWithSize(6));
assertThat(graph.getEdges(), IsIterableWithSize.<Edge>iterableWithSize(4));
}
@Test
public void pathsAreTranslated() {
Iterable<PropertyContainer> path = newArrayList(node, relationship, otherNode);
TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil);
tgu.addPath(path);
assertThat(graph.getVertices(), is(IsIterableWithSize.<Vertex>iterableWithSize(2)));
assertThat(graph.getEdges(), is(IsIterableWithSize.<Edge>iterableWithSize(1)));
}
@Test
public void testMultiTypedNeighborhood() {
Graph graph = graphApi.getNeighbors(newHashSet(b), 1,
newHashSet(new DirectedRelationshipType(OwlRelationships.RDFS_SUBCLASS_OF, Direction.INCOMING),
new DirectedRelationshipType(fizz, Direction.INCOMING)), absent);
assertThat(graph.getVertices(), IsIterableWithSize.<Vertex>iterableWithSize(3));
assertThat(graph.getEdges(), IsIterableWithSize.<Edge>iterableWithSize(2));
}
@Test
public void multipleAncestors_areReturned() {
Graph graph = graphApi.getNeighbors(newHashSet(i), 10,
newHashSet(new DirectedRelationshipType(OwlRelationships.RDFS_SUBCLASS_OF, Direction.OUTGOING)), absent);
assertThat(graph.getVertices(), IsIterableWithSize.<Vertex>iterableWithSize(4));
assertThat(graph.getEdges(), IsIterableWithSize.<Edge>iterableWithSize(4));
}
@Override
public Void apply(Iterable<Variant> actual) {
assertThat(actual, CoreMatchers.hasItem(expectedSnp1));
assertThat(actual, CoreMatchers.hasItem(expectedSnp2));
assertThat(actual, CoreMatchers.hasItem(expectedInsert));
assertThat(actual, IsIterableWithSize.<Variant>iterableWithSize(3));
return null;
}
@Test // #61
void findOrdersBetween() {
order = orderManager.save(order);
LocalDateTime dateCreated = order.getDateCreated();
Iterable<Order> result = orderManager.findBy(Interval.from(dateCreated).to(dateCreated.plusHours(1L)));
assertThat(result, IsIterableWithSize.<Order> iterableWithSize(1));
assertThat(result.iterator().next(), is(order));
}
@Test // #61
void findOrdersBetweenWhenFromToEqual() {
order = orderManager.save(order);
LocalDateTime dateCreated = order.getDateCreated();
Iterable<Order> result = orderManager.findBy(Interval.from(dateCreated).to(dateCreated));
assertThat(result, IsIterableWithSize.<Order> iterableWithSize(1));
assertThat(result.iterator().next(), is(order));
}
@Test // #61
void findOrdersByOrderStatus_OPEN() {
Order openOrder = new Order(user, Cash.CASH);
openOrder = orderManager.save(openOrder);
orderManager.save(order);
orderManager.payOrder(order);
Iterable<Order> openOrders = orderManager.findBy(OrderStatus.OPEN);
assertThat(openOrders, IsIterableWithSize.<Order> iterableWithSize(1));
assertThat(openOrders.iterator().next(), is(openOrder));
}
@Test
public void requestIsCorrectlyCreated() {
ThriftService<FrontendRequest, FrontendResponse> thriftService = createDefaultListener();
logger = new ThriftScribeLogger(thriftService, executorService);
logger.log(CATEGORY, LINES);
// Test request outside as otherwise an assertion could fail silently.
assertEquals(request.getType(), FrontendRequestType.LOG);
assertEquals(request.getLogRequest().getType(), LogRequestType.SCRIBE_DATA);
assertEquals(request.getLogRequest().getScribeData().getCategory(), CATEGORY);
assertThat(
request.getLogRequest().getScribeData().getLines(),
Matchers.allOf(
hasItem(LINES.get(0)), hasItem(LINES.get(1)), IsIterableWithSize.iterableWithSize(2)));
}