Search in sources :

Example 46 with ValueMap

use of org.apache.sling.api.resource.ValueMap 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 47 with ValueMap

use of org.apache.sling.api.resource.ValueMap 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)

Example 48 with ValueMap

use of org.apache.sling.api.resource.ValueMap in project sling by apache.

the class AdapterFactoryTest method testCreatedNestedModelWithMissingElements.

@Test(expected = MissingElementsException.class)
public void testCreatedNestedModelWithMissingElements() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("invalid", "required");
    ValueMap vm = new ValueMapDecorator(map);
    when(resource.adaptTo(ValueMap.class)).thenReturn(vm);
    factory.createModel(resource, NestedModel.class);
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Test(org.junit.Test)

Example 49 with ValueMap

use of org.apache.sling.api.resource.ValueMap in project sling by apache.

the class OptionalPrimitivesTest method testFieldInjectionInterface.

@Test
public void testFieldInjectionInterface() {
    ValueMap vm = ValueMap.EMPTY;
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    org.apache.sling.models.testmodels.interfaces.OptionalPrimitivesModel model = factory.getAdapter(res, org.apache.sling.models.testmodels.interfaces.OptionalPrimitivesModel.class);
    assertNotNull(model);
    // make sure primitives are initialized with initial value
    assertEquals(0, model.getByteValue());
    assertEquals(0, model.getShortValue());
    assertEquals(0, model.getIntValue());
    assertEquals(0L, model.getLongValue());
    assertEquals(0.0f, model.getFloatValue(), 0.00001d);
    assertEquals(0.0d, model.getDoubleValue(), 0.00001d);
    assertEquals('', model.getCharValue());
    assertEquals(false, model.getBooleanValue());
    // make sure object wrapper of primitives are null
    assertNull(model.getByteObjectValue());
    assertNull(model.getShortObjectValue());
    assertNull(model.getIntObjectValue());
    assertNull(model.getLongObjectValue());
    assertNull(model.getFloatObjectValue());
    assertNull(model.getDoubleObjectValue());
    assertNull(model.getCharObjectValue());
    assertNull(model.getBooleanObjectValue());
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 50 with ValueMap

use of org.apache.sling.api.resource.ValueMap in project sling by apache.

the class OptionalPrimitivesTest method testFieldInjectionClass.

@Test
public void testFieldInjectionClass() {
    ValueMap vm = ValueMap.EMPTY;
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    org.apache.sling.models.testmodels.classes.OptionalPrimitivesModel model = factory.getAdapter(res, org.apache.sling.models.testmodels.classes.OptionalPrimitivesModel.class);
    assertNotNull(model);
    // make sure primitives are initialized with initial value
    assertEquals(0, model.getByteValue());
    assertEquals(0, model.getShortValue());
    assertEquals(0, model.getIntValue());
    assertEquals(0L, model.getLongValue());
    assertEquals(0.0f, model.getFloatValue(), 0.00001d);
    assertEquals(0.0d, model.getDoubleValue(), 0.00001d);
    assertEquals('', model.getCharValue());
    assertEquals(false, model.getBooleanValue());
    // make sure object wrapper of primitives are null
    assertNull(model.getByteObjectValue());
    assertNull(model.getShortObjectValue());
    assertNull(model.getIntObjectValue());
    assertNull(model.getLongObjectValue());
    assertNull(model.getFloatObjectValue());
    assertNull(model.getDoubleObjectValue());
    assertNull(model.getCharObjectValue());
    assertNull(model.getBooleanObjectValue());
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Aggregations

ValueMap (org.apache.sling.api.resource.ValueMap)278 Resource (org.apache.sling.api.resource.Resource)205 Test (org.junit.Test)160 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)59 HashMap (java.util.HashMap)54 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)44 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)26 Map (java.util.Map)21 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)21 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)20 RewriterResponse (org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse)20 Expectations (org.jmock.Expectations)20 ArrayList (java.util.ArrayList)18 PersistenceException (org.apache.sling.api.resource.PersistenceException)17 Calendar (java.util.Calendar)16 Date (java.util.Date)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)12 InputStream (java.io.InputStream)11 Node (javax.jcr.Node)11