/** * Returns a new instance with the union of the param expressions * from "this" and the "other" instance. */ @Override public ParamsRequestCondition combine(ParamsRequestCondition other) { Set<ParamExpression> set = new LinkedHashSet<>(this.expressions); set.addAll(other.expressions); return new ParamsRequestCondition(set); }
@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 paramEquals() { assertEquals(new ParamsRequestCondition("foo"), new ParamsRequestCondition("foo")); assertFalse(new ParamsRequestCondition("foo").equals(new ParamsRequestCondition("bar"))); assertFalse(new ParamsRequestCondition("foo").equals(new ParamsRequestCondition("FOO"))); assertEquals(new ParamsRequestCondition("foo=bar"), new ParamsRequestCondition("foo=bar")); assertFalse(new ParamsRequestCondition("foo=bar").equals(new ParamsRequestCondition("FOO=bar"))); }
@Test public void combine() { ParamsRequestCondition condition1 = new ParamsRequestCondition("foo=bar"); ParamsRequestCondition condition2 = new ParamsRequestCondition("foo=baz"); ParamsRequestCondition result = condition1.combine(condition2); Collection<?> conditions = result.getContent(); assertEquals(2, conditions.size()); }
@Test public void compare() { RequestConditionHolder params11 = new RequestConditionHolder(new ParamsRequestCondition("1")); RequestConditionHolder params12 = new RequestConditionHolder(new ParamsRequestCondition("1", "2")); assertEquals(1, params11.compareTo(params12, this.exchange)); assertEquals(-1, params12.compareTo(params11, this.exchange)); }
@Before public void setup() throws Exception { this.param1 = new ParamsRequestCondition("param1"); this.param2 = new ParamsRequestCondition("param2"); this.param3 = this.param1.combine(this.param2); this.header1 = new HeadersRequestCondition("header1"); this.header2 = new HeadersRequestCondition("header2"); this.header3 = this.header1.combine(this.header2); }
@Test // SPR-16674 public void compareToWithMoreSpecificMatchByValue() { ServerWebExchange exchange = MockServerWebExchange.from(get("/")); ParamsRequestCondition condition1 = new ParamsRequestCondition("response_type=code"); ParamsRequestCondition condition2 = new ParamsRequestCondition("response_type"); int result = condition1.compareTo(condition2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); }
@Test public void compareToWithNegatedMatch() { ServerWebExchange exchange = MockServerWebExchange.from(get("/")); ParamsRequestCondition condition1 = new ParamsRequestCondition("response_type!=code"); ParamsRequestCondition condition2 = new ParamsRequestCondition("response_type"); assertEquals("Negated match should not count as more specific", 0, condition1.compareTo(condition2, exchange)); }
@Test(expected = ClassCastException.class) public void combineIncompatible() { RequestConditionHolder params = new RequestConditionHolder(new ParamsRequestCondition("name")); RequestConditionHolder headers = new RequestConditionHolder(new HeadersRequestCondition("name")); params.combine(headers); }
@Test(expected = ClassCastException.class) public void compareIncompatible() { RequestConditionHolder params = new RequestConditionHolder(new ParamsRequestCondition("name")); RequestConditionHolder headers = new RequestConditionHolder(new HeadersRequestCondition("name")); params.compareTo(headers, this.exchange); }
@Test public void compareTo() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from(get("/")); ParamsRequestCondition condition1 = new ParamsRequestCondition("foo", "bar", "baz"); ParamsRequestCondition condition2 = new ParamsRequestCondition("foo", "bar"); int result = condition1.compareTo(condition2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); result = condition2.compareTo(condition1, exchange); assertTrue("Invalid comparison result: " + result, result > 0); }
@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 paramPresent() throws Exception { ParamsRequestCondition condition = new ParamsRequestCondition("foo"); assertNotNull(condition.getMatchingCondition(MockServerWebExchange.from(get("/path?foo=")))); }
@Test public void paramValueMatch() throws Exception { ParamsRequestCondition condition = new ParamsRequestCondition("foo=bar"); assertNotNull(condition.getMatchingCondition(MockServerWebExchange.from(get("/path?foo=bar")))); }
@Test // SPR-15831 public void paramPresentNullValue() throws Exception { ParamsRequestCondition condition = new ParamsRequestCondition("foo"); assertNotNull(condition.getMatchingCondition(MockServerWebExchange.from(get("/path?foo")))); }
@Test public void paramNotPresent() throws Exception { MockServerWebExchange exchange = MockServerWebExchange.from(get("/")); assertNotNull(new ParamsRequestCondition("!foo").getMatchingCondition(exchange)); }
@Test public void paramPresentNoMatch() throws Exception { ParamsRequestCondition condition = new ParamsRequestCondition("foo"); assertNull(condition.getMatchingCondition(MockServerWebExchange.from(get("/path?bar=")))); }
@Test public void paramValueNoMatch() throws Exception { ParamsRequestCondition condition = new ParamsRequestCondition("foo=bar"); assertNull(condition.getMatchingCondition(MockServerWebExchange.from(get("/path?foo=bazz")))); }
@Test public void compareEmpty() { RequestConditionHolder empty = new RequestConditionHolder(null); RequestConditionHolder empty2 = new RequestConditionHolder(null); RequestConditionHolder notEmpty = new RequestConditionHolder(new ParamsRequestCondition("name")); assertEquals(0, empty.compareTo(empty2, this.exchange)); assertEquals(-1, notEmpty.compareTo(empty, this.exchange)); assertEquals(1, empty.compareTo(notEmpty, this.exchange)); }
@Test public void matchCustomCondition() { ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/foo?foo=bar")); RequestMappingInfo info = paths("/foo").params("foo=bar").build(); RequestMappingInfo match = info.getMatchingCondition(exchange); assertNotNull(match); info = paths("/foo").params("foo!=bar") .customCondition(new ParamsRequestCondition("foo!=bar")).build(); match = info.getMatchingCondition(exchange); assertNull(match); }