joerghoh commented on code in PR #73: URL: https://github.com/apache/sling-org-apache-sling-distribution-core/pull/73#discussion_r2670015452
########## src/test/java/org/apache/sling/distribution/serialization/impl/DistributionPackageBuilderFactoryTest.java: ########## @@ -0,0 +1,84 @@ +/* + * 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.distribution.serialization.impl; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Dictionary; + +import org.apache.sling.api.resource.ResourceResolverFactory; +import org.apache.sling.commons.scheduler.Scheduler; +import org.apache.sling.distribution.serialization.DistributionContentSerializer; +import org.apache.sling.distribution.packaging.impl.ResourceDistributionPackageCleanup; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.osgi.framework.BundleContext; + +public class DistributionPackageBuilderFactoryTest { + + /** + * Tests that the cleanup task has a dedicated thread pool. + * see SLING-11026 + */ + @Test + public void testCleanupTaskHasDedicatedThreadPool() { + DistributionPackageBuilderFactory factory = new DistributionPackageBuilderFactory(); + + BundleContext context = mock(BundleContext.class); + DistributionPackageBuilderFactory.Config config = mock(DistributionPackageBuilderFactory.Config.class); + ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class); + DistributionContentSerializer contentSerializer = mock(DistributionContentSerializer.class); + + try { + java.lang.reflect.Field resolverFactoryField = DistributionPackageBuilderFactory.class.getDeclaredField("resolverFactory"); + resolverFactoryField.setAccessible(true); + resolverFactoryField.set(factory, resolverFactory); + + java.lang.reflect.Field contentSerializerField = DistributionPackageBuilderFactory.class.getDeclaredField("contentSerializer"); + contentSerializerField.setAccessible(true); + contentSerializerField.set(factory, contentSerializer); + } catch (Exception e) { + throw new RuntimeException(e); + } + when(config.type()).thenReturn("resource"); + when(config.package_filters()).thenReturn(new String[0]); + when(config.property_filters()).thenReturn(new String[0]); + when(config.tempFsFolder()).thenReturn("/tmp"); + when(config.digestAlgorithm()).thenReturn("NONE"); + + when(config.fileThreshold()).thenReturn(1); + when(config.memoryUnit()).thenReturn("MEGA_BYTES"); + when(config.useOffHeapMemory()).thenReturn(false); + when(config.monitoringQueueSize()).thenReturn(0); + when(config.cleanupDelay()).thenReturn(60L); Review Comment: this is a massive amount of mocking, and I don't feel comfortable with it. Have you thought about using Sling Mocks (https://sling.apache.org/documentation/development/sling-mock.html) instead of these manual mocks? From what I see they are already referenced and therefor available to be used. ########## src/test/java/org/apache/sling/distribution/serialization/impl/DistributionPackageBuilderFactoryTest.java: ########## @@ -0,0 +1,84 @@ +/* + * 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.distribution.serialization.impl; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Dictionary; + +import org.apache.sling.api.resource.ResourceResolverFactory; +import org.apache.sling.commons.scheduler.Scheduler; +import org.apache.sling.distribution.serialization.DistributionContentSerializer; +import org.apache.sling.distribution.packaging.impl.ResourceDistributionPackageCleanup; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.osgi.framework.BundleContext; + +public class DistributionPackageBuilderFactoryTest { + + /** + * Tests that the cleanup task has a dedicated thread pool. + * see SLING-11026 + */ + @Test + public void testCleanupTaskHasDedicatedThreadPool() { + DistributionPackageBuilderFactory factory = new DistributionPackageBuilderFactory(); + + BundleContext context = mock(BundleContext.class); + DistributionPackageBuilderFactory.Config config = mock(DistributionPackageBuilderFactory.Config.class); + ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class); + DistributionContentSerializer contentSerializer = mock(DistributionContentSerializer.class); + + try { + java.lang.reflect.Field resolverFactoryField = DistributionPackageBuilderFactory.class.getDeclaredField("resolverFactory"); + resolverFactoryField.setAccessible(true); + resolverFactoryField.set(factory, resolverFactory); + + java.lang.reflect.Field contentSerializerField = DistributionPackageBuilderFactory.class.getDeclaredField("contentSerializer"); + contentSerializerField.setAccessible(true); + contentSerializerField.set(factory, contentSerializer); + } catch (Exception e) { + throw new RuntimeException(e); + } + when(config.type()).thenReturn("resource"); + when(config.package_filters()).thenReturn(new String[0]); + when(config.property_filters()).thenReturn(new String[0]); + when(config.tempFsFolder()).thenReturn("/tmp"); + when(config.digestAlgorithm()).thenReturn("NONE"); + + when(config.fileThreshold()).thenReturn(1); + when(config.memoryUnit()).thenReturn("MEGA_BYTES"); + when(config.useOffHeapMemory()).thenReturn(false); + when(config.monitoringQueueSize()).thenReturn(0); + when(config.cleanupDelay()).thenReturn(60L); + + factory.activate(context, config); + + ArgumentCaptor<Dictionary<String, Object>> propsCaptor = ArgumentCaptor.forClass(Dictionary.class); + verify(context).registerService(eq(Runnable.class), any(ResourceDistributionPackageCleanup.class), propsCaptor.capture()); + + Dictionary<String, Object> props = propsCaptor.getValue(); + assertEquals("org-apache-sling-distribution", props.get(Scheduler.PROPERTY_SCHEDULER_THREAD_POOL)); Review Comment: I see this value ``org-apache-sling-distribution`` being used 3 times, we should extract it to a package-level constant. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
