import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.web.ServletFilter;
+import org.sonar.api.web.ServletFilter.UrlPattern;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
@Rule
public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public LogTester logTester = new LogTester();
+
@Before
public void resetSingleton() {
MasterServletFilter.INSTANCE = null;
@Test
public void should_init_and_destroy_filters() throws Exception {
- ServletFilter filter = mock(ServletFilter.class);
+ ServletFilter filter = createMockFilter();
FilterConfig config = mock(FilterConfig.class);
MasterServletFilter master = new MasterServletFilter();
master.init(config, singletonList(filter));
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("foo");
- ServletFilter filter = mock(ServletFilter.class);
+ ServletFilter filter = createMockFilter();
doThrow(new IllegalStateException("foo")).when(filter).init(any(FilterConfig.class));
FilterConfig config = mock(FilterConfig.class);
assertThat(filter2.count).isEqualTo(2);
}
+ @Test
+ public void display_servlet_filter_patterns_in_INFO_log() throws Exception {
+ ServletFilter filter = new PatternFilter(UrlPattern.builder().includes("/api/issues").excludes("/batch/projects").build());
+ FilterConfig config = mock(FilterConfig.class);
+ MasterServletFilter master = new MasterServletFilter();
+
+ master.init(config, singletonList(filter));
+
+ assertThat(logTester.logs(LoggerLevel.INFO)).containsOnly("Initializing servlet filter PatternFilter [pattern=UrlPattern{inclusions=[/api/issues], exclusions=[/batch/projects]}]");
+ }
+
+ private static ServletFilter createMockFilter() {
+ ServletFilter filter = mock(ServletFilter.class);
+ when(filter.doGetPattern()).thenReturn(UrlPattern.builder().build());
+ return filter;
+ }
+
private static final class TrueFilter extends ServletFilter {
private static int globalCount = 0;
private int count = 0;
}
}
+ private static class PatternFilter extends ServletFilter {
+
+ private final UrlPattern urlPattern;
+
+ PatternFilter(UrlPattern urlPattern) {
+ this.urlPattern = urlPattern;
+ }
+
+ @Override
+ public UrlPattern doGetPattern() {
+ return urlPattern;
+ }
+
+ @Override
+ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
+ // Nothing to do
+ }
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ // Nothing to do
+ }
+
+ @Override
+ public void destroy() {
+ // Nothing to do
+ }
+
+ @Override
+ public String toString() {
+ return "PatternFilter";
+ }
+ }
+
}
throw new IllegalStateException("this method is deprecated and should not be used anymore");
}
+ public String label() {
+ return "UrlPattern{" +
+ "inclusions=[" + convertPatternsToString(inclusions) + "]" +
+ ", exclusions=[" + convertPatternsToString(exclusions) + "]" +
+ '}';
+ }
+
+ private static String convertPatternsToString(List<String> input) {
+ StringBuilder output = new StringBuilder();
+ if (input.isEmpty()) {
+ return "";
+ }
+ if (input.size() == 1) {
+ return output.append(input.get(0)).toString();
+ }
+ return output.append(input.get(0)).append(", ...").toString();
+ }
+
/**
* Defines only a single inclusion pattern. This is a shortcut for {@code builder().includes(inclusionPattern).build()}.
*/
"/mstile*");
}
+ @Test
+ public void test_label() throws Exception {
+ assertThat(ServletFilter.UrlPattern.builder().build().label()).isEqualTo("UrlPattern{inclusions=[], exclusions=[]}");
+ assertThat(ServletFilter.UrlPattern.builder()
+ .includes("/foo/*")
+ .excludes("/foo/login")
+ .build().label()).isEqualTo("UrlPattern{inclusions=[/foo/*], exclusions=[/foo/login]}");
+ assertThat(ServletFilter.UrlPattern.builder()
+ .includes("/foo/*", "/foo/lo*")
+ .excludes("/foo/login", "/foo/logout", "/foo/list")
+ .build().label()).isEqualTo("UrlPattern{inclusions=[/foo/*, ...], exclusions=[/foo/login, ...]}");
+ }
+
private static class FakeFilter extends ServletFilter {
@Override
public UrlPattern doGetPattern() {