Skip to content

Commit

Permalink
Merge pull request spring-projects#12715 from Wenwei Liao
Browse files Browse the repository at this point in the history
* spring-projectsgh-12715:
  Polish "Use modifiable set for @ServletComponentScan with no packages"
  Use modifiable set for @ServletComponentScan with no packages
  • Loading branch information
wilkinsona committed Apr 3, 2018
2 parents 2fd177f + 6078fda commit 399455f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2018 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.
Expand All @@ -17,7 +17,6 @@
package org.springframework.boot.web.servlet;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

Expand Down Expand Up @@ -84,8 +83,7 @@ private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
}
if (packagesToScan.isEmpty()) {
return Collections
.singleton(ClassUtils.getPackageName(metadata.getClassName()));
packagesToScan.add(ClassUtils.getPackageName(metadata.getClassName()));
}
return packagesToScan;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2018 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.
Expand Down Expand Up @@ -107,6 +107,37 @@ public void packagesFromMultipleAnnotationsAreMerged() {
"com.example.bar", "com.example.baz");
}

@Test
public void withNoBasePackagesScanningUsesBasePackageOfAnnotatedClass() {
this.context = new AnnotationConfigApplicationContext(NoBasePackages.class);
ServletComponentRegisteringPostProcessor postProcessor = this.context
.getBean(ServletComponentRegisteringPostProcessor.class);
assertThat(postProcessor.getPackagesToScan())
.containsExactly("org.springframework.boot.web.servlet");
}

@Test
public void noBasePackageAndBasePackageAreCombinedCorrectly() {
this.context = new AnnotationConfigApplicationContext(NoBasePackages.class,
BasePackages.class);
ServletComponentRegisteringPostProcessor postProcessor = this.context
.getBean(ServletComponentRegisteringPostProcessor.class);
assertThat(postProcessor.getPackagesToScan()).containsExactlyInAnyOrder(
"org.springframework.boot.web.servlet", "com.example.foo",
"com.example.bar");
}

@Test
public void basePackageAndNoBasePackageAreCombinedCorrectly() {
this.context = new AnnotationConfigApplicationContext(BasePackages.class,
NoBasePackages.class);
ServletComponentRegisteringPostProcessor postProcessor = this.context
.getBean(ServletComponentRegisteringPostProcessor.class);
assertThat(postProcessor.getPackagesToScan()).containsExactlyInAnyOrder(
"org.springframework.boot.web.servlet", "com.example.foo",
"com.example.bar");
}

@Configuration
@ServletComponentScan({ "com.example.foo", "com.example.bar" })
static class ValuePackages {
Expand Down Expand Up @@ -137,4 +168,10 @@ static class ValueAndBasePackages {

}

@Configuration
@ServletComponentScan
static class NoBasePackages {

}

}

0 comments on commit 399455f

Please sign in to comment.