Search in sources :

Example 1 with ValueMapInjector

use of org.apache.sling.models.impl.injectors.ValueMapInjector in project sling by apache.

the class DefaultTest method setup.

@Before
public void setup() {
    when(componentCtx.getBundleContext()).thenReturn(bundleContext);
    when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
    factory = new ModelAdapterFactory();
    factory.activate(componentCtx);
    factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(0, 0));
    factory.adapterImplementations.addClassesAsAdapterAndImplementation(DefaultStringModel.class, PropertyModelWithDefaults.class, DefaultPrimitivesModel.class, DefaultWrappersModel.class, org.apache.sling.models.testmodels.classes.constructorinjection.DefaultPrimitivesModel.class, org.apache.sling.models.testmodels.classes.constructorinjection.DefaultStringModel.class, org.apache.sling.models.testmodels.classes.constructorinjection.DefaultWrappersModel.class);
}
Also used : ValueMapInjector(org.apache.sling.models.impl.injectors.ValueMapInjector) Before(org.junit.Before)

Example 2 with ValueMapInjector

use of org.apache.sling.models.impl.injectors.ValueMapInjector in project sling by apache.

the class ImplementsExtendsTest method setup.

@SuppressWarnings("unchecked")
@Before
public void setup() throws ClassNotFoundException, MalformedURLException {
    when(componentCtx.getBundleContext()).thenReturn(bundleContext);
    when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
    when(bundleContext.registerService(anyString(), anyObject(), any(Dictionary.class))).then(new Answer<ServiceRegistration>() {

        @Override
        public ServiceRegistration answer(InvocationOnMock invocation) throws Throwable {
            final Dictionary<String, Object> props = (Dictionary<String, Object>) invocation.getArguments()[2];
            ServiceRegistration reg = mock(ServiceRegistration.class);
            ServiceReference ref = mock(ServiceReference.class);
            when(reg.getReference()).thenReturn(ref);
            when(ref.getProperty(anyString())).thenAnswer(new Answer<Object>() {

                @Override
                public Object answer(InvocationOnMock invocation) throws Throwable {
                    String key = (String) invocation.getArguments()[0];
                    return props.get(key);
                }
            });
            return reg;
        }
    });
    factory = new ModelAdapterFactory();
    factory.activate(componentCtx);
    factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(2, 2));
    factory.bindImplementationPicker(firstImplementationPicker, firstImplementationPickerProps);
    // simulate bundle add for ModelPackageBundleListener
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(ModelPackageBundleListener.PACKAGE_HEADER, "org.apache.sling.models.testmodels.classes.implextend");
    when(bundle.getHeaders()).thenReturn(headers);
    Vector<URL> classUrls = new Vector<URL>();
    classUrls.add(getClassUrl(ExtendsClassPropertyModel.class));
    classUrls.add(getClassUrl(ImplementsInterfacePropertyModel.class));
    classUrls.add(getClassUrl(ImplementsInterfacePropertyModel2.class));
    classUrls.add(getClassUrl(InvalidImplementsInterfacePropertyModel.class));
    classUrls.add(getClassUrl(InvalidSampleServiceInterface.class));
    classUrls.add(getClassUrl(SampleServiceInterface.class));
    classUrls.add(getClassUrl(SimplePropertyModel.class));
    when(bundle.findEntries(anyString(), anyString(), anyBoolean())).thenReturn(classUrls.elements());
    when(bundle.loadClass(anyString())).then(new Answer<Class<?>>() {

        @Override
        public Class<?> answer(InvocationOnMock invocation) throws ClassNotFoundException {
            String className = (String) invocation.getArguments()[0];
            return ImplementsExtendsTest.this.getClass().getClassLoader().loadClass(className);
        }
    });
    registeredAdapterFactories = (ServiceRegistration[]) factory.listener.addingBundle(bundle, bundleEvent);
}
Also used : Dictionary(java.util.Dictionary) SimplePropertyModel(org.apache.sling.models.testmodels.classes.implextend.SimplePropertyModel) ImplementsInterfacePropertyModel2(org.apache.sling.models.testmodels.classes.implextend.ImplementsInterfacePropertyModel2) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) InvalidImplementsInterfacePropertyModel(org.apache.sling.models.testmodels.classes.implextend.InvalidImplementsInterfacePropertyModel) Vector(java.util.Vector) ServiceRegistration(org.osgi.framework.ServiceRegistration) ValueMapInjector(org.apache.sling.models.impl.injectors.ValueMapInjector) Hashtable(java.util.Hashtable) SampleServiceInterface(org.apache.sling.models.testmodels.classes.implextend.SampleServiceInterface) InvalidSampleServiceInterface(org.apache.sling.models.testmodels.classes.implextend.InvalidSampleServiceInterface) ImplementsInterfacePropertyModel(org.apache.sling.models.testmodels.classes.implextend.ImplementsInterfacePropertyModel) InvalidImplementsInterfacePropertyModel(org.apache.sling.models.testmodels.classes.implextend.InvalidImplementsInterfacePropertyModel) ServiceReference(org.osgi.framework.ServiceReference) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ExtendsClassPropertyModel(org.apache.sling.models.testmodels.classes.implextend.ExtendsClassPropertyModel) Matchers.anyObject(org.mockito.Matchers.anyObject) InvalidSampleServiceInterface(org.apache.sling.models.testmodels.classes.implextend.InvalidSampleServiceInterface) Before(org.junit.Before)

Example 3 with ValueMapInjector

use of org.apache.sling.models.impl.injectors.ValueMapInjector in project sling by apache.

the class InjectorSpecificAnnotationTest method setup.

