Search in sources :

Example 31 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class ResourcePathInjectionTest method setup.

@Before
public void setup() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("propertyContainingAPath", "/some/other/path");
    map.put("anotherPropertyContainingAPath", "/some/other/path2");
    String[] paths = new String[2];
    paths[0] = "/some/other/path";
    paths[1] = "/some/other/path2";
    String[] invalidPaths = new String[3];
    invalidPaths[0] = "/does/not/exist";
    invalidPaths[1] = "/does/not/exist2";
    invalidPaths[2] = "/some/other/path";
    map.put("propertyWithSeveralPaths", paths);
    map.put("propertyWithMissingPaths", invalidPaths);
    ValueMap properties = new ValueMapDecorator(map);
    when(componentCtx.getBundleContext()).thenReturn(bundleContext);
    when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
    when(adaptable.getResourceResolver()).thenReturn(resourceResolver);
    when(adaptable.adaptTo(ValueMap.class)).thenReturn(properties);
    when(resourceResolver.getResource("/some/path")).thenReturn(byPathResource);
    when(resourceResolver.getResource("/some/path2")).thenReturn(byPathResource2);
    when(resourceResolver.getResource("/some/other/path")).thenReturn(byPropertyValueResource);
    when(resourceResolver.getResource("/some/other/path2")).thenReturn(byPropertyValueResource2);
    factory = new ModelAdapterFactory();
    factory.activate(componentCtx);
    factory.bindInjector(new SelfInjector(), new ServicePropertiesMap(1, Integer.MAX_VALUE));
    factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(2, 2000));
    factory.bindInjector(new ResourcePathInjector(), new ServicePropertiesMap(3, 2500));
    factory.bindStaticInjectAnnotationProcessorFactory(new ResourcePathInjector(), new ServicePropertiesMap(3, 2500));
    factory.adapterImplementations.addClassesAsAdapterAndImplementation(ResourcePathModel.class, ResourcePathPartialModel.class, ResourcePathAllOptionalModel.class, ResourcePathModelWrapping.class);
}
Also used : ValueMapInjector(org.apache.sling.models.impl.injectors.ValueMapInjector) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) SelfInjector(org.apache.sling.models.impl.injectors.SelfInjector) ResourcePathInjector(org.apache.sling.models.impl.injectors.ResourcePathInjector) Before(org.junit.Before)

Example 32 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class DefaultTest method testDefaultWrappersField.

@Test
public void testDefaultWrappersField() {
    ValueMap vm = new ValueMapDecorator(Collections.<String, Object>emptyMap());
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    DefaultWrappersModel model = factory.getAdapter(res, DefaultWrappersModel.class);
    assertNotNull(model);
    assertEquals(Boolean.valueOf(true), model.getBooleanWrapperProperty());
    // we need to wait for JUnit 4.12 for this assertArrayEquals to be working on primitive boolean arrays, https://github.com/junit-team/junit/issues/86!
    assertTrue(Arrays.equals(new Boolean[] { Boolean.TRUE, Boolean.TRUE }, model.getBooleanWrapperArrayProperty()));
    assertEquals(Long.valueOf(1L), model.getLongWrapperProperty());
    assertArrayEquals(new Long[] { Long.valueOf(1L), Long.valueOf(1L) }, model.getLongWrapperArrayProperty());
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) DefaultWrappersModel(org.apache.sling.models.testmodels.classes.DefaultWrappersModel) Test(org.junit.Test)

Example 33 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class DefaultTest method testDefaultStringValueField.

@Test
public void testDefaultStringValueField() {
    ValueMap vm = new ValueMapDecorator(Collections.<String, Object>emptyMap());
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    DefaultStringModel model = factory.getAdapter(res, DefaultStringModel.class);
    assertNotNull(model);
    assertEquals("firstDefault", model.getFirstProperty());
    assertEquals(2, model.getSecondProperty().length);
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) DefaultStringModel(org.apache.sling.models.testmodels.classes.DefaultStringModel) Test(org.junit.Test)

Example 34 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class DefaultTest method testDefaultStringValueConstructor.

@Test
public void testDefaultStringValueConstructor() {
    ValueMap vm = new ValueMapDecorator(Collections.<String, Object>emptyMap());
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    org.apache.sling.models.testmodels.classes.constructorinjection.DefaultStringModel model = factory.getAdapter(res, org.apache.sling.models.testmodels.classes.constructorinjection.DefaultStringModel.class);
    assertNotNull(model);
    assertEquals("firstDefault", model.getFirstProperty());
    assertEquals(2, model.getSecondProperty().length);
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 35 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class DefaultTest method testDefaultPrimitivesField.

@Test
public void testDefaultPrimitivesField() {
    ValueMap vm = new ValueMapDecorator(Collections.<String, Object>emptyMap());
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    DefaultPrimitivesModel model = factory.getAdapter(res, DefaultPrimitivesModel.class);
    assertNotNull(model);
    assertEquals(true, model.getBooleanProperty());
    // we need to wait for JUnit 4.12 for this assertArrayEquals to be working on primitive boolean arrays, https://github.com/junit-team/junit/issues/86!
    assertTrue(Arrays.equals(new boolean[] { true, true }, model.getBooleanArrayProperty()));
    assertEquals(1L, model.getLongProperty());
    assertArrayEquals(new long[] { 1L, 1L }, model.getLongArrayProperty());
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) DefaultPrimitivesModel(org.apache.sling.models.testmodels.classes.DefaultPrimitivesModel) Test(org.junit.Test)

Aggregations

ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)63 Test (org.junit.Test)45 ValueMap (org.apache.sling.api.resource.ValueMap)44 HashMap (java.util.HashMap)36 Resource (org.apache.sling.api.resource.Resource)36 ValidationModel (org.apache.sling.validation.model.ValidationModel)9 ValidationResult (org.apache.sling.validation.ValidationResult)8 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)6 ArrayList (java.util.ArrayList)3 RepositoryException (javax.jcr.RepositoryException)3 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)3 ResourceModelWithRequiredField (org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredField)3 MockResource (org.apache.sling.testing.resourceresolver.MockResource)3 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)3 Calendar (java.util.Calendar)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2