### Question:
CompositeRequest extends Request implements Iterable<Request> { public static Request with( Request... requests ) { CheckArg.isNotEmpty(requests, "requests"); if (requests.length == 1) { CheckArg.isNotNull(requests[0], "requests[0]"); return requests[0]; } boolean readOnly = true; List<Request> list = new ArrayList<Request>(requests.length); for (Request request : requests) { if (request == null) continue; if (RequestType.COMPOSITE == request.getType()) { CompositeRequest composite = (CompositeRequest)request; list.addAll(composite.getRequests()); if (!composite.isReadOnly()) readOnly = false; } else { list.add(request); if (!request.isReadOnly()) readOnly = false; } } CheckArg.isNotEmpty(list, "requests"); return new CompositeRequest(list, readOnly); } protected  CompositeRequest( List<? extends Request> requests,
                                boolean readOnly ); protected  CompositeRequest( boolean readOnly ); static Request with( Request... requests ); static Request with( Iterator<? extends Request> requests ); static Request with( List<? extends Request> requests ); static CompositeRequest add( CompositeRequest composite,
                                        Request... requests ); static CompositeRequest add( CompositeRequest composite,
                                        Iterator<? extends Request> requests ); List<Request> getRequests(); int size(); Iterator<Request> iterator(); @Override boolean isReadOnly(); void checkForErrors(); @Override boolean equals( Object obj ); @Override String toString(); @Override RequestType getType(); static final int UNKNOWN_NUMBER_OF_REQUESTS; }### Answer:
@Test public void shouldCancelAllNestedRequestsAndCompositeAfterCallingCancel() { Request composite = CompositeRequest.with(requests); assertThat(composite.isCancelled(), is(false)); for (Request request : requests) { assertThat(request.isCancelled(), is(false)); } composite.cancel(); assertThat(composite.isCancelled(), is(true)); for (Request request : requests) { assertThat(request.isCancelled(), is(true)); } }
@Test public void shouldCancelAllNestedRequestsAndCompositeAfterCallingCancelOnNestedRequest() { Request composite = CompositeRequest.with(requests); assertThat(composite.isCancelled(), is(false)); for (Request request : requests) { assertThat(request.isCancelled(), is(false)); } requests[0].cancel(); assertThat(composite.isCancelled(), is(true)); for (Request request : requests) { assertThat(request.isCancelled(), is(true)); } }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowCreatingCompositeRequestWithNullRequest() { CompositeRequest.with((Request)null); }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowCreatingCompositeRequestWithNullRequestArray() { CompositeRequest.with((Request[])null); }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowCreatingCompositeRequestWithNullRequestIterator() { CompositeRequest.with((Iterator<Request>)null); }
@Test public void shouldReturnRequestWhenCreatingCompositeFromSingleRequest() { request = CompositeRequest.with(requests[0]); assertThat(request, is(sameInstance(requests[0]))); }
@Test public void shouldConsiderTwoCompositesOfSameRequestsToBeEqual() { request = CompositeRequest.with(requests); Request request2 = CompositeRequest.with(requests); assertThat(request, is(request2)); assertThat(request.hasError(), is(false)); }