/** Used so that AllTests can set the persistent test context. */ public static void initializeSystem(PersistentTestContext context) throws Exception { AbstractTest.setContext(context); BaseElement.setContext(context); TestUtils.setContext(context); AbstractTest.componentManager = new EmbeddableComponentManager(); // Only load the minimal number of components required for the test framework, for both performance reasons // and for avoiding having to declare dependencies such as HttpServletRequest. ComponentAnnotationLoader loader = new ComponentAnnotationLoader(); List<ComponentDeclaration> componentDeclarations = new ArrayList<>(); componentDeclarations.add(new ComponentDeclaration(DefaultStringEntityReferenceResolver.class.getName())); componentDeclarations.add(new ComponentDeclaration(DefaultStringEntityReferenceSerializer.class.getName())); componentDeclarations.add(new ComponentDeclaration(RelativeStringEntityReferenceResolver.class.getName())); componentDeclarations.add(new ComponentDeclaration(DefaultEntityReferenceProvider.class.getName())); componentDeclarations.add(new ComponentDeclaration(DefaultModelConfiguration.class.getName())); componentDeclarations.add(new ComponentDeclaration(DefaultSymbolScheme.class.getName())); loader.initialize(AbstractTest.componentManager, AbstractTest.class.getClassLoader(), componentDeclarations); TestUtils.initializeComponent(AbstractTest.componentManager); }
/** * @param annotations the list of annotations to check * @param alreadyProcessedAnnotations the list of already processed annotation to avoid circular dependencies * @return the list of Component Declarations declared by any passed annotation annotated by {@link ComponentList} */ private List<ComponentDeclaration> getComponentDeclarationsFromAnnotation(Annotation[] annotations, List<Annotation> alreadyProcessedAnnotations) { Set<ComponentDeclaration> componentDeclarations = new LinkedHashSet<>(); for (Annotation annotation : annotations) { // Is the Annotation (or one of the Annotation annotating the Annotation) tagged with @ComponentList? // If so, then call the "value()" method to get the list of Component Declarations if (annotation instanceof ComponentList) { ComponentList componentListAnnotation = (ComponentList) annotation; for (Class<?> componentClass : componentListAnnotation.value()) { componentDeclarations.add(new ComponentDeclaration(componentClass.getName())); } } else { // Protection against Annotations that tag themselves with themselves like @Retention, @Documented, etc if (!alreadyProcessedAnnotations.contains(annotation)) { alreadyProcessedAnnotations.add(annotation); componentDeclarations.addAll(getComponentDeclarationsFromAnnotation( annotation.annotationType().getAnnotations(), alreadyProcessedAnnotations)); } } } return new ArrayList<>(componentDeclarations); }
/** * If the user has specified the {@link org.xwiki.test.annotation.AllComponents} annotation then all components * are lodaded; however this is not recommended since it slows down the execution time and makes the test less * controlled; we recommend instead to use the {@link ComponentList} annotation which only registers the component * implementation you pass to it; or even better don't use any annotation which means no component is registered * explicitly which should be the default since you're mocking all component dependencies with the * {@link MockingRequirement} annotation. */ private void registerComponents() { AllComponents allComponentsAnnotation = this.getClass().getAnnotation(AllComponents.class); if (allComponentsAnnotation != null) { this.loader.initialize(this.componentManager, getClass().getClassLoader()); } else { ComponentList componentListAnnotation = this.getClass().getAnnotation(ComponentList.class); if (componentListAnnotation != null) { List<ComponentDeclaration> componentDeclarations = new ArrayList<ComponentDeclaration>(); for (Class<?> componentClass : componentListAnnotation.value()) { componentDeclarations.add(new ComponentDeclaration(componentClass.getName())); } this.loader.initialize(this.componentManager, getClass().getClassLoader(), componentDeclarations); } } }
ComponentDeclaration componentDeclaration; if (chunks.length > 1) { componentDeclaration = new ComponentDeclaration(chunks[1], Integer.parseInt(chunks[0])); } else { componentDeclaration = new ComponentDeclaration(chunks[0]);
componentDeclarations.add(new ComponentDeclaration(componentOverrideDeclaration .getImplementationClassName(), 0));
componentDeclarations.add(new ComponentDeclaration(componentOverrideDeclaration .getImplementationClassName(), 0));