+ * - Specify @ComponentScan with basePackageClasses, includeFilters, and
+ * useDefaultFilters=true. See the following example.
+ *
+ *
+ * @ComponentScan(basePackageClasses={AffinityGroupServiceImpl.class, EventUtils.class},
+ * includeFilters={@Filter(value=TestConfiguration.Library.class, type=FilterType.CUSTOM)},
+ * useDefaultFilters=false)
+ *
+ *
+ * - 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.
+ *
+ *
+ * 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);
+ * }
+ * }
+ *
+ *
+ * @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;
+ }
+
+ public static class CloudStackTestConfiguration {
+
+ @Bean
+ public ComponentContext componentContext() {
+ return new ComponentContext();
+ }
+
+ @Bean
+ public TransactionContextBuilder transactionContextBuilder() {
+ return new TransactionContextBuilder();
+ }
+
+ @Bean
+ public ComponentInstantiationPostProcessor instantiatePostProcessor() {
+ ComponentInstantiationPostProcessor processor = new ComponentInstantiationPostProcessor();
+
+ List interceptors = new ArrayList();
+ interceptors.add(new TransactionContextBuilder());
+ processor.setInterceptors(interceptors);
+
+ return processor;
+ }
+ }
+}