/** * Return root package of spring boot application. * * @param applicationContext * @return list of packages */ public static List<String> getApplicationPackageName(final ApplicationContext applicationContext) { Set<String> candidateClasses = new HashSet<>(); candidateClasses.addAll(Arrays.asList(applicationContext.getBeanNamesForAnnotation(SpringBootApplication.class))); candidateClasses.addAll(Arrays.asList(applicationContext.getBeanNamesForAnnotation(EnableAutoConfiguration.class))); candidateClasses.addAll(Arrays.asList(applicationContext.getBeanNamesForAnnotation(ComponentScan.class))); if (candidateClasses.isEmpty()) { throw new RuntimeException("Is mandatory for the starter have @SpringBootApplication, @EnableAutoConfiguration or @ComponentScan annotation"); } else { return candidateClasses.parallelStream() .map(candidateClazz -> applicationContext.getBean(candidateClazz).getClass().getPackage().getName()) .distinct() .collect(Collectors.toList()); } }
private TypeSpec buildTypeSpec(List<MethodSpec> methodSpecs, List<FieldSpec> fieldSpecs, MethodSpec setUp) { return TypeSpec.classBuilder(this.restControllerModel.getSimpleClassName()) .addAnnotation(Transactional.class) .addAnnotation( AnnotationSpec.builder(RunWith.class) .addMember("value", "$T.class", SpringJUnit4ClassRunner.class) .build() ) .addAnnotation( AnnotationSpec.builder(ComponentScan.class) .addMember("basePackages", "{$S, $S}", "YOUR_DTOs_PACKAGE", "YOUR_SERVICEs_PACKAGE") .build() ) .addAnnotation(SpringBootTest.class) .addModifiers(Modifier.PUBLIC) .addFields(fieldSpecs) .addMethod(setUp) .addMethods(methodSpecs) .build(); }
public static boolean includedInBasePackageClasses(String clazzName, ComponentScan cs) { Class<?> clazzToCheck; try { clazzToCheck = Class.forName(clazzName); } catch (ClassNotFoundException e) { System.out.println("Unable to find " + clazzName); return false; } Class<?>[] clazzes = cs.basePackageClasses(); for (Class<?> clazz : clazzes) { if (clazzToCheck.isAssignableFrom(clazz)) { return true; } } return false; }
@Test public void springBootUseSpringBootApplicationGroovy() { ProjectRequest request = createProjectRequest("web"); request.setLanguage("groovy"); request.setBootVersion("1.2.0.RC1"); request.setName("MyDemo"); request.setPackageName("foo"); generateProject(request) .sourceCodeAssert("src/main/groovy/foo/MyDemoApplication.groovy") .hasImports(SpringBootApplication.class.getName()) .doesNotHaveImports(EnableAutoConfiguration.class.getName(), ComponentScan.class.getName(), Configuration.class.getName()) .contains("@SpringBootApplication").doesNotContain( "@EnableAutoConfiguration", "@Configuration", "@ComponentScan"); }
private void addComponentScanningPackages(Set<String> packages, AnnotationMetadata metadata) { AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata .getAnnotationAttributes(ComponentScan.class.getName(), true)); if (attributes != null) { addPackages(packages, attributes.getStringArray("value")); addPackages(packages, attributes.getStringArray("basePackages")); addClasses(packages, attributes.getStringArray("basePackageClasses")); if (packages.isEmpty()) { packages.add(ClassUtils.getPackageName(metadata.getClassName())); } } }
@Override protected ComponentScan.Filter[] getFilters(FilterType type) { switch (type) { case INCLUDE: return this.annotation.includeFilters(); case EXCLUDE: return this.annotation.excludeFilters(); } throw new IllegalStateException("Unsupported type " + type); }
private boolean isScanningDefaultPackage(AnnotationMetadata metadata) { AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata .getAnnotationAttributes(ComponentScan.class.getName(), true)); if (attributes != null && hasNoScanPackageSpecified(attributes)) { if (isInDefaultPackage(metadata.getClassName())) { return true; } } return false; }
@Override public void afterPropertiesSet() throws Exception { packages = applicationContext .getBeansWithAnnotation(ComponentScan.class).values() .stream() .map(obj -> obj.getClass().getPackage().getName()) .collect(Collectors.toList()); }
@Test public void springBoot11UseEnableAutoConfigurationJava() { ProjectRequest request = createProjectRequest("web"); request.setBootVersion("1.1.9.RELEASE"); request.setName("MyDemo"); request.setPackageName("foo"); generateProject(request) .sourceCodeAssert("src/main/java/foo/MyDemoApplication.java") .hasImports(EnableAutoConfiguration.class.getName(), ComponentScan.class.getName(), Configuration.class.getName()) .doesNotHaveImports(SpringBootApplication.class.getName()) .contains("@EnableAutoConfiguration", "@Configuration", "@ComponentScan") .doesNotContain("@SpringBootApplication"); }
@Test public void springBootUseSpringBootApplicationJava() { ProjectRequest request = createProjectRequest("web"); request.setBootVersion("1.2.0.RC1"); request.setName("MyDemo"); request.setPackageName("foo"); generateProject(request) .sourceCodeAssert("src/main/java/foo/MyDemoApplication.java") .hasImports(SpringBootApplication.class.getName()) .doesNotHaveImports(EnableAutoConfiguration.class.getName(), ComponentScan.class.getName(), Configuration.class.getName()) .contains("@SpringBootApplication").doesNotContain( "@EnableAutoConfiguration", "@Configuration", "@ComponentScan"); }
@Test public void springBoot11UseEnableAutoConfigurationGroovy() { ProjectRequest request = createProjectRequest("web"); request.setLanguage("groovy"); request.setBootVersion("1.1.9.RELEASE"); request.setName("MyDemo"); request.setPackageName("foo"); generateProject(request) .sourceCodeAssert("src/main/groovy/foo/MyDemoApplication.groovy") .hasImports(EnableAutoConfiguration.class.getName(), ComponentScan.class.getName(), Configuration.class.getName()) .doesNotHaveImports(SpringBootApplication.class.getName()) .contains("@EnableAutoConfiguration", "@Configuration", "@ComponentScan") .doesNotContain("@SpringBootApplication"); }
@Test public void springBoot11UseEnableAutoConfigurationKotlin() { ProjectRequest request = createProjectRequest("web"); request.setLanguage("kotlin"); request.setBootVersion("1.1.9.RELEASE"); request.setName("MyDemo"); request.setPackageName("foo"); generateProject(request) .sourceCodeAssert("src/main/kotlin/foo/MyDemoApplication.kt") .hasImports(EnableAutoConfiguration.class.getName(), ComponentScan.class.getName(), Configuration.class.getName()) .doesNotHaveImports(SpringBootApplication.class.getName()) .contains("@EnableAutoConfiguration", "@Configuration", "@ComponentScan") .doesNotContain("@SpringBootApplication"); }
@Test public void springBootUseSpringBootApplicationKotlin() { ProjectRequest request = createProjectRequest("web"); request.setLanguage("kotlin"); request.setBootVersion("1.2.0.RC1"); request.setName("MyDemo"); request.setPackageName("foo"); generateProject(request) .sourceCodeAssert("src/main/kotlin/foo/MyDemoApplication.kt") .hasImports(SpringBootApplication.class.getName()) .doesNotHaveImports(EnableAutoConfiguration.class.getName(), ComponentScan.class.getName(), Configuration.class.getName()) .contains("@SpringBootApplication").doesNotContain( "@EnableAutoConfiguration", "@Configuration", "@ComponentScan"); }
@Test public void simpleNameTest() throws ParseException { AnnotatedWith annotatedWith = new AnnotatedWith(ComponentScan.class); Assert.assertTrue(annotatedWith.evaluate(compilationUnit)); }
/** * This method allows you to use @ComponentScan for your unit testing but * it limits the scope of the classes found to the class specified in * the @ComponentScan annotation. * <p> * Without using this method, the default behavior of @ComponentScan is * to actually scan in the package of the class specified rather than * only the class. This can cause extra classes to be loaded which causes * the classes these extra classes depend on to be loaded. The end effect * is often most of the project gets loaded. * <p> * In order to use this method properly, you must do the following: <li> * - Specify @ComponentScan with basePackageClasses, includeFilters, and * useDefaultFilters=true. See the following example. * <p> * <pre> * @ComponentScan(basePackageClasses={AffinityGroupServiceImpl.class, EventUtils.class}, * includeFilters={@Filter(value=TestConfiguration.Library.class, type=FilterType.CUSTOM)}, * useDefaultFilters=false) * </pre> * <p> * - Create a Library class and use that to call this method. See the * following example. The Library class you define here is the Library * class being added in the filter above. * <p> * <pre> * public static class Library implements TypeFilter { * @Override * public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { * ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class); * return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); * } * } * </pre> * * @param clazzName name of the class that should be included in the Spring components * @param cs ComponentScan annotation that was declared on the configuration * @return */ public static boolean includedInBasePackageClasses(final String clazzName, final ComponentScan cs) { final Class<?> clazzToCheck; try { clazzToCheck = Class.forName(clazzName); } catch (final ClassNotFoundException e) { throw new CloudRuntimeException("Unable to find " + clazzName); } final Class<?>[] clazzes = cs.basePackageClasses(); for (final Class<?> clazz : clazzes) { if (clazzToCheck.isAssignableFrom(clazz)) { return true; } } return false; }
@Override public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException { mdr.getClassMetadata().getClassName(); final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException { final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) { mdr.getClassMetadata().getClassName(); final ComponentScan cs = SecurityGroupManagerTestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException { mdr.getClassMetadata().getClassName(); final ComponentScan cs = NetworkACLTestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException { mdr.getClassMetadata().getClassName(); final ComponentScan cs = VpcTestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException { mdr.getClassMetadata().getClassName(); final ComponentScan cs = SnapshotDaoTestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException { mdr.getClassMetadata().getClassName(); final ComponentScan cs = StoragePoolDaoTestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) { mdr.getClassMetadata().getClassName(); final ComponentScan cs = ChildTestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
/** * This method allows you to use @ComponentScan for your unit testing but * it limits the scope of the classes found to the class specified in * the @ComponentScan annotation. * * Without using this method, the default behavior of @ComponentScan is * to actually scan in the package of the class specified rather than * only the class. This can cause extra classes to be loaded which causes * the classes these extra classes depend on to be loaded. The end effect * is often most of the project gets loaded. * * In order to use this method properly, you must do the following: <li> * - Specify @ComponentScan with basePackageClasses, includeFilters, and * useDefaultFilters=true. See the following example. * * <pre> * @ComponentScan(basePackageClasses={AffinityGroupServiceImpl.class, EventUtils.class}, * includeFilters={@Filter(value=TestConfiguration.Library.class, type=FilterType.CUSTOM)}, * useDefaultFilters=false) * </pre> * * - Create a Library class and use that to call this method. See the * following example. The Library class you define here is the Library * class being added in the filter above. * * <pre> * public static class Library implements TypeFilter { * @Override * public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { * ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class); * return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); * } * } * </pre> * * @param clazzName name of the class that should be included in the Spring components * @param cs ComponentScan annotation that was declared on the configuration * * @return */ public static boolean includedInBasePackageClasses(String clazzName, ComponentScan cs) { Class<?> clazzToCheck; try { clazzToCheck = Class.forName(clazzName); } catch (ClassNotFoundException e) { throw new CloudRuntimeException("Unable to find " + clazzName); } Class<?>[] clazzes = cs.basePackageClasses(); for (Class<?> clazz : clazzes) { if (clazzToCheck.isAssignableFrom(clazz)) { return true; } } return false; }
@Override public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { mdr.getClassMetadata().getClassName(); ComponentScan cs = StorageAllocatorTestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { mdr.getClassMetadata().getClassName(); ComponentScan cs = ChildTestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { mdr.getClassMetadata().getClassName(); ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { mdr.getClassMetadata().getClassName(); ComponentScan cs = UsageManagerTestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }
@Override public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class); return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); }