@Test(expected = ClassCastException.class)
 public void combineIncompatible() {
   RequestConditionHolder params = new RequestConditionHolder(new ParamsRequestCondition("name"));
   RequestConditionHolder headers =
       new RequestConditionHolder(new HeadersRequestCondition("name"));
   params.combine(headers);
 }
  /**
   * Checks if all conditions in this request mapping info match the provided request and returns a
   * potentially new request mapping info with conditions tailored to the current request.
   *
   * <p>For example the returned instance may contain the subset of URL patterns that match to the
   * current request, sorted with best matching patterns on top.
   *
   * @return a new instance in case all conditions match; or {@code null} otherwise
   */
  @Override
  public RequestMappingInfo getMatchingCondition(HttpServletRequest request) {
    RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request);
    ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request);
    HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request);
    ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request);
    ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request);

    if (methods == null
        || params == null
        || headers == null
        || consumes == null
        || produces == null) {
      return null;
    }

    PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(request);
    if (patterns == null) {
      return null;
    }

    RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request);
    if (custom == null) {
      return null;
    }

    return new RequestMappingInfo(
        patterns, methods, params, headers, consumes, produces, custom.getCondition());
  }
 @Test(expected = ClassCastException.class)
 public void compareIncompatible() {
   RequestConditionHolder params = new RequestConditionHolder(new ParamsRequestCondition("name"));
   RequestConditionHolder headers =
       new RequestConditionHolder(new HeadersRequestCondition("name"));
   params.compareTo(headers, new MockHttpServletRequest());
 }
  @Test
  public void noMatch() {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");

    RequestMethodsRequestCondition rm = new RequestMethodsRequestCondition(RequestMethod.POST);
    RequestConditionHolder custom = new RequestConditionHolder(rm);

    assertNull(custom.getMatchingCondition(request));
  }
  @Test
  public void combineEmpty() {
    RequestConditionHolder empty = new RequestConditionHolder(null);
    RequestConditionHolder notEmpty =
        new RequestConditionHolder(new ParamsRequestCondition("name"));

    assertSame(empty, empty.combine(empty));
    assertSame(notEmpty, notEmpty.combine(empty));
    assertSame(notEmpty, empty.combine(notEmpty));
  }
  @Test
  public void compare() {
    HttpServletRequest request = new MockHttpServletRequest();

    RequestConditionHolder params11 = new RequestConditionHolder(new ParamsRequestCondition("1"));
    RequestConditionHolder params12 =
        new RequestConditionHolder(new ParamsRequestCondition("1", "2"));

    assertEquals(1, params11.compareTo(params12, request));
    assertEquals(-1, params12.compareTo(params11, request));
  }
  @Test
  public void combine() {
    RequestConditionHolder params1 =
        new RequestConditionHolder(new ParamsRequestCondition("name1"));
    RequestConditionHolder params2 =
        new RequestConditionHolder(new ParamsRequestCondition("name2"));
    RequestConditionHolder expected =
        new RequestConditionHolder(new ParamsRequestCondition("name1", "name2"));

    assertEquals(expected, params1.combine(params2));
  }
  @Test
  public void match() {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    request.setParameter("name1", "value1");

    RequestMethodsRequestCondition rm =
        new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
    RequestConditionHolder custom = new RequestConditionHolder(rm);
    RequestMethodsRequestCondition expected = new RequestMethodsRequestCondition(RequestMethod.GET);

    assertEquals(expected, custom.getMatchingCondition(request).getCondition());
  }
  /**
   * Combines "this" request mapping info (i.e. the current instance) with another request mapping
   * info instance.
   *
   * <p>Example: combine type- and method-level request mappings.
   *
   * @return a new request mapping info instance; never {@code null}
   */
  @Override
  public RequestMappingInfo combine(RequestMappingInfo other) {
    PatternsRequestCondition patterns = this.patternsCondition.combine(other.patternsCondition);
    RequestMethodsRequestCondition methods = this.methodsCondition.combine(other.methodsCondition);
    ParamsRequestCondition params = this.paramsCondition.combine(other.paramsCondition);
    HeadersRequestCondition headers = this.headersCondition.combine(other.headersCondition);
    ConsumesRequestCondition consumes = this.consumesCondition.combine(other.consumesCondition);
    ProducesRequestCondition produces = this.producesCondition.combine(other.producesCondition);
    RequestConditionHolder custom = this.customConditionHolder.combine(other.customConditionHolder);

    return new RequestMappingInfo(
        patterns, methods, params, headers, consumes, produces, custom.getCondition());
  }
  @Test
  public void compareEmpty() {
    HttpServletRequest request = new MockHttpServletRequest();

    RequestConditionHolder empty = new RequestConditionHolder(null);
    RequestConditionHolder empty2 = new RequestConditionHolder(null);
    RequestConditionHolder notEmpty =
        new RequestConditionHolder(new ParamsRequestCondition("name"));

    assertEquals(0, empty.compareTo(empty2, request));
    assertEquals(-1, notEmpty.compareTo(empty, request));
    assertEquals(1, empty.compareTo(notEmpty, request));
  }
 @Test
 public void matchEmpty() {
   RequestConditionHolder empty = new RequestConditionHolder(null);
   assertSame(empty, empty.getMatchingCondition(new MockHttpServletRequest()));
 }