cleanup work and test naming

This commit is contained in:
eugenp 2017-05-14 19:23:11 +03:00
parent 2b374fa8ae
commit e0b85586e2
19 changed files with 69 additions and 103 deletions

View File

@ -1,7 +1,7 @@
package org.baeldung.bean.injection;
public class Helm {
private String brandOfHelm = "HelmBrand";
public String getBrandOfHelm() {

View File

@ -1,16 +1,14 @@
package org.baeldung.customscope;
public class TenantBean {
private final String name;
public TenantBean(String name) {
this.name = name;
}
public void sayHello() {
System.out.println(String.format("Hello from %s of type %s",
this.name,
this.getClass().getName()));
System.out.println(String.format("Hello from %s of type %s", this.name, this.getClass().getName()));
}
}

View File

@ -12,7 +12,7 @@ public class TenantBeansConfig {
public TenantBean foo() {
return new TenantBean("foo");
}
@Scope(scopeName = "tenant")
@Bean
public TenantBean bar() {

View File

@ -8,13 +8,13 @@ import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
public class TenantScope implements Scope {
private Map<String, Object> scopedObjects = Collections.synchronizedMap(new HashMap<String, Object>());
private Map<String, Runnable> destructionCallbacks = Collections.synchronizedMap(new HashMap<String, Runnable>());
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
if(!scopedObjects.containsKey(name)) {
if (!scopedObjects.containsKey(name)) {
scopedObjects.put(name, objectFactory.getObject());
}
return scopedObjects.get(name);

View File

@ -19,13 +19,13 @@ import org.springframework.stereotype.Component;
@Component
public class SimpleReportExporter {
private JasperPrint jasperPrint;
public SimpleReportExporter() {
}
public SimpleReportExporter (JasperPrint jasperPrint){
public SimpleReportExporter(JasperPrint jasperPrint) {
this.jasperPrint = jasperPrint;
}
@ -36,9 +36,7 @@ public class SimpleReportExporter {
public void setJasperPrint(JasperPrint jasperPrint) {
this.jasperPrint = jasperPrint;
}
public void exportToPdf(String fileName, String author) {
// print report to file
@ -61,27 +59,25 @@ public class SimpleReportExporter {
try {
exporter.exportReport();
} catch (JRException ex) {
Logger.getLogger(SimpleReportFiller.class.getName())
.log(Level.SEVERE, null, ex);
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void exportToXlsx(String fileName,String sheetName) {
public void exportToXlsx(String fileName, String sheetName) {
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(fileName));
SimpleXlsxReportConfiguration reportConfig = new SimpleXlsxReportConfiguration();
reportConfig.setSheetNames(new String[]{sheetName});
reportConfig.setSheetNames(new String[] { sheetName });
exporter.setConfiguration(reportConfig);
try {
exporter.exportReport();
} catch (JRException ex) {
Logger.getLogger(SimpleReportFiller.class.getName())
.log(Level.SEVERE, null, ex);
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
}
}
@ -94,8 +90,7 @@ public class SimpleReportExporter {
try {
exporter.exportReport();
} catch (JRException ex) {
Logger.getLogger(SimpleReportFiller.class.getName())
.log(Level.SEVERE, null, ex);
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
}
}
@ -108,8 +103,7 @@ public class SimpleReportExporter {
try {
exporter.exportReport();
} catch (JRException ex) {
Logger.getLogger(SimpleReportFiller.class.getName())
.log(Level.SEVERE, null, ex);
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

View File

@ -45,8 +45,7 @@ public class SimpleReportFiller {
jasperReport = JasperCompileManager.compileReport(reportStream);
JRSaver.saveObject(jasperReport, reportFileName.replace(".jrxml", ".jasper"));
} catch (JRException ex) {
Logger.getLogger(SimpleReportFiller.class.getName())
.log(Level.SEVERE, null, ex);
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
}
}
@ -54,8 +53,7 @@ public class SimpleReportFiller {
try {
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
} catch (JRException | SQLException ex) {
Logger.getLogger(SimpleReportFiller.class.getName())
.log(Level.SEVERE, null, ex);
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
}
}

View File

@ -13,10 +13,7 @@ public class JasperRerportsSimpleConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:employee-schema.sql")
.build();
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).addScript("classpath:employee-schema.sql").build();
}
@Bean

View File

@ -33,36 +33,25 @@ public class SimpleCLI implements CommandMarker {
return sb.toString();
}
@CliCommand(
value = {"web-get", "wg"},
help = "Displays the contents of a URL."
)
public String webGet(
@CliOption(
key = {"", "url"},
help = "URL whose contents will be displayed."
) URL url) {
@CliCommand(value = { "web-get", "wg" }, 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);
}
@CliCommand(
value = {"web-save", "ws"},
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) {
@CliCommand(value = { "web-save", "ws" }, 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);
try (PrintWriter out = new PrintWriter(file)) {
out.write(contents);
} catch (FileNotFoundException ex) {
//Ignore
// Ignore
}
return "Done.";
}
private boolean adminEnableExecuted = false;
@CliAvailabilityIndicator(value = {"web-save"})
@CliAvailabilityIndicator(value = { "web-save" })
public boolean isAdminEnabled() {
return adminEnableExecuted;
}
@ -72,7 +61,7 @@ public class SimpleCLI implements CommandMarker {
adminEnableExecuted = true;
return "Admin commands enabled.";
}
@CliCommand(value = "admin-disable")
public String adminDisable() {
adminEnableExecuted = false;

View File

@ -16,14 +16,13 @@ public class SimpleURLConverter implements Converter<URL> {
try {
return new URL(value);
} catch (MalformedURLException ex) {
//Ignore
// Ignore
}
return null;
}
@Override
public boolean getAllPossibleValues(List<Completion> completions, Class<?> requiredType,
String existingData, String optionContext, MethodTarget target) {
public boolean getAllPossibleValues(List<Completion> completions, Class<?> requiredType, String existingData, String optionContext, MethodTarget target) {
return false;
}

View File

@ -11,9 +11,9 @@ import org.springframework.retry.support.RetryTemplate;
@Configuration
@ComponentScan(basePackages = "org.baeldung.springretry")
@EnableRetry
//Uncomment this two lines if we need XML configuration
//@EnableAspectJAutoProxy
//@ImportResource("classpath:/retryadvice.xml")
// Uncomment this two lines if we need XML configuration
// @EnableAspectJAutoProxy
// @ImportResource("classpath:/retryadvice.xml")
public class AppConfig {
@Bean

View File

@ -7,7 +7,7 @@ import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
public interface MyService {
@Retryable
void retryService();

View File

@ -14,34 +14,35 @@ import org.springframework.stereotype.Component;
public class ThreadPoolTaskSchedulerExamples {
@Autowired
private ThreadPoolTaskScheduler taskScheduler;
@Autowired
private CronTrigger cronTrigger;
@Autowired
private PeriodicTrigger periodicTrigger;
@PostConstruct
public void scheduleRunnableWithCronTrigger(){
public void scheduleRunnableWithCronTrigger() {
taskScheduler.schedule(new RunnableTask("Current Date"), new Date());
taskScheduler.scheduleWithFixedDelay(new RunnableTask("Fixed 1 second Delay"), 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.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"), 2000);
taskScheduler.schedule(new RunnableTask("Cron Trigger"), cronTrigger);
taskScheduler.schedule(new RunnableTask("Periodic Trigger"), periodicTrigger);
}
class RunnableTask implements Runnable{
class RunnableTask implements Runnable {
private String message;
public RunnableTask(String message){
public RunnableTask(String message) {
this.message = message;
}
@Override
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());
}
}
}

View File

@ -5,19 +5,17 @@ import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ConstructorBasedBeanInjectionWithJavaConfigTest {
public class ConstructorBasedBeanInjectionWithJavaConfigIntegrationTest {
private static final String HELM_NAME = "HelmBrand";
@Test
public void givenJavaConfigFile_whenUsingConstructorBasedBeanInjection_thenCorrectHelmName() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConstructorBasedShipConfig.class);
ctx.refresh();
Ship ship = ctx.getBean(Ship.class);
Assert.assertEquals(HELM_NAME, ship.getHelm()
.getBrandOfHelm());
Assert.assertEquals(HELM_NAME, ship.getHelm().getBrandOfHelm());
}
}

View File

@ -5,7 +5,7 @@ import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConstructorBasedBeanInjectionWithXMLConfigTest {
public class ConstructorBasedBeanInjectionWithXMLConfigIntegrationTest {
private static final String HELM_NAME = "HelmBrand";
@ -14,7 +14,6 @@ public class ConstructorBasedBeanInjectionWithXMLConfigTest {
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-constructor.xml");
final Ship shipConstructorBean = (Ship) applicationContext.getBean("ship");
Assert.assertEquals(HELM_NAME, shipConstructorBean.getHelm()
.getBrandOfHelm());
Assert.assertEquals(HELM_NAME, shipConstructorBean.getHelm().getBrandOfHelm());
}
}

View File

@ -5,7 +5,7 @@ import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SetterBasedBeanInjectionWithJavaConfigTest {
public class SetterBasedBeanInjectionWithJavaConfigIntegrationTest {
private static final String HELM_NAME = "HelmBrand";
@ -18,7 +18,6 @@ public class SetterBasedBeanInjectionWithJavaConfigTest {
Ship ship = ctx.getBean(Ship.class);
Assert.assertEquals(HELM_NAME, ship.getHelm()
.getBrandOfHelm());
Assert.assertEquals(HELM_NAME, ship.getHelm().getBrandOfHelm());
}
}

View File

@ -5,7 +5,7 @@ import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SetterBasedBeanInjectionWithXMLConfigTest {
public class SetterBasedBeanInjectionWithXMLConfigIntegrationTest {
private static final String HELM_NAME = "HelmBrand";
@ -14,7 +14,6 @@ public class SetterBasedBeanInjectionWithXMLConfigTest {
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-setter.xml");
final Ship shipSetterBean = (Ship) applicationContext.getBean("ship");
Assert.assertEquals(HELM_NAME, shipSetterBean.getHelm()
.getBrandOfHelm());
Assert.assertEquals(HELM_NAME, shipSetterBean.getHelm().getBrandOfHelm());
}
}

View File

@ -16,17 +16,17 @@ public class TenantScopeTest {
@Test
public final void whenRegisterScopeAndBeans_thenContextContainsFooAndBar() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
try{
try {
ctx.register(TenantScopeConfig.class);
ctx.register(TenantBeansConfig.class);
ctx.refresh();
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
foo.sayHello();
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
bar.sayHello();
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
assertThat(foo, not(equalTo(bar)));
assertThat(foos.size(), equalTo(2));
assertTrue(foos.containsValue(foo));
@ -34,40 +34,38 @@ public class TenantScopeTest {
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
assertThat(fooDefinition.getScope(), equalTo("tenant"));
assertThat(barDefinition.getScope(), equalTo("tenant"));
}
finally {
} finally {
ctx.close();
}
}
@Test
public final void whenComponentScan_thenContextContainsFooAndBar() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
try{
try {
ctx.scan("org.baeldung.customscope");
ctx.refresh();
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
foo.sayHello();
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
bar.sayHello();
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
assertThat(foo, not(equalTo(bar)));
assertThat(foos.size(), equalTo(2));
assertTrue(foos.containsValue(foo));
assertTrue(foos.containsValue(bar));
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
assertThat(fooDefinition.getScope(), equalTo("tenant"));
assertThat(barDefinition.getScope(), equalTo("tenant"));
}
finally {
} finally {
ctx.close();
}
}

View File

@ -21,8 +21,7 @@ public class SquareCalculatorTest {
public void whenCalculatingSquareValueOnce_thenCacheDontHaveValues() {
for (int i = 10; i < 15; i++) {
assertFalse(cacheHelper.getSquareNumberCache().containsKey(i));
System.out.println("Square value of " + i + " is: "
+ squaredCalculator.getSquareValueOfNumber(i) + "\n");
System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n");
}
}
@ -30,14 +29,12 @@ public class SquareCalculatorTest {
public void whenCalculatingSquareValueAgain_thenCacheHasAllValues() {
for (int i = 10; i < 15; i++) {
assertFalse(cacheHelper.getSquareNumberCache().containsKey(i));
System.out.println("Square value of " + i + " is: "
+ squaredCalculator.getSquareValueOfNumber(i) + "\n");
System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n");
}
for (int i = 10; i < 15; i++) {
assertTrue(cacheHelper.getSquareNumberCache().containsKey(i));
System.out.println("Square value of " + i + " is: "
+ squaredCalculator.getSquareValueOfNumber(i) + "\n");
System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n");
}
}
}

View File

@ -9,7 +9,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class)
public class ThreadPoolTaskSchedulerIntegrationTest {
@Test
public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException {
Thread.sleep(2550);