This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-nosql-generic.git
commit 539ff298329518b08c5e2428ac55794cafb816a4 Author: Stefan Seifert <[email protected]> AuthorDate: Wed May 20 23:28:04 2015 +0000 SLING-4381 add special handling for root resource git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1680700 13f79535-47bb-0310-9956-ffa450edef68 --- .../resource/impl/NoSqlResourceProvider.java | 12 +++- .../AbstractNoSqlResourceProviderRootTest.java | 79 ++++++++++++++++++++++ .../SimpleNoSqlResourceProviderRootTest.java | 39 +++++++++++ 3 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/sling/nosql/generic/resource/impl/NoSqlResourceProvider.java b/src/main/java/org/apache/sling/nosql/generic/resource/impl/NoSqlResourceProvider.java index 6a52315..5621566 100644 --- a/src/main/java/org/apache/sling/nosql/generic/resource/impl/NoSqlResourceProvider.java +++ b/src/main/java/org/apache/sling/nosql/generic/resource/impl/NoSqlResourceProvider.java @@ -18,6 +18,7 @@ */ package org.apache.sling.nosql.generic.resource.impl; +import java.util.Collections; import java.util.Dictionary; import java.util.HashMap; import java.util.HashSet; @@ -50,6 +51,9 @@ import org.osgi.service.event.EventAdmin; */ public class NoSqlResourceProvider implements ResourceProvider, ModifyingResourceProvider, QueriableResourceProvider { + private static final String ROOT_PATH = "/"; + private static final NoSqlData ROOT_DATA = new NoSqlData(ROOT_PATH, Collections.<String, Object>emptyMap()); + private final NoSqlAdapter adapter; private final EventAdmin eventAdmin; private final Map<String, NoSqlData> changedResources = new HashMap<String, NoSqlData>(); @@ -64,6 +68,10 @@ public class NoSqlResourceProvider implements ResourceProvider, ModifyingResourc // ### READONLY ACCESS ### public Resource getResource(ResourceResolver resourceResolver, String path) { + if (ROOT_PATH.equals(path)) { + return new NoSqlResource(ROOT_DATA, resourceResolver, this); + } + if (!adapter.validPath(path)) { return null; } @@ -128,7 +136,7 @@ public class NoSqlResourceProvider implements ResourceProvider, ModifyingResourc public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException { - if (!adapter.validPath(path)) { + if (ROOT_PATH.equals(path) || !adapter.validPath(path)) { throw new PersistenceException("Illegal path - unable to create resource at " + path, null, path, null); } @@ -146,7 +154,7 @@ public class NoSqlResourceProvider implements ResourceProvider, ModifyingResourc } public void delete(ResourceResolver resolver, String path) throws PersistenceException { - if (!adapter.validPath(path)) { + if (ROOT_PATH.equals(path) || !adapter.validPath(path)) { throw new PersistenceException("Unable to delete resource at {}" + path, null, path, null); } diff --git a/src/test/java/org/apache/sling/nosql/generic/resource/impl/AbstractNoSqlResourceProviderRootTest.java b/src/test/java/org/apache/sling/nosql/generic/resource/impl/AbstractNoSqlResourceProviderRootTest.java new file mode 100644 index 0000000..b346aa6 --- /dev/null +++ b/src/test/java/org/apache/sling/nosql/generic/resource/impl/AbstractNoSqlResourceProviderRootTest.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.nosql.generic.resource.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.apache.jackrabbit.JcrConstants; +import org.apache.sling.api.resource.PersistenceException; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceUtil; +import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import com.google.common.collect.ImmutableMap; + +/** + * Test monting NoSqlResourceProvider as root resource provider. + */ +public abstract class AbstractNoSqlResourceProviderRootTest { + + @Rule + public SlingContext context = new SlingContext(ResourceResolverType.NONE); + + protected abstract void registerResourceProviderFactoryAsRoot(); + + @Before + public void setUp() throws Exception { + registerResourceProviderFactoryAsRoot(); + } + + @After + public void tearDown() { + context.resourceResolver().revert(); + } + + @Test + public void testRoot() { + Resource root = context.resourceResolver().getResource("/"); + assertNotNull(root); + assertTrue(root instanceof NoSqlResource); + } + + @Test + public void testCreatePath() throws PersistenceException { + ResourceUtil.getOrCreateResource(context.resourceResolver(), "/test/test1", + ImmutableMap.<String, Object>of(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED), + JcrConstants.NT_UNSTRUCTURED, true); + + Resource test = context.resourceResolver().getResource("/test"); + assertNotNull(test); + + Resource test1 = context.resourceResolver().getResource("/test/test1"); + assertNotNull(test1); + + context.resourceResolver().delete(test); + } + +} diff --git a/src/test/java/org/apache/sling/nosql/generic/simple/SimpleNoSqlResourceProviderRootTest.java b/src/test/java/org/apache/sling/nosql/generic/simple/SimpleNoSqlResourceProviderRootTest.java new file mode 100644 index 0000000..61cf09a --- /dev/null +++ b/src/test/java/org/apache/sling/nosql/generic/simple/SimpleNoSqlResourceProviderRootTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.nosql.generic.simple; + +import org.apache.sling.api.resource.ResourceProvider; +import org.apache.sling.nosql.generic.resource.impl.AbstractNoSqlResourceProviderRootTest; +import org.apache.sling.nosql.generic.simple.provider.SimpleNoSqlResourceProviderFactory; + +import com.google.common.collect.ImmutableMap; + +/** + * Test basic ResourceResolver and ValueMap with different data types. + */ +public class SimpleNoSqlResourceProviderRootTest extends AbstractNoSqlResourceProviderRootTest { + + @Override + protected void registerResourceProviderFactoryAsRoot() { + context.registerInjectActivateService(new SimpleNoSqlResourceProviderFactory(), ImmutableMap.<String, Object>builder() + .put(ResourceProvider.ROOTS, "/") + .build()); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
