Search in sources :

Example 71 with ModifiableValueMap

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

the class JcrModifiableValueMapTest method testNamesUpdate.

/**
     * Checks property names encoding, see SLING-2502.
     */
public void testNamesUpdate() throws Exception {
    this.rootNode.getSession().refresh(false);
    final Node testNode = this.rootNode.addNode("nameUpdateTest" + System.currentTimeMillis());
    testNode.setProperty(PROP3, VALUE);
    testNode.getSession().save();
    final ModifiableValueMap pvm = new JcrModifiableValueMap(testNode, getHelperData());
    pvm.put(PROP3, VALUE3);
    pvm.put("jcr:a:b", VALUE3);
    pvm.put("jcr:", VALUE3);
    getSession().save();
    // read with property map
    final ValueMap vm = new JcrModifiableValueMap(testNode, getHelperData());
    assertEquals(VALUE3, vm.get(PROP3));
    assertEquals(VALUE3, vm.get("jcr:a:b"));
    assertEquals(VALUE3, vm.get("jcr:"));
    // read properties
    assertEquals(VALUE3, testNode.getProperty(PROP3).getString());
    assertEquals(VALUE3, testNode.getProperty("jcr:" + Text.escapeIllegalJcrChars("a:b")).getString());
    assertEquals(VALUE3, testNode.getProperty(Text.escapeIllegalJcrChars("jcr:")).getString());
    assertFalse(testNode.hasProperty(Text.escapeIllegalJcrChars(PROP3)));
    assertFalse(testNode.hasProperty(Text.escapeIllegalJcrChars("jcr:a:b")));
}
Also used : Node(javax.jcr.Node) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 72 with ModifiableValueMap

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

the class ResourceCollectionImpl method remove.

/**
     * {@inheritDoc}
     */
public boolean remove(Resource res) throws PersistenceException {
    //remove the resource
    Resource tobeRemovedRes = findRes(res);
    if (tobeRemovedRes == null) {
        return false;
    }
    resolver.delete(tobeRemovedRes);
    //remove from order array
    ModifiableValueMap vm = membersResource.adaptTo(ModifiableValueMap.class);
    String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[] {});
    int index = ArrayUtils.indexOf(order, res.getPath(), 0);
    order = (String[]) ArrayUtils.remove(order, index);
    vm.put(ResourceCollectionConstants.REFERENCES_PROP, order);
    return true;
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 73 with ModifiableValueMap

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

the class AbstractNoSqlResourceProviderTransactionalTest method testUpdateWithCommit.

@Test
public void testUpdateWithCommit() throws PersistenceException {
    Resource node1 = context.resourceResolver().create(testRoot(), "node1", ImmutableMap.<String, Object>of("prop1", "value1"));
    assertEquals("value1", node1.getValueMap().get("prop1", String.class));
    context.resourceResolver().commit();
    ModifiableValueMap props = node1.adaptTo(ModifiableValueMap.class);
    props.put("prop1", "value2");
    context.resourceResolver().commit();
    node1 = testRoot().getChild("node1");
    assertEquals("value2", node1.getValueMap().get("prop1", String.class));
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 74 with ModifiableValueMap

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

the class CustomConfigurationPersistenceStrategy method replaceProperties.

private void replaceProperties(Resource resource, Map<String, Object> properties) {
    ModifiableValueMap modValueMap = resource.adaptTo(ModifiableValueMap.class);
    // remove all existing properties that do not have jcr: namespace
    for (String propertyName : new HashSet<>(modValueMap.keySet())) {
        if (StringUtils.startsWith(propertyName, "jcr:")) {
            continue;
        }
        modValueMap.remove(propertyName);
    }
    modValueMap.putAll(properties);
}
Also used : ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) HashSet(java.util.HashSet)

Example 75 with ModifiableValueMap

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

the class ValidationServiceImplTest method testResourceWithMultivalueProperties.

@Test
public void testResourceWithMultivalueProperties() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
    propertyBuilder.multiple();
    modelBuilder.resourceProperty(propertyBuilder.build("field"));
    ValidationModel vm = modelBuilder.build("type", "some source");
    ResourceResolver rr = context.resourceResolver();
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
    ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
    mvm.put("field", new String[] { "1", "abc", "2" });
    ValidationResult vr = validationService.validate(testResource, vm);
    Assert.assertFalse("resource should have been considered invalid", vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("field[1]", 0, defaultResourceBundle, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "\\d")));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Aggregations

ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)111 Resource (org.apache.sling.api.resource.Resource)74 PersistenceException (org.apache.sling.api.resource.PersistenceException)32 Test (org.junit.Test)28 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)26 HashMap (java.util.HashMap)22 ValueMap (org.apache.sling.api.resource.ValueMap)22 Calendar (java.util.Calendar)13 LoginException (org.apache.sling.api.resource.LoginException)9 ChildResource (org.apache.sling.validation.model.ChildResource)9 Date (java.util.Date)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 ValidationModel (org.apache.sling.validation.model.ValidationModel)7 InputStream (java.io.InputStream)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6