INT-3936: GlobalChIntercep: add negative pattern

JIRA: https://jira.spring.io/browse/INT-3936

* Extract `smartMatch()` logic from the `IntegrationManagementConfigurer`
and `IntegrationMBeanExporter` into the `PatternMatchUtils` class
* Add negative (`!`) pattern matching configuration support to the
`GlobalChannelInterceptor` annotation and `<int:channel-interceptor>` component
* Code style, Docs and JavaDocs polishing
This commit is contained in:
Meherzad Lahewala
2017-09-13 00:41:53 +05:30
committed by Artem Bilan
parent 671097fd50
commit 40783ff547
12 changed files with 347 additions and 127 deletions

View File

@@ -32,6 +32,11 @@
<int:channel id="baz"/>
<int:channel id="test"/>
<int:channel-interceptor pattern="!tes*" order="8">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="twelve"/>
</int:channel-interceptor>
<int:channel-interceptor pattern="input*, foo" order="3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="one"/>
</int:channel-interceptor>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,13 +16,16 @@
package org.springframework.integration.channel.interceptor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -42,6 +45,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Oleg Zhurakousky
* @author David Turanski
* @author Artem Bilan
* @author Meherzad Lahewala
*
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@@ -65,52 +70,63 @@ public class GlobalChannelInterceptorTests {
continue;
}
ChannelInterceptor[] interceptors = channel.getChannelInterceptors().toArray(new ChannelInterceptor[channel.getChannelInterceptors().size()]);
ChannelInterceptor[] interceptors = channel.getChannelInterceptors()
.toArray(new ChannelInterceptor[channel.getChannelInterceptors().size()]);
if (channelName.equals("inputA")) { // 328741
Assert.assertTrue(interceptors.length == 10);
Assert.assertEquals("interceptor-three", interceptors[0].toString());
Assert.assertEquals("interceptor-two", interceptors[1].toString());
Assert.assertEquals("interceptor-eight", interceptors[2].toString());
Assert.assertEquals("interceptor-seven", interceptors[3].toString());
Assert.assertEquals("interceptor-five", interceptors[4].toString());
Assert.assertEquals("interceptor-six", interceptors[5].toString());
Assert.assertEquals("interceptor-ten", interceptors[6].toString());
Assert.assertEquals("interceptor-eleven", interceptors[7].toString());
Assert.assertEquals("interceptor-four", interceptors[8].toString());
Assert.assertEquals("interceptor-one", interceptors[9].toString());
assertTrue(interceptors.length == 10);
assertEquals("interceptor-three", interceptors[0].toString());
assertEquals("interceptor-two", interceptors[1].toString());
assertEquals("interceptor-eight", interceptors[2].toString());
assertEquals("interceptor-seven", interceptors[3].toString());
assertEquals("interceptor-five", interceptors[4].toString());
assertEquals("interceptor-six", interceptors[5].toString());
assertEquals("interceptor-ten", interceptors[6].toString());
assertEquals("interceptor-eleven", interceptors[7].toString());
assertEquals("interceptor-four", interceptors[8].toString());
assertEquals("interceptor-one", interceptors[9].toString());
}
else if (channelName.equals("inputB")) {
Assert.assertTrue(interceptors.length == 6);
Assert.assertEquals("interceptor-three", interceptors[0].toString());
Assert.assertEquals("interceptor-two", interceptors[1].toString());
Assert.assertEquals("interceptor-ten", interceptors[2].toString());
Assert.assertEquals("interceptor-eleven", interceptors[3].toString());
Assert.assertEquals("interceptor-four", interceptors[4].toString());
Assert.assertEquals("interceptor-one", interceptors[5].toString());
assertTrue(interceptors.length == 6);
assertEquals("interceptor-three", interceptors[0].toString());
assertEquals("interceptor-two", interceptors[1].toString());
assertEquals("interceptor-ten", interceptors[2].toString());
assertEquals("interceptor-eleven", interceptors[3].toString());
assertEquals("interceptor-four", interceptors[4].toString());
assertEquals("interceptor-one", interceptors[5].toString());
}
else if (channelName.equals("foo")) {
Assert.assertTrue(interceptors.length == 6);
Assert.assertEquals("interceptor-two", interceptors[0].toString());
Assert.assertEquals("interceptor-five", interceptors[1].toString());
Assert.assertEquals("interceptor-ten", interceptors[2].toString());
Assert.assertEquals("interceptor-eleven", interceptors[3].toString());
Assert.assertEquals("interceptor-four", interceptors[4].toString());
Assert.assertEquals("interceptor-one", interceptors[5].toString());
assertTrue(interceptors.length == 6);
assertEquals("interceptor-two", interceptors[0].toString());
assertEquals("interceptor-five", interceptors[1].toString());
assertEquals("interceptor-ten", interceptors[2].toString());
assertEquals("interceptor-eleven", interceptors[3].toString());
assertEquals("interceptor-four", interceptors[4].toString());
assertEquals("interceptor-one", interceptors[5].toString());
}
else if (channelName.equals("bar")) {
Assert.assertTrue(interceptors.length == 4);
Assert.assertEquals("interceptor-eight", interceptors[0].toString());
Assert.assertEquals("interceptor-seven", interceptors[1].toString());
Assert.assertEquals("interceptor-ten", interceptors[2].toString());
Assert.assertEquals("interceptor-eleven", interceptors[3].toString());
assertTrue(interceptors.length == 4);
assertEquals("interceptor-eight", interceptors[0].toString());
assertEquals("interceptor-seven", interceptors[1].toString());
assertEquals("interceptor-ten", interceptors[2].toString());
assertEquals("interceptor-eleven", interceptors[3].toString());
}
else if (channelName.equals("baz")) {
Assert.assertTrue(interceptors.length == 2);
Assert.assertEquals("interceptor-ten", interceptors[0].toString());
Assert.assertEquals("interceptor-eleven", interceptors[1].toString());
assertTrue(interceptors.length == 2);
assertEquals("interceptor-ten", interceptors[0].toString());
assertEquals("interceptor-eleven", interceptors[1].toString());
}
else if (channelName.equals("inputWithProxy")) {
Assert.assertTrue(interceptors.length == 6);
assertTrue(interceptors.length == 6);
}
else if (channelName.equals("test")) {
assertNotNull(interceptors);
assertTrue(interceptors.length == 2);
List<String> interceptorNames = new ArrayList<String>();
for (ChannelInterceptor interceptor : interceptors) {
interceptorNames.add(interceptor.toString());
}
assertTrue(interceptorNames.contains("interceptor-ten"));
assertTrue(interceptorNames.contains("interceptor-eleven"));
}
}
}
@@ -123,8 +139,8 @@ public class GlobalChannelInterceptorTests {
for (ChannelInterceptor interceptor : channelInterceptors) {
interceptorNames.add(interceptor.toString());
}
Assert.assertTrue(interceptorNames.contains("interceptor-ten"));
Assert.assertTrue(interceptorNames.contains("interceptor-eleven"));
assertTrue(interceptorNames.contains("interceptor-ten"));
assertTrue(interceptorNames.contains("interceptor-eleven"));
}
@@ -159,6 +175,7 @@ public class GlobalChannelInterceptorTests {
public String toString() {
return "interceptor-" + testIdentifier;
}
}
@@ -174,6 +191,7 @@ public class GlobalChannelInterceptorTests {
public void setOrder(int order) {
this.order = order;
}
}
public static class TestInterceptor implements MethodInterceptor {

View File

@@ -0,0 +1,158 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.config;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.integration.channel.ChannelInterceptorAware;
import org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper;
import org.springframework.messaging.support.ChannelInterceptor;
/**
* @author Meherzad Lahewala
*
* @since 5.0
*/
public class GlobalChannelInterceptorProcessorTests {
private GlobalChannelInterceptorProcessor globalChannelInterceptorProcessor;
private ListableBeanFactory beanFactory;
@Before
public void setup() {
this.globalChannelInterceptorProcessor = new GlobalChannelInterceptorProcessor();
this.beanFactory = mock(ListableBeanFactory.class);
this.globalChannelInterceptorProcessor.setBeanFactory(this.beanFactory);
}
@Test
public void testProcessorWithNoInterceptor() {
when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class))
.thenReturn(Collections.emptyMap());
this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
verify(this.beanFactory)
.getBeansOfType(GlobalChannelInterceptorWrapper.class);
verify(this.beanFactory, Mockito.never())
.getBeansOfType(ChannelInterceptorAware.class);
}
@Test
public void testProcessorWithInterceptorDefaultPattern() {
Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
Map<String, ChannelInterceptorAware> channels = new HashMap<>();
ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper =
new GlobalChannelInterceptorWrapper(channelInterceptor);
ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
interceptors.put("Test-1", globalChannelInterceptorWrapper);
channels.put("Test-1", channel);
when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class))
.thenReturn(interceptors);
when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class))
.thenReturn(channels);
this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
verify(channel)
.addInterceptor(channelInterceptor);
}
@Test
public void testProcessorWithInterceptorMatchingPattern() {
Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
Map<String, ChannelInterceptorAware> channels = new HashMap<>();
ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper =
new GlobalChannelInterceptorWrapper(channelInterceptor);
ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
globalChannelInterceptorWrapper.setPatterns(new String[] { "Te*" });
interceptors.put("Test-1", globalChannelInterceptorWrapper);
channels.put("Test-1", channel);
when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class))
.thenReturn(interceptors);
when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class))
.thenReturn(channels);
this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
verify(channel)
.addInterceptor(channelInterceptor);
}
@Test
public void testProcessorWithInterceptorNotMatchingPattern() {
Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
Map<String, ChannelInterceptorAware> channels = new HashMap<>();
ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper =
new GlobalChannelInterceptorWrapper(channelInterceptor);
ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
globalChannelInterceptorWrapper.setPatterns(new String[] { "te*" });
interceptors.put("Test-1", globalChannelInterceptorWrapper);
channels.put("Test-1", channel);
when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class))
.thenReturn(interceptors);
when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class))
.thenReturn(channels);
this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
verify(channel, Mockito.never())
.addInterceptor(channelInterceptor);
}
@Test
public void testProcessorWithInterceptorMatchingNegativePattern() {
Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
Map<String, ChannelInterceptorAware> channels = new HashMap<>();
ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper =
new GlobalChannelInterceptorWrapper(channelInterceptor);
ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
globalChannelInterceptorWrapper.setPatterns(new String[] { "!te*", "!Te*" });
interceptors.put("Test-1", globalChannelInterceptorWrapper);
channels.put("Test-1", channel);
when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class))
.thenReturn(interceptors);
when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class))
.thenReturn(channels);
this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
verify(channel, Mockito.never())
.addInterceptor(channelInterceptor);
}
}