Search in sources :

Example 46 with ValueMapDecorator

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

the class InjectorSpecificAnnotationTest method testSimpleValueModelConstructor.

@Test
public void testSimpleValueModelConstructor() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("first", "first-value");
    map.put("second", "second-value");
    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("second-value", model.getSecond());
}
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) Test(org.junit.Test)

Example 47 with ValueMapDecorator

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

the class InterfaceInheritanceTest method testSimplePropertyModel.

@Test
public void testSimplePropertyModel() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("superClassString", "first-value");
    map.put("subClassString", "second-value");
    ValueMap vm = new ValueMapDecorator(map);
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    SubClassModel model = factory.getAdapter(res, SubClassModel.class);
    assertNotNull(model);
    assertEquals("first-value", model.getSuperClassString());
    assertEquals("second-value", model.getSubClassString());
}
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) SubClassModel(org.apache.sling.models.testmodels.interfaces.SubClassModel) Test(org.junit.Test)

Example 48 with ValueMapDecorator

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

the class InjectorSpecificAnnotationTest method testSimpleValueModelField.

@Test
public void testSimpleValueModelField() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("first", "first-value");
    map.put("second", "second-value");
    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("second-value", model.getSecond());
}
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) Test(org.junit.Test)

Example 49 with ValueMapDecorator

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

the class InvalidAdaptationsTest method testWrongAdaptableClass.

@Test
public void testWrongAdaptableClass() {
    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, RequestModel.class));
}
Also used : Resource(org.apache.sling.api.resource.Resource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Test(org.junit.Test)

Example 50 with ValueMapDecorator

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

the class MockedResource method adaptTo.

@SuppressWarnings("unchecked")
@Override
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
    if (type.equals(Node.class)) {
        try {
            return (AdapterType) getSession().getNode(getPath());
        } catch (Exception e) {
            logger.error("Exception occurred: " + e, e);
            throw new RuntimeException("Exception occurred: " + e, e);
        }
    } else if (type.equals(ValueMap.class)) {
        try {
            Session session = getSession();
            Node node = session.getNode(getPath());
            HashMap<String, Object> map = new HashMap<String, Object>();
            PropertyIterator properties = node.getProperties();
            while (properties.hasNext()) {
                Property p = properties.nextProperty();
                if (p.getType() == PropertyType.BOOLEAN) {
                    map.put(p.getName(), p.getBoolean());
                } else if (p.getType() == PropertyType.STRING) {
                    map.put(p.getName(), p.getString());
                } else if (p.getType() == PropertyType.DATE) {
                    map.put(p.getName(), p.getDate().getTime());
                } else if (p.getType() == PropertyType.NAME) {
                    map.put(p.getName(), p.getName());
                } else if (p.getType() == PropertyType.LONG) {
                    map.put(p.getName(), p.getLong());
                } else {
                    throw new RuntimeException("Unsupported property type: " + p.getType());
                }
            }
            ValueMap valueMap = new ValueMapDecorator(map);
            return (AdapterType) valueMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    } else if (type.equals(ModifiableValueMap.class)) {
        return (AdapterType) new ModifiableValueMap() {

            public Collection<Object> values() {
                throw new UnsupportedOperationException();
            }

            public int size() {
                throw new UnsupportedOperationException();
            }

            public Object remove(Object arg0) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    final Property p = node.getProperty(String.valueOf(arg0));
                    if (p != null) {
                        p.remove();
                    }
                    // the return value is never used
                    return null;
                } catch (PathNotFoundException pnfe) {
                    // perfectly fine
                    return null;
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }

            public void putAll(Map<? extends String, ? extends Object> arg0) {
                throw new UnsupportedOperationException();
            }

            public Object put(String arg0, Object arg1) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    Object result = null;
                    if (node.hasProperty(arg0)) {
                        final Property previous = node.getProperty(arg0);
                        if (previous == null) {
                        // null
                        } else if (previous.getType() == PropertyType.STRING) {
                            result = previous.getString();
                        } else if (previous.getType() == PropertyType.DATE) {
                            result = previous.getDate();
                        } else if (previous.getType() == PropertyType.BOOLEAN) {
                            result = previous.getBoolean();
                        } else {
                            throw new UnsupportedOperationException();
                        }
                    }
                    if (arg1 instanceof String) {
                        node.setProperty(arg0, (String) arg1);
                    } else if (arg1 instanceof Calendar) {
                        node.setProperty(arg0, (Calendar) arg1);
                    } else if (arg1 instanceof Boolean) {
                        node.setProperty(arg0, (Boolean) arg1);
                    } else if (arg1 instanceof Date) {
                        final Calendar c = Calendar.getInstance();
                        c.setTime((Date) arg1);
                        node.setProperty(arg0, c);
                    } else if (arg1 instanceof Number) {
                        Number n = (Number) arg1;
                        node.setProperty(arg0, n.longValue());
                    } else {
                        throw new UnsupportedOperationException();
                    }
                    return result;
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }

            public Set<String> keySet() {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    final PropertyIterator pi = node.getProperties();
                    final Set<String> result = new HashSet<String>();
                    while (pi.hasNext()) {
                        final Property p = pi.nextProperty();
                        result.add(p.getName());
                    }
                    return result;
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }

            public boolean isEmpty() {
                throw new UnsupportedOperationException();
            }

            public Object get(Object arg0) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    final String key = String.valueOf(arg0);
                    if (node.hasProperty(key)) {
                        return node.getProperty(key);
                    } else {
                        return null;
                    }
                } catch (RepositoryException re) {
                    throw new RuntimeException(re);
                }
            }

            public Set<Entry<String, Object>> entrySet() {
                throw new UnsupportedOperationException();
            }

            public boolean containsValue(Object arg0) {
                throw new UnsupportedOperationException();
            }

            public boolean containsKey(Object arg0) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    return node.hasProperty(String.valueOf(arg0));
                } catch (RepositoryException re) {
                    throw new RuntimeException(re);
                }
            }

            public void clear() {
                throw new UnsupportedOperationException();
            }

            public <T> T get(String name, T defaultValue) {
                throw new UnsupportedOperationException();
            }

            public <T> T get(String name, Class<T> type) {
                Session session = getSession();
                try {
                    final Node node = session.getNode(getPath());
                    if (node == null) {
                        return null;
                    }
                    if (!node.hasProperty(name)) {
                        return null;
                    }
                    Property p = node.getProperty(name);
                    if (p == null) {
                        return null;
                    }
                    if (type.equals(Calendar.class)) {
                        return (T) p.getDate();
                    } else if (type.equals(String.class)) {
                        return (T) p.getString();
                    } else {
                        throw new UnsupportedOperationException();
                    }
                } catch (RepositoryException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    } else {
        return super.adaptTo(type);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Node(javax.jcr.Node) Property(javax.jcr.Property) Calendar(java.util.Calendar) PropertyIterator(javax.jcr.PropertyIterator) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Date(java.util.Date) Collection(java.util.Collection) PathNotFoundException(javax.jcr.PathNotFoundException) Session(javax.jcr.Session)

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