cleanup work and test naming
This commit is contained in:
parent
2b374fa8ae
commit
e0b85586e2
|
@ -1,7 +1,7 @@
|
||||||
package org.baeldung.bean.injection;
|
package org.baeldung.bean.injection;
|
||||||
|
|
||||||
public class Helm {
|
public class Helm {
|
||||||
|
|
||||||
private String brandOfHelm = "HelmBrand";
|
private String brandOfHelm = "HelmBrand";
|
||||||
|
|
||||||
public String getBrandOfHelm() {
|
public String getBrandOfHelm() {
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
package org.baeldung.customscope;
|
package org.baeldung.customscope;
|
||||||
|
|
||||||
public class TenantBean {
|
public class TenantBean {
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
public TenantBean(String name) {
|
public TenantBean(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sayHello() {
|
public void sayHello() {
|
||||||
System.out.println(String.format("Hello from %s of type %s",
|
System.out.println(String.format("Hello from %s of type %s", this.name, this.getClass().getName()));
|
||||||
this.name,
|
|
||||||
this.getClass().getName()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ public class TenantBeansConfig {
|
||||||
public TenantBean foo() {
|
public TenantBean foo() {
|
||||||
return new TenantBean("foo");
|
return new TenantBean("foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scope(scopeName = "tenant")
|
@Scope(scopeName = "tenant")
|
||||||
@Bean
|
@Bean
|
||||||
public TenantBean bar() {
|
public TenantBean bar() {
|
||||||
|
|
|
@ -8,13 +8,13 @@ import org.springframework.beans.factory.ObjectFactory;
|
||||||
import org.springframework.beans.factory.config.Scope;
|
import org.springframework.beans.factory.config.Scope;
|
||||||
|
|
||||||
public class TenantScope implements Scope {
|
public class TenantScope implements Scope {
|
||||||
|
|
||||||
private Map<String, Object> scopedObjects = Collections.synchronizedMap(new HashMap<String, Object>());
|
private Map<String, Object> scopedObjects = Collections.synchronizedMap(new HashMap<String, Object>());
|
||||||
private Map<String, Runnable> destructionCallbacks = Collections.synchronizedMap(new HashMap<String, Runnable>());
|
private Map<String, Runnable> destructionCallbacks = Collections.synchronizedMap(new HashMap<String, Runnable>());
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object get(String name, ObjectFactory<?> objectFactory) {
|
public Object get(String name, ObjectFactory<?> objectFactory) {
|
||||||
if(!scopedObjects.containsKey(name)) {
|
if (!scopedObjects.containsKey(name)) {
|
||||||
scopedObjects.put(name, objectFactory.getObject());
|
scopedObjects.put(name, objectFactory.getObject());
|
||||||
}
|
}
|
||||||
return scopedObjects.get(name);
|
return scopedObjects.get(name);
|
||||||
|
|
|
@ -19,13 +19,13 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class SimpleReportExporter {
|
public class SimpleReportExporter {
|
||||||
|
|
||||||
private JasperPrint jasperPrint;
|
private JasperPrint jasperPrint;
|
||||||
|
|
||||||
public SimpleReportExporter() {
|
public SimpleReportExporter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleReportExporter (JasperPrint jasperPrint){
|
public SimpleReportExporter(JasperPrint jasperPrint) {
|
||||||
this.jasperPrint = jasperPrint;
|
this.jasperPrint = jasperPrint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,9 +36,7 @@ public class SimpleReportExporter {
|
||||||
public void setJasperPrint(JasperPrint jasperPrint) {
|
public void setJasperPrint(JasperPrint jasperPrint) {
|
||||||
this.jasperPrint = jasperPrint;
|
this.jasperPrint = jasperPrint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void exportToPdf(String fileName, String author) {
|
public void exportToPdf(String fileName, String author) {
|
||||||
|
|
||||||
// print report to file
|
// print report to file
|
||||||
|
@ -61,27 +59,25 @@ public class SimpleReportExporter {
|
||||||
try {
|
try {
|
||||||
exporter.exportReport();
|
exporter.exportReport();
|
||||||
} catch (JRException ex) {
|
} catch (JRException ex) {
|
||||||
Logger.getLogger(SimpleReportFiller.class.getName())
|
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
.log(Level.SEVERE, null, ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void exportToXlsx(String fileName,String sheetName) {
|
public void exportToXlsx(String fileName, String sheetName) {
|
||||||
JRXlsxExporter exporter = new JRXlsxExporter();
|
JRXlsxExporter exporter = new JRXlsxExporter();
|
||||||
|
|
||||||
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||||||
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(fileName));
|
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(fileName));
|
||||||
|
|
||||||
SimpleXlsxReportConfiguration reportConfig = new SimpleXlsxReportConfiguration();
|
SimpleXlsxReportConfiguration reportConfig = new SimpleXlsxReportConfiguration();
|
||||||
reportConfig.setSheetNames(new String[]{sheetName});
|
reportConfig.setSheetNames(new String[] { sheetName });
|
||||||
|
|
||||||
exporter.setConfiguration(reportConfig);
|
exporter.setConfiguration(reportConfig);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
exporter.exportReport();
|
exporter.exportReport();
|
||||||
} catch (JRException ex) {
|
} catch (JRException ex) {
|
||||||
Logger.getLogger(SimpleReportFiller.class.getName())
|
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
.log(Level.SEVERE, null, ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,8 +90,7 @@ public class SimpleReportExporter {
|
||||||
try {
|
try {
|
||||||
exporter.exportReport();
|
exporter.exportReport();
|
||||||
} catch (JRException ex) {
|
} catch (JRException ex) {
|
||||||
Logger.getLogger(SimpleReportFiller.class.getName())
|
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
.log(Level.SEVERE, null, ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,8 +103,7 @@ public class SimpleReportExporter {
|
||||||
try {
|
try {
|
||||||
exporter.exportReport();
|
exporter.exportReport();
|
||||||
} catch (JRException ex) {
|
} catch (JRException ex) {
|
||||||
Logger.getLogger(SimpleReportFiller.class.getName())
|
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
.log(Level.SEVERE, null, ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,8 +45,7 @@ public class SimpleReportFiller {
|
||||||
jasperReport = JasperCompileManager.compileReport(reportStream);
|
jasperReport = JasperCompileManager.compileReport(reportStream);
|
||||||
JRSaver.saveObject(jasperReport, reportFileName.replace(".jrxml", ".jasper"));
|
JRSaver.saveObject(jasperReport, reportFileName.replace(".jrxml", ".jasper"));
|
||||||
} catch (JRException ex) {
|
} catch (JRException ex) {
|
||||||
Logger.getLogger(SimpleReportFiller.class.getName())
|
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
.log(Level.SEVERE, null, ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,8 +53,7 @@ public class SimpleReportFiller {
|
||||||
try {
|
try {
|
||||||
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
|
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
|
||||||
} catch (JRException | SQLException ex) {
|
} catch (JRException | SQLException ex) {
|
||||||
Logger.getLogger(SimpleReportFiller.class.getName())
|
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
.log(Level.SEVERE, null, ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,7 @@ public class JasperRerportsSimpleConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
return new EmbeddedDatabaseBuilder()
|
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).addScript("classpath:employee-schema.sql").build();
|
||||||
.setType(EmbeddedDatabaseType.HSQL)
|
|
||||||
.addScript("classpath:employee-schema.sql")
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -33,36 +33,25 @@ public class SimpleCLI implements CommandMarker {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@CliCommand(
|
@CliCommand(value = { "web-get", "wg" }, help = "Displays the contents of a URL.")
|
||||||
value = {"web-get", "wg"},
|
public String webGet(@CliOption(key = { "", "url" }, help = "URL whose contents will be displayed.") URL url) {
|
||||||
help = "Displays the contents of a URL."
|
|
||||||
)
|
|
||||||
public String webGet(
|
|
||||||
@CliOption(
|
|
||||||
key = {"", "url"},
|
|
||||||
help = "URL whose contents will be displayed."
|
|
||||||
) URL url) {
|
|
||||||
return getContentsOfUrlAsString(url);
|
return getContentsOfUrlAsString(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@CliCommand(
|
@CliCommand(value = { "web-save", "ws" }, help = "Saves the contents of a URL.")
|
||||||
value = {"web-save", "ws"},
|
public String webSave(@CliOption(key = { "", "url" }, help = "URL whose contents will be saved.") URL url, @CliOption(key = { "out", "file" }, mandatory = true, help = "The name of the file.") String file) {
|
||||||
help = "Saves the contents of a URL.")
|
|
||||||
public String webSave(
|
|
||||||
@CliOption(key = {"", "url"}, help = "URL whose contents will be saved.") URL url,
|
|
||||||
@CliOption(key = {"out", "file"}, mandatory = true, help = "The name of the file.") String file) {
|
|
||||||
String contents = getContentsOfUrlAsString(url);
|
String contents = getContentsOfUrlAsString(url);
|
||||||
try (PrintWriter out = new PrintWriter(file)) {
|
try (PrintWriter out = new PrintWriter(file)) {
|
||||||
out.write(contents);
|
out.write(contents);
|
||||||
} catch (FileNotFoundException ex) {
|
} catch (FileNotFoundException ex) {
|
||||||
//Ignore
|
// Ignore
|
||||||
}
|
}
|
||||||
return "Done.";
|
return "Done.";
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean adminEnableExecuted = false;
|
private boolean adminEnableExecuted = false;
|
||||||
|
|
||||||
@CliAvailabilityIndicator(value = {"web-save"})
|
@CliAvailabilityIndicator(value = { "web-save" })
|
||||||
public boolean isAdminEnabled() {
|
public boolean isAdminEnabled() {
|
||||||
return adminEnableExecuted;
|
return adminEnableExecuted;
|
||||||
}
|
}
|
||||||
|
@ -72,7 +61,7 @@ public class SimpleCLI implements CommandMarker {
|
||||||
adminEnableExecuted = true;
|
adminEnableExecuted = true;
|
||||||
return "Admin commands enabled.";
|
return "Admin commands enabled.";
|
||||||
}
|
}
|
||||||
|
|
||||||
@CliCommand(value = "admin-disable")
|
@CliCommand(value = "admin-disable")
|
||||||
public String adminDisable() {
|
public String adminDisable() {
|
||||||
adminEnableExecuted = false;
|
adminEnableExecuted = false;
|
||||||
|
|
|
@ -16,14 +16,13 @@ public class SimpleURLConverter implements Converter<URL> {
|
||||||
try {
|
try {
|
||||||
return new URL(value);
|
return new URL(value);
|
||||||
} catch (MalformedURLException ex) {
|
} catch (MalformedURLException ex) {
|
||||||
//Ignore
|
// Ignore
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean getAllPossibleValues(List<Completion> completions, Class<?> requiredType,
|
public boolean getAllPossibleValues(List<Completion> completions, Class<?> requiredType, String existingData, String optionContext, MethodTarget target) {
|
||||||
String existingData, String optionContext, MethodTarget target) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,9 @@ import org.springframework.retry.support.RetryTemplate;
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(basePackages = "org.baeldung.springretry")
|
@ComponentScan(basePackages = "org.baeldung.springretry")
|
||||||
@EnableRetry
|
@EnableRetry
|
||||||
//Uncomment this two lines if we need XML configuration
|
// Uncomment this two lines if we need XML configuration
|
||||||
//@EnableAspectJAutoProxy
|
// @EnableAspectJAutoProxy
|
||||||
//@ImportResource("classpath:/retryadvice.xml")
|
// @ImportResource("classpath:/retryadvice.xml")
|
||||||
public class AppConfig {
|
public class AppConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.retry.annotation.Recover;
|
||||||
import org.springframework.retry.annotation.Retryable;
|
import org.springframework.retry.annotation.Retryable;
|
||||||
|
|
||||||
public interface MyService {
|
public interface MyService {
|
||||||
|
|
||||||
@Retryable
|
@Retryable
|
||||||
void retryService();
|
void retryService();
|
||||||
|
|
||||||
|
|
|
@ -14,34 +14,35 @@ import org.springframework.stereotype.Component;
|
||||||
public class ThreadPoolTaskSchedulerExamples {
|
public class ThreadPoolTaskSchedulerExamples {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ThreadPoolTaskScheduler taskScheduler;
|
private ThreadPoolTaskScheduler taskScheduler;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CronTrigger cronTrigger;
|
private CronTrigger cronTrigger;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PeriodicTrigger periodicTrigger;
|
private PeriodicTrigger periodicTrigger;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void scheduleRunnableWithCronTrigger(){
|
public void scheduleRunnableWithCronTrigger() {
|
||||||
taskScheduler.schedule(new RunnableTask("Current Date"), new Date());
|
taskScheduler.schedule(new RunnableTask("Current Date"), new Date());
|
||||||
taskScheduler.scheduleWithFixedDelay(new RunnableTask("Fixed 1 second Delay"), 1000);
|
taskScheduler.scheduleWithFixedDelay(new RunnableTask("Fixed 1 second Delay"), 1000);
|
||||||
taskScheduler.scheduleWithFixedDelay(new RunnableTask("Current Date Fixed 1 second Delay"),new Date() , 1000);
|
taskScheduler.scheduleWithFixedDelay(new RunnableTask("Current Date Fixed 1 second Delay"), new Date(), 1000);
|
||||||
taskScheduler.scheduleAtFixedRate(new RunnableTask("Fixed Rate of 2 seconds"),new Date(), 2000);
|
taskScheduler.scheduleAtFixedRate(new RunnableTask("Fixed Rate of 2 seconds"), new Date(), 2000);
|
||||||
taskScheduler.scheduleAtFixedRate(new RunnableTask("Fixed Rate of 2 seconds"), 2000);
|
taskScheduler.scheduleAtFixedRate(new RunnableTask("Fixed Rate of 2 seconds"), 2000);
|
||||||
taskScheduler.schedule(new RunnableTask("Cron Trigger"), cronTrigger);
|
taskScheduler.schedule(new RunnableTask("Cron Trigger"), cronTrigger);
|
||||||
taskScheduler.schedule(new RunnableTask("Periodic Trigger"), periodicTrigger);
|
taskScheduler.schedule(new RunnableTask("Periodic Trigger"), periodicTrigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
class RunnableTask implements Runnable{
|
class RunnableTask implements Runnable {
|
||||||
|
|
||||||
private String message;
|
private String message;
|
||||||
|
|
||||||
public RunnableTask(String message){
|
public RunnableTask(String message) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
System.out.println("Runnable Task with "+message+" on thread "+Thread.currentThread().getName());
|
System.out.println("Runnable Task with " + message + " on thread " + Thread.currentThread().getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,19 +5,17 @@ import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
public class ConstructorBasedBeanInjectionWithJavaConfigTest {
|
public class ConstructorBasedBeanInjectionWithJavaConfigIntegrationTest {
|
||||||
private static final String HELM_NAME = "HelmBrand";
|
private static final String HELM_NAME = "HelmBrand";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJavaConfigFile_whenUsingConstructorBasedBeanInjection_thenCorrectHelmName() {
|
public void givenJavaConfigFile_whenUsingConstructorBasedBeanInjection_thenCorrectHelmName() {
|
||||||
|
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
ctx.register(ConstructorBasedShipConfig.class);
|
ctx.register(ConstructorBasedShipConfig.class);
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
Ship ship = ctx.getBean(Ship.class);
|
Ship ship = ctx.getBean(Ship.class);
|
||||||
|
|
||||||
Assert.assertEquals(HELM_NAME, ship.getHelm()
|
Assert.assertEquals(HELM_NAME, ship.getHelm().getBrandOfHelm());
|
||||||
.getBrandOfHelm());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@ import org.junit.Test;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
public class ConstructorBasedBeanInjectionWithXMLConfigTest {
|
public class ConstructorBasedBeanInjectionWithXMLConfigIntegrationTest {
|
||||||
|
|
||||||
private static final String HELM_NAME = "HelmBrand";
|
private static final String HELM_NAME = "HelmBrand";
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ public class ConstructorBasedBeanInjectionWithXMLConfigTest {
|
||||||
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-constructor.xml");
|
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-constructor.xml");
|
||||||
|
|
||||||
final Ship shipConstructorBean = (Ship) applicationContext.getBean("ship");
|
final Ship shipConstructorBean = (Ship) applicationContext.getBean("ship");
|
||||||
Assert.assertEquals(HELM_NAME, shipConstructorBean.getHelm()
|
Assert.assertEquals(HELM_NAME, shipConstructorBean.getHelm().getBrandOfHelm());
|
||||||
.getBrandOfHelm());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@ import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
public class SetterBasedBeanInjectionWithJavaConfigTest {
|
public class SetterBasedBeanInjectionWithJavaConfigIntegrationTest {
|
||||||
|
|
||||||
private static final String HELM_NAME = "HelmBrand";
|
private static final String HELM_NAME = "HelmBrand";
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@ public class SetterBasedBeanInjectionWithJavaConfigTest {
|
||||||
|
|
||||||
Ship ship = ctx.getBean(Ship.class);
|
Ship ship = ctx.getBean(Ship.class);
|
||||||
|
|
||||||
Assert.assertEquals(HELM_NAME, ship.getHelm()
|
Assert.assertEquals(HELM_NAME, ship.getHelm().getBrandOfHelm());
|
||||||
.getBrandOfHelm());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@ import org.junit.Test;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
public class SetterBasedBeanInjectionWithXMLConfigTest {
|
public class SetterBasedBeanInjectionWithXMLConfigIntegrationTest {
|
||||||
|
|
||||||
private static final String HELM_NAME = "HelmBrand";
|
private static final String HELM_NAME = "HelmBrand";
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ public class SetterBasedBeanInjectionWithXMLConfigTest {
|
||||||
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-setter.xml");
|
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-setter.xml");
|
||||||
|
|
||||||
final Ship shipSetterBean = (Ship) applicationContext.getBean("ship");
|
final Ship shipSetterBean = (Ship) applicationContext.getBean("ship");
|
||||||
Assert.assertEquals(HELM_NAME, shipSetterBean.getHelm()
|
Assert.assertEquals(HELM_NAME, shipSetterBean.getHelm().getBrandOfHelm());
|
||||||
.getBrandOfHelm());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -16,17 +16,17 @@ public class TenantScopeTest {
|
||||||
@Test
|
@Test
|
||||||
public final void whenRegisterScopeAndBeans_thenContextContainsFooAndBar() {
|
public final void whenRegisterScopeAndBeans_thenContextContainsFooAndBar() {
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
try{
|
try {
|
||||||
ctx.register(TenantScopeConfig.class);
|
ctx.register(TenantScopeConfig.class);
|
||||||
ctx.register(TenantBeansConfig.class);
|
ctx.register(TenantBeansConfig.class);
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
|
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
|
||||||
foo.sayHello();
|
foo.sayHello();
|
||||||
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
|
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
|
||||||
bar.sayHello();
|
bar.sayHello();
|
||||||
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
|
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
|
||||||
|
|
||||||
assertThat(foo, not(equalTo(bar)));
|
assertThat(foo, not(equalTo(bar)));
|
||||||
assertThat(foos.size(), equalTo(2));
|
assertThat(foos.size(), equalTo(2));
|
||||||
assertTrue(foos.containsValue(foo));
|
assertTrue(foos.containsValue(foo));
|
||||||
|
@ -34,40 +34,38 @@ public class TenantScopeTest {
|
||||||
|
|
||||||
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
|
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
|
||||||
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
|
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
|
||||||
|
|
||||||
assertThat(fooDefinition.getScope(), equalTo("tenant"));
|
assertThat(fooDefinition.getScope(), equalTo("tenant"));
|
||||||
assertThat(barDefinition.getScope(), equalTo("tenant"));
|
assertThat(barDefinition.getScope(), equalTo("tenant"));
|
||||||
}
|
} finally {
|
||||||
finally {
|
|
||||||
ctx.close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenComponentScan_thenContextContainsFooAndBar() {
|
public final void whenComponentScan_thenContextContainsFooAndBar() {
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
try{
|
try {
|
||||||
ctx.scan("org.baeldung.customscope");
|
ctx.scan("org.baeldung.customscope");
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
|
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
|
||||||
foo.sayHello();
|
foo.sayHello();
|
||||||
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
|
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
|
||||||
bar.sayHello();
|
bar.sayHello();
|
||||||
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
|
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
|
||||||
|
|
||||||
assertThat(foo, not(equalTo(bar)));
|
assertThat(foo, not(equalTo(bar)));
|
||||||
assertThat(foos.size(), equalTo(2));
|
assertThat(foos.size(), equalTo(2));
|
||||||
assertTrue(foos.containsValue(foo));
|
assertTrue(foos.containsValue(foo));
|
||||||
assertTrue(foos.containsValue(bar));
|
assertTrue(foos.containsValue(bar));
|
||||||
|
|
||||||
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
|
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
|
||||||
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
|
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
|
||||||
|
|
||||||
assertThat(fooDefinition.getScope(), equalTo("tenant"));
|
assertThat(fooDefinition.getScope(), equalTo("tenant"));
|
||||||
assertThat(barDefinition.getScope(), equalTo("tenant"));
|
assertThat(barDefinition.getScope(), equalTo("tenant"));
|
||||||
}
|
} finally {
|
||||||
finally {
|
|
||||||
ctx.close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,8 +21,7 @@ public class SquareCalculatorTest {
|
||||||
public void whenCalculatingSquareValueOnce_thenCacheDontHaveValues() {
|
public void whenCalculatingSquareValueOnce_thenCacheDontHaveValues() {
|
||||||
for (int i = 10; i < 15; i++) {
|
for (int i = 10; i < 15; i++) {
|
||||||
assertFalse(cacheHelper.getSquareNumberCache().containsKey(i));
|
assertFalse(cacheHelper.getSquareNumberCache().containsKey(i));
|
||||||
System.out.println("Square value of " + i + " is: "
|
System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n");
|
||||||
+ squaredCalculator.getSquareValueOfNumber(i) + "\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,14 +29,12 @@ public class SquareCalculatorTest {
|
||||||
public void whenCalculatingSquareValueAgain_thenCacheHasAllValues() {
|
public void whenCalculatingSquareValueAgain_thenCacheHasAllValues() {
|
||||||
for (int i = 10; i < 15; i++) {
|
for (int i = 10; i < 15; i++) {
|
||||||
assertFalse(cacheHelper.getSquareNumberCache().containsKey(i));
|
assertFalse(cacheHelper.getSquareNumberCache().containsKey(i));
|
||||||
System.out.println("Square value of " + i + " is: "
|
System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n");
|
||||||
+ squaredCalculator.getSquareValueOfNumber(i) + "\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 10; i < 15; i++) {
|
for (int i = 10; i < 15; i++) {
|
||||||
assertTrue(cacheHelper.getSquareNumberCache().containsKey(i));
|
assertTrue(cacheHelper.getSquareNumberCache().containsKey(i));
|
||||||
System.out.println("Square value of " + i + " is: "
|
System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n");
|
||||||
+ squaredCalculator.getSquareValueOfNumber(i) + "\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
public class ThreadPoolTaskSchedulerIntegrationTest {
|
public class ThreadPoolTaskSchedulerIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException {
|
public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException {
|
||||||
Thread.sleep(2550);
|
Thread.sleep(2550);
|
||||||
|
|
Loading…
Reference in New Issue