Search in sources :

Example 6 with ValueMapDecorator

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

the class DefaultTest method testDefaultWrappersConstructor.

@Test
public void testDefaultWrappersConstructor() {
    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.DefaultWrappersModel model = factory.getAdapter(res, org.apache.sling.models.testmodels.classes.constructorinjection.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) Test(org.junit.Test)

Example 7 with ValueMapDecorator

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

the class DefaultTest method testDefaultStringValueOnInterfaceField.

@Test
public void testDefaultStringValueOnInterfaceField() {
    ValueMap vm = new ValueMapDecorator(Collections.<String, Object>singletonMap("first", "first value"));
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    PropertyModelWithDefaults model = factory.getAdapter(res, PropertyModelWithDefaults.class);
    assertNotNull(model);
    assertEquals("first value", model.getFirst());
    assertEquals("second default", model.getSecond());
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) PropertyModelWithDefaults(org.apache.sling.models.testmodels.interfaces.PropertyModelWithDefaults) Test(org.junit.Test)

Example 8 with ValueMapDecorator

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

the class DefaultTest method testDefaultPrimitivesConstructor.

@Test
public void testDefaultPrimitivesConstructor() {
    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.DefaultPrimitivesModel model = factory.getAdapter(res, org.apache.sling.models.testmodels.classes.constructorinjection.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) Test(org.junit.Test)

Example 9 with ValueMapDecorator

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

the class ImplementsExtendsTest method getMockResourceWithProps.

private Resource getMockResourceWithProps() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("first", "first-value");
    map.put("third", "third-value");
    ValueMap vm = new ValueMapDecorator(map);
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    return res;
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) Matchers.anyObject(org.mockito.Matchers.anyObject) Matchers.anyString(org.mockito.Matchers.anyString)

Example 10 with ValueMapDecorator

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

the class InjectorSpecificAnnotationTest method testOrderForValueAnnotationField.

@Test
public void testOrderForValueAnnotationField() {
    // make sure that that the correct injection is used
    // make sure that log is adapted from value map
    // and not coming from request attribute
    Logger logFromValueMap = LoggerFactory.getLogger(this.getClass());
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("first", "first-value");
    map.put("log", logFromValueMap);
    ValueMap vm = new ValueMapDecorator(map);
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    when(request.getResource()).thenReturn(res);
    InjectorSpecificAnnotationModel model = factory.getAdapter(request, InjectorSpecificAnnotationModel.class);
    assertNotNull("Could not instanciate model", model);
    assertEquals("first-value", model.getFirst());
    assertEquals(logFromValueMap, model.getLog());
}
Also used : InjectorSpecificAnnotationModel(org.apache.sling.models.testmodels.classes.InjectorSpecificAnnotationModel) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) Logger(org.slf4j.Logger) 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