Introducing Timestamp-task 1.0 from samples (#149)
This will be the replacement for the timestamp-task from Spring-Cloud-Task-Starters Co-authored-by: bono007 <chris.bono@gmail.com>
This commit is contained in:
38
timestamp-task/src/main/docker/assembly.xml
Normal file
38
timestamp-task/src/main/docker/assembly.xml
Normal file
@@ -0,0 +1,38 @@
|
||||
<!--
|
||||
~ Copyright 2020 the original author or authors.
|
||||
~
|
||||
~ 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
|
||||
~
|
||||
~ https://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.
|
||||
-->
|
||||
|
||||
<assembly
|
||||
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 https://maven.apache.org/xsd/assembly-1.1.2.xsd">
|
||||
<id>scenario-task</id>
|
||||
<dependencySets>
|
||||
<dependencySet>
|
||||
<includes>
|
||||
<include>io.spring:timestamp-task</include>
|
||||
</includes>
|
||||
<outputDirectory>maven</outputDirectory>
|
||||
<outputFileNameMapping>timestamp-task.jar</outputFileNameMapping>
|
||||
</dependencySet>
|
||||
</dependencySets>
|
||||
<files>
|
||||
<file>
|
||||
<source>./target/timestamp-task-${project.version}.jar</source>
|
||||
<outputDirectory>.</outputDirectory>
|
||||
<destName>timestamp-task.jar</destName>
|
||||
</file>
|
||||
</files>
|
||||
</assembly>
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 io.spring.timestamp;
|
||||
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.task.configuration.EnableTask;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Spring Boot Application that has tasks enabled.
|
||||
*/
|
||||
@EnableTask
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties({TimestampTaskProperties.class})
|
||||
public class TaskApplication {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(TaskApplication.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TaskApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TimestampTask timeStampTask() {
|
||||
return new TimestampTask();
|
||||
}
|
||||
|
||||
/**
|
||||
* A commandline runner that prints a timestamp.
|
||||
*/
|
||||
public static class TimestampTask implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private TimestampTaskProperties config;
|
||||
|
||||
@Override
|
||||
public void run(String... strings) throws Exception {
|
||||
DateFormat dateFormat = new SimpleDateFormat(this.config.getFormat());
|
||||
logger.info(dateFormat.format(new Date()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 io.spring.timestamp;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "timestamp")
|
||||
public class TimestampTaskProperties {
|
||||
|
||||
/**
|
||||
* The timestamp format, "yyyy-MM-dd HH:mm:ss.SSS" by default.
|
||||
*/
|
||||
private String format = "yyyy-MM-dd HH:mm:ss.SSS";
|
||||
|
||||
public String getFormat() {
|
||||
Assert.hasText(this.format, "format must not be empty nor null");
|
||||
return this.format;
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
configuration-properties.classes=io.spring.timestamp.TimestampTaskProperties
|
||||
1
timestamp-task/src/main/resources/application.properties
Normal file
1
timestamp-task/src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
logging.level.org.springframework.cloud.task=debug
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 io.spring.timestamp;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.test.system.CapturedOutput;
|
||||
import org.springframework.boot.test.system.OutputCaptureExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Verifies that the Task Application outputs the correct task log entries.
|
||||
*
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
@ExtendWith(OutputCaptureExtension.class)
|
||||
public class TaskApplicationTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void testTimeStampApp(CapturedOutput capturedOutput) throws Exception {
|
||||
final String TEST_DATE_DOTS = ".......";
|
||||
final String CREATE_TASK_MESSAGE = "Creating: TaskExecution{executionId=";
|
||||
final String UPDATE_TASK_MESSAGE = "Updating: TaskExecution with executionId=";
|
||||
final String EXIT_CODE_MESSAGE = "with the following {exitCode=0";
|
||||
String[] args = {"--timestamp.format=yyyy" + TEST_DATE_DOTS};
|
||||
|
||||
SpringApplication.run(TaskApplication.class, args);
|
||||
|
||||
String output = capturedOutput.toString();
|
||||
assertThat(output.contains(TEST_DATE_DOTS))
|
||||
.as("Unable to find the timestamp: " + output).isTrue();
|
||||
assertThat(output.contains(CREATE_TASK_MESSAGE))
|
||||
.as("Test results do not show create task message: " + output).isTrue();
|
||||
assertThat(output.contains(UPDATE_TASK_MESSAGE))
|
||||
.as("Test results do not show success message: " + output).isTrue();
|
||||
assertThat(output.contains(EXIT_CODE_MESSAGE))
|
||||
.as("Test results have incorrect exit code: " + output).isTrue();
|
||||
|
||||
String taskTitle = "Demo Timestamp Task";
|
||||
Pattern pattern = Pattern.compile(taskTitle);
|
||||
Matcher matcher = pattern.matcher(output);
|
||||
int count = 0;
|
||||
while (matcher.find()) {
|
||||
count++;
|
||||
}
|
||||
assertThat(count).as("The number of task titles did not match expected: ")
|
||||
.isEqualTo(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 io.spring.timestamp;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
public class TimestampTaskPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void testEmptyFormat() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues testPropertyValues = TestPropertyValues.of("timestamp.format:");
|
||||
testPropertyValues.applyTo(context);
|
||||
context.register(Conf.class);
|
||||
context.refresh();
|
||||
TimestampTaskProperties properties = context
|
||||
.getBean(TimestampTaskProperties.class);
|
||||
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
|
||||
properties.getFormat();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatDefault() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(Conf.class);
|
||||
context.refresh();
|
||||
TimestampTaskProperties properties = context
|
||||
.getBean(TimestampTaskProperties.class);
|
||||
assertThat(properties.getFormat()).as("result does not match default format.")
|
||||
.isEqualTo("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatSet() {
|
||||
final String FORMAT = "yyyy";
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(Conf.class);
|
||||
context.refresh();
|
||||
TimestampTaskProperties properties = context
|
||||
.getBean(TimestampTaskProperties.class);
|
||||
properties.setFormat(FORMAT);
|
||||
assertThat(properties.getFormat()).as("result does not match established format.")
|
||||
.isEqualTo(FORMAT);
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties(TimestampTaskProperties.class)
|
||||
static class Conf {
|
||||
}
|
||||
}
|
||||
2
timestamp-task/src/test/resources/application.properties
Normal file
2
timestamp-task/src/test/resources/application.properties
Normal file
@@ -0,0 +1,2 @@
|
||||
logging.level.org.springframework.cloud.task=DEBUG
|
||||
spring.application.name=Demo Timestamp Task
|
||||
Reference in New Issue
Block a user