use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class InjectorSpecificAnnotationTest method testOrderForValueAnnotationConstructor.
@Test
public void testOrderForValueAnnotationConstructor() {
// 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);
org.apache.sling.models.testmodels.classes.constructorinjection.InjectorSpecificAnnotationModel model = factory.getAdapter(request, org.apache.sling.models.testmodels.classes.constructorinjection.InjectorSpecificAnnotationModel.class);
assertNotNull("Could not instanciate model", model);
assertEquals("first-value", model.getFirst());
assertEquals(logFromValueMap, model.getLog());
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class InvalidAdaptationsTest method testNonModelClass.
@Test
public void testNonModelClass() {
Map<String, Object> emptyMap = Collections.<String, Object>emptyMap();
Resource res = mock(Resource.class);
when(res.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(emptyMap));
assertNull(factory.getAdapter(res, NonModel.class));
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class ResourceModelClassesTest method testListModel.
@Test
public void testListModel() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("intList", new Integer[] { 1, 2, 9, 8 });
map.put("stringList", new String[] { "hello", "world" });
ValueMap vm = new ValueMapDecorator(map);
Resource res = mock(Resource.class);
when(res.adaptTo(ValueMap.class)).thenReturn(vm);
ListModel model = factory.getAdapter(res, ListModel.class);
assertNotNull(model);
assertEquals(4, model.getIntList().size());
assertEquals(new Integer(2), model.getIntList().get(1));
assertEquals(2, model.getStringList().size());
assertEquals("hello", model.getStringList().get(0));
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class ResourceModelClassesTest method testArrayWrappersModel.
@Test
public void testArrayWrappersModel() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("intArray", new Integer[] { 1, 2, 9, 8 });
map.put("secondIntArray", new int[] { 1, 2, 9, 8 });
ValueMap vm = new ValueMapDecorator(map);
Resource res = mock(Resource.class);
when(res.adaptTo(ValueMap.class)).thenReturn(vm);
ArrayWrappersModel model = factory.getAdapter(res, ArrayWrappersModel.class);
assertNotNull(model);
Integer[] intArray = model.getIntArray();
assertEquals(4, intArray.length);
assertEquals(new Integer(2), intArray[1]);
Integer[] secondIntArray = model.getSecondIntArray();
assertEquals(4, secondIntArray.length);
assertEquals(new Integer(2), secondIntArray[1]);
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class ResourceModelClassesTest method testChildModel.
@Test
public void testChildModel() {
Object firstValue = RandomStringUtils.randomAlphabetic(10);
ValueMap firstMap = new ValueMapDecorator(Collections.singletonMap("property", firstValue));
final Resource firstChild = mock(Resource.class);
when(firstChild.adaptTo(ValueMap.class)).thenReturn(firstMap);
when(firstChild.adaptTo(ChildModel.class)).thenAnswer(new AdaptToChildModel());
Object firstGrandChildValue = RandomStringUtils.randomAlphabetic(10);
ValueMap firstGrandChildMap = new ValueMapDecorator(Collections.singletonMap("property", firstGrandChildValue));
Object secondGrandChildValue = RandomStringUtils.randomAlphabetic(10);
ValueMap secondGrandChildMap = new ValueMapDecorator(Collections.singletonMap("property", secondGrandChildValue));
final Resource firstGrandChild = mock(Resource.class);
when(firstGrandChild.adaptTo(ValueMap.class)).thenReturn(firstGrandChildMap);
when(firstGrandChild.adaptTo(ChildModel.class)).thenAnswer(new AdaptToChildModel());
final Resource secondGrandChild = mock(Resource.class);
when(secondGrandChild.adaptTo(ValueMap.class)).thenReturn(secondGrandChildMap);
when(secondGrandChild.adaptTo(ChildModel.class)).thenAnswer(new AdaptToChildModel());
Resource secondChild = mock(Resource.class);
when(secondChild.listChildren()).thenReturn(Arrays.asList(firstGrandChild, secondGrandChild).iterator());
Resource emptyChild = mock(Resource.class);
when(emptyChild.listChildren()).thenReturn(Collections.<Resource>emptySet().iterator());
Resource res = mock(Resource.class);
when(res.getChild("firstChild")).thenReturn(firstChild);
when(res.getChild("secondChild")).thenReturn(secondChild);
when(res.getChild("emptyChild")).thenReturn(emptyChild);
ParentModel model = factory.getAdapter(res, ParentModel.class);
assertNotNull(model);
ChildModel childModel = model.getFirstChild();
assertNotNull(childModel);
assertEquals(firstValue, childModel.getProperty());
assertEquals(2, model.getGrandChildren().size());
assertEquals(firstGrandChildValue, model.getGrandChildren().get(0).getProperty());
assertEquals(secondGrandChildValue, model.getGrandChildren().get(1).getProperty());
assertEquals(0, model.getEmptyGrandChildren().size());
}
Aggregations