From b5cc62b50e5bdf720dffdb6ee01501582ca264f2 Mon Sep 17 00:00:00 2001 From: mgooty Date: Wed, 1 Oct 2014 14:30:11 +0530 Subject: [PATCH] Added sample code for Spring events and Spring profiles. --- spring-all/pom.xml | 3 +- .../baeldung/profiles/DatasourceConfig.java | 5 ++ .../profiles/DevDatasourceConfig.java | 15 ++++++ .../profiles/ProductionDatasourceConfig.java | 15 ++++++ .../profiles/SpringProfilesConfig.java | 10 ++++ .../ScheduledAnnotationExample.java | 52 +++++++++++++++++++ .../scheduling/SchedulingWithXmlConfig.java | 16 ++++++ .../scheduling/SpringSchedulingConfig.java | 20 +++++++ .../AsynchronousSpringEventsConfig.java | 20 +++++++ .../synchronous/ContextRefreshedListener.java | 15 ++++++ .../synchronous/CustomSpringEvent.java | 19 +++++++ .../CustomSpringEventListener.java | 14 +++++ .../CustomSpringEventPublisher.java | 18 +++++++ .../SynchronousSpringEventsConfig.java | 10 ++++ .../main/resources/springProfiles-config.xml | 15 ++++++ .../main/resources/springScheduled-config.xml | 26 ++++++++++ .../main/resources/springScheduled.properties | 3 ++ .../DevProfileWithAnnotationTest.java | 23 ++++++++ .../ProductionProfileWithAnnotationTest.java | 32 ++++++++++++ .../profiles/SpringProfilesWithXMLTest.java | 33 ++++++++++++ .../ScheduledAnnotationExampleTest.java | 17 ++++++ .../SchedulingWithXmlConfigTest.java | 16 ++++++ .../AsynchronousCustomSpringEventsTest.java | 23 ++++++++ .../ContextRefreshedListenerTest.java | 18 +++++++ .../SynchronousCustomSpringEventsTest.java | 22 ++++++++ 25 files changed, 458 insertions(+), 2 deletions(-) create mode 100644 spring-all/src/main/java/org/baeldung/profiles/DatasourceConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/profiles/DevDatasourceConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/profiles/ProductionDatasourceConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java create mode 100644 spring-all/src/main/java/org/baeldung/scheduling/SchedulingWithXmlConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/ContextRefreshedListener.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEvent.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventListener.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/SynchronousSpringEventsConfig.java create mode 100644 spring-all/src/main/resources/springProfiles-config.xml create mode 100644 spring-all/src/main/resources/springScheduled-config.xml create mode 100644 spring-all/src/main/resources/springScheduled.properties create mode 100644 spring-all/src/test/java/org/baeldung/profiles/DevProfileWithAnnotationTest.java create mode 100644 spring-all/src/test/java/org/baeldung/profiles/ProductionProfileWithAnnotationTest.java create mode 100644 spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithXMLTest.java create mode 100644 spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java create mode 100644 spring-all/src/test/java/org/baeldung/scheduling/SchedulingWithXmlConfigTest.java create mode 100644 spring-all/src/test/java/org/baeldung/springevents/asynchronous/AsynchronousCustomSpringEventsTest.java create mode 100644 spring-all/src/test/java/org/baeldung/springevents/synchronous/ContextRefreshedListenerTest.java create mode 100644 spring-all/src/test/java/org/baeldung/springevents/synchronous/SynchronousCustomSpringEventsTest.java diff --git a/spring-all/pom.xml b/spring-all/pom.xml index e7350e4612..feb25dc987 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -33,7 +33,7 @@ - + org.hibernate hibernate-core @@ -118,7 +118,6 @@ ${mockito.version} test - diff --git a/spring-all/src/main/java/org/baeldung/profiles/DatasourceConfig.java b/spring-all/src/main/java/org/baeldung/profiles/DatasourceConfig.java new file mode 100644 index 0000000000..80cb060c7e --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/profiles/DatasourceConfig.java @@ -0,0 +1,5 @@ +package org.baeldung.profiles; + +public interface DatasourceConfig { + public void setup(); +} diff --git a/spring-all/src/main/java/org/baeldung/profiles/DevDatasourceConfig.java b/spring-all/src/main/java/org/baeldung/profiles/DevDatasourceConfig.java new file mode 100644 index 0000000000..e357280c43 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/profiles/DevDatasourceConfig.java @@ -0,0 +1,15 @@ +package org.baeldung.profiles; + +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Component +@Profile("dev") +public class DevDatasourceConfig implements DatasourceConfig { + + @Override + public void setup() { + System.out.println("Setting up datasource for DEV environment. "); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/profiles/ProductionDatasourceConfig.java b/spring-all/src/main/java/org/baeldung/profiles/ProductionDatasourceConfig.java new file mode 100644 index 0000000000..f1adff9c96 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/profiles/ProductionDatasourceConfig.java @@ -0,0 +1,15 @@ +package org.baeldung.profiles; + +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Component +@Profile("production") +public class ProductionDatasourceConfig implements DatasourceConfig { + + @Override + public void setup() { + System.out.println("Setting up datasource for PRODUCTION environment. "); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java b/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java new file mode 100644 index 0000000000..eb5543e3db --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java @@ -0,0 +1,10 @@ +package org.baeldung.profiles; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("org.baeldung.profiles") +public class SpringProfilesConfig { + +} diff --git a/spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java b/spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java new file mode 100644 index 0000000000..284bcf5e6a --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java @@ -0,0 +1,52 @@ +package org.baeldung.scheduling; + +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component("scheduledAnnotationExample") +public class ScheduledAnnotationExample { + + @Scheduled(fixedDelay = 1000) + public void scheduleFixedDelayTask() { + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}") + public void scheduleFixedDelayTaskUsingExpression() { + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedDelay = 1000, initialDelay = 2000) + public void scheduleFixedDelayWithInitialDelayTask() { + System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedRate = 1000) + public void scheduleFixedRateTask() { + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedRateString = "${fixedRate.in.milliseconds}") + public void scheduleFixedRateTaskUsingExpression() { + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedDelay = 1000, initialDelay = 100) + public void scheduleFixedRateWithInitialDelayTask() { + System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000); + } + + /** + * Scheduled task is executed at 10:15 AM on the 15th day of every month + */ + @Scheduled(cron = "0 15 10 15 * ?") + public void scheduleTaskUsingCronExpression() { + System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(cron = "${cron.expression}") + public void scheduleTaskUsingExternalizedCronExpression() { + System.out.println("schedule tasks using externalized cron expressions - " + System.currentTimeMillis() / 1000); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/scheduling/SchedulingWithXmlConfig.java b/spring-all/src/main/java/org/baeldung/scheduling/SchedulingWithXmlConfig.java new file mode 100644 index 0000000000..d7da9ac19c --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scheduling/SchedulingWithXmlConfig.java @@ -0,0 +1,16 @@ +package org.baeldung.scheduling; + +public class SchedulingWithXmlConfig { + + public void scheduleFixedDelayTask() { + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); + } + + public void scheduleFixedRateTask() { + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); + } + + public void scheduleTaskUsingCronExpression() { + System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000); + } +} diff --git a/spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java b/spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java new file mode 100644 index 0000000000..1d81fa0aa8 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java @@ -0,0 +1,20 @@ +package org.baeldung.scheduling; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@EnableScheduling +@ComponentScan("org.baeldung.scheduling") +@PropertySource("classpath:springScheduled.properties") +public class SpringSchedulingConfig { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } +} \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java b/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java new file mode 100644 index 0000000000..93f61bf517 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java @@ -0,0 +1,20 @@ +package org.baeldung.springevents.asynchronous; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.event.ApplicationEventMulticaster; +import org.springframework.context.event.SimpleApplicationEventMulticaster; +import org.springframework.core.task.SimpleAsyncTaskExecutor; + +@Configuration +@ComponentScan("org.baeldung.springevents.synchronous") +public class AsynchronousSpringEventsConfig { + + @Bean(name = "applicationEventMulticaster") + public static ApplicationEventMulticaster simpleApplicationEventMulticaster() { + final SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster(); + simpleApplicationEventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor()); + return simpleApplicationEventMulticaster; + } +} diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/ContextRefreshedListener.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/ContextRefreshedListener.java new file mode 100644 index 0000000000..052437e555 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/ContextRefreshedListener.java @@ -0,0 +1,15 @@ +package org.baeldung.springevents.synchronous; + +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.stereotype.Component; + +@Component +public class ContextRefreshedListener implements ApplicationListener { + + @Override + public void onApplicationEvent(final ContextRefreshedEvent cse) { + System.out.println("Handling context re-freshed event. "); + } + +} \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEvent.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEvent.java new file mode 100644 index 0000000000..1631801ffe --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEvent.java @@ -0,0 +1,19 @@ +package org.baeldung.springevents.synchronous; + +import org.springframework.context.ApplicationEvent; + +public class CustomSpringEvent extends ApplicationEvent { + private static final long serialVersionUID = -8053143381029977953L; + + private String message; + + public CustomSpringEvent(final Object source, final String message) { + super(source); + this.message = message; + } + + public String getMessage() { + return message; + } + +} diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventListener.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventListener.java new file mode 100644 index 0000000000..69f96966ec --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventListener.java @@ -0,0 +1,14 @@ +package org.baeldung.springevents.synchronous; + +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Component; + +@Component +public class CustomSpringEventListener implements ApplicationListener { + + @Override + public void onApplicationEvent(final CustomSpringEvent event) { + System.out.println("Received spring custom event - " + event.getMessage()); + } + +} \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java new file mode 100644 index 0000000000..decbcf446a --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java @@ -0,0 +1,18 @@ +package org.baeldung.springevents.synchronous; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Component; + +@Component +public class CustomSpringEventPublisher { + + @Autowired + private ApplicationEventPublisher applicationEventPublisher; + + public void publishEvent(final String message) { + System.out.println("Publishing custom event. "); + final CustomSpringEvent customSpringEvent = new CustomSpringEvent(this, message); + applicationEventPublisher.publishEvent(customSpringEvent); + } +} diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/SynchronousSpringEventsConfig.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/SynchronousSpringEventsConfig.java new file mode 100644 index 0000000000..fff8a7e4b5 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/SynchronousSpringEventsConfig.java @@ -0,0 +1,10 @@ +package org.baeldung.springevents.synchronous; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("org.baeldung.springevents.synchronous") +public class SynchronousSpringEventsConfig { + +} diff --git a/spring-all/src/main/resources/springProfiles-config.xml b/spring-all/src/main/resources/springProfiles-config.xml new file mode 100644 index 0000000000..a290f83b26 --- /dev/null +++ b/spring-all/src/main/resources/springProfiles-config.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/resources/springScheduled-config.xml b/spring-all/src/main/resources/springScheduled-config.xml new file mode 100644 index 0000000000..68d42fdd57 --- /dev/null +++ b/spring-all/src/main/resources/springScheduled-config.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/resources/springScheduled.properties b/spring-all/src/main/resources/springScheduled.properties new file mode 100644 index 0000000000..336fc83937 --- /dev/null +++ b/spring-all/src/main/resources/springScheduled.properties @@ -0,0 +1,3 @@ +cron.expression=0 15 10 15 * ? +fixedRate.in.milliseconds=1000 +fixedDelay.in.milliseconds=1000 \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/profiles/DevProfileWithAnnotationTest.java b/spring-all/src/test/java/org/baeldung/profiles/DevProfileWithAnnotationTest.java new file mode 100644 index 0000000000..2b65928da8 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/profiles/DevProfileWithAnnotationTest.java @@ -0,0 +1,23 @@ +package org.baeldung.profiles; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ActiveProfiles("dev") +@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class) +public class DevProfileWithAnnotationTest { + @Autowired + DatasourceConfig datasourceConfig; + + @Test + public void testSpringProfiles() { + Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig); + } +} \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/profiles/ProductionProfileWithAnnotationTest.java b/spring-all/src/test/java/org/baeldung/profiles/ProductionProfileWithAnnotationTest.java new file mode 100644 index 0000000000..551636bd31 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/profiles/ProductionProfileWithAnnotationTest.java @@ -0,0 +1,32 @@ +package org.baeldung.profiles; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ActiveProfiles("production") +@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ProductionProfileWithAnnotationTest { + + @Autowired + DatasourceConfig datasourceConfig; + + @Autowired + Environment environment; + + @Test + public void testSpringProfiles() { + for (final String profileName : environment.getActiveProfiles()) { + System.out.println("Currently active profile - " + profileName); + } + Assert.assertEquals("production", environment.getActiveProfiles()[0]); + Assert.assertTrue(datasourceConfig instanceof ProductionDatasourceConfig); + } +} \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithXMLTest.java b/spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithXMLTest.java new file mode 100644 index 0000000000..15c9265a13 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithXMLTest.java @@ -0,0 +1,33 @@ +package org.baeldung.profiles; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; + +public class SpringProfilesWithXMLTest { + + private ClassPathXmlApplicationContext classPathXmlApplicationContext; + + @Test + public void testSpringProfilesForDevEnvironment() { + classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:springProfiles-config.xml"); + final ConfigurableEnvironment configurableEnvironment = classPathXmlApplicationContext.getEnvironment(); + configurableEnvironment.setActiveProfiles("dev"); + classPathXmlApplicationContext.refresh(); + final DatasourceConfig datasourceConfig = classPathXmlApplicationContext.getBean("devDatasourceConfig", DatasourceConfig.class); + + Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig); + } + + @Test + public void testSpringProfilesForProdEnvironment() { + classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:springProfiles-config.xml"); + final ConfigurableEnvironment configurableEnvironment = classPathXmlApplicationContext.getEnvironment(); + configurableEnvironment.setActiveProfiles("production"); + classPathXmlApplicationContext.refresh(); + final DatasourceConfig datasourceConfig = classPathXmlApplicationContext.getBean("productionDatasourceConfig", DatasourceConfig.class); + + Assert.assertTrue(datasourceConfig instanceof ProductionDatasourceConfig); + } +} diff --git a/spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java b/spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java new file mode 100644 index 0000000000..9317c7bb7f --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java @@ -0,0 +1,17 @@ +package org.baeldung.scheduling; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringSchedulingConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ScheduledAnnotationExampleTest { + + @Test + public void testScheduledAnnotation() throws InterruptedException { + Thread.sleep(5000); + } +} diff --git a/spring-all/src/test/java/org/baeldung/scheduling/SchedulingWithXmlConfigTest.java b/spring-all/src/test/java/org/baeldung/scheduling/SchedulingWithXmlConfigTest.java new file mode 100644 index 0000000000..0fca4d21c8 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/scheduling/SchedulingWithXmlConfigTest.java @@ -0,0 +1,16 @@ +package org.baeldung.scheduling; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:springScheduled-config.xml") +public class SchedulingWithXmlConfigTest { + + @Test + public void testXmlBasedScheduling() throws InterruptedException { + Thread.sleep(5000); + } +} diff --git a/spring-all/src/test/java/org/baeldung/springevents/asynchronous/AsynchronousCustomSpringEventsTest.java b/spring-all/src/test/java/org/baeldung/springevents/asynchronous/AsynchronousCustomSpringEventsTest.java new file mode 100644 index 0000000000..2b45ae4e68 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/springevents/asynchronous/AsynchronousCustomSpringEventsTest.java @@ -0,0 +1,23 @@ +package org.baeldung.springevents.asynchronous; + +import org.baeldung.springevents.synchronous.CustomSpringEventPublisher; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { AsynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class) +public class AsynchronousCustomSpringEventsTest { + + @Autowired + private CustomSpringEventPublisher publisher; + + @Test + public void testCustomSpringEvents() throws InterruptedException { + publisher.publishEvent("Hello world!!"); + System.out.println("Done publishing asynchronous custom event. "); + } +} diff --git a/spring-all/src/test/java/org/baeldung/springevents/synchronous/ContextRefreshedListenerTest.java b/spring-all/src/test/java/org/baeldung/springevents/synchronous/ContextRefreshedListenerTest.java new file mode 100644 index 0000000000..d971698e3f --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/springevents/synchronous/ContextRefreshedListenerTest.java @@ -0,0 +1,18 @@ +package org.baeldung.springevents.synchronous; + +import org.baeldung.springevents.synchronous.SynchronousSpringEventsConfig; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ContextRefreshedListenerTest { + + @Test + public void testContextRefreshedListener() throws InterruptedException { + System.out.println("Test context re-freshed listener."); + } +} \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/springevents/synchronous/SynchronousCustomSpringEventsTest.java b/spring-all/src/test/java/org/baeldung/springevents/synchronous/SynchronousCustomSpringEventsTest.java new file mode 100644 index 0000000000..b559ca9fc9 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/springevents/synchronous/SynchronousCustomSpringEventsTest.java @@ -0,0 +1,22 @@ +package org.baeldung.springevents.synchronous; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class) +public class SynchronousCustomSpringEventsTest { + + @Autowired + private CustomSpringEventPublisher publisher; + + @Test + public void testCustomSpringEvents() throws InterruptedException { + publisher.publishEvent("Hello world!!"); + System.out.println("Done publishing synchronous custom event. "); + } +}