You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2016/01/29 08:34:53 UTC

svn commit: r1727489 - in /sling/trunk/bundles/extensions/event: ./ src/main/java/org/apache/sling/event/impl/jobs/tasks/ src/test/java/org/apache/sling/event/impl/jobs/tasks/

Author: cziegeler
Date: Fri Jan 29 07:34:53 2016
New Revision: 1727489

URL: http://svn.apache.org/viewvc?rev=1727489&view=rev
Log:
SLING-5422 : Sling Event - HistoryCleanUpTask removes Jobs younger than configured. Apply tests from Christoph Nagel

Added:
    sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/tasks/
    sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTaskTest.java   (with props)
Modified:
    sling/trunk/bundles/extensions/event/pom.xml
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java

Modified: sling/trunk/bundles/extensions/event/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/pom.xml?rev=1727489&r1=1727488&r2=1727489&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/pom.xml (original)
+++ sling/trunk/bundles/extensions/event/pom.xml Fri Jan 29 07:34:53 2016
@@ -309,6 +309,12 @@
             <version>1.0.6</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+          <groupId>org.apache.sling</groupId>
+          <artifactId>org.apache.sling.testing.sling-mock</artifactId>
+          <version>1.6.0</version>
+          <scope>test</scope>
+        </dependency>
 
         <dependency>
             <groupId>org.ops4j.pax.exam</groupId>

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java?rev=1727489&r1=1727488&r2=1727489&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java Fri Jan 29 07:34:53 2016
@@ -143,33 +143,51 @@ public class HistoryCleanUpTask implemen
                     continue;
                 }
 
-                // now years
+                final int removeYear = removeDate.get(Calendar.YEAR);
+                final int removeMonth = removeDate.get(Calendar.MONTH) + 1;
+                final int removeDay = removeDate.get(Calendar.DAY_OF_MONTH);
+                final int removeHour = removeDate.get(Calendar.HOUR_OF_DAY);
+                final int removeMinute = removeDate.get(Calendar.MINUTE);
+
+                // start with years
                 final Iterator<Resource> yearIter = topicResource.listChildren();
                 while ( !context.isStopped() && yearIter.hasNext() ) {
                     final Resource yearResource = yearIter.next();
                     final int year = Integer.valueOf(yearResource.getName());
-                    final boolean oldYear = year < removeDate.get(Calendar.YEAR);
+                    if ( year > removeYear ) {
+                        continue;
+                    }
+                    final boolean oldYear = year < removeYear;
 
                     // months
                     final Iterator<Resource> monthIter = yearResource.listChildren();
                     while ( !context.isStopped() && monthIter.hasNext() ) {
                         final Resource monthResource = monthIter.next();
                         final int month = Integer.valueOf(monthResource.getName());
-                        final boolean oldMonth = oldYear || month < (removeDate.get(Calendar.MONTH) + 1);
+                        if ( !oldYear && month > removeMonth) {
+                            continue;
+                        }
+                        final boolean oldMonth = oldYear || month < removeMonth;
 
                         // days
                         final Iterator<Resource> dayIter = monthResource.listChildren();
                         while ( !context.isStopped() && dayIter.hasNext() ) {
                             final Resource dayResource = dayIter.next();
                             final int day = Integer.valueOf(dayResource.getName());
-                            final boolean oldDay = oldMonth || day < removeDate.get(Calendar.DAY_OF_MONTH);
+                            if ( !oldMonth && day > removeDay) {
+                                continue;
+                            }
+                            final boolean oldDay = oldMonth || day < removeDay;
 
                             // hours
                             final Iterator<Resource> hourIter = dayResource.listChildren();
                             while ( !context.isStopped() && hourIter.hasNext() ) {
                                 final Resource hourResource = hourIter.next();
                                 final int hour = Integer.valueOf(hourResource.getName());
-                                final boolean oldHour = oldDay || hour < removeDate.get(Calendar.HOUR_OF_DAY);
+                                if ( !oldDay && hour > removeHour) {
+                                    continue;
+                                }
+                                final boolean oldHour = oldDay || hour < removeHour;
 
                                 // minutes
                                 final Iterator<Resource> minuteIter = hourResource.listChildren();
@@ -178,7 +196,8 @@ public class HistoryCleanUpTask implemen
 
                                     // check if we can delete the minute
                                     final int minute = Integer.valueOf(minuteResource.getName());
-                                    final boolean oldMinute = oldHour || minute <= removeDate.get(Calendar.MINUTE);
+                                    final boolean oldMinute = oldHour || minute <= removeMinute;
+
                                     if ( oldMinute ) {
                                         final Iterator<Resource> jobIter = minuteResource.listChildren();
                                         while ( !context.isStopped() && jobIter.hasNext() ) {
@@ -196,11 +215,11 @@ public class HistoryCleanUpTask implemen
                                                 resolver.commit();
                                             }
                                         }
-                                    }
-                                    // check if we can delete the minute
-                                    if ( !context.isStopped() && oldMinute && !minuteResource.listChildren().hasNext()) {
-                                        resolver.delete(minuteResource);
-                                        resolver.commit();
+                                        // check if we can delete the minute
+                                        if ( !context.isStopped() && !minuteResource.listChildren().hasNext()) {
+                                            resolver.delete(minuteResource);
+                                            resolver.commit();
+                                        }
                                     }
                                 }
 

Added: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTaskTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTaskTest.java?rev=1727489&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTaskTest.java (added)
+++ sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTaskTest.java Fri Jan 29 07:34:53 2016
@@ -0,0 +1,106 @@
+/*
+ * 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.event.impl.jobs.tasks;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Map;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.event.impl.jobs.JobImpl;
+import org.apache.sling.event.impl.jobs.config.JobManagerConfiguration;
+import org.apache.sling.event.jobs.Job;
+import org.apache.sling.event.jobs.consumer.JobExecutionContext;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import com.google.common.collect.Maps;
+
+@RunWith(MockitoJUnitRunner.class)
+public class HistoryCleanUpTaskTest {
+
+    private static final String JCR_PATH = JobManagerConfiguration.DEFAULT_REPOSITORY_PATH + "/finished";
+    private static final String JCR_TOPIC = "test";
+    private static final String JCR_JOB_NAME = "test-job";
+    private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy/MM/dd/HH/mm");
+    private static final int MAX_AGE_IN_DAYS = 60;
+
+    @Rule
+    public final SlingContext ctx = new SlingContext();
+
+    @Mock
+    private JobManagerConfiguration configuration;
+    private Job job;
+    @Mock(answer = Answers.RETURNS_MOCKS)
+    private JobExecutionContext jobContext;
+
+    private HistoryCleanUpTask task;
+
+    @Before
+    public void setUp() {
+        setUpJob();
+        setupConfiguration();
+        task = ctx.registerInjectActivateService(new HistoryCleanUpTask());
+    }
+
+    private void setupConfiguration() {
+        Mockito.when(configuration.getStoredSuccessfulJobsPath()).thenReturn(JCR_PATH);
+        Mockito.when(configuration.createResourceResolver()).thenReturn(ctx.resourceResolver());
+        ctx.registerService(JobManagerConfiguration.class, configuration);
+    }
+
+    private void setUpJob() {
+        Map<String, Object> parameters = Maps.<String, Object> newHashMap();
+        parameters.put("age", MAX_AGE_IN_DAYS * 24 * 60);
+        job = new JobImpl("not-relevant", "not-relevant_123", parameters);
+        Mockito.when(jobContext.isStopped()).thenReturn(false);
+    }
+
+    @Test
+    public void shouldNotDeleteResourcesYoungerThanRemoveDate() {
+        Resource resource = createResourceWithDaysBeforeDate(MAX_AGE_IN_DAYS / 2);
+        task.process(job, jobContext);
+        assertNotNull(ctx.resourceResolver().getResource(resource.getPath()));
+    }
+
+    @Test
+    public void shouldDeleteResourcesOlderThanRemoveDate() {
+        Resource resource = createResourceWithDaysBeforeDate(MAX_AGE_IN_DAYS * 2);
+        task.process(job, jobContext);
+        assertNull(ctx.resourceResolver().getResource(resource.getPath()));
+    }
+
+    private Resource createResourceWithDaysBeforeDate(int days) {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.DAY_OF_YEAR, -days);
+        String path = JCR_PATH + '/' + JCR_TOPIC + '/' + DATE_FORMATTER.format(cal.getTime()) + '/' + JCR_JOB_NAME;
+        return ctx.create().resource(path);
+    }
+
+}

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTaskTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTaskTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url