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-tenant.git
commit 261044b5f74c878132d218e98572669dfcf96df4 Author: Carsten Ziegeler <[email protected]> AuthorDate: Mon Jan 4 13:45:54 2016 +0000 SLING-3695 : TenantProvider throws NPE when listing tenants root tenant resource does not exist. Apply patch from Timothee Maret git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1722868 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 10 ++++ .../sling/tenant/internal/TenantProviderImpl.java | 27 ++++++----- .../tenant/internal/TenantProviderImplTest.java | 56 ++++++++++++++++++++++ 3 files changed, 81 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 8bd500c..12aaba9 100644 --- a/pom.xml +++ b/pom.xml @@ -127,5 +127,15 @@ <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>1.10.19</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + </dependency> </dependencies> </project> diff --git a/src/main/java/org/apache/sling/tenant/internal/TenantProviderImpl.java b/src/main/java/org/apache/sling/tenant/internal/TenantProviderImpl.java index 83be26e..367dff0 100644 --- a/src/main/java/org/apache/sling/tenant/internal/TenantProviderImpl.java +++ b/src/main/java/org/apache/sling/tenant/internal/TenantProviderImpl.java @@ -122,14 +122,14 @@ public class TenantProviderImpl implements TenantProvider, TenantManager { private WebConsolePlugin plugin; @Activate - private void activate(final BundleContext bundleContext, final Map<String, Object> properties) { + protected void activate(final BundleContext bundleContext, final Map<String, Object> properties) { this.tenantRootPath = PropertiesUtil.toString(properties.get(TENANT_ROOT), RESOURCE_TENANT_ROOT); this.adapterFactory = new TenantAdapterFactory(bundleContext, this, PropertiesUtil.toStringArray(properties.get(TENANT_PATH_MATCHER), DEFAULT_PATH_MATCHER)); this.plugin = new WebConsolePlugin(bundleContext, this); } @Deactivate - private void deactivate() { + protected void deactivate() { if (this.adapterFactory != null) { this.adapterFactory.dispose(); this.adapterFactory = null; @@ -204,21 +204,24 @@ public class TenantProviderImpl implements TenantProvider, TenantManager { } Iterator<Tenant> result = call(new ResourceResolverTask<Iterator<Tenant>>() { + @SuppressWarnings("unchecked") @Override public Iterator<Tenant> call(ResourceResolver resolver) { Resource tenantRootRes = resolver.getResource(tenantRootPath); - - List<Tenant> tenantList = new ArrayList<Tenant>(); - Iterator<Resource> tenantResourceList = tenantRootRes.listChildren(); - while (tenantResourceList.hasNext()) { - Resource tenantRes = tenantResourceList.next(); - - if (filter == null || filter.matches(ResourceUtil.getValueMap(tenantRes))) { - TenantImpl tenant = new TenantImpl(tenantRes); - tenantList.add(tenant); + if ( tenantRootRes != null ) { + List<Tenant> tenantList = new ArrayList<Tenant>(); + Iterator<Resource> tenantResourceList = tenantRootRes.listChildren(); + while (tenantResourceList.hasNext()) { + Resource tenantRes = tenantResourceList.next(); + + if (filter == null || filter.matches(ResourceUtil.getValueMap(tenantRes))) { + TenantImpl tenant = new TenantImpl(tenantRes); + tenantList.add(tenant); + } } + return tenantList.iterator(); } - return tenantList.iterator(); + return Collections.EMPTY_LIST.iterator(); } }); diff --git a/src/test/java/org/apache/sling/tenant/internal/TenantProviderImplTest.java b/src/test/java/org/apache/sling/tenant/internal/TenantProviderImplTest.java new file mode 100644 index 0000000..495d103 --- /dev/null +++ b/src/test/java/org/apache/sling/tenant/internal/TenantProviderImplTest.java @@ -0,0 +1,56 @@ +/* + * 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.tenant.internal; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Iterator; + +import junit.framework.TestCase; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ResourceResolverFactory; +import org.apache.sling.tenant.Tenant; +import org.junit.Test; +import org.mockito.Mockito; +import org.osgi.framework.BundleContext; + +public class TenantProviderImplTest { + + @Test + public void testListTenantsWithoutTenantRoot() throws Exception { + TenantProviderImpl provider = new TenantProviderImpl(); + final ResourceResolverFactory rrf = Mockito.mock(ResourceResolverFactory.class); + final BundleContext context = Mockito.mock(BundleContext.class); + final ResourceResolver rr = Mockito.mock(ResourceResolver.class); + Mockito.when(rrf.getAdministrativeResourceResolver( + Mockito.anyMapOf(String.class, Object.class))).thenReturn(rr); + set(provider, "factory", rrf); + provider.activate(context, new HashMap<String, Object>()); + Iterator<Tenant> tenants = provider.getTenants(); + TestCase.assertNotNull(tenants); + TestCase.assertFalse(tenants.hasNext()); + } + + private static void set(Object o, String name, Object value) throws Exception { + final Field f = o.getClass().getDeclaredField(name); + f.setAccessible(true); + f.set(o, value); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