@Before
public void setup() {
    when(componentCtx.getBundleContext()).thenReturn(bundleContext);
    when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
    factory = new ModelAdapterFactory();
    factory.activate(componentCtx);
    osgiInjector = new OSGiServiceInjector();
    osgiInjector.activate(componentCtx);
    BindingsInjector bindingsInjector = new BindingsInjector();
    ValueMapInjector valueMapInjector = new ValueMapInjector();
    ChildResourceInjector childResourceInjector = new ChildResourceInjector();
    RequestAttributeInjector requestAttributeInjector = new RequestAttributeInjector();
    factory.bindInjector(bindingsInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 1L));
    factory.bindInjector(valueMapInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 2L));
    factory.bindInjector(childResourceInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 3L));
    factory.bindInjector(requestAttributeInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 4L));
    factory.bindInjector(osgiInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 5L));
    factory.bindStaticInjectAnnotationProcessorFactory(bindingsInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 1L));
    factory.bindInjectAnnotationProcessorFactory(valueMapInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 2L));
    factory.bindInjectAnnotationProcessorFactory2(childResourceInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 3L));
    factory.bindStaticInjectAnnotationProcessorFactory(requestAttributeInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 4L));
    factory.bindStaticInjectAnnotationProcessorFactory(osgiInjector, Collections.<String, Object>singletonMap(Constants.SERVICE_ID, 5L));
    factory.bindViaProvider(new BeanPropertyViaProvider(), null);
    SlingBindings bindings = new SlingBindings();
    bindings.setLog(log);
    Mockito.when(request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings);
    factory.adapterImplementations.addClassesAsAdapterAndImplementation(InjectorSpecificAnnotationModel.class, org.apache.sling.models.testmodels.classes.constructorinjection.InjectorSpecificAnnotationModel.class);
}
Also used : BindingsInjector(org.apache.sling.models.impl.injectors.BindingsInjector) ValueMapInjector(org.apache.sling.models.impl.injectors.ValueMapInjector) ChildResourceInjector(org.apache.sling.models.impl.injectors.ChildResourceInjector) SlingBindings(org.apache.sling.api.scripting.SlingBindings) OSGiServiceInjector(org.apache.sling.models.impl.injectors.OSGiServiceInjector) RequestAttributeInjector(org.apache.sling.models.impl.injectors.RequestAttributeInjector) BeanPropertyViaProvider(org.apache.sling.models.impl.via.BeanPropertyViaProvider) Before(org.junit.Before)

Example 4 with ValueMapInjector

use of org.apache.sling.models.impl.injectors.ValueMapInjector in project sling by apache.

the class InvalidAdaptationsTest method setup.

@Before
public void setup() {
    when(componentCtx.getBundleContext()).thenReturn(bundleContext);
    when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
    factory = new ModelAdapterFactory();
    factory.activate(componentCtx);
    factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(1, 1));
    factory.bindInjector(new ChildResourceInjector(), new ServicePropertiesMap(2, 0));
    factory.adapterImplementations.addClassesAsAdapterAndImplementation(NonModel.class, RequestModel.class);
}
Also used : ValueMapInjector(org.apache.sling.models.impl.injectors.ValueMapInjector) ChildResourceInjector(org.apache.sling.models.impl.injectors.ChildResourceInjector) Before(org.junit.Before)

Example 5 with ValueMapInjector

use of org.apache.sling.models.impl.injectors.ValueMapInjector in project sling by apache.

the class AdapterFactoryTest method setup.

@Before
public void setup() {
    when(componentCtx.getBundleContext()).thenReturn(bundleContext);
    when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
    factory = new ModelAdapterFactory();
    factory.activate(componentCtx);
    factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(0, 0));
    factory.bindInjector(new SelfInjector(), new ServicePropertiesMap(1, 1));
    factory.adapterImplementations.addClassesAsAdapterAndImplementation(DefaultStringModel.class, ConstructorWithExceptionModel.class, NestedModel.class, NestedModelWithInvalidAdaptable.class, NestedModelWithInvalidAdaptable2.class, ResourceModelWithRequiredField.class);
}
Also used : ValueMapInjector(org.apache.sling.models.impl.injectors.ValueMapInjector) SelfInjector(org.apache.sling.models.impl.injectors.SelfInjector) Before(org.junit.Before)

Aggregations

ValueMapInjector (org.apache.sling.models.impl.injectors.ValueMapInjector)12 Before (org.junit.Before)12 ChildResourceInjector (org.apache.sling.models.impl.injectors.ChildResourceInjector)6 SelfInjector (org.apache.sling.models.impl.injectors.SelfInjector)2 BeanPropertyViaProvider (org.apache.sling.models.impl.via.BeanPropertyViaProvider)2 URL (java.net.URL)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Vector (java.util.Vector)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)1 BindingsInjector (org.apache.sling.models.impl.injectors.BindingsInjector)1 OSGiServiceInjector (org.apache.sling.models.impl.injectors.OSGiServiceInjector)1 RequestAttributeInjector (org.apache.sling.models.impl.injectors.RequestAttributeInjector)1 ResourcePathInjector (org.apache.sling.models.impl.injectors.ResourcePathInjector)1 ChildResourceViaProvider (org.apache.sling.models.impl.via.ChildResourceViaProvider)1 ExtendsClassPropertyModel (org.apache.sling.models.testmodels.classes.implextend.ExtendsClassPropertyModel)1 ImplementsInterfacePropertyModel (org.apache.sling.models.testmodels.classes.implextend.ImplementsInterfacePropertyModel)1