Skip to content

Commit

Permalink
Merge pull request #405 from brusazorzo/master
Browse files Browse the repository at this point in the history
Added test for issue #404
  • Loading branch information
jmrozanec authored Nov 5, 2019
2 parents adf59c7 + c44d3c6 commit 3c47859
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/test/java/com/cronutils/Issue404Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2015 jmrozanec
* Licensed 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 com.cronutils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import org.junit.Assert;
import org.junit.Test;

import com.cronutils.model.Cron;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;

/**
* America/Sao_Paulo is only 3 hours behind UTC. Even with less difference, november 3 seems to be
* ignored by timeFromLastExecution
*/
public class Issue404Test {

@Test
public void testNovember3Midnight() {
final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron().withMinutes().and().withHours().and()
.withDayOfWeek().and().instance();

final Cron cron = new CronParser(cronDefinition).parse("0 0 *");

final ExecutionTime executionTime = ExecutionTime.forCron(cron);

final ZonedDateTime time = LocalDateTime.of(2019, 11, 3, 0, 0, 1).atZone(ZoneId.of("America/Sao_Paulo"));

final Duration timeFromLastExecution = executionTime.timeFromLastExecution(time).get();

Assert.assertEquals(1, timeFromLastExecution.getSeconds());
}


@Test
public void testNovember3Noon() {
final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron().withMinutes().and().withHours().and()
.withDayOfWeek().and().instance();

final Cron cron = new CronParser(cronDefinition).parse("0 0 *");

final ExecutionTime executionTime = ExecutionTime.forCron(cron);

final ZonedDateTime time = LocalDateTime.of(2019, 11, 3, 12, 0, 0).atZone(ZoneId.of("America/Sao_Paulo"));

final Duration timeFromLastExecution = executionTime.timeFromLastExecution(time).get();

Assert.assertEquals(12, timeFromLastExecution.toHours());
}


@Test
public void testSaturdayMidnight() {
final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron().withMinutes().and().withHours().and()
.withDayOfWeek().and().instance();

final Cron cron = new CronParser(cronDefinition).parse("0 0 *");

final ExecutionTime executionTime = ExecutionTime.forCron(cron);

final ZonedDateTime time = LocalDateTime.of(2019, 11, 2, 0, 0, 1).atZone(ZoneId.of("America/Sao_Paulo"));

final Duration timeFromLastExecution = executionTime.timeFromLastExecution(time).get();

Assert.assertEquals(1, timeFromLastExecution.getSeconds());
}


@Test
public void testNoTimezone() {
final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron().withMinutes().and().withHours().and()
.withDayOfWeek().and().instance();

final Cron cron = new CronParser(cronDefinition).parse("0 0 *");

final ExecutionTime executionTime = ExecutionTime.forCron(cron);

final ZonedDateTime time = LocalDateTime.of(2019, 11, 3, 16, 0, 0).atZone(ZoneId.of("UTC"));

final Duration timeFromLastExecution = executionTime.timeFromLastExecution(time).get();

Assert.assertEquals(16, timeFromLastExecution.toHours());
}
}

0 comments on commit 3c47859

Please sign in to comment.