/** * Create component descriptors for the passed component implementation class and component role class. There can be * more than one descriptor if the component class has specified several hints. * * @param componentClass the component implementation class * @param componentRoleClass the component role class * @return the component descriptors with resolved component dependencies * @deprecated since 4.0M1 use {@link #createComponentDescriptors(Class, Type)} instead */ @Deprecated public List<ComponentDescriptor> createComponentDescriptors(Class<?> componentClass, Class<?> componentRoleClass) { return createComponentDescriptors(componentClass, (Type) componentRoleClass); }
/** * Create a component descriptor for the passed component implementation class, hint and component role class. * * @param componentClass the component implementation class * @param hint the hint * @param componentRoleType the component role type * @return the component descriptor with resolved component dependencies */ private ComponentDescriptor createComponentDescriptor(Class<?> componentClass, String hint, Type componentRoleType) { DefaultComponentDescriptor descriptor = new DefaultComponentDescriptor(); descriptor.setRoleType(componentRoleType); descriptor.setImplementation(componentClass); descriptor.setRoleHint(hint); descriptor.setInstantiationStrategy(createComponentInstantiationStrategy(componentClass)); // Set the injected fields. // Note: that we need to find all fields since we can have some inherited fields which are annotated in a // superclass. Since Java doesn't offer a method to return all fields we have to traverse all parent classes // looking for declared fields. for (Field field : ReflectionUtils.getAllFields(componentClass)) { ComponentDependency dependency = createComponentDependency(field); if (dependency != null) { descriptor.addComponentDependency(dependency); } } return descriptor; }
descriptors.add(createComponentDescriptor(componentClass, hint, componentRoleType));
public List<ComponentDescriptor> getComponentsDescriptors(Class<?> componentClass) { List<ComponentDescriptor> descriptors = new ArrayList<ComponentDescriptor>(); // Look for ComponentRole annotations and register one component per ComponentRole found for (Type componentRoleType : findComponentRoleTypes(componentClass)) { descriptors.addAll(this.factory.createComponentDescriptors(componentClass, componentRoleType)); } return descriptors; }
for (ComponentDescriptor<?> componentDescriptor : this.factory.createComponentDescriptors( componentClass, componentRoleType)) {
@Before public void setUp() throws Exception { this.componentManager = new EmbeddableComponentManager(); // Step 1: Register all components available // TODO: Remove this so that tests are executed faster. Need to offer a way to register components manually. this.loader.initialize(this.componentManager, getClass().getClassLoader()); // Step 2: Inject all fields annotated with @MockingRequirement. for (Field field : ReflectionUtils.getAllFields(getClass())) { MockingRequirement mockingRequirement = field.getAnnotation(MockingRequirement.class); if (mockingRequirement != null) { Class< ? > componentRoleClass = findComponentRoleClass(field, mockingRequirement); for (ComponentDescriptor descriptor : factory.createComponentDescriptors(field.getType(), componentRoleClass)) { // Only use the descriptor for the specified hint if ((mockingRequirement.hint().length() > 0 && mockingRequirement.hint().equals( descriptor.getRoleHint())) || mockingRequirement.hint().length() == 0) { registerMockDependencies(descriptor, mockingRequirement); getComponentManager().registerComponent(descriptor); configure(); ReflectionUtils.setFieldValue(this, field.getName(), getComponentManager().lookup(descriptor.getRole(), descriptor.getRoleHint())); break; } } } } }
@Before public void setUp() throws Exception { this.componentManager = new EmbeddableComponentManager(); // Step 1: Register all components available // TODO: Remove this so that tests are executed faster. Need to offer a way to register components manually. this.loader.initialize(this.componentManager, getClass().getClassLoader()); // Step 2: Inject all fields annotated with @MockingRequirement. for (Field field : ReflectionUtils.getAllFields(getClass())) { MockingRequirement mockingRequirement = field.getAnnotation(MockingRequirement.class); if (mockingRequirement != null) { Class< ? > componentRoleClass = findComponentRoleClass(field, mockingRequirement); for (ComponentDescriptor descriptor : factory.createComponentDescriptors(field.getType(), componentRoleClass)) { // Only use the descriptor for the specified hint if ((mockingRequirement.hint().length() > 0 && mockingRequirement.hint().equals( descriptor.getRoleHint())) || mockingRequirement.hint().length() == 0) { registerMockDependencies(descriptor, mockingRequirement); getComponentManager().registerComponent(descriptor); configure(); ReflectionUtils.setFieldValue(this, field.getName(), getComponentManager().lookup(descriptor.getRole(), descriptor.getRoleHint())); break; } } } } }
/** * Mock the injected components for the specified component implementation. * * @param testInstance the test instance * @return the mocked component role/hint or null if invalid * @throws Exception in case of an error while mocking */ public RoleHint<T> mockComponent(final Object testInstance) throws Exception { // Handle component fields for (ComponentDescriptor<T> descriptor : this.factory .createComponentDescriptors(this.componentImplementationClass, findComponentRoleType())) { // Only use the descriptor for the specified hint if ((this.componentRoleHint != null && this.componentRoleHint.equals(descriptor.getRoleHint())) || this.componentRoleHint == null) { registerMockDependencies(descriptor); this.componentManager.registerComponent(descriptor); return new RoleHint<T>(descriptor.getRoleType(), descriptor.getRoleHint()); } } return null; }
for (ComponentDescriptor descriptor : this.factory.createComponentDescriptors(mockingRequirement.value(), componentRoleType))