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")));
}
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;
}
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));
}
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);
}
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")));
}
Aggregations